forked from tjgalvin/GLEAM-X-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 4
/
postimage.tmpl
288 lines (255 loc) · 10.8 KB
/
postimage.tmpl
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
#! /bin/bash -l
set -x
pipeuser=PIPEUSER
obsnum=OBSNUM
# If obsnum is a file, then we are in an array job
if [[ -f ${obsnum} ]]
then
taskid="${SLURM_ARRAY_TASK_ID}"
jobid="${SLURM_ARRAY_JOB_ID}"
echo "obsfile ${obsnum}"
obsnum=$(sed -n -e "${SLURM_ARRAY_TASK_ID}"p "${obsnum}")
echo "postimage obsid ${obsnum}"
else
taskid=1
jobid="${SLURM_JOB_ID}"
fi
echo "jobid: ${jobid}"
echo "taskid: ${taskid}"
function test_fail {
if [[ $1 != 0 ]]
then
track_task.py fail --jobid="${jobid}" --taskid="${taskid}" --finish_time="$(date +%s)"
exit "$1"
fi
}
track_task.py start --jobid="${jobid}" --taskid="${taskid}" --start_time="$(date +%s)"
# Makes fits_warp parallelisation work on Zeus
# export KMP_INIT_AT_FORK=false
# Sub-channels
subchans="0000 0001 0002 0003 MFS"
# flux_warp method
method=scaled
# Sky model
POS_MODEL_CATALOGUE="${GXBASE}/models/NVSS_SUMSS_psfcal.fits"
FLUX_MODEL_CATALOGUE="${GXBASE}/models/GGSM_sparse_unresolved.fits"
# Set max separation for flux_warp crossmatch as ~ 1' -- unlikely that the ionosphere would be that brutal
separation=$(echo "60/3600" | bc -l)
# Set exclusion for flux_warp internal exclusive crossmatch as ~ 3'
exclusion=$(echo "180/3600" | bc -l)
datadir=BASEDIR
cd "${datadir}/${obsnum}" || exit 1
metafits="${obsnum}.metafits"
if [[ ! -e ${metafits} ]] || [[ ! -s ${metafits} ]]
then
wget -O "${metafits}" http://ws.mwatelescope.org/metadata/fits?obs_id=${obsnum}
test_fail $?
fi
chan=$( pyhead.py -p CENTCHAN "$metafits" | awk '{print $3}' )
ra=$(pyhead.py -p RA "$metafits" | awk '{print $3}')
dec=$(pyhead.py -p DEC "$metafits" | awk '{print $3}')
b=$(python -c "import astropy.units as u; from astropy.coordinates import SkyCoord; print(abs(SkyCoord($ra*u.deg, $dec*u.deg).galactic.b.deg))")
minsrcs=500
if [[ "${chan}" -eq 69 ]] && (( $(echo "$b < 10" | bc -l) ))
then
minsrcs=50
fi
metafits=$(ls -t ${obsnum}*metafits* | head -1)
for subchan in ${subchans}
do
BMAJ=$(pyhead.py -p BMAJ "${obsnum}_deep-${subchan}-image-pb.fits" | awk '{print $3}' )
if [[ "$BMAJ" == "0" ]]
then
echo "${obsnum}_deep-${subchan}-image-pb.fits has zero-size PSF: something is broken!"
exit 1
fi
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_rms.fits" ]]
then
BANE --cores 1 --compress --noclobber "${obsnum}_deep-${subchan}-image-pb.fits"
fi
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_comp.fits" ]]
then
# The mask_image.py operation will destructively remove the pixels. I can not think of a case
# where we actually want to keep pixels below 5% of the PB, apart from _maybe_ causing non-uniform
# noise issues in the swarp image. In any case, the below attempts to preserve the original image
# as a just in case.
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_original.fits" ]]
then
cp -v "${obsnum}_deep-${subchan}-image-pb.fits" "${obsnum}_deep-${subchan}-image-pb_original.fits"
fi
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb.fits" ]]
then
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_original.fits" ]]
then
echo "Missing ${obsnum}_deep-${subchan}-image-pb.fits file, and no copy found. This should not happen. "
test_fail 2
fi
cp -v "${obsnum}_deep-${subchan}-image-pb_original.fits" "${obsnum}_deep-${subchan}-image-pb.fits"
fi
# Generate a weight map for mosaicking
chans=($( pyhead.py -p CHANNELS "${obsnum}.metafits" | awk '{print $3}' | sed "s/,/ /g" ))
if [[ ${subchan} == "MFS" ]]
then
i=0
j=23
else
n=${subchan:3}
i=$((n * 6))
j=$((i + 5))
fi
cstart=${chans[$i]}
cend=${chans[$j]}
lookup_beam.py "${obsnum}" \
"_deep-${subchan}-image-pb.fits" \
"${obsnum}_deep-${subchan}-image-pb-" \
-c "$cstart-$cend"
mask_image.py "${obsnum}_deep-${subchan}-image-pb.fits" \
"${obsnum}_deep-${subchan}-image-pb-XX-beam.fits" \
"${obsnum}_deep-${subchan}-image-pb-YY-beam.fits" \
--apply-mask -v
# move into place the new masked image
# TODO: this will delete the -image-pb.fits but if mask didn't work you're just left with original and not -pb.fits: fix
rm "${obsnum}_deep-${subchan}-image-pb.fits" && mv "${obsnum}_deep-${subchan}-image-pb_mask.fits" "${obsnum}_deep-${subchan}-image-pb.fits"
BANE --cores 1 --compress --noclobber "${obsnum}_deep-${subchan}-image-pb.fits"
# TODO: Replace the redirection
aegean --autoload --cores=${GXNPCPUS} --table="./${obsnum}_deep-${subchan}-image-pb.fits" "./${obsnum}_deep-${subchan}-image-pb.fits" > >(tee -a "${obsnum}_deep-${subchan}_aegean.log") 2> >(tee -a "${obsnum}_deep-${subchan}_aegean.log" >&2)
fi
nsrc=$(grep "INFO found" "${obsnum}_deep-${subchan}_aegean.log" | head -1 | awk '{print $3}')
if [[ $nsrc -lt $minsrcs ]]
then
echo "Can't warp ${obsnum} -- only $nsrc sources and minimum required id $minsrcs -- probably a horrible image"
else
RA=$( pyhead.py -p RA "${obsnum}.metafits" | awk '{print $3}' )
Dec=$( pyhead.py -p DEC "${obsnum}.metafits" | awk '{print $3}' )
chan=$( pyhead.py -p CENTCHAN "${obsnum}.metafits" | awk '{print $3}' )
mid=$( pyhead.py -p CRVAL3 "${obsnum}_deep-${subchan}-image-pb.fits" | awk '{print $3}' )
freqq=$(echo "$mid" | awk '{printf "%03.0f",($1)/1e6}')
# Roughly the centre and radius of the image:
coords="$RA $Dec"
# TODO make this dependent on CENTCHAN
radius=50. #24.8
if [[ ! -e "${obsnum}_${subchan}_complete_sources_xm.fits" ]]
then
fits_warp.py \
--incat "${obsnum}_deep-${subchan}-image-pb_comp.fits" \
--refcat "${POS_MODEL_CATALOGUE}" \
--xm "${obsnum}_${subchan}_complete_sources_xm.fits" \
--plot \
--ra1 ra \
--dec1 dec \
--ra2 RAJ2000 \
--dec2 DEJ2000 \
--infits "${obsnum}_deep-${subchan}-image-pb.fits"
fi
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_warp.fits" ]]
then
fits_warp.py --incat "./${obsnum}_deep-${subchan}-image-pb_comp.fits" \
--refcat "${POS_MODEL_CATALOGUE}" \
--corrected "./${obsnum}_deep-${subchan}-image-pb_comp_warp-corrected.fits" \
--xm "./${obsnum}_${subchan}_fits_warp_xm.fits" \
--suffix warp \
--infits "./${obsnum}_deep-${subchan}-image-pb.fits" \
--ra1 ra --dec1 dec \
--ra2 RAJ2000 --dec2 DEJ2000 \
--plot \
--nsrcs 750 \
--vm 10 \
--progress \
--testimage \
--cores ${GXNPCPUS} \
--signal peak_flux_1 \
--enforce-min-srcs 100
# --signal peak_flux --noise local_rms --SNR 10
fi
if [[ ! -e ${obsnum}_${subchan}_xm.fits ]]
then
# flux_wrap dependency here
# Match the image catalogue to the model table:
match_catalogues \
"${obsnum}_deep-${subchan}-image-pb_comp_warp-corrected.fits" \
"${FLUX_MODEL_CATALOGUE}" \
--separation "${separation}" \
--exclusion_zone "${exclusion}" \
--outname "./${obsnum}_${subchan}_xm.fits" \
--threshold 0.5 \
--nmax 1000 \
--coords ${coords} \
--radius "${radius}" \
--ra2 "RAJ2000" \
--dec2 "DEJ2000" \
--ra1 "ra" \
--dec1 "dec" \
-F "int_flux" \
--eflux "err_int_flux" \
--localrms "local_rms"
fi
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_warp_${method}_cf_output.txt" ]]
then
flux_warp \
"${obsnum}_${subchan}_xm.fits" \
"${obsnum}_deep-${subchan}-image-pb_warp.fits" \
--mode mean \
--freq "${freqq}" \
--threshold 0.5 \
--nmax 400 \
--flux_key "flux" \
--smooth 5.0 \
--ignore_magellanic \
--localrms_key "local_rms" \
--add-to-header \
--ra_key "RAJ2000" \
--dec_key "DEJ2000" \
--index "alpha" \
--curvature "beta" \
--ref_flux_key "S_200" \
--ref_freq 200.0 \
--alpha -0.77 \
--plot \
--cmap "gnuplot2" \
--update-bscale \
--order 2 \
--ext png \
--nolatex
fi
factor=$(pyhead.py -p BSCALE "${obsnum}_deep-${subchan}-image-pb_warp.fits" | awk '{print $3}')
# The RMS and BKG maps will not have changed much from the ionospheric warping, so I can just
# rename them and update BSCALE
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_rms.fits" ]]
then
BANE --cores 1 --compress --noclobber "${obsnum}_deep-${subchan}-image-pb.fits"
fi
mv "${obsnum}_deep-${subchan}-image-pb_rms.fits" "${obsnum}_deep-${subchan}-image-pb_warp_rms.fits"
mv "${obsnum}_deep-${subchan}-image-pb_bkg.fits" "${obsnum}_deep-${subchan}-image-pb_warp_bkg.fits"
pyhead.py -u BSCALE "$factor" "${obsnum}_deep-${subchan}-image-pb_warp_bkg.fits"
pyhead.py -u BSCALE "$factor" "${obsnum}_deep-${subchan}-image-pb_warp_rms.fits"
# rerun the source-finding -- don't save to the log this time as the numbers should not have changed
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_warp_comp.fits" ]]
then
aegean --autoload --cores=${GXNPCPUS} --table="./${obsnum}_deep-${subchan}-image-pb_warp.fits" "./${obsnum}_deep-${subchan}-image-pb_warp.fits"
fi
if [[ ! -e "${obsnum}_deep-${subchan}-image-pb_warp_weight.fits" ]]
then
# Generate a weight map for mosaicking
chans=($( pyhead.py -p CHANNELS "${obsnum}.metafits" | awk '{print $3}' | sed "s/,/ /g" ))
if [[ ${subchan} == "MFS" ]]
then
i=0
j=23
else
n=${subchan:3}
i=$((n * 6))
j=$((i + 5))
fi
cstart=${chans[$i]}
cend=${chans[$j]}
lookup_beam.py "${obsnum}" \
"_deep-${subchan}-image-pb_warp.fits" \
"${obsnum}_deep-${subchan}-image-pb_warp-" \
-c "$cstart-$cend"
generate_weight_map.py "${obsnum}_deep-${subchan}-image-pb_warp-XX-beam.fits" \
"${obsnum}_deep-${subchan}-image-pb_warp-YY-beam.fits" \
"${obsnum}_deep-${subchan}-image-pb_warp_rms.fits"
fi
fi
done
track_task.py finish --jobid="${jobid}" --taskid="${taskid}" --finish_time="$(date +%s)"