-
Notifications
You must be signed in to change notification settings - Fork 1
/
bandpasscal.py
379 lines (364 loc) · 20.3 KB
/
bandpasscal.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
import sources
import logger as log
import globals
import random
import flagging
from bootflux import getCatalogFlux
from threading import Thread
import calculations
"""
Module to calculate and apply bandpass solutions
Part of the CARMA data reduction pipeline
Author: D. N. Friedel
"""
class bandpassThread(Thread) :
def __init__(self,objects,refant,window) :
Thread.__init__(self)
self.objects = objects
self.objects = objects
self.refant = refant
self.window = window
def run(self) :
bandpass(self.objects,self.refant,self.window)
def bandpass(objects,refant,window) :
""" Method to apply bandpasses, works for both wideband and narrowband windows
input :
objects - the observed objects
refant - the reference antenna
"""
fileEnd = str(random.randint(1,100000))
# find an appropriate passband calibrator (quasars are preferred but a planet will do if we have no choice)
passcal = ""
passcalFile = "" # in case there are multiple passband calls
passcalEnd = ""
selectedPcal = None
passcalFlux = 0.0
found = False
numChans = 1
startChan = 1
chanList = [-1]
# do superwide windows
if(objects._passcals[0].isSuper(window)) :
tempPcal = None
for pcal in objects._passcals :
if(pcal._bandwidths[window - 1] == objects._sources[0]._bandwidths[window - 1] or (globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1]))) :
if(pcal._type == sources.QUASAR) :
flux = getCatalogFlux(pcal._name,calculations.gregorianToNumeric(globals.obsDate()), globals.obsFreq())
if(flux < passcalFlux or pcal._type == sources.PLANET) :
continue
found = True
tempPcal = pcal
if(globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1])) :
passcal = pcal._file + ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1],window)
passcalFile = pcal._file
passcalEnd = ".%i.w%i" %(objects._sources[0]._bandwidths[window - 1],window)
selectedPcal = pcal
numChans = pcal._hybridChans.get(objects._sources[0]._bandwidths[window - 1])
else :
chanList = []
passcal = pcal._file + ".w%i" %(window)
if(window <= len(pcal._bandwidths)/2) :
passcalFile = pcal._file
passcalEnd = ".LSB"
selectedPcal = pcal
for i in range(0,len(pcal._superwindow)/2) :
win = pcal._superwindow[i]
chanList.append(pcal.getNumChan(win))
else :
passcalFile = pcal._file
passcalEnd = ".USB"
selectedPcal = pcal
for i in range(len(pcal._superwindow)/2,len(pcal._superwindow)) :
win = pcal._superwindow[i]
chanList.append(pcal.getNumChan(win))
numChans = pcal.getSuperNumChans()
if(not found) :
log.writeComment("Did not find a quasar to use as a passband calibrator, now searching for a planet.")
for pcal in objects._passcals :
if(found) :
break
if(pcal._bandwidths[window - 1] == objects._sources[0]._bandwidths[window - 1] or (globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1]))) :
if(pcal._type == sources.PLANET) :
found = True
# if the data are taken in hybrid mode then make sure we get the right files
if(globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1])) :
passcal = + pcal._file + ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1],window)
passcalFile = pcal._file
passcalEnd = ".%i.w%i" %(objects._sources[0]._bandwidths[window - 1],window)
selectedPcal = pcal
numChans = pcal._hybridChans.get(objects._sources[0]._bandwidths[window - 1])
else :
chanList = []
passcal = pcal._file + ".w%i" %(window)
if(window <= len(pcal._bandwidths)/2) :
passcalFile = pcal._file
passcalEnd = ".LSB"
selectedPcal = pcal
for i in range(0,len(pcal._superwindow)/2) :
win = pcal._superwindow[i]
chanList.append(pcal.getNumChan(win))
else :
passcalFile = pcal._file
passcalEnd = ".USB"
selectedPcal = pcal
for i in range(len(pcal._superwindow)/2,len(pcal._superwindow)) :
win = pcal._superwindow[i]
chanList.append(pcal.getNumChan(win))
numChans = pcal.getSuperNumChans()
log.writeComment("Found a planet to use as passband.")
if(tempPcal != None) :
pcal = tempPcal
# noise source is no good for 500 MHz windows
elif(objects._sources[0].getBandwidth(window) >= 300) :
tempPcal = None
for pcal in objects._passcals :
if(pcal._bandwidths[window - 1] == objects._sources[0]._bandwidths[window - 1] or (globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1]))) :
if(pcal._type == sources.QUASAR) :
flux = getCatalogFlux(pcal._name,calculations.gregorianToNumeric(globals.obsDate()), globals.obsFreq())
if(flux < passcalFlux or pcal._type == sources.PLANET) :
continue
found = True
tempPcal = pcal
if(globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1])) :
passcal = pcal._file + ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1],window)
passcalFile = pcal._file
passcalEnd = ".%i.w%i" %(objects._sources[0]._bandwidths[window - 1],window)
selectedPcal = pcal
numChans = pcal._hybridChans.get(objects._sources[0]._bandwidths[window - 1])
else :
passcal = pcal._file + ".w%i" %(window)
passcalFile = pcal._file
passcalEnd = ".w%i" %(window)
selectedPcal = pcal
numChans = pcal._numChans[window - 1]
if(not found) :
log.writeComment("Did not find a quasar to use as a passband calibrator, now searching for a planet.")
for pcal in objects._passcals :
if(found) :
break
if(pcal._bandwidths[window - 1] == objects._sources[0]._bandwidths[window - 1] or (globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1]))) :
if(pcal._type == sources.PLANET) :
found = True
# if the data are taken in hybrid mode then make sure we get the right files
if(globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1])) :
passcal = pcal._file + ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1],window)
passcalFile = pcal._file
passcalEnd = ".%i.w%i" %(objects._sources[0]._bandwidths[window - 1],window)
selectedPcal = pcal
numChans = pcal._hybridChans.get(objects._sources[0]._bandwidths[window - 1])
else :
passcal = pcal._file + ".w%i" %(window)
passcalFile = pcal._file
passcalEnd = ".w%i" %(window)
selectedPcal = pcal
numChans = pcal._numChans[window - 1]
log.writeComment("Found a planet to use as passband.")
if(tempPcal != None) :
pcal = tempPcal
# astronomical source is preferred for 62 MHz, but noise source will do if need be
elif(objects._sources[0].getBandwidth(window) >= 50) :
tempPcal = None
for pcal in objects._passcals :
if(pcal._bandwidths[window - 1] == objects._sources[0]._bandwidths[window - 1] or (globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1]))) :
if(pcal._type == sources.QUASAR) :
flux = getCatalogFlux(pcal._name,calculations.gregorianToNumeric(globals.obsDate()), globals.obsFreq())
if(flux < passcalFlux or pcal._type == sources.PLANET) :
continue
found = True
tempPcal = pcal
startChan = 4
# if the data are taken in hybrid mode then make sure we get the right files
if(globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1])) :
passcal = pcal._file + ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1],window)
passcalFile = pcal._file
passcalEnd = ".%i.w%i" %(objects._sources[0]._bandwidths[window - 1],window)
selectedPcal = pcal
numChans = pcal._hybridChans.get(objects._sources[0]._bandwidths[window - 1])
else :
if(pcal.getBandwidth(window) == 62) :
numChans = pcal._numChans[window - 1] - 6
else :
numChans = pcal._numChans[window - 1]
starChan = 1
passcal = pcal._file + ".w%i" %(window)
passcalFile = pcal._file
passcalEnd = ".w%i" %(window)
selectedPcal = pcal
numChans = pcal._numChans[window - 1] - 6
if(not found) :
log.writeComment("Did not find a quasar to use as a passband calibrator, locating noise source.")
for pcal in objects._passcals :
if(found) :
break
if(pcal._bandwidths[window - 1] == objects._sources[0]._bandwidths[window - 1] or (globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1]))) :
if(pcal._type == sources.NOISE) :
found = True
# if the data are taken in hybrid mode then make sure we get the right files
if(globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1])) :
passcal = pcal._file + ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1],window)
passcalFile = pcal._file
passcalEnd = ".%i.w%i" %(objects._sources[0]._bandwidths[window - 1],window)
selectedPcal = pcal
numChans = pcal._hybridChans.get(objects._sources[0]._bandwidths[window - 1])
else :
passcal = pcal._file + ".w%i" %(window)
passcalFile = pcal._file
passcalEnd = ".w%i" %(window)
selectedPcal = pcal
numChans = pcal._numChans[window - 1] - 6
startChan = 4
log.writeComment("Found the noise source to use as passband.")
if(not found) :
log.writeComment("Did not find a quasar or noise source to use as a passband calibrator, now searching for a planet.")
for pcal in objects._passcals :
if(found) :
break
if(pcal._bandwidths[window - 1] == objects._sources[0]._bandwidths[window - 1] or (globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1]))) :
if(pcal._type == sources.PLANET) :
found = True
startChan = 4
# if the data are taken in hybrid mode then make sure we get the right files
if(globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1])) :
passcal = pcal._file + ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1],window)
passcalFile = pcal._file
passcalEnd = ".%i.w%i" %(objects._sources[0]._bandwidths[window - 1],window)
selectedPcal = pcal
numChans = pcal._hybridChans.get(objects._sources[0]._bandwidths[window - 1])
else :
if(pcal.getBandwidth(window) == 62) :
numChans = pcal._numChans[window - 1] - 6
else :
numChans = pcal._numChans[window - 1]
startChan = 1
passcal = pcal._file + ".w%i" %(window)
passcalFile = pcal._file
passcalEnd = ".w%i" %(window)
selectedPcal = pcal
numChans = pcal._numChans[window - 1] - 6
log.writeComment("Found a planet to use as passband.")
# only the noise source is useable for the narrow windows
if(tempPcal != None) :
pcal = tempPcal
else :
for pcal in objects._passcals :
if(found) :
break
if(pcal._bandwidths[window - 1] == objects._sources[0]._bandwidths[window - 1] or (globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1]))) :
if(pcal._type == sources.NOISE) :
found = True
# if the data are taken in hybrid mode then make sure we get the right files
if(globals.hybrid() and pcal._hybridConf.get(objects._sources[0]._bandwidths[window - 1])) :
passcal = pcal._file + ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1], window)
passcalFile = pcal._file
passcalEnd = ".%i.w%i" % (objects._sources[0]._bandwidths[window - 1], window)
selectedPcal = pcal
numChans = pcal._hybridChans.get(objects._sources[0]._bandwidths[window - 1])
else :
passcal = pcal._file + ".w%i" %(window)
passcalFile = pcal._file
passcalEnd = ".w%i" %(window)
selectedPcal = pcal
numChans = pcal._numChans[window - 1]
if(not found) :
#cannot do passband
log.writeComment("No appropriate sources found for passband. No passband correction done.")
return
log.writeComment("Using %s as passband calibrator" % (passcal))
# calculate the passband
log.writeComment("Calculate the bandpass.")
anyFlag = True
pcfile = ""
pcend = ""
pcal = selectedPcal
if("passcal" in passcalFile) :
pcfile = "passcal"
pcend = passcalFile[7:]
else :
pcfile = passcalFile
pcend = passcalEnd
# if the channel width is too small, average more channels together to get good S/N
avgChan = 1
if(pcal._type != sources.NOISE and pcal.getChannelWidth(window) < 0.75) :
temp = 0.75/pcal.getChannelWidth(window)
temp2 = int(temp/2.0) + 1
avgChan = int(temp2*2)
numChans /= int(temp2*2)
objects.updateIndividualNumChans(window,numChans * avgChan + ((startChan - 1) * 2))
args = []
while(anyFlag) :
log.run("rm -rf %s/gains %s/bandpass" % (passcalFile,passcalFile),[],logit=False)
args = []
args.append(globals.Variable("vis",pcfile,pcend))
args.append(globals.Variable("interval","1"))
args.append(globals.Variable("line","channel,%i,%i,%i,%i" % (numChans,startChan,avgChan,avgChan)))
args.append(globals.Variable("refant",str(refant)))
args.append(globals.Variable("tol","0.001"))
sys = log.run("mfcal",args,logit=False)
if(sys != 0) :
return
# flag bad bandpass solutions
#if(not globals.isSci2 or not objects._passcals[0].isSuper(window)) :
anyFlag = flagging.flagByBandpass(pcfile,pcend,chanList)
sys = log.run("mfcal",args,logit=True,execute=False)
# apply the passband to all sources, gaincals, and fluxcals
fEnd = ""
if(pcal.isSuper(window)) :
if(window <= len(pcal._bandwidths)/2) :
fEnd = ".LSB"
else :
fEnd = ".USB"
else :
fEnd = ".w%i" % (window)
for source in objects._sources :
log.writeAll("\n")
log.writeComment("Copying bandpass to source file %s" % (source._file))
args = []
args.append(globals.Variable("vis",pcfile,pcend))
args.append(globals.Variable("out",source._file,fEnd))
args.append(globals.Variable("options","nocal"))
log.run("gpcopy",args)
args = []
args.append(globals.Variable("vis",source._file,fEnd))
args.append(globals.Variable("out",source._file,"." + fileEnd + "; sleep 3; rm -r"))
args.append(globals.Variable(None,source._file,"%s/*; rm -r " % (fEnd)))
args.append(globals.Variable(None,source._file,"%s; sleep 3; mv" % (fEnd)))
args.append(globals.Variable(None,source._file,"." + fileEnd))
args.append(globals.Variable(None,source._file,fEnd))
log.run("uvcat",args)
log.writeAll("\n")
for gcal in objects._gaincals :
if(gcal._file != passcal) :
log.writeAll("\n")
log.writeComment("Copying bandpass to gaincal file %s" % (gcal._file))
args = []
args.append(globals.Variable("vis",pcfile,pcend))
args.append(globals.Variable("out",gcal._file,fEnd))
args.append(globals.Variable("options","nocal"))
log.run("gpcopy",args)
args = []
args.append(globals.Variable("vis",gcal._file,fEnd))
args.append(globals.Variable("out",gcal._file,"." + fileEnd + "; sleep 3; rm -r"))
args.append(globals.Variable(None,gcal._file,"%s/*; rm -r " % (fEnd)))
args.append(globals.Variable(None,gcal._file,"%s; sleep 3; mv" % (fEnd)))
args.append(globals.Variable(None,gcal._file,"." + fileEnd))
args.append(globals.Variable(None,gcal._file,fEnd))
log.run("uvcat",args)
log.writeAll("\n")
for fcal in objects._fluxcals :
if(fcal._file != passcal) :
log.writeAll("\n")
log.writeComment("Copying bandpass to flux file %s" % (fcal._file))
args = []
args.append(globals.Variable("vis",pcfile,pcend))
args.append(globals.Variable("out",fcal._file,fEnd))
args.append(globals.Variable("options","nocal"))
log.run("gpcopy",args)
args = []
args.append(globals.Variable("vis",fcal._file,fEnd))
args.append(globals.Variable("out",fcal._file,"." + fileEnd + "; sleep 3; rm -r"))
args.append(globals.Variable(None,fcal._file,"%s/*; rm -r " % (fEnd)))
args.append(globals.Variable(None,fcal._file,"%s; sleep 3; mv" % (fEnd)))
args.append(globals.Variable(None,fcal._file,"." + fileEnd))
args.append(globals.Variable(None,fcal._file,fEnd))
log.run("uvcat",args)
log.writeAll("\n")