-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootflux.py
360 lines (342 loc) · 16.8 KB
/
bootflux.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
import sources
import calculations
import globals
import logger as log
from pipeline_miriadwrap import *
import calflux
import math
try :
import preferences as p
except ImportError:
import defaultPreferences as p
haveLog = False
try :
import fluxlog
haveLog = True
except :
pass
"""
Module to calculate the flux of the gain calibrator(s) by bootstrapping from flux calibrator(s) or by looking up the flux in FluxSource.cat as a fallback
Part of the CARMA data reduction pipeline
Author: D. N. Friedel
"""
averagingTime = p.preferences.get("bootfluxInterval")
def runBootflux(project,gaincal, fluxcal, obsDate, obsFreq=95.0) :
""" Method to calculate the flux of the gain calibrator(s)
by bootstrapping from flux calibrator(s) or by looking up
the flux in FluxSource.cat as a fallback
input :
gaincal - the gain calibrator
fluxcal -the flux calibrator
obsDate - observation date
obsFreq - observation frequency in GHz
returns :
the flux of gaincal in Jy
"""
global averagingTime
fluxes = []
rms = []
log.writeComment("Using %s as flux calibrator" % (fluxcal._name))
# prefer a planet for bootflux
# it is done one window at a time and then averaged to get flux
if(fluxcal._type == sources.PLANET) :
# only rely on super windwos if we have them
if(fluxcal.haveSuper()) :
if(gaincal._lsbGood) :
args = []
args.append(globals.Variable("vis",gaincal._file,".LSB"))
args.append(globals.Variable("ADD",fluxcal._file,".LSB"))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("line","channel,1,1,%i" % (fluxcal.getSuperNumChans())))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.LSB.log" % (gaincal._file)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.LSB.log" % (gaincal._file),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for the LSB of %s\n" % (fluxes[0],gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
if(gaincal._usbGood) :
indx = 1
if(globals.isSci2 and globals.obsFreq() > 50.0) :
indx = 0
args = []
args.append(globals.Variable("vis",gaincal._file,".USB"))
args.append(globals.Variable("ADD",fluxcal._file,".USB"))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("line","channel,1,1,%i" % (fluxcal.getSuperNumChans())))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.USB.log" % (gaincal._file)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.USB.log" % (gaincal._file),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for the USB of %s\n" % (fluxes[indx],gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
flux,uncert = calculations.weightedAverage(fluxes, rms)
if(flux != 0.0) :
fluxlog.writeLog(project,gaincal._name,flux,uncert,obsFreq,"WB",obsDate,fluxcal._name)
else :
for window in range(1, len(gaincal._bandwidths) + 1):
args = []
args.append(globals.Variable("vis",gaincal._file,".w%i" % (window)))
args.append(globals.Variable("ADD",fluxcal._file,".w%i" % (window)))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("line","channel,1,1,%i" % (gaincal._numChans[window - 1])))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.w%i.log" % (gaincal._file,window)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.w%i.log" % (gaincal._file,window),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for window %i of %s\n" % (fluxes[window - 1], window,gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
flux,uncert = calculations.weightedAverage(fluxes, rms)
if(flux == 0.0) :
log.writeComment("Obtaining flux from catalog.")
return getCatalogFlux(gaincal._name, obsDate, obsFreq)
else :
pass
#fluxlog.writeLog(project,gaincal._name,flux,uncert,obsFreq,"NB",obsDate,fluxcal._name)
return flux
# if we do not have a planet then using MWC349 (if present), since is 1.0 Jy at 3mm and 1mm
elif(fluxcal._name == "MWC349") :
mflux = 1.0
if(obsFreq > 120.0) :
mflux = 1.8
log.writeComment("Assuming a flux of %f for MWC349" % (mflux))
if(fluxcal.haveSuper()) :
if(gaincal._lsbGood) :
args = []
args.append(globals.Variable("vis",gaincal._file,".LSB"))
args.append(globals.Variable("ADD",fluxcal._file,".LSB"))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("ADD",str(mflux)))
args.append(globals.Variable("line","channel,1,1,%i" % (fluxcal.getSuperNumChans())))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.LSB.log" % (gaincal._file)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.LSB.log" % (gaincal._file),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for the LSB of %s\n" % (fluxes[0],gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
if(gaincal._usbGood) :
args = []
args.append(globals.Variable("vis",gaincal._file,".USB"))
args.append(globals.Variable("ADD",fluxcal._file,".USB"))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("ADD",str(mflux)))
args.append(globals.Variable("line","channel,1,1,%i" % (fluxcal.getSuperNumChans())))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.USB.log" % (gaincal._file)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.USB.log" % (gaincal._file),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for the USB of %s\n" % (fluxes[0],gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
flux,uncert = calculations.weightedAverage(fluxes, rms)
if(flux != 0.0) :
fluxlog.writeLog(project,gaincal._name,flux,uncert,obsFreq,"WB",obsDate,fluxcal._name + "(" + str(mflux) + ")")
else :
for window in range(1, len(gaincal._bandwidths) + 1):
args = []
args.append(globals.Variable("vis",gaincal._file,".w%i" % (window)))
args.append(globals.Variable("ADD",fluxcal._file,"w%i" % (window)))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("ADD",str(mflux)))
args.append(globals.Variable("line","channel,1,1,%i" % (gaincal._numChans[window - 1])))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.w%i.log" % (gaincal._file,window)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.w%i.log" % (gaincal._file,window),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for window %i of %s\n" % (fluxes[1],gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
flux,uncert = calculations.weightedAverage(fluxes, rms)
if(flux == 0.0) :
log.writeComment("Obtaining flux from catalog.")
return getCatalogFlux(gaincal._name, obsDate, obsFreq)
else :
pass
#fluxlog.writeLog(project,gaincal._name,flux,uncert,obsFreq,"NB",obsDate,fluxcal._name + "(" + str(mflux) + ")")
return flux
else :
# use a gain calibrator as last resort
log.writeComment("Obtaining flux for %s from catalog." % (fluxcal._name))
mflux = getCatalogFlux(fluxcal._name, obsDate, obsFreq)
if(gaincal._name == fluxcal._name) :
return mflux
if(fluxcal.haveSuper()) :
if(gaincal._lsbGood) :
args = []
args.append(globals.Variable("vis",gaincal._file,".LSB"))
args.append(globals.Variable("ADD",fluxcal._file,".LSB"))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("ADD",str(mflux)))
args.append(globals.Variable("line","channel,1,1,%i" % (fluxcal.getSuperNumChans())))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.LSB.log" % (gaincal._file)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.LSB.log" % (gaincal._file),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for the LSB of %s\n" % (fluxes[0],gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
if(gaincal._usbGood) :
args = []
args.append(globals.Variable("vis",gaincal._file,".USB"))
args.append(globals.Variable("ADD",fluxcal._file,".USB"))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("ADD",str(mflux)))
args.append(globals.Variable("line","channel,1,1,%i" % (fluxcal.getSuperNumChans())))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.USB.log" % (gaincal._file)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.USB.log" % (gaincal._file),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for the USB of %s\n" % (fluxes[1] ,gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
flux,uncert = calculations.weightedAverage(fluxes, rms)
if(flux == 0.0) :
log.writeComment("Obtaining flux from catalog.")
return getCatalogFlux(gaincal._name, obsDate, obsFreq)
else :
fluxlog.writeLog(project,gaincal._name,flux,uncert,obsFreq,"WB",obsDate,fluxcal._name + "(" + str(mflux) + ")")
else :
for window in range(1, len(gaincal._bandwidths) + 1):
mflux = getCatalogFlux(fluxcal._name, obsDate, obsFreq)
if(gaincal._name == fluxcal._name) :
return mflux
args = []
args.append(globals.Variable("vis",gaincal._file,".w%i" % (window)))
args.append(globals.Variable("ADD",fluxcal._file,".w%i" % (window)))
args.append(globals.Variable("primary",fluxcal._name))
args.append(globals.Variable("ADD",str(mflux)))
args.append(globals.Variable("line","channel,1,1,%i" % (gaincal._numChans[window - 1])))
args.append(globals.Variable("taver",str(averagingTime)))
args.append(globals.Variable("log","boot.%s.w%i.log" % (gaincal._file,window)))
sys = log.run("bootflux",args)
if(sys == 0) :
if(getFlux("boot.%s.w%i.log" % (gaincal._file,window),fluxes, rms)) :
log.writeComment("Bootflux got a flux of %f for window %i of %s\n" % (fluxes[window - 1], window,gaincal._name))
else :
fluxes.append(0.0)
rms.append(0.0)
else :
fluxes.append(0.0)
rms.append(0.0)
flux,uncert = calculations.weightedAverage(fluxes, rms)
if(flux == 0.0) :
log.writeComment("Obtaining flux from catalog.")
return getCatalogFlux(gaincal._name, obsDate, obsFreq)
else :
fluxlog.writeLog(project,gaincal._name,flux,uncert,obsFreq,"WB",obsDate,fluxcal._name + "(" + str(mflux) + ")")
return flux
def getFlux(bootFile,fluxes, rms) :
""" Method to read in the bootflux log and get the average flux
input :
fluxes - a list to hold the fluxes
rms - a list to hold their rms uncertainties
returns :
True/False if the flux was found
"""
# open the input file
input = open(bootFile,'r')
fileList = input.readlines()
input.close()
# get the average flux value and rms and add them to the lists
while(len(fileList) > 0) :
line = fileList.pop()
if("Average Flux" in line) :
splitLine = line.split()
if(splitLine[2][0].isdigit() and splitLine[3][0].isdigit()) :
fluxes.append(float(splitLine[2]))
rms.append(float(splitLine[3]))
return True
return False
def getCatalogFlux(source, date, freq) :
""" Method to get the catalog flux for the given source based on
observation frequency (3mm/1mm) and observation date
if an exact date match is not found then the flux is linearly interpolated
from the closest dates
input :
source - the source name
date - observation date
freq - frequency in GHz
returns :
the flux of the source
"""
scaleFactor = 1.0 #only needed if we are scaling from 3mm to 1mm
fluxes = None
fluxes = calflux.calflux(source,freq)
if(len(fluxes) == 0 and freq > 130) :
log.writeComment("No 1mm fluxes found, scaling the 3mm flux")
fluxes = calflux.calflux(source,95.0)
scaleFactor = 0.7
# read in the flux file
if(len(fluxes) == 0) :
log.writeComment("No flux found for %s in FluxSource.cat" % (source))
return 0.0
low = dict()
hi = dict()
# sort the dates and find the 2 date bracketing the observation date
# but only accept fluxes within 1 year of observation date
for f in fluxes :
fluxDate = f
delta = date - fluxDate
if(delta <= 1.0 and delta > 0) :
low[fluxDate] = fluxes[f]
elif(delta >= -1.0 and delta < 0) :
hi[fluxDate] = fluxes[f]
else :
return fluxes[f]
if(len(low) == 0) :
if(len(hi) == 0) :
log.writeComment("No flux found for %s in FluxSource.cat" % (source))
return 0.0
log.writeComment("Found flux of %d for %s in FluxSource.cat" % (hi.get(min(hi)), source))
return hi.get(min(hi))
elif(len(hi) == 0) :
log.writeComment("Found flux of %d for %s in FluxSource.cat" % (low.get(max(low)), source))
return low.get(max(low))
minKey = max(low)
maxKey = min(hi)
flux = calculations.linearInterpolation(minKey,low.get(minKey),maxKey,hi.get(maxKey),date)*scaleFactor
log.writeComment("Found flux of %d for %s in FluxSource.cat" % (flux, source))
return flux