-
Notifications
You must be signed in to change notification settings - Fork 1
/
CHILES_pipe_split.py
232 lines (189 loc) · 6.91 KB
/
CHILES_pipe_split.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
# CHILES_pipe_split.py
# This module is run after the user is satisfied with the results of the CHILES
# pipeline.
# 9/21/16 DJP
# 1/10/17 DJP: Include html, logs, and plots in FINAL directory
logprint ("Starting CHILES_pipe_split.py", logfileout='logs/split.log')
time_list=runtiming('split', 'start')
# If running this module right after starting casa, must run CHILES_pipe_restore first.
# Step 1, load needed functions.
import copy
import numpy as np
import pylab as pylab
import re as re
import sys
# Set up directory to save results
if os.path.exists("FINAL")==False:
os.system("mkdir FINAL")
logprint ('Split calibrated deepfield uv data', logfileout='logs/split.log')
# Do final flag summary and compile statistics.
# Summary of flagging, after final flagging (for testing purposes only)
logprint ("Summary of flags after flagging target", logfileout='logs/split.log')
default('flagdata')
vis=ms_active
mode='summary'
spw='0~14'
field='deepfield' # Only flagged deepfield, no need to check others here.
correlation='RR,LL'
spwchan=True
spwcorr=True
basecnt = True
action='calculate'
s_t=flagdata()
target_flag=s_t['flagged']/s_t['total']
logprint ("Final percentage of all data flagged: "+str(target_flag*100)+'%', logfileout='logs/split.log')
# Save final version of flags
logprint("Saving flags before split",logfileout='logs/target.log')
default('flagmanager')
vis=ms_active
mode='save'
versionname='finalflags'
comment='Final flags saved before split'
merge='replace'
flagmanager()
logprint ("Flag column saved to "+versionname, logfileout='logs/split.log')
# Step 3: Diagnostic Plots
logprint("Making diagnostic plots",logfileout='logs/split.log')
# Make plot of flagging statistics
# s_t is the output of flagdata run (above)
# Get information for flagging percentage vs. uvdistance
#gantdata = get_antenna_data(ms_active)
#create adictionary with flagging info
#base_dict = create_baseline_dict(ms_active, gantdata)
#gantdata and base_dict are already in the initial module so no need to retrieve that information again.
#match flagging data to dictionary entry
datamatch = flag_match_baseline(s_t['baseline'], base_dict)
#bin the statistics
binned_stats = bin_statistics(datamatch, 'B', 25) # 25 is the number of uvdist bins such that there is minimal error in uvdist.
#Plot flagging % vs. uvdist
### Plot the Data
barwidth = binned_stats[0][1]
totflagged = 'Target Flagging: '+ str(target_flag*100) + '% Data Flagged'
pylab.close()
pylab.bar(binned_stats[0],binned_stats[1], width=barwidth, color='grey', align='edge')
pylab.title(totflagged)
pylab.grid()
pylab.ylabel('flagged data [%]')
pylab.xlabel('average UV distance [m]')
pylab.savefig('finaltarget_flag_uvdist.png')
pylab.close()
os.system("mv finaltarget_flag_uvdist.png plots/.")
# Make plot of percentage of data flagged as a function of channel (for both correlations combined):
flag_frac=[]
ct=-1
chan=[]
freq=[]
flagged=[]
totals=[]
# Extract frequency of first channel of spw=0 from listobs output
nu0=reference_frequencies[0]/1.e6 #get reference frequency in MHz
dnu=0.015625 # channel width in MHz
freq=[]
for s in range(15):
for c in range(2048):
ct+=1
chan.append(ct)
freq.append(nu0+dnu*ct)
flagged.append(s_t['spw:channel'][str(s)+':'+str(c)]['flagged'])
totals.append(s_t['spw:channel'][str(s)+':'+str(c)]['total'])
flag_frac.append(flagged[ct]/totals[ct])
# Make updated plot of fraction of data flagged.
fig=pylab.figure()
pylab.plot(freq,flag_frac,'k-')
pylab.xlim(940.,1445.)
pylab.xlabel('Frequency [MHz]')
pylab.ylabel('Fraction of data flagged')
pylab.savefig("finaltarget_flag.png")
pylab.close(fig)
# Write flagging statistics to file
flagstatname=msname.rstrip('ms') + 'flag_stats.txt'
np.savetxt(flagstatname,np.column_stack((freq,flagged,totals,flag_frac)),fmt='%s',header='freq [MHz] #flags #total %flagged')
# Split with no averaging.
outputms=ms_active[:-3]+'_calibrated_deepfield.ms'
targetfile=outputms
# Delete final MS if already present
if os.path.exists(targetfile):
os.system("rm -rf "+targetfile)
os.system("rm -rf FINAL/"+targetfile)
# Split full spectral, temporal resolution of Target:
default('oldsplit')
vis=ms_active
datacolumn='corrected'
outputvis=targetfile
field='deepfield'
spw ='0~14'
width=1
timebin=''
oldsplit()
os.system("mv "+targetfile+" FINAL/")
# Split with temporal and spectral smoothing.
outputms=ms_active[:-3]+'_calibrated_deepfield_SMOOTH.ms'
targetfile=outputms
# Delete final MS if already present
if os.path.exists(targetfile):
os.system("rm -rf "+targetfile)
os.system("rm -rf FINAL/"+targetfile)
# Smooth to 16s time resolution and 62.4 kHz channels
default('oldsplit')
vis=ms_active
datacolumn='corrected'
outputvis=targetfile
field='deepfield'
spw ='0~14'
width=4
timebin='16s'
oldsplit()
os.system("mv "+targetfile+" FINAL/")
# Save calibration tables
if os.path.exists('antposcal.p')==True:
os.system("cp -r antposcal.p FINAL/.")
os.system("cp -r gain_curves.g FINAL/.")
os.system("cp -r finaldelay.k FINAL/.")
os.system("cp -r finalBPcal.b FINAL/.")
os.system("cp -r finalphase_scan.gcal FINAL/.")
os.system("cp -r finalphase_int.gcal FINAL/.")
os.system("cp -r finalamp.gcal FINAL/.")
os.system("cp -r finalflux.gcal FINAL/.")
# Save flagging commands & statistics
os.system("cp manual_flags_*.txt FINAL/.")
os.system("cp *flag_stats.txt FINAL/.")
# Save all Flagversions tables
os.system("cp -r "+ms_active+".flagversions FINAL/.")
#Move plots, images to sub-directory, and save those.
os.system("mv *.png plots")
#Create webpage with results
if os.path.exists("split.html"):
os.system("rm split.html")
wlog = open("split.html","w")
wlog.write('<html>\n')
wlog.write('<head>\n')
wlog.write('<title>CHILES Pipeline Web Log</title>\n')
wlog.write('<style>table tr {page-break-inside: avoid}</style>\n')
wlog.write('</head>\n')
wlog.write('<body>\n')
wlog.write('<br>\n')
wlog.write('<hr>\n')
wlog.write('<center>CHILES_pipe_split results:</center>')
wlog.write('<li> Session: '+SDM_name+'</li>\n')
wlog.write('<li><a href="logs/split.log">Split Log</a></li>\n')
wlog.write('<li> Percentage of data flagged as a function of frequency: \n')
wlog.write('<br><img src="plots/finaltarget_flag.png">\n')
wlog.write('<li> Percentage of data flagged as a function of uvdistance: \n')
wlog.write('<br><img src="plots/finaltarget_flag_uvdist.png">\n')
wlog.write('<br>')
wlog.write('<li><a href="logs/timing.log">Timing Log</a></li>\n')
wlog.write('<hr>\n')
wlog.write('</body>\n')
wlog.write('</html>\n')
wlog.close()
# Copy html files and plots to FINAL directory
os.system("cp -r *html FINAL/.")
os.system("cp -r plots FINAL/.")
logprint ("Finished CHILES_pipe_split.py", logfileout='logs/split.log')
time_list=runtiming('split', 'end')
pipeline_save()
# Save variable values
os.system("cp -r pipeline_shelf.restore FINAL/.")
# Copy logs to FINAL directory (needs to be after final "save" to preserve all information
os.system("cp *.log logs")
os.system("cp -r logs FINAL/.")