forked from IanHeywood/oxkat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FLAG.py
272 lines (200 loc) · 8.62 KB
/
FLAG.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
#!/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()+'FLAG (target flagging & initial mask-making) setup')
gen.print_spacer()
# ------------------------------------------------------------------------------
#
# Setup paths, required containers, infrastructure
#
# ------------------------------------------------------------------------------
OXKAT = cfg.OXKAT
DATA = cfg.DATA
IMAGES = cfg.IMAGES
SCRIPTS = cfg.SCRIPTS
TOOLS = cfg.TOOLS
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=''
ASTROPY_CONTAINER = gen.get_container(CONTAINER_PATH,cfg.ASTROPY_PATTERN,USE_SINGULARITY)
CASA_CONTAINER = gen.get_container(CONTAINER_PATH,cfg.CASA_PATTERN,USE_SINGULARITY)
TRICOLOUR_CONTAINER = gen.get_container(CONTAINER_PATH,cfg.TRICOLOUR_PATTERN,USE_SINGULARITY)
WSCLEAN_CONTAINER = gen.get_container(CONTAINER_PATH,cfg.WSCLEAN_PATTERN,USE_SINGULARITY)
# Get target information from project json
with open('project_info.json') as f:
project_info = json.load(f)
band = project_info['band']
target_ids = project_info['target_ids']
target_names = project_info['target_names']
target_ms = project_info['target_ms']
# ------------------------------------------------------------------------------
#
# FLAG recipe definition
#
# ------------------------------------------------------------------------------
target_steps = []
codes = []
ii = 1
# Loop over targets
for tt in range(0,len(target_ids)):
targetname = target_names[tt]
myms = target_ms[tt]
if not o.isdir(myms):
gen.print_spacer()
print(gen.col('Target')+targetname)
print(gen.col('MS')+'not found, skipping')
else:
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)
# Image prefix
img_prefix = IMAGES+'/img_'+myms+'_datablind'
# Target-specific kill file
kill_file = SCRIPTS+'/kill_flag_jobs_'+filename_targetname+'.sh'
gen.print_spacer()
print(gen.col('Target')+targetname)
print(gen.col('Measurement Set')+myms)
print(gen.col('Code')+code)
step = {}
step['step'] = 0
step['comment'] = 'Run Tricolour on '+myms
step['dependency'] = None
step['id'] = 'TRIC0'+code
step['slurm_config'] = cfg.SLURM_TRICOLOUR
step['pbs_config'] = cfg.PBS_TRICOLOUR
syscall = CONTAINER_RUNNER+TRICOLOUR_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += gen.generate_syscall_tricolour(myms = myms,
config = DATA+'/tricolour/target_flagging_1_narrow.yaml',
datacol = 'DATA',
fields = '0',
strategy = 'polarisation')
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 1
step['comment'] = 'Blind wsclean on DATA column of '+myms
step['dependency'] = 0
step['id'] = 'WSDBL'+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_wsclean(mslist = [myms],
imgname = img_prefix,
datacol = 'DATA',
nomodel = True,
automask = False,
autothreshold = False,
localrms = False,
mask = False,
absmem = absmem)
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 2
step['comment'] = 'Make initial cleaning mask for '+targetname
step['dependency'] = 1
step['id'] = 'MASK0'+code
syscall = CONTAINER_RUNNER+ASTROPY_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += gen.generate_syscall_makemask(restoredimage = img_prefix+'-MFS-image.fits',
outfile = img_prefix+'-MFS-image.mask0.fits',
zoompix = '')[0]
step['syscall'] = syscall
steps.append(step)
step = {}
step['step'] = 3
step['comment'] = 'Apply primary beam correction to '+targetname+' image'
step['dependency'] = 1
step['id'] = 'PBCOR'+code
syscall = CONTAINER_RUNNER+ASTROPY_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += 'python3 '+TOOLS+'/pbcor_katbeam.py --band '+band[0]+' '+img_prefix+'-MFS-image.fits'
step['syscall'] = syscall
steps.append(step)
if cfg.SAVE_FLAGS:
step = {}
step['step'] = 4
step['comment'] = 'Backup flag table for '+myms
step['dependency'] = 1
step['id'] = 'SAVFG'+code
syscall = CONTAINER_RUNNER+CASA_CONTAINER+' ' if USE_SINGULARITY else ''
syscall += 'casa -c '+OXKAT+'/FLAG_casa_backup_flag_table.py --nologger --log2term --nogui '
syscall += 'versionname=tricolour1 mslist='+myms
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_flag_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()