-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhybrid.py
226 lines (211 loc) · 11.3 KB
/
hybrid.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
import sources
import globals
import logger as log
import random
try :
import preferences as p
except ImportError:
import defaultPreferences as p
"""
Module for handling data taken in a hybrid configuration (i.e. source correlator bandwidths are different from gain calibrator correlator bandwidths
Part of the CARMA data reduction pipeline
Current Version (this file): 1.1
Released with pipeline version: 1.1
Author: D. N. Friedel
"""
def splitHybrid(passbandcal, numberOfWins) :
""" Method to split hybrid data into seperate files for each bandwidth and window
inputs :
passbandcal - a passband calibrator object
numberOfWins - the total number of windows
returns :
none
"""
log.writeComment("Splitting %s into separate hybrid components" % (passbandcal._name))
array = []
halfWin = numberOfWins/2
if(globals.isSci2) :
halfWin = globals.ENDWINDOW
for i in range(globals.STARTWINDOW, halfWin + 1) :
array.append(0)
for window in range(1, halfWin + 1) :
# select out the individual windows
fileEnd = str(random.randint(1,100000))
sys = log.run("rm -rf temp.w%i.%s; sleep 3",[],fatal=True)
args = []
args.append(globals.Variable("vis",passbandcal._file))
args.append(globals.Variable("select","win'('%i')'" % (window)))
args.append(globals.Variable("out","temp.w%i.%s" % (window,fileEnd)))
sys = log.run("uvcat",args,fatal=True)
if(sys != 0) :
log.writeLog("FAILED: uvcat vis=%s select=win'('%i')' out=temp.w%i%s" % (passbandcal._file, window, window,fileEnd))
exit(0)
# list all spectral data for the window
args = []
args.append(globals.Variable("vis","temp.w%i.%s" % (window,fileEnd)))
args.append(globals.Variable("options","spec"))
args.append(globals.Variable("recnum","0"))
args.append(globals.Variable("log","pcal.log"))
log.run("uvlist",args,fatal=True)
input = open("pcal.log",'r')
uvList = input.readlines()
input.close()
uvList.reverse()
bandwidths = []
nchan = 0
# gather the different bandwidth data
while(len(uvList) > 0) :
line = uvList.pop()
if("number of channels" in line) :
splitLine = line.split()
nchan = int(splitLine[4])
elif("frequency interval" in line) :
splitLine = line.split()
bw = sources.findBandwidth(float(splitLine[3])*1000.0*nchan)
bandwidths.append(bw)
passbandcal.setHybridChans(bw, nchan)
first = True
# for each bandwidth detected separate out the data for both USB and LSB windows
for bw in bandwidths :
if(bw != bandwidths[0]) :
if(first) :
passbandcal.setHybridConf(bandwidths[0])
first = False
passbandcal.setHybridConf(bw)
for config in passbandcal._hybridConf :
if(passbandcal._hybridConf.get(config)) :
fileEnd = str(random.randint(1,100000))
array[window - 1] = config
arrayString = "%i" % (array[0])
for m in range(1, len(array)) :
arrayString = arrayString + ",%i" % (array[m])
args = []
args.append(globals.Variable("vis",passbandcal._file))
args.append(globals.Variable("bw",arrayString))
args.append(globals.Variable("out","temp.pbcal." + fileEnd))
log.run("bwsel",args)
args = []
args.append(globals.Variable("vis","temp.pbcal." + fileEnd))
args.append(globals.Variable("select","win'('%i')'" % (window)))
args.append(globals.Variable("out",passbandcal._file,".%i.w%i" % (config, window)))
log.run("uvcat",args)
args = []
args.append(globals.Variable("vis","temp.pbcal." + fileEnd))
args.append(globals.Variable("select","win'('%i')'" % (window + halfWin)))
args.append(globals.Variable("out",passbandcal._file,".%i.w%i" % (config, window + halfWin)))
log.run("uvcat",args)
array[window - 1] = 0
log.run("rm -rf temp.*",[])
def calculateOffsets(passcals, refant) :
""" Method to calculate the phase offsets between wideband and narrow band observations
input :
passcals - the passband calibrator objects
refant - the reference antenna
returns :
True/False if the offset was found
"""
log.writeComment("Calculating phase offsets between windows")
found = False
for pcal in passcals :
# we do not want to use the noise source
if(pcal._type != sources.NOISE) :
found = True
# go through each window
for window in range(globals.STARTWINDOW, globals.ENDWINDOW + 1) :
# find the widest band size for the window
largest = 0
startChan = 1
numChans = 1
if(pcal._hybridConf.get(500)) :
largest = 500
numChans = pcal._numChans[window - 1]
elif(pcal._hybridConf.get(62)) :
largest = 62
startChan = 3
numChans = pcal._numChans[window - 1] - 4
elif(pcal._hybridConf.get(31)) :
largest = 31
numChans = pcal._numChans[window - 1]
else :
found = False
# selfcal the widest band to get rid of atmospheric variations
if(found) :
args = []
args.append(globals.Variable("vis",pcal._file,".%i.w%i" % (largest,window)))
args.append(globals.Variable("interval",str(p.preferences.get("selfcalInterval"))))
args.append(globals.Variable("options","phase,noscale,apriori"))
args.append(globals.Variable("refant",str(refant)))
args.append(globals.Variable("line","line=chan,1,%i,%i" % (startChan, numChans)))
log.run("mselfcal",args)
for config in pcal._hybridConf :
fileEnd = str(random.randint(1,100000))
# copy the gains to get rid of the atmospheric variations
if(pcal._hybridConf.get(config) and config != largest) :
args = []
args.append(globals.Variable("vis",pcal._file,".%i.w%i" %(largest,window)))
args.append(globals.Variable("mode","merge"))
args.append(globals.Variable("out",pcal._file,".%i.w%i" %(config,window)))
log.run("gpcopy",args,fatal=True)
args = []
args.append(globals.Variable("in",pcal._file,".%i.w%i/interval" %(config,window)))
args.append(globals.Variable("value","1"))
log.run("puthd",args)
args = []
args.append(globals.Variable("vis",pcal._file,".%i.w%i" % (config,window)))
args.append(globals.Variable("out","temp." + fileEnd + "; sleep 3; rm -rf "))
args.append(globals.Variable(None,pcal._file,".%i.w%i/*; rm -rf " % (config,window)))
args.append(globals.Variable(None,pcal._file,".%i.w%i; sleep 3; mv " % (config,window)))
args.append(globals.Variable(None,"temp." + fileEnd))
args.append(globals.Variable(None,pcal._file,".%i.w%i" % (config,window)))
log.run("uvcat",args)
# calculate the phase offsets
args = []
args.append(globals.Variable("vis",pcal._file,".%i.w%i" % (largest,window)))
args.append(globals.Variable("interval","99999"))
args.append(globals.Variable("options","phase,noscale,apriori"))
args.append(globals.Variable("refant",str(refant)))
args.append(globals.Variable("line","line=chan,1,%i,%i" % (startChan, numChans)))
log.run("mselfcal",args)
return found
def applyOffsets(passcals, insources) :
""" Method to apply the band phase offsets to the source data
input :
passcals - the passband calibrators
source - the source(s)
returns :
none
"""
# correct the offset for each source
for source in insources :
log.writeComment("Applying phase offset corrections to %s" % (source._name))
applied = [False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False] # so we do not apply more than one offset solution to each window
# loop over all passband cals
for pcal in passcals :
# ignore the noise source
if(pcal._type != sources.NOISE and len(pcal._bandwidths) == len(source._bandwidths)) :
# go through each window
for window in range(globals.STARTWINDOW, globals.ENDWINDOW + 1) :
fileEnd = str(random.randint(1,100000))
# make sure only the matching bandwidth is selected
if(source._bandwidths[window - 1] == pcal._bandwidths[window - 1] and not(applied[window - 1])) :
args = []
args.append(globals.Variable("vis",pcal._file,".%i.w%i" %(source._bandwidths[window - 1],window)))
args.append(globals.Variable("mode","merge"))
args.append(globals.Variable("out",source._file,".%i.w%i" %(source._bandwidths[window - 1],window)))
log.run("gpcopy",args,fatal=True)
args = []
args.append(globals.Variable("in",source._file,".%i.w%i/interval" %(source._bandwidths[window - 1],window)))
args.append(globals.Variable("value","1"))
log.run("puthd",args)
args = []
args.append(globals.Variable("vis",source._file,".%i.w%i" % (source._bandwidths[window - 1],window)))
args.append(globals.Variable("out","temp." + fileEnd + "; sleep 3; rm -rf "))
args.append(globals.Variable(None,source._file,".%i.w%i/*; rm -rf " % (source._bandwidths[window - 1],window)))
args.append(globals.Variable(None,source._file,".%i.w%i; sleep 3; mv " % (source._bandwidths[window - 1],window)))
args.append(globals.Variable(None,"temp." + fileEnd))
args.append(globals.Variable(None,source._file,".%i.w%i" % (source._bandwidths[window - 1],window)))
log.run("uvcat",args)
applied[window - 1] = True
for window in range(globals.STARTWINDOW, globals.ENDWINDOW + 1) :
if(not applied[window - 1]) :
log.writeComment("Could not get band offsets for %s window %i" % (source._name, window))