-
Notifications
You must be signed in to change notification settings - Fork 1
/
postprocessing_automations.py
341 lines (230 loc) · 10.5 KB
/
postprocessing_automations.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import re
import os
import sys
import pandas
import glob
import multiprocessing as mlp
import easyaccess
import configparser
import argparse
import shutil
import datetime
from collections import Counter
#accept arguments
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--triggermjd')
parser.add_argument('--propid')
# In[2]:
# Get a list of exposures
# Get season
# Check if they've finished in image processing pipeline
# Document and report what hasn't
# Figure out why it didn't finish it
# Build appropriate postproc.ini file
# Check if user wants to run from scratch or start wherever the pipeline failed last
# If the latter, deduce where the pipeline finished off last, set as SKIPTO flag
# Run post-processing pipeline
# In[ ]:
#check if Post-Processing folder exists. If not, clone in from github
filepath = ['../Post-Processing']
isExist = os.path.exists(filepath[0])
if isExist:
print('Post-Processing found')
else:
git_command = ['git clone https://github.com/SSantosLab/Post-Processing.git ../Post-Processing' ]
print('Running'+git_command[0]+'in order to clone necessary folder post processing...')
git_output = os.system(git_command[0])
#check if the system command will run successfully. if it does, output will be 0 with os.system. if it doesn't, raise exception. if you got this error, try manually git cloning https://github.com/SSantosLab/gw_workflow.git one folder back in a folder called gw_workflow
if git_output != 0:
raise ValueError('Something went wrong with cloning Post-Processing. Please manually clone or try again.')
# In[3]:
#Read file outputted from image processing to get season, nite, exposure, band
#If no file, input manually
#Read file outputted from image processing to get season, nite, exposure, band
#If no file, input manually
try:
img_proc_file = open("./image_proc_outputs/output.txt")
lines = img_proc_file.readlines()
print('\nExtracting season, exposure, and band from output.txt file\n')
exposures = lines[0].strip()
exposures = exposures[1:-1]
exposures = list(exposures.split(","))
season = lines[1].strip()
print('Season: ' + season)
print('Exposures: ' + str(exposures))
except:
print('no .txt file, must input exposures\n')
exposures = []
exposures_num = [str(item) for item in raw_input("Enter each exposure separated by spaces (ex. '938524 938511 938522'): ").split(' ')]
season = raw_input('Season: ')
#get exposure band
try:
for exposure in exposures_num:
band_dirs = glob.glob('/pnfs/des/persistent/gw/exp/' + '*' + '/' + exposure +'/' + 'dp' + season + '/*')
first_band_dir = str(band_dirs[:1]).split('/')[-1:]
band_only = re.sub(r"[^a-zA-Z]+", "", str(first_band_dir))
exposure_w_band = exposure + ' ' + band_only
exposures.append(exposure_w_band)
except Exception as e:
print(e)
print('invalid exposures input')
print('\nExposures with bands: ' + str(exposures))
# In[12]:
# Get exposure, format: /pnfs/des/persistent/gw/exp/NITE/EXPOSURE_NUMBER/dpSEASON/BAND_CCD
# Check CCDs from image processing, check forcephoto files
# Output list of exposures that can move on to post processing
dir_prefix = '/pnfs/des/persistent/gw/exp/'
dpSeason = ('dp' + str(season) + '/')
exposures_to_cont = []
bandslist = []
for exposure in exposures:
exposure = exposure.split()
band = exposure[1]
exposure = exposure[0]
if band not in bandslist:
bandslist.append(band)
#term_size = os.get_terminal_size()
print('=' * 12)
print("\nFOR EXPOSURE " + str(exposure) + ":\n")
exposure_dir = dir_prefix + '*' + '/' + exposure +'/' + dpSeason + band
band_dirs = glob.glob(exposure_dir + '_*' + '/') #what we're counting to make sure they're all there
print('There are '+ str(len(band_dirs)) + ' ' + str(band) + ' ccds\n')
if glob.glob(dir_prefix + '*' + '/' + exposure +'/' + dpSeason + 'input_files'):
print('input_files found\n')
else:
print('input files not found\n')
complete_ccds=0
incomplete_ccds=0
failed_ccds=0
complete_ccds_list = []
fail_files = []
for dir in band_dirs:
if glob.glob(dir+'*.FAIL'):
fail_file = str(glob.glob(dir+'*.FAIL'))
fail_file = str(fail_file.split('/')[-1:])
fail_file = fail_file.strip('[]"')
print('ccd ' + dir[-5:-1] + ' failed on ' + fail_file)
failed_ccds += 1
fail_files.append(fail_file)
elif glob.glob(dir + 'outputs_*'):
complete_ccds += 1
complete_ccds_list.append(dir[-5:-1])
else:
print('ccd ' + dir[-5:-1] + ' incomplete')
incomplete_ccds += 1
print('\nThere are ' + str(failed_ccds) + ' failed ' + str(band) + ' ccds')
print('There are ' + str(incomplete_ccds) + ' incomplete ' + str(band) + ' ccds')
print('There are ' + str(complete_ccds) + ' complete '+ str(band) + ' ccds')
print('\n'+str(Counter(fail_files)))
if complete_ccds >= 50:
print('\nover 50 ' + str(band) + ' ccds completed: acceptable')
num_ccds = complete_ccds
else:
print('\nnot enough ccds in exposure ' + str(exposure) + ' for post processing\n')
continue
# Get forcephoto exposure, format: /pnfs/des/persistent/gw/forcephoto/images/dpSEASON/NITE/EXPOSURE/
expected_forcephoto_files = num_ccds * 2
print('\nexpected forcephoto files for band ' + str(band) + ': ' + str(expected_forcephoto_files))
forcephoto_dir_prefix = '/pnfs/des/persistent/gw/forcephoto/images/'
forcephoto_dir = forcephoto_dir_prefix + dpSeason + '*' + '/' + exposure + '/'
forcephoto_files = glob.glob(forcephoto_dir + '/' + '*' + '_' + str(band) + '_' + '*')
print('found forcephoto files for exposure '+ str(exposure) + ': ' + str(len(forcephoto_files)) + '\n')
if len(forcephoto_files) == expected_forcephoto_files:
print('all forcephoto files completed in exposure ' + str(exposure) + ' -> transferring to post processing\n')
exposures_to_cont.append(exposure)
elif len(forcephoto_files) < expected_forcephoto_files and len(forcephoto_files) > expected_forcephoto_files / 2 :
print('some forcephoto files not yet completed in exposure ' + str(exposure) + '\n')
exposures_to_cont.append(exposure)
for num in complete_ccds_list:
if not glob.glob(forcephoto_dir + '*' + str(num) + '*.fits') or not (forcephoto_dir + '*' + str(num) + '*.psf'):
print('forcephoto files for ' + num + ' not completed / missing')
#elif glob.glob(forcephoto_dir + '*' + num + '*.fits') and glob.glob(forcephoto_dir + '*' + num + '*.psf'):
#print('exposure ' + num + ' completed')
print('\nover 50% forcephoto files in exposure ' + exposure + ' completed -> transferring to post processing\n')
elif len(forcephoto_files) < expected_forcephoto_files and not len(forcephoto_files) > expected_forcephoto_files / 2 :
print('fewer than 50% forcephoto files completed, will not add to exposures.list')
continue
elif len(forcephoto_files) > expected_forcephoto_files:
print('check: More forcephoto files than expected for this exposure -> will not add to exposures.list')
continue
print('exposures moving to post processing:\n' + str(exposures_to_cont))
# In[5]:
#create custom exposure.list file
print('creating .list file for completed exposures\n')
current_exposures = 'complete_exposures' + '_S' + str(season) + '_' + str(datetime.datetime.now().strftime("%Y%m%d_%H-%M")) + '.list'
with open(current_exposures, 'w') as f:
for exposure in exposures_to_cont:
f.write("%s\n" % exposure)
# In[6]:
#create .ini file
#ask user for
#ligoid = input('ligoid (ex. GW170814): ')
#triggerid = input('triggerid (ex. G298048): ')
try:
propid = args.propid
triggermjd = args.triggermjd
except:
pass
print('creating .ini file with completed exposures list\n')
exposures_listfile = str(current_exposures)
os.system('cp ./template_postproc.ini ./postproc_' + str(season) + '.ini')
postproc_season_file = 'postproc_'+ str(season) + '.ini'
edit = configparser.ConfigParser()
edit.read(postproc_season_file)
general = edit["general"]
general["season"] = season
#general["ligoid"] = ligoid
#general["triggerid"] = triggerid
#general["propid"] = propid
#general["triggermjd"] = triggermjd
general["exposures_listfile"] = exposures_listfile
#editing bandslist
bandslist = str(bandslist)
bandslist = bandslist.strip("[]'")
bandslist = bandslist.replace("'","")
bandslist += ' ;'
general["bands"] = str(bandslist)
with open(postproc_season_file, 'w') as configfile:
edit.write(configfile)
# In[ ]:
#fetching directories from .ini file for SKIPTO
outdir = general["outdir"]
truthtable = edit["truthtable"]
truthplusfile = truthtable['plusname']
# In[ ]:
#Check if we want to SKIPTO
if glob.glob('../Post-Processing/'+ outdir[2:] + '/makedatafiles/LightCurvesReal/*.dat'):
SKIPTO_flag = 6
print('\nWill run post processing from step 6')
elif os.path.exists('../Post-Processing/' + outdir[2:] + '/truthtable'+str(season)+'/'+truthplusfile): #output from step 4
SKIPTO_flag = 5
print('\nWill run post processing from step 5')
else:
print('No evidence of steps already completed in post processing, will not skip')
print('\nContinuing to post processing')
# In[ ]:
#move .ini file and exposures list into Post-Processing
os.system('cp ./' + str(postproc_season_file) + ' ../Post-Processing')
os.system('cp ./' + str(current_exposures) + ' ../Post-Processing')
#setup for Post Processing
#os.system('source ../Post-Processing/diffimg_setup.sh')
#print('running diffimg_setup.sh\n')
os.system('../Post-Processing/update_forcephoto_links.sh')
#run_postproc.py
#changing directory because this code needs to run in postproc
os.chdir('../Post-Processing')
try:
SKIPTO_flag
except NameError:
print("\nRunning run_postproc.py\n")
os.system('nohup python run_postproc.py --outputdir outdir --season '+ str(season)+ ' &> postproc_run.out &')
else:
print("\nRunning run_postproc.py with skip\n")
os.system('nohup python run_postproc.py --SKIPTO ' + str(SKIPTO_flag) + ' --outputdir outdir --season '+ str(season)+ ' &> postproc_run.out &')
# In[ ]:
#make cuts
# In[ ]: