-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_calibrate.sh
executable file
·319 lines (279 loc) · 8.81 KB
/
run_calibrate.sh
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
#!/usr/bin/env bash
# Originaly written by Tom Hassall
# Extensively modified by John Swinbank
# Updates by George Heald
#
# Bug reports, patches etc to <[email protected]>
# If anything goes wrong, we'll print a helpful(-ish) messge and exit
COLOUR_RED_BOLD=`tput setaf 1 && tput bold`
COLOUR_YELLOW_BOLD=`tput setaf 3 && tput bold`
COLOUR_GREEN_BOLD=`tput setaf 2 && tput bold`
COLOUR_NONE=`tput sgr0`
error()
{
echo ${COLOUR_RED_BOLD}[`date +%FT%T` ERR ]${COLOUR_NONE} ${1}
}
warning()
{
echo ${COLOUR_YELLOW_BOLD}[`date +%FT%T` WARN]${COLOUR_NONE} ${1}
}
log()
{
echo ${COLOUR_GREEN_BOLD}[`date +%FT%T` INFO]${COLOUR_NONE} ${1}
}
failure()
{
error "${BASH_COMMAND} failed"
exit 1
}
# For safety reasons, failures are fatal
trap failure ERR
# Default values; can be overriden on command line
CAL_PARSET=cal.parset
CORRECT_PARSET=correct.parset
PHASE_PARSET=phaseonly.parset
DUMMY_MODEL=/home/hassall/MSSS/dummy.model
CLOBBER=FALSE
ROBUST=FALSE
AUTO_FLAG_STATIONS=FALSE
COLLECT=FALSE
CAL_MODEL=NONE
CAL_BEAM=NONE
declare -a BAD_STATION_LIST
declare -i ctr=0 # length of BAD_STATION_LIST
usage() {
echo -e "Usage:"
echo -e " ${0} [options] <obs_id> <beam> <band> <skymodel> \n"
echo -e "Processing of calibrator band:"
echo -e " -b Calibrator beam (default: ${CAL_BEAM})"
echo -e " -m Model for calibration of calibrator (default: ${CAL_MODEL})"
echo -e " -a Parset for calibration of calibrator (default: ${CAL_PARSET})"
echo -e " -g Parset applying gain calibration to target (default: ${CORRECT_PARSET})"
echo -e " -d Dummy sky model for use in applying gains (default: ${DUMMY_MODEL})\n"
echo -e "Other options with string arguments:"
echo -e ' -o Output filename (default: ${obs_id}_SAP00${beam}_BAND${band}.MS)'
echo -e " -p Parset for phase-only calibration of target (default: ${PHASE_PARSET})"
echo -e " -s Flag a specific station in the output\n"
echo -e "Options which take no argument:"
echo -e " -c Collect data prior to processing"
echo -e " -r Robust mode: continue even if some subbands are not available"
echo -e " -f Automatically identify & flag bad stations"
echo -e " -w Overwrite output file if it already exists"
echo -e " -h Display this message\n"
echo -e "Example:"
echo -e " ${0} L42025 0 6 sky.model"
}
while getopts ":o:b:m:a:g:p:d:s:crfhw" opt; do
case $opt in
o)
OUTPUT_NAME=${OPTARG}
;;
b)
cal_band=`printf %02d $((${OPTARG}*8))`
;;
a)
CAL_PARSET=${OPTARG}
;;
m)
CAL_MODEL=${OPTARG}
;;
g)
CORRECT_PARSET=${OPTARG}
;;
p)
PHASE_PARSET=${OPTARG}
;;
d)
DUMMY_MODEL=${OPTARG}
;;
c)
COLLECT=TRUE
;;
r)
ROBUST=TRUE
;;
w)
CLOBBER=TRUE
;;
f)
AUTO_FLAG_STATIONS=TRUE
;;
s)
BAD_STATION_LIST[$((ctr++))]=${OPTARG}
;;
h)
usage
exit 0
;;
\?)
echo -e "Invalid option: -${OPTARG}\n"
usage
exit 1
;;
esac
done
# Now read out required positional parameters
shift $(( OPTIND-1 ))
if [ $# -ne 4 ]; then
usage
exit 1
fi
obs_id=${1}
beam=${2}
band=${3}
skyModel=${4}
log "Starting ${0}"
if [ ! ${OUTPUT_NAME} ]; then
OUTPUT_NAME=${obs_id}_SAP00${beam}_BAND${band}.MS
fi
if [ -d ${OUTPUT_NAME} ]; then
if [ ${CLOBBER} = "TRUE" ]; then
log "Removing ${OUTPUT_NAME}"
rm -rf ${OUTPUT_NAME}
else
error "${OUTPUT_NAME} already exists; aborting"
exit 1
fi
fi
WORK_NAME=${OUTPUT_NAME}.tmp
rm -rf ${WORK_NAME}
test -d log || mkdir log
test -d plots || mkdir plots
targ_band=`printf %02d $(($beam*8+$band))`
if [ ${COLLECT} = "TRUE" ]; then
trap ERR # scp commands will fail by design!
log "Collecting data"
for node in {01..100} ; do
echo locus$node
done | xargs -n1 -P4 -Ihost scp -r host:/data/scratch/pipeline/${obs_id}*/*SB{${targ_band},${cal_band}}?_target_sub* . > log/collect.log 2>&1
log "Data collected"
trap failure ERR
fi
require_file() {
if [ ! -f ${1} ]; then
error "${1} does not exist"
exit 1
fi
}
require_file ${skyModel}
require_file ${PHASE_PARSET}
if [ ${cal_band} ]; then
# Check for required inputs.
require_file ${CAL_MODEL}
require_file ${CAL_PARSET}
require_file ${CORRECT_PARSET}
require_file ${DUMMY_MODEL}
# We'll need these sourcedbs a number of times, so might as well build them
# once and reuse.
log "Building calibrator sourcedb"
test -d sky.calibrator && rm -rf sky.calibrator
makesourcedb in=${CAL_MODEL} out=sky.calibrator format='<' > log/make_calibrator_sourcedb.log 2>&1
log "Building dummy sourcedb"
test -d sky.dummy && rm -rf sky.dummy
makesourcedb in=${DUMMY_MODEL} out=sky.dummy format='<' > log/make_dummy_sourcedb.log 2>&1
fi
process_subband() {
num=${1}
source=${2}
cal=${3}
trap failure ERR # Enable error handler in subshell
log "Starting work on ${source}"
log "Calibrating ${cal}"
calibrate-stand-alone --replace-parmdb --sourcedb sky.calibrator ${cal} ${CAL_PARSET} ${CAL_MODEL} > log/calibrate_cal_${num}.txt 2>&1
log "Zapping suspect points in ${cal}/instrument"
~swinbank/edit_parmdb/edit_parmdb.py --sigma=1 --auto ${cal}/instrument/ > log/edit_parmdb_${num}.txt 2>&1
log "Making diagnostic plots for ${cal}/instrument"
~heald/bin/solplot.py -q -m -o SB${cal_band}${num} ${cal}/instrument/ > log/solplot_${num}.txt 2>&1
log "Calibrating ${source}"
calibrate-stand-alone --sourcedb sky.dummy --parmdb ${cal}/instrument ${source} ${CORRECT_PARSET} /home/hassall/MSSS/dummy.model > log/calibrate_transfer_${num}.txt 2>&1
log "Finished ${source}"
}
declare -a SUBBAND_LIST
declare -a CALBAND_LIST
declare -i bandctr=0
for num in {0..9}; do
# Check if required data exists
source=${obs_id}_SAP00${beam}_SB${targ_band}${num}_target_sub.MS.dppp
cal=${obs_id}_SAP002_SB${cal_band}${num}_target_sub.MS.dppp
if [ -d ${source} ]; then
SUBBAND_LIST[${bandctr}]=${source}
else
warning "Target ${source} and not found"
if [ ${ROBUST} != "TRUE" ]; then
error "Missing data"
exit 1
fi
fi
if [ ${cal_band} ]; then
if [ -d ${cal} ]; then
CALBAND_LIST[${bandctr}]=${cal}
else
warning "Calibrator ${cal} and not found"
if [ ${ROBUST} != "TRUE" ]; then
error "Missing data"
exit 1
fi
fi
fi
let bandctr=${bandctr}+1
done
if [ ${cal_band} ]; then
bandctr=0
if [ ${SUBBAND_LIST} ]; then
for source in ${SUBBAND_LIST[@]}; do
process_subband ${bandctr} ${source} ${CALBAND_LIST[$bandctr]} &
child_pids[num]=$!
let bandctr=${bandctr}+1
done
for pid in ${child_pids[*]}; do
# Wait for all bands to be processed
wait $pid
done
else
error "No data to process"
exit 1
fi
mv SB*.pdf plots
fi
log "Combining subbands"
OLDIFS=${IFS}
IFS="," # We need to separate subbands with commas for NDPPP
cat >NDPPP.parset <<-EOF
msin=[${SUBBAND_LIST[*]}]
msin.missingdata=true
msin.orderms=false
msin.datacolumn=CORRECTED_DATA
msin.baseline=[CR]S*&
msout=${WORK_NAME}
steps=[]
EOF
IFS=${OLDIFS}
NDPPP NDPPP.parset > log/ndppp_log.txt 2>&1
log "Removing RFI"
rficonsole -indirect-read ${WORK_NAME} > log/rficonsole.txt 2>&1
log "Starting phase-only calibration of ${WORK_NAME}"
calibrate-stand-alone -f ${WORK_NAME} ${PHASE_PARSET} ${skyModel} > log/calibrate_phaseonly.txt 2>&1
log "Finished phase-only calibration"
mv calibrate-stand-alone*log log
if [ ${AUTO_FLAG_STATIONS} = "TRUE" ]; then
log "Identifying bad stations"
rm -rf stats
~martinez/plotting/asciistats.py -i ${WORK_NAME} -r stats > log/asciistats.txt
~martinez/plotting/statsplot.py -i `pwd`/stats/${WORK_NAME}.stats -o ${obs_id} > log/statsplot.txt
for station in `grep True$ ${obs_id}.tab | cut -f2`; do
BAD_STATION_LIST[$((ctr++))]=${station}
done
fi
if [ ${BAD_STATION_LIST} ]; then
log "Flagging stations: ${BAD_STATION_LIST[*]}"
FILTER=`echo "!${BAD_STATION_LIST[*]}" | sed -e's/ /;!/g'`
else
FILTER=""
fi
msselect in=${WORK_NAME} out=${OUTPUT_NAME} baseline=${FILTER} deep=True > log/msselect.log 2>&1
if [ -d ${WORK_NAME}/instrument ] && [ ! -d ${OUTPUT_NAME}/instrument ]; then
cp -r ${WORK_NAME}/instrument ${OUTPUT_NAME}
fi
rm -rf ${WORK_NAME}
log "Data written to ${OUTPUT_NAME}"
log "${0} finished"