-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
380 lines (287 loc) · 11 KB
/
helpers.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import os
import subprocess
import shlex
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import glob
from functools import partial
from astropy.utils.console import ProgressBar
from astropy.io import fits
from astropy.table import Table
import json
from multiprocessing import Pool
import seaborn as sns
import scipy.stats as st
import heasoftpy as hsp
import xspec as xs
def _sort_extra_pars(obsids, **kwargs):
"""sort out kwargs, are they a single value (same for all obsid)
or multi-valued (one per obsid)
"""
nobs = len(obsids)
extra_pars = [{} for _ in range(nobs)]
for k,v in kwargs.items():
try:
nv = len(v)
except:
nv = 0
if nv == 0:
for io in range(nobs):
extra_pars[io][k] = v
elif nv != nobs:
raise ValueError(f'Input {k} does not match length of obsids')
else:
for io in range(nobs):
extra_pars[io][k] = v[io]
return extra_pars
def process_nicer_data_runner(args):
"""Run a single task"""
obsid, extra_pars = args
print(f'processing {obsid} ...')
# input
inPars = {
'indir': obsid,
'geomag_path': 'geomag',
'filtcolumns': 'NICERV4,3C50',
'detlist': 'launch,-14,-34',
'min_fpm': 50,
'clobber': True,
'noprompt': True,
}
inPars.update(extra_pars)
# set local pfile
pfiles = os.environ['PFILES']
pdir = hsp.utils.local_pfiles(f'/tmp/{obsid}.pfiles')
out = hsp.nicerl2(**inPars)
os.environ['PFILES'] = pfiles
os.system(f'rm -rf {pdir}')
if out.returncode == 0:
os.system(f'rm -rf {pdir}')
print(f'{obsid} processed sucessfully!')
else:
logfile = f'process_nicer_{obsid}.log'
print(f'ERROR processing {obsid}; Writing log to {logfile}')
with open(logfile, 'w') as fp:
fp.write(out.__str__())
def process_nicer_data(obsids, nproc=1, fresh=False, **kwargs):
"""Process a single or multiple nicer obsid
Parameters:
-----------
obsid: observation id assumed in current location.
Either a single obsid or a list. If a list, they are
run in parallel.
nproc: maximum number of parallel processess; default: 5
fresh: fresh data reduction; default: False
Keywords:
---------
keywords to override the default of nicerl2
"""
if isinstance(obsids, str):
obsids = [obsids]
# sort out kwargs, are they a single value (same for all obsid)
# or multi-valued (one per obsid)
extra_pars = _sort_extra_pars(obsids, **kwargs)
unp_obsids, unp_extra_pars = [], []
for io,oid in enumerate(obsids):
evtfile = f'{oid}/xti/event_cl/ni{oid}_0mpu7_cl.evt'
if fresh and os.path.exists(evtfile):
os.system(f'rm -rf {evtfile}')
if not os.path.exists(evtfile):
unp_obsids.append(oid)
unp_extra_pars.append(extra_pars[io])
obsids = unp_obsids
extra_pars = unp_extra_pars
nobs = len(obsids)
assert(len(extra_pars) == nobs)
with Pool(nproc) as p:
p.map(process_nicer_data_runner, zip(obsids, extra_pars))
def extract_nicer_spec_runner(args):
"""Run a single task"""
obsid, ispec, extra_pars = args
print(f'processing {obsid} ...')
# set local pfile
pfiles = os.environ['PFILES']
pdir = hsp.utils.local_pfiles(f'/tmp/{obsid}.pfiles')
phafile = extra_pars.get('phafile', f'{obsid}/spec/spec{ispec}.pha')
pharoot = phafile[:-4]
outdir = os.path.dirname(phafile)
os.system(f'mkdir -p {outdir}')
# input
inPars = {
'indir' : obsid,
'phafile' : phafile,
'rmffile' : f'{pharoot}.rmf',
'arffile' : f'{pharoot}.arf',
'grouptype' : 'NONE',
'loadfile' : f'{pharoot}.xcm',
'bkgformat' : 'file',
'clobber' : True,
'noprompt' : True,
}
bgd = extra_pars.get('bgd', ['sc', '3c', 'sw'])
if 'bgd' in extra_pars: extra_pars.pop('bgd')
inPars.update(extra_pars)
phafile = inPars['phafile']
bg_models = {
'sc': 'scorpeon',
'3c': '3c50',
'sw': 'sw'
}
for k in list(bg_models.keys()):
if k not in bgd:
bg_models.pop(k)
if len(glob.glob(f'{phafile[:-4]}_??.???')) != 6:
# get the spectrum and 3 background models
incr = 'no'
for blabel,bname in bg_models.items():
bgfile = f'{phafile[:-4]}_{blabel}.bgd'
out = hsp.nicerl3_spect(bkgmodeltype=bname,
bkgfile=bgfile,
incremental=incr,
**inPars)
if out.returncode == 0:
print(f'{obsid}:{blabel} spectra extracted sucessfully!')
incr = 'yes'
with fits.open(phafile) as fp:
fp['spectrum'].header['backfile'] = bgfile.split('/')[-1]
fp['spectrum'].header['respfile'] = inPars['rmffile'].split('/')[-1]
fp['spectrum'].header['arffile'] = inPars['arffile'].split('/')[-1]
fp.writeto(f'{phafile[:-4]}_{blabel}.pha', overwrite=True)
else:
logfile = f'extract_nicer_spec_{obsid}_{blabel}.log'
print(f'ERROR provessing {obsid}; Writing log to {logfile}')
with open(logfile, 'w') as fp:
fp.write(out.__str__())
# clean
os.system(f'rm {outdir}/*bkg_ngt.pi {outdir}/*xcm >dev/null 2>&1')
os.environ['PFILES'] = pfiles
os.system(f'rm -rf {pdir}')
def extract_nicer_spec(obsids, nproc=1, fresh=False, **kwargs):
"""Extract spectra from a single or multiple nicer obsid.
Assume we are in folder containing obsids.
Also assume that heasoft commonds can be accessed.
nicerl2 is assumed to have been run
Spectra written to {obsid}/spec
Parameters:
----------
obsid: observation id assumed in current location.
Either a single obsid (obsid, or '{obsid}:{iobs}' so the
output is spec_{iobs}_sc|3c|sw.) or a list of obsids.
If a list, they are run in parallel
nproc: maximum number of parallel processess; default: 5
Keywords:
---------
keywords to override the default of nicerl3_spect
"""
if isinstance(obsids, str):
obsids = [obsids]
# extract suffic if present or set it as a counter from 1..nobs
ispec, obsids2 = [], []
for io,o in enumerate(obsids):
so = o.split(':')
obsids2.append(so[0])
ii = so[1] if len(so) == 2 else f'_{io+1}'
ispec.append(ii)
obsids = obsids2
nobs = len(obsids)
# sort out kwargs, are they a single value (same for all obsid)
# or multi-valued (one per obsid)
extra_pars = _sort_extra_pars(obsids, **kwargs)
assert(len(ispec) == nobs == len(extra_pars))
with Pool(nproc) as p:
p.map(extract_nicer_spec_runner, zip(obsids, ispec, extra_pars))
def extract_nicer_lc_runner(args):
"""Run a single task"""
obsid, extra_pars = args
print(f'processing {obsid} ...')
# set local pfile
pfiles = os.environ['PFILES']
pdir = hsp.utils.local_pfiles(f'/tmp/{obsid}.pfiles')
outdir = f'{obsid}/lc'
os.system(f'mkdir -p {outdir}')
# input
inPars = {
'indir' : obsid,
'pirange' : '30:500',
'timebin' : 10,
'bkgformat' : 'file',
'incremental' : 'no',
'clobber' : 'yes',
'noprompt' : True,
}
inPars.update(extra_pars)
bg_models = {
# 'scorpeon': 'sc',
# '3c50' : '3c',
'sw' : 'sw'
}
if len(glob.glob(f'{outdir}/lc_*.fits')) != 2:
# get the lightcurve and sw background
for bname, blabel in bg_models.items():
out = hsp.nicerl3_lc(bkgmodeltype=bname,
lcfile=f'{outdir}/lc_sr_{blabel}.fits',
bkgfile=f'{outdir}/lc_{blabel}.fits',
**inPars)
if out.returncode == 0:
print(f'{obsid}:{blabel} lightcurve extracted sucessfully!')
else:
logfile = f'extract_nicer_lc_{obsid}_{blabel}.log'
print(f'ERROR provessing {obsid}; Writing log to {logfile}')
with open(logfile, 'w') as fp:
fp.write(out.__str__())
# clean
os.system(f'rm {outdir}/*.tco')
os.environ['PFILES'] = pfiles
os.system(f'rm -rf {pdir}')
def extract_nicer_lc(obsids, nproc=1, fresh=False, **kwargs):
"""Extract light curves from a single or multiple nicer obsid.
Assume we are in folder containing obsids.
Also assume that heasoft commonds can be accessed.
nicerl2 is assumed to have been run
Spectra written to {obsid}/spec
Parameters:
----------
obsid: observation id assumed in current location.
Either a single obsid or a list of obsids.
If a list, they are run in parallel
nproc: maximum number of parallel processess; default: 5
Keywords:
---------
keywords to override the default of nicerl3_lc
"""
if isinstance(obsids, str):
obsids = [obsids]
nobs = len(obsids)
# sort out kwargs, are they a single value (same for all obsid)
# or multi-valued (one per obsid)
extra_pars = _sort_extra_pars(obsids, **kwargs)
assert(nobs == len(extra_pars))
with Pool(nproc) as p:
p.map(extract_nicer_lc_runner, zip(obsids, extra_pars))
def spec_summary(sfiles):
"""Print a short summary of a list of spectra
Parameters:
sfiles: a list of spectral files
"""
# summary of data
spec_data = []
fmt = '{:5} | {:12} | {:10.8} | {:10.8} | {:10.3} | {:10.5}\n'
text = fmt.format('num', 'obsid', 'mjd_s', 'mjd_e', 'rate', 'exposure')
for ispec,sfile in enumerate(sfiles):
with fits.open(sfile) as fp:
obsid = 'NA'
for k in ['obs_id', 'obsid']:
if k in fp[1].header:
obsid = fp[1].header['obs_id']
break
exposure = fp[1].header['exposure']
counts = fp[1].data.field('counts').sum()
tmid = np.array([fp[0].header['tstart'], fp[0].header['tstop']])
mref = fp[0].header['mjdrefi'] + fp[0].header['mjdreff']
mjd = tmid / (24*3600) + mref
spec_data.append([mjd[0], mjd[1], counts/exposure, exposure/1e3])
text += fmt.format(ispec+1, obsid, mjd[0], mjd[1], counts/exposure, exposure/1e3)
print(text)
spec_data = np.array(spec_data)
return spec_data, text