-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path3GC_peel.py
363 lines (276 loc) · 12.5 KB
/
3GC_peel.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
361
362
363
#!/usr/bin/env python
import glob
import json
import os.path as o
import sys
sys.path.append(o.abspath(o.join(o.dirname(sys.modules[__name__].__file__), "..")))
from oxkat import generate_jobs as gen
from oxkat import config as cfg
def main():
USE_SINGULARITY = cfg.USE_SINGULARITY
gen.preamble()
print(gen.col()+'3GC (peeling) setup')
gen.print_spacer()
# ------------------------------------------------------------------------------
#
# Setup paths, required containers, infrastructure
#
# ------------------------------------------------------------------------------
OXKAT = cfg.OXKAT
DATA = cfg.DATA
GAINTABLES = cfg.GAINTABLES
IMAGES = cfg.IMAGES
SCRIPTS = cfg.SCRIPTS
TOOLS = cfg.TOOLS
gen.setup_dir(GAINTABLES)
gen.setup_dir(IMAGES)
gen.setup_dir(cfg.LOGS)
gen.setup_dir(cfg.SCRIPTS)
INFRASTRUCTURE, CONTAINER_PATH = gen.set_infrastructure(sys.argv)
if CONTAINER_PATH is not None:
CONTAINER_RUNNER='singularity exec '
else:
CONTAINER_RUNNER=''
CUBICAL_CONTAINER = gen.get_container(CONTAINER_PATH,cfg.CUBICAL_PATTERN,USE_SINGULARITY)
WSCLEAN_CONTAINER = gen.get_container(CONTAINER_PATH,cfg.WSCLEAN_PATTERN,USE_SINGULARITY)
# Get target information from project info json file
with open('project_info.json') as f:
project_info = json.load(f)
target_ids = project_info['target_ids']
target_names = project_info['target_names']
target_ms = project_info['target_ms']
# ------------------------------------------------------------------------------
#
# 3GC peeling recipe definition
#
# ------------------------------------------------------------------------------
target_steps = []
codes = []
ii = 1
stamp = gen.timenow()
# Loop over targets
for tt in range(0,len(target_ids)):
targetname = target_names[tt]
myms = target_ms[tt]
CAL_3GC_PEEL_REGION = cfg.CAL_3GC_PEEL_REGION
skip = False
if not o.isdir(myms):
gen.print_spacer()
print(gen.col('Target')+targetname)
print(gen.col('MS')+'not found, skipping')
skip = True
if CAL_3GC_PEEL_REGION == '':
region = glob.glob('*'+targetname+'*peel*.reg')
if len(region) == 0:
CAL_3GC_PEEL_REGION = ''
else:
CAL_3GC_PEEL_REGION = region[0]
if not o.isfile(CAL_3GC_PEEL_REGION):
gen.print_spacer()
print(gen.col('Target')+targetname)
print(gen.col('Measurement Set')+myms)
print(gen.col()+'Please provide a DS9 region file definining the source you wish to peel.')
print(gen.col()+'This can be specified in the config or by placing a file of the form:')
print(gen.col()+' *'+targetname+'*peel*.reg')
print(gen.col()+'in this folder. Skipping.')
skip = True
if not skip:
steps = []
filename_targetname = gen.scrub_target_name(targetname)
code = gen.get_target_code(targetname)
if code in codes:
code += '_'+str(ii)
ii += 1
codes.append(code)
# Look for the FITS mask for this target
mask1 = sorted(glob.glob(IMAGES+'/*'+filename_targetname+'*.mask1.fits'))
if len(mask1) > 0:
mask = mask1[0]
else:
mask = 'auto'
# Generate output dir for CubiCal
outdir = GAINTABLES+'/peeling_'+filename_targetname+'_'+stamp+'.cc/'
outname = 'peeling_'+filename_targetname+'_'+stamp
gen.print_spacer()
print(gen.col('Target')+targetname)
print(gen.col('Measurement Set')+myms)
print(gen.col('Code')+code)
print(gen.col('Mask')+mask)
print(gen.col('Peeling region')+CAL_3GC_PEEL_REGION)
# Image prefixes
prepeel_img_prefix = IMAGES+'/img_'+myms+'_prepeel'
dir1_img_prefix = prepeel_img_prefix+'-'+CAL_3GC_PEEL_REGION.split('/')[-1].split('.')[0]
# Target-specific kill file
kill_file = SCRIPTS+'/kill_3GC_peel_jobs_'+filename_targetname+'.sh'
step = {}
step['step'] = 0
step['comment'] = 'Run masked wsclean, high freq/angular resolution, on CORRECTED_DATA column of '+myms
step['dependency'] = None
step['id'] = 'WSDMA'+code
step['slurm_config'] = cfg.SLURM_EXTRALONG
step['pbs_config'] = cfg.PBS_EXTRALONG
absmem = gen.absmem_helper(step,INFRASTRUCTURE,cfg.WSC_ABSMEM)
syscall = CONTAINER_RUNNER+WSCLEAN_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += gen.generate_syscall_wsclean(mslist = [myms],
imgname = prepeel_img_prefix,
datacol = 'CORRECTED_DATA',
briggs = cfg.CAL_3GC_PEEL_BRIGGS,
chanout = cfg.CAL_3GC_PEEL_NCHAN,
automask = False,
autothreshold = False,
localrms = False,
nomodel = True,
mask = mask,
absmem = absmem)
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 1
step['comment'] = 'Fix any NaN values in blanked wsclean sub-band models'
step['dependency'] = 0
step['id'] = 'FXNAN'+code
syscall = CONTAINER_RUNNER+CUBICAL_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += 'python3 '+TOOLS+'/fix_nan_models.py '+prepeel_img_prefix+'-0'
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 2
step['comment'] = 'Extract problem source defined by region into a separate set of model images'
step['dependency'] = 1
step['id'] = 'IMSPL'+code
syscall = CONTAINER_RUNNER+CUBICAL_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += 'python3 '+OXKAT+'/3GC_split_model_images.py '
syscall += '--region '+CAL_3GC_PEEL_REGION+' '
syscall += '--prefix '+prepeel_img_prefix+' '
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 3
step['comment'] = 'Predict problem source visibilities into MODEL_DATA column of '+myms
step['dependency'] = 2
step['id'] = 'WS1PR'+code
step['slurm_config'] = cfg.SLURM_WSCLEAN
step['pbs_config'] = cfg.PBS_WSCLEAN
absmem = gen.absmem_helper(step,INFRASTRUCTURE,cfg.WSC_ABSMEM)
syscall = CONTAINER_RUNNER+WSCLEAN_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += gen.generate_syscall_predict(msname = myms, imgbase = dir1_img_prefix, chanout = cfg.CAL_3GC_PEEL_NCHAN, absmem = absmem)
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 4
step['comment'] = 'Add '+cfg.CAL_3GC_PEEL_DIR1COLNAME+' column to '+myms
step['dependency'] = 3
step['id'] = 'ADDIR'+code
syscall = CONTAINER_RUNNER+CUBICAL_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += 'python3 '+TOOLS+'/add_MS_column.py '
syscall += '--colname '+cfg.CAL_3GC_PEEL_DIR1COLNAME+' '
syscall += myms
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 5
step['comment'] = 'Copy MODEL_DATA to '+cfg.CAL_3GC_PEEL_DIR1COLNAME
step['dependency'] = 4
step['id'] = 'CPMOD'+code
syscall = CONTAINER_RUNNER+CUBICAL_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += 'python3 '+TOOLS+'/copy_MS_column.py '
syscall += '--fromcol MODEL_DATA '
syscall += '--tocol '+cfg.CAL_3GC_PEEL_DIR1COLNAME+' '
syscall += myms
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 6
step['comment'] = 'Predict full sky model visibilities into MODEL_DATA column of '+myms
step['dependency'] = 5
step['id'] = 'WS2PR'+code
step['slurm_config'] = cfg.SLURM_WSCLEAN
step['pbs_config'] = cfg.PBS_WSCLEAN
absmem = gen.absmem_helper(step,INFRASTRUCTURE,cfg.WSC_ABSMEM)
syscall = CONTAINER_RUNNER+WSCLEAN_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += gen.generate_syscall_predict(msname = myms, imgbase = prepeel_img_prefix, chanout = cfg.CAL_3GC_PEEL_NCHAN, absmem = absmem)
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 7
step['comment'] = 'Copy CORRECTED_DATA to DATA'
step['dependency'] = 6
step['id'] = 'CPCOR'+code
syscall = CONTAINER_RUNNER+CUBICAL_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += 'python3 '+TOOLS+'/copy_MS_column.py '
syscall += '--fromcol CORRECTED_DATA '
syscall += '--tocol DATA '
syscall += myms
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 8
step['comment'] = 'Run CubiCal to solve for G (full model) and dE (problem source), peel out problem source'
step['dependency'] = 7
step['id'] = 'CL3GC'+code
step['slurm_config'] = cfg.SLURM_WSCLEAN
step['pbs_config'] = cfg.PBS_WSCLEAN
syscall = CONTAINER_RUNNER+CUBICAL_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += gen.generate_syscall_cubical(parset=cfg.CAL_3GC_PEEL_PARSET, myms=myms, extra_args='--out-name '+outname+' --out-dir '+outdir)
step['syscall'] = syscall
steps.append(step)
target_steps.append((steps,kill_file,targetname))
# ------------------------------------------------------------------------------
#
# Write the run file and kill file based on the recipe
#
# ------------------------------------------------------------------------------
submit_file = 'submit_3GC_peel_jobs.sh'
f = open(submit_file,'w')
f.write('#!/usr/bin/env bash\n')
f.write('export SINGULARITY_BINDPATH='+cfg.BINDPATH+'\n')
for content in target_steps:
steps = content[0]
kill_file = content[1]
targetname = content[2]
id_list = []
f.write('\n#---------------------------------------\n')
f.write('# '+targetname)
f.write('\n#---------------------------------------\n')
for step in steps:
step_id = step['id']
id_list.append(step_id)
if step['dependency'] is not None:
dependency = steps[step['dependency']]['id']
else:
dependency = None
syscall = step['syscall']
if 'slurm_config' in step.keys():
slurm_config = step['slurm_config']
else:
slurm_config = cfg.SLURM_DEFAULTS
if 'pbs_config' in step.keys():
pbs_config = step['pbs_config']
else:
pbs_config = cfg.PBS_DEFAULTS
comment = step['comment']
run_command = gen.job_handler(syscall = syscall,
jobname = step_id,
infrastructure = INFRASTRUCTURE,
dependency = dependency,
slurm_config = slurm_config,
pbs_config = pbs_config)
f.write('\n# '+comment+'\n')
f.write(run_command)
if INFRASTRUCTURE != 'node':
f.write('\n# Generate kill script for '+targetname+'\n')
if INFRASTRUCTURE == 'idia' or INFRASTRUCTURE == 'hippo':
kill = 'echo "scancel "$'+'" "$'.join(id_list)+' > '+kill_file+'\n'
f.write(kill)
elif INFRASTRUCTURE == 'chpc':
kill = 'echo "qdel "$'+'" "$'.join(id_list)+' > '+kill_file+'\n'
f.write(kill)
f.close()
gen.make_executable(submit_file)
gen.print_spacer()
print(gen.col('Run file')+submit_file)
gen.print_spacer()
# ------------------------------------------------------------------------------
if __name__ == "__main__":
main()