-
Notifications
You must be signed in to change notification settings - Fork 3
/
plot_solutions_all_stations_v2.py
executable file
·531 lines (399 loc) · 19.2 KB
/
plot_solutions_all_stations_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/usr/bin/env python
#
#################################################################################
# #
# Written by Wendy Williams, 26 May 2014 #
# #
# #
#################################################################################
import lofar.parmdb as lp
import numpy as np
import sys, os
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import argparse
mpl.rc('font',size =8 )
mpl.rc('figure.subplot',left=0.05, bottom=0.05, right=0.95, top=0.95 )
def normalize(phase):
"""
Normalize phase to the range [-pi, pi].
"""
# Convert to range [-2*pi, 2*pi].
out = np.fmod(phase, 2.0 * np.pi)
# Convert to range [-pi, pi]
out[out < -np.pi] += 2.0 * np.pi
out[out > np.pi] -= 2.0 * np.pi
return out
def scaletimes(t):
t = t-t[0]
t = t/3600.
return t
def solplot_scalarphase(parmdb, imageroot, refstationi, plot_international=False):
parmdbmtable = lp.parmdb(parmdb)
soldict = parmdbmtable.getValuesGrid('*')
names = parmdbmtable.getNames()
'Gain:1:1:Phase:RS508HBA'
stationsnames = np.array([name.split(':')[-1] for name in names])
stationsnames = np.unique(stationsnames)
if not plot_international:
stationsnames = np.array([name for name in stationsnames if name[0] in ['C','R'] ])
Nstat = len(stationsnames)
refstation = stationsnames[refstationi]
phase_ref = soldict['CommonScalarPhase:{s}'.format(s=refstation)]['values']
times= soldict['CommonScalarPhase:{s}'.format(s=refstation)]['times']
#Nr = int(np.ceil(np.sqrt(Nstat)))
#Nc = int(np.ceil(np.float(Nstat)/Nr))
Nr = int(Nstat)
Nc = 1
f, ax = plt.subplots(Nr, Nc, sharex=True, sharey=True, figsize=(12,72))
axs = ax.reshape((Nr*Nc,1))
for istat, station in enumerate(stationsnames):
phase = soldict['CommonScalarPhase:{s}'.format(s=station)]['values']
# don't plot flagged phases
phase = np.ma.masked_where(phase==0, phase)
#try:
if len(phase) > 1000:
fmt = ','
else:
fmt = '.'
#except TypeError:
#print "no phases"
#fmt = '.'
ls= 'none'
axs[istat][0].plot(times, normalize(phase-phase_ref), color='b', marker=fmt, ls=ls, label='CommonScalarPhase',mec='b')
axs[istat][0].set_ylim(-3.2, 3.2)
axs[istat][0].set_xlim(times.min(), times.max())
axs[istat][0].set_title(station)
f.savefig(imageroot+"_scalarphase.png",dpi=100)
return
def solplot_tec(parmdb, imageroot, refstationi, plot_international=False, freq=None):
parmdbmtable = lp.parmdb(parmdb)
soldict = parmdbmtable.getValuesGrid('*')
if freq is None:
tfrange = parmdbmtable.getRange()
print 'freqrange', tfrange[0:2]
freq = np.average(tfrange[0:2])
print 'freq', freq/1e6, 'MHz'
names = parmdbmtable.getNames()
'Gain:1:1:Phase:RS508HBA'
stationsnames = np.array([name.split(':')[-1] for name in names])
stationsnames = np.unique(stationsnames)
if not plot_international:
stationsnames = np.array([name for name in stationsnames if name[0] in ['C','R'] ])
Nstat = len(stationsnames)
refstation = stationsnames[refstationi]
times = soldict['CommonScalarPhase:{s}'.format(s=refstation)]['times']
times = scaletimes(times)
phase_ref = soldict['CommonScalarPhase:{s}'.format(s=refstation)]['values']
tec_ref = soldict['TEC:{s}'.format(s=refstation)]['values']
#Nr = int(np.ceil(np.sqrt(Nstat)))
#Nc = int(np.ceil(np.float(Nstat)/Nr))
Nr = int(Nstat)
Nc = 1
f, ax = plt.subplots(Nr, Nc, sharex=True, sharey=True, figsize=(12,72))
axs = ax.reshape((Nr*Nc,1))
ymin = 2
ymax = 0
for istat, station in enumerate(stationsnames):
phase = soldict['CommonScalarPhase:{s}'.format(s=station)]['values']
tec = soldict['TEC:{s}'.format(s=station)]['values']
phase = np.ma.masked_where(phase==0, phase)
if len(times) > 1000:
fmt = ','
else:
fmt = '.'
ls='none'
phasep = phase - phase_ref
tecp = -8.44797245e9*(tec - tec_ref)/freq
axs[istat][0].plot(times, np.mod(phasep+tecp +np.pi, 2*np.pi) - np.pi, color='b', marker=fmt, ls=ls, label='Phase+TEC', mec='b')
axs[istat][0].set_ylim(-np.pi, np.pi)
axs[istat][0].set_xlim(times.min(), times.max())
axs[istat][0].set_title(station)
#phase_ref = parms['CommonScalarPhase:'+ ref_antenna]['values'][::]
#phase = parms['CommonScalarPhase:'+ antenna ]['values'][::]
##print phase-phase_ref
#clock = parms['Clock:0:'+ antenna ]['values'][::]
#clock_ref = parms['Clock:0:'+ ref_antenna ]['values'][::]
#TEC = parms['TEC:'+ antenna ]['values'][::]
#TEC_ref = parms['TEC:'+ ref_antenna ]['values'][::]
#phasep = phase-phase_ref
#clockp = 2.*pi*(clock-clock_ref)*freq
#tecp = -8.44797245e9*(TEC-TEC_ref)/freq
##plot(numpy.mod(phasep+tecp + clockp +pi, 2*pi) - pi, '.')
f.savefig(imageroot+"_tec.png",dpi=100)
return
def solplot_clock(parmdb, imageroot, refstationi, plot_international=False):
parmdbmtable = lp.parmdb(parmdb)
soldict = parmdbmtable.getValuesGrid('*')
names = parmdbmtable.getNames()
'Gain:1:1:Phase:RS508HBA'
stationsnames = np.array([name.split(':')[-1] for name in names])
stationsnames = np.unique(stationsnames)
if not plot_international:
stationsnames = np.array([name for name in stationsnames if name[0] in ['C','R'] ])
Nstat = len(stationsnames)
refstation = stationsnames[refstationi]
#phase_ref = soldict['Clock:{s}'.format(s=refstation)]['values']
times= soldict['Clock:1:{s}'.format(s=refstation)]['times']
Nr = int(np.ceil(np.sqrt(Nstat)))
Nc = int(np.ceil(np.float(Nstat)/Nr))
f, ax = plt.subplots(Nr, Nc, sharex=True, sharey=True, figsize=(16,12))
axs = ax.reshape((Nr*Nc,1))
ymin = 2
ymax = 0
for istat, station in enumerate(stationsnames):
clock00 = soldict['Clock:0:{s}'.format(s=station)]['values']
clock11 = soldict['Clock:1:{s}'.format(s=station)]['values']
if len(clock00) > 0:
ymax = max(np.max(clock00),ymax)
if len(clock11) > 0:
ymax = max(np.max(clock11),ymax)
if len(clock00) > 0:
ymin = min(np.min(clock00),ymin)
if len(clock11) > 0:
ymin = min(np.min(clock11),ymin)
# don't plot flagged phases
#phase = np.ma.masked_where(phase==0, phase)
#try:
#if len(phase11) > 1000:
#fmt = ','
#else:
#fmt = '.'
#except TypeError:
#print "no phases"
fmt = '.'
ls='none'
axs[istat][0].plot(times, clock00, color='b', marker=fmt, ls=ls, label='Clock 0:0', mec='b')
axs[istat][0].plot(times, clock11, color='g', marker=fmt, ls=ls, label='Clock 1:1', mec='g')
axs[istat][0].set_ylim(ymin, ymax)
axs[istat][0].set_xlim(times.min(), times.max())
axs[istat][0].set_title(station)
f.savefig(imageroot+"_clock.png",dpi=100)
return
def solplot_phase_phasors(parmdb, imageroot, refstationi, plot_international=False):
parmdbmtable = lp.parmdb(parmdb)
soldict = parmdbmtable.getValuesGrid('*')
names = parmdbmtable.getNames()
'Gain:1:1:Phase:RS508HBA'
stationsnames = np.array([name.split(':')[-1] for name in names])
stationsnames = np.unique(stationsnames)
if not plot_international:
stationsnames = np.array([name for name in stationsnames if name[0] in ['C','R'] ])
Nstat = len(stationsnames)
refstation = stationsnames[refstationi]
phase11_ref = soldict['Gain:1:1:Phase:{s}'.format(s=refstation)]['values']
phase00_ref = soldict['Gain:0:0:Phase:{s}'.format(s=refstation)]['values']
times= soldict['Gain:1:1:Phase:{s}'.format(s=refstation)]['times']
Nr = int(np.ceil(np.sqrt(Nstat)))
Nc = int(np.ceil(np.float(Nstat)/Nr))
f, ax = plt.subplots(Nr, Nc, sharex=True, sharey=True, figsize=(16,12))
axs = ax.reshape((Nr*Nc,1))
for istat, station in enumerate(stationsnames):
phase11 = soldict['Gain:1:1:Phase:{s}'.format(s=station)]['values']
phase00 = soldict['Gain:0:0:Phase:{s}'.format(s=station)]['values']
# don't plot flagged phases
phase00 = np.ma.masked_where(phase00==0, phase00)
phase11 = np.ma.masked_where(phase11==0, phase11)
#try:
#if len(phase11) > 1000:
#fmt = ','
#else:
#fmt = '.'
#except TypeError:
#print "no phases"
if len(times) > 1000:
fmt = ','
else:
fmt = '.'
ls='none'
axs[istat][0].plot(times, normalize(phase00-phase00_ref), color='b', marker=fmt, ls=ls, label='Gain:0:0:Phase',mec='b')
axs[istat][0].plot(times, normalize(phase11-phase11_ref), color='g', marker=fmt, ls=ls, label='Gain:1:1:Phase',mec='g')
axs[istat][0].set_ylim(-3.2, 3.2)
axs[istat][0].set_xlim(times.min(), times.max())
axs[istat][0].set_title(station)
f.savefig(imageroot+"_phase.png",dpi=100)
return
def solplot_phase(parmdb, imageroot, refstationi, norm_amp_lim=False, median_amp=False, plot_international=False):
parmdbmtable = lp.parmdb(parmdb)
soldict = parmdbmtable.getValuesGrid('*')
names = parmdbmtable.getNames()
'Gain:1:1:Phase:RS508HBA'
stationsnames = np.array([name.split(':')[-1] for name in names])
stationsnames = np.unique(stationsnames)
if not plot_international:
stationsnames = np.array([name for name in stationsnames if name[0] in ['C','R'] ])
Nstat = len(stationsnames)
refstation = stationsnames[refstationi]
times = soldict['Gain:1:1:Real:{s}'.format(s=refstation)]['times']
times = scaletimes(times)
real11_ref = soldict['Gain:1:1:Real:{s}'.format(s=refstation)]['values']
real00_ref = soldict['Gain:0:0:Real:{s}'.format(s=refstation)]['values']
imag11_ref = soldict['Gain:1:1:Imag:{s}'.format(s=refstation)]['values']
imag00_ref = soldict['Gain:0:0:Imag:{s}'.format(s=refstation)]['values']
valscorr00 = real00_ref +1.j*imag00_ref
valscorr11 = real11_ref +1.j*imag11_ref
phase00_ref = np.angle(valscorr00)
phase11_ref = np.angle(valscorr11)
Nr = int(np.ceil(np.sqrt(Nstat)))
Nc = int(np.ceil(np.float(Nstat)/Nr))
fp, axp = plt.subplots(Nr, Nc, sharex=True, sharey=True, figsize=(16,12))
axsp = axp.reshape((Nr*Nc,1))
for istat, station in enumerate(stationsnames):
real11 = soldict['Gain:1:1:Real:{s}'.format(s=station)]['values']
real00 = soldict['Gain:0:0:Real:{s}'.format(s=station)]['values']
imag11 = soldict['Gain:1:1:Imag:{s}'.format(s=station)]['values']
imag00 = soldict['Gain:0:0:Imag:{s}'.format(s=station)]['values']
valscorr00 = real00 +1.j*imag00
valscorr11 = real11 +1.j*imag11
if len(np.unique(real11)) > 500:
fmt = ','
else:
fmt = '.'
ls='none'
phase00 = np.angle(valscorr00)
phase11 = np.angle(valscorr11)
#phase11 = soldict['Gain:1:1:Phase:{s}'.format(s=station)]
#phase00 = soldict['Gain:0:0:Phase:{s}'.format(s=station)]
# don't plot flagged phases
phase00 = np.ma.masked_where(phase00==0, phase00)
phase11 = np.ma.masked_where(phase11==0, phase11)
axsp[istat][0].plot(times, normalize(phase00-phase00_ref), color='b', marker=fmt, ls=ls, label='Gain:0:0:Phase',mec='b')
axsp[istat][0].plot(times, normalize(phase11-phase11_ref), color='g', marker=fmt, ls=ls, label='Gain:1:1:Phase',mec='g')
axsp[istat][0].set_ylim(-3.2, 3.2)
axsp[istat][0].set_xlim(times.min(), times.max())
axsp[istat][0].set_title(station)
fp.savefig(imageroot+"_phase.png",dpi=100)
return
def solplot_amp(parmdb, imageroot, refstationi, norm_amp_lim=False, median_amp=False, plot_international=False):
parmdbmtable = lp.parmdb(parmdb)
soldict = parmdbmtable.getValuesGrid('*')
names = parmdbmtable.getNames()
'Gain:1:1:Phase:RS508HBA'
stationsnames = np.array([name.split(':')[-1] for name in names])
stationsnames = np.unique(stationsnames)
if not plot_international:
stationsnames = np.array([name for name in stationsnames if name[0] in ['C','R'] ])
Nstat = len(stationsnames)
refstation = stationsnames[refstationi]
times = soldict['Gain:1:1:Real:{s}'.format(s=refstation)]['times']
times = scaletimes(times)
real11_ref = soldict['Gain:1:1:Real:{s}'.format(s=refstation)]['values']
real00_ref = soldict['Gain:0:0:Real:{s}'.format(s=refstation)]['values']
imag11_ref = soldict['Gain:1:1:Imag:{s}'.format(s=refstation)]['values']
imag00_ref = soldict['Gain:0:0:Imag:{s}'.format(s=refstation)]['values']
valscorr00 = real00_ref +1.j*imag00_ref
valscorr11 = real11_ref +1.j*imag11_ref
amp00_ref = np.abs(valscorr00)
amp11_ref = np.abs(valscorr11)
Nr = int(np.ceil(np.sqrt(Nstat)))
Nc = int(np.ceil(np.float(Nstat)/Nr))
fa, axa = plt.subplots(Nr, Nc, sharex=True, sharey=True, figsize=(16,12))
axsa = axa.reshape((Nr*Nc,1))
ymin = 2
ymax = 0
for istat, station in enumerate(stationsnames):
real11 = soldict['Gain:1:1:Real:{s}'.format(s=station)]['values']
real00 = soldict['Gain:0:0:Real:{s}'.format(s=station)]['values']
imag11 = soldict['Gain:1:1:Imag:{s}'.format(s=station)]['values']
imag00 = soldict['Gain:0:0:Imag:{s}'.format(s=station)]['values']
valscorr00 = real00 +1.j*imag00
valscorr11 = real11 +1.j*imag11
#if len(valscorr11) > 1000:
#fmt = ','
#else:
#fmt = '.'
if len(np.unique(real11)) > 500:
fmt = ','
else:
fmt = '.'
ls='none'
amp00 = np.abs(valscorr00)
amp11 = np.abs(valscorr11)
#phase11 = soldict['Gain:1:1:Phase:{s}'.format(s=station)]
#phase00 = soldict['Gain:0:0:Phase:{s}'.format(s=station)]
## for y scale: check max and min values
amp00m = np.ma.masked_where(amp00==1, amp00).compressed()
amp11m = np.ma.masked_where(amp11==1, amp11).compressed()
if len(amp00m) > 0:
ymax = max(np.max(amp00m),ymax)
if len(amp11m) > 0:
ymax = max(np.max(amp11m),ymax)
if len(amp00m) > 0:
ymin = min(np.min(amp00m),ymin)
if len(amp11m) > 0:
ymin = min(np.min(amp11m),ymin)
# don't plot flagged amplitudes
amp00 = np.ma.masked_where(amp00==1, amp00)
amp11 = np.ma.masked_where(amp11==1, amp11)
axsa[istat][0].plot(times, amp00, color='b', marker=fmt, ls=ls, label='Gain:0:0:Amp',mec='b')
axsa[istat][0].plot(times, amp11, color='g', marker=fmt, ls=ls, label='Gain:1:1:Amp',mec='g')
if median_amp:
median_amp00 = np.median(amp00)
median_amp11 = np.median(amp11)
#if abs(median_amp00) < 1.0e-10:
#if np.sum(amp00==median_amp00) != len(amp00):
#amp00 = np.ma.masked_where(amp00==1, amp00).compressed()
#median_amp00 = np.median(amp00)
#if abs(median_amp11) < 1.0e-10:
#if np.sum(amp11==median_amp11) != len(amp11):
#amp11 = np.ma.masked_where(amp11==1, amp11).compressed()
#median_amp11 = np.median(amp11)
#print median_amp00,median_amp11
axsa[istat][0].plot([times[0], times[-1]], [median_amp00,median_amp00], color='b', label='<Gain:0:0:Amp>')
axsa[istat][0].plot([times[0], times[-1]], [median_amp11,median_amp11], color='g', label='<Gain:1:1:Amp>')
if norm_amp_lim:
axsa[istat][0].set_ylim(0,2 )
else:
axsa[istat][0].set_ylim(ymin,ymax)
#print istat, station,ymin, ymax
axsa[istat][0].set_xlim(times.min(), times.max())
axsa[istat][0].set_title(station)
fa.savefig(imageroot+"_amp.png",dpi=100)
return
def main():
parser = argparse.ArgumentParser() #prog='plot_solutions_all_stations.py',usage='[options] <parmdb> <imageroot> ')
parser.add_argument('-c', '--clock', dest='clock', action="store_true", default=False, help="plot clock solutions")
parser.add_argument('-a', '--amp', dest='amp', action="store_true", default=False, help="plot amp solutions")
parser.add_argument('-p', '--phase', dest='phase', action="store_true", default=False, help="plot phase solutions")
parser.add_argument('--phasors', dest='phasors', action="store_true", default=False, help="set phase-only")
parser.add_argument('--freq', dest='freq', default=0, help="reference frequency for TEC plotting in MHz")
parser.add_argument('-t','--tec', dest='tec', action="store_true", default=False, help="set tec-mode plotting on and plot phase (TEC+scalarpahse)")
parser.add_argument('-s', '--scalarphase', dest='scalarphase', action="store_true", default=False, help="plot scalarphase solutions")
parser.add_argument('-n', '--norm-amplitude-limits', dest='norm_amp_lim', action="store_true", default=False, help="plot amps between 0 and 2")
parser.add_argument('-m', '--plot-median-amplitude', dest='median_amp', action="store_true", default=False, help="plot median amplitudes")
parser.add_argument('-i', '--plot-international-stations', dest='plot_international', action="store_true", default=False, help="plot international stations")
parser.add_argument('-r', '--refstation', dest='refstation', default=0, help="given reference station (integer)")
parser.add_argument('parmdb', help="Name of solution parmdb")
parser.add_argument('imageroot', help="Root name for output images")
args = parser.parse_args()
parmdb = args.parmdb
imageroot = args.imageroot
norm_amp_lim = args.norm_amp_lim
median_amp = args.median_amp
plot_international = args.plot_international
refstation = int(args.refstation)
if args.freq != 0:
reffreq = float(args.freq) * 1e6
else:
reffreq = None
#if len(args) <= 2:
#print 'Insufficient arguments'
#print 'Usage: python %prog [options] <parmdbname> <imagename>'
#filename = args[0]
#imagename = args[1]
if args.scalarphase:
solplot_scalarphase(parmdb, imageroot, refstation, plot_international=plot_international)
if args.phase:
if args.phasors:
solplot_phase_phasors(parmdb, imageroot, refstation, plot_international=plot_international)
else:
solplot_phase(parmdb, imageroot, refstation, plot_international=plot_international)
if args.amp:
solplot_amp(parmdb, imageroot, refstation, norm_amp_lim=norm_amp_lim, median_amp=median_amp, plot_international=plot_international)
if args.tec:
solplot_tec(parmdb, imageroot, refstation, plot_international=plot_international, freq=reffreq)
if args.clock:
solplot_clock(parmdb, imageroot, refstation, plot_international=plot_international)
if __name__ == "__main__":
main()