-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_bandpass.py
253 lines (198 loc) · 7.67 KB
/
plot_bandpass.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
#!/usr/bin/env python
import matplotlib
matplotlib.use('Agg')
from pyrap.tables import table
from optparse import OptionParser
import matplotlib.colors as colors
import matplotlib.cm as cmx
import glob
import numpy
import pylab
import sys
def setup_plot(ax):
ax.grid(b=True,which='minor',color='white',linestyle='-',lw=2)
ax.grid(b=True,which='major',color='white',linestyle='-',lw=2)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.tick_params(axis='x',which='both',bottom='off',top='off')
ax.tick_params(axis='y',which='both',left='off',right='off')
def set_fontsize(fig,fontsize):
def match(artist):
return artist.__module__ == 'matplotlib.text'
for textobj in fig.findobj(match=match):
textobj.set_fontsize(fontsize)
parser = OptionParser(usage='%prog [options] tablename')
parser.add_option('-f','--field',dest='field',help='Field ID to plot (default = 0)',default=0)
parser.add_option('-d','--doplot',dest='doplot',help='Plot complex values as amp and phase (ap) or real and imag (ri) (default = ap)',default='ap')
parser.add_option('-a','--ant',dest='plotants',help='Plot only this antenna, or comma-separated list of antennas',default=[-1])
parser.add_option('-c','--corr',dest='corr',help='Correlation index to plot (usually just 0 or 1, default = 0)',default=0)
parser.add_option('--t0',dest='t0',help='Minimum time to plot (default = full range)',default=-1)
parser.add_option('--t1',dest='t1',help='Maximum time to plot (default = full range)',default=-1)
parser.add_option('--yu0',dest='yu0',help='Minimum y-value to plot for upper panel (default = full range)',default=-1)
parser.add_option('--yu1',dest='yu1',help='Maximum y-value to plot for upper panel (default = full range)',default=-1)
parser.add_option('--yl0',dest='yl0',help='Minimum y-value to plot for lower panel (default = full range)',default=-1)
parser.add_option('--yl1',dest='yl1',help='Maximum y-value to plot for lower panel (default = full range)',default=-1)
parser.add_option('--cmap',dest='mycmap',help='Matplotlib colour map to use for antennas (default = coolwarm)',default='coolwarm')
parser.add_option('--size',dest='mysize',help='Font size for figure labels (default = 20)',default=20)
parser.add_option('--ms',dest='myms',help='Measurement Set to consult for proper antenna names',default='')
parser.add_option('-p','--plotname',dest='pngname',help='Output PNG name (default = something sensible)',default='')
(options,args) = parser.parse_args()
field = int(options.field)
doplot = options.doplot
plotants = options.plotants
corr = int(options.corr)
t0 = float(options.t0)
t1 = float(options.t1)
yu0 = float(options.yu0)
yu1 = float(options.yu1)
yl0 = float(options.yl0)
yl1 = float(options.yl1)
mycmap = options.mycmap
mysize = int(options.mysize)
myms = options.myms
pngname = options.pngname
if len(args) != 1:
print 'Please specify a bandpass table to plot.'
sys.exit(-1)
else:
mytab = args[0].rstrip('/')
if pngname == '':
pngname = 'plot_'+mytab.split('/')[-1]+'_corr'+str(corr)+'_'+doplot+'_field'+str(field)+'.png'
if doplot not in ['ap','ri']:
print 'Plot selection must be either ap (amp and phase) or ri (real and imag)'
sys.exit(-1)
tt = table(mytab,ack=False)
ants = numpy.unique(tt.getcol('ANTENNA1'))
fields = numpy.unique(tt.getcol('FIELD_ID'))
spws = numpy.unique(tt.getcol('SPECTRAL_WINDOW_ID'))
cNorm = colors.Normalize(vmin=0,vmax=len(ants)-1)
mymap = cm = pylab.get_cmap(mycmap)
scalarMap = cmx.ScalarMappable(norm=cNorm,cmap=mymap)
if int(field) not in fields.tolist():
print 'Field ID '+str(field)+' not found'
sys.exit(-1)
if plotants[0] != -1:
plotants = plotants.split(',')
for ant in plotants:
if int(ant) not in ants:
plotants.remove(ant)
print 'Requested antenna ID '+str(ant)+' not found'
if len(plotants) == 0:
print 'No valid antennas indices requested'
sys.exit(-1)
else:
plotants = ants
if myms != '':
anttab = table(myms.rstrip('/')+'/ANTENNA')
antnames = anttab.getcol('NAME')
anttab.done()
else:
antnames = ''
fig = pylab.figure(figsize=(24,18))
ax1 = fig.add_subplot(211,facecolor='#EEEEEE')
ax2 = fig.add_subplot(212,facecolor='#EEEEEE')
setup_plot(ax1)
setup_plot(ax2)
xmin = 1e20
xmax = -1e20
ylmin = 1e20
ylmax = -1e20
yumin = 1e20
yumax = -1e20
for ant in plotants:
y1col = scalarMap.to_rgba(float(ant))
y2col = scalarMap.to_rgba(float(ant))
chans = numpy.array(())
gains = numpy.array(())
flags = numpy.array(())
for spw in spws:
mytaql = 'ANTENNA1=='+str(ant)+' && '
mytaql+= 'FIELD_ID=='+str(field)+' && '
mytaql+= 'SPECTRAL_WINDOW_ID=='+str(spw)
subtab = tt.query(query=mytaql)
cparam = subtab.getcol('CPARAM')
flagcol = subtab.getcol('FLAG')
nchan = cparam.shape[1]
nrows = cparam.shape[0]
chans = numpy.append(chans,numpy.tile((spw*nchan)+numpy.arange(nchan),nrows))
gains = numpy.append(gains,numpy.ravel(cparam[:,:,corr]))
flags = numpy.append(flags,numpy.ravel(flagcol[:,:,corr]))
masked_gains = numpy.ma.array(data=gains,mask=flags)
masked_chans = numpy.ma.array(data=chans,mask=flags)
if doplot == 'ap':
y1 = numpy.ma.abs(masked_gains)
y2 = numpy.ma.angle(masked_gains)
#y2 = numpy.ma.array(y2)
# y2 = numpy.unwrap(y2)
ax1.plot(masked_chans,y1,'.',markersize=4,alpha=0.7,zorder=100,color=y1col)
ax1.plot(masked_chans,y1,'-',lw=2,alpha=0.4,zorder=100,color=y1col)
ax2.plot(masked_chans,y2,'.',markersize=4,alpha=0.7,zorder=100,color=y2col)
ax2.plot(masked_chans,y2,'-',lw=2,alpha=0.4,zorder=100,color=y2col)
elif doplot == 'ri':
y1 = numpy.ma.real(masked_gains)
y2 = numpy.ma.imag(masked_gains)
ax1.plot(masked_chans,y1,'.',markersize=4,alpha=0.7,zorder=100,color=y1col)
ax1.plot(masked_chans,y1,'-',lw=2,alpha=0.4,zorder=100,color=y1col)
ax2.plot(masked_chans,y2,'.',markersize=4,alpha=0.7,zorder=100,color=y2col)
ax2.plot(masked_chans,y2,'-',lw=2,alpha=0.4,zorder=100,color=y2col)
subtab.close()
dx = 1.0/float(len(ants)-1)
if antnames == '':
antlabel = str(ant)
else:
antlabel = antnames[ant]
ax1.text(float(ant)*dx,1.05,antlabel,size='large',horizontalalignment='center',color=y1col,transform=ax1.transAxes,weight='heavy',rotation=90)
if numpy.min(chans) < xmin:
xmin = numpy.min(chans)
if numpy.max(chans) > xmax:
xmax = numpy.max(chans)
if numpy.min(y1) < yumin:
yumin = numpy.min(y1)
if numpy.max(y1) > yumax:
yumax = numpy.max(y1)
if numpy.min(y2) < ylmin:
ylmin = numpy.min(y2)
if numpy.max(y2) > ylmax:
ylmax = numpy.max(y2)
# xmin = xmin-4
# xmax = xmax+4
# if yumin < 0.0:
# yumin = -1*(1.05*numpy.abs(yumin))
# else:
# yumin = yumin*0.95
# yumax = yumax*1.1
# if ylmin < 0.0:
# ylmin = -1*(1.05*numpy.abs(ylmin))
# else:
# ylmin = ylmin*0.95
# ylmax = ylmax*1.1
if t0 != -1:
xmin = float(t0)
if t1 != -1:
xmax = float(t1)
if yl0 != -1:
ylmin = yl0
if yl1 != -1:
ylmax = yl1
if yu0 != -1:
yumin = yu0
if yu1 != -1:
yumax = yu1
ax1.set_xlim((xmin,xmax))
ax2.set_xlim((xmin,xmax))
ax1.set_ylim((yumin,yumax))
ax2.set_ylim((ylmin,ylmax))
if doplot == 'ap':
ax1.set_ylabel('Amplitude')
ax2.set_ylabel('Phase [rad]')
elif doplot == 'ri':
ax1.set_ylabel('Real')
ax2.set_ylabel('Imaginary')
ax1.set_xlabel('Channel')
ax2.set_xlabel('Channel')
fig.suptitle(pngname)
set_fontsize(fig,mysize)
fig.savefig(pngname,bbox_inches='tight')
print 'Rendered: '+pngname