-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetMockTrueDLAcat.py
86 lines (61 loc) · 1.86 KB
/
getMockTrueDLAcat.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
import argparse
import glob
from multiprocessing import Pool
import numpy as np
import fitsio
final_dtype = np.dtype([
('NHI', 'f8'), ('Z', 'f8'), ('TARGETID', 'i8'), ('DLAID', 'i8')
])
def parse():
parser = argparse.ArgumentParser()
parser.add_argument("Directory", help="Directory for spectra-16.")
parser.add_argument(
"SaveDirectory", help="Directory for to save final catalog.")
parser.add_argument("--nproc", type=int, default=None)
args = parser.parse_args()
return args
def _getDLACat(ftruth):
f1 = fitsio.FITS(ftruth)
hdr_dla = f1['DLA_META'].read_header()
nrows = hdr_dla['NAXIS']
if nrows == 0:
f1.close()
return None
dat_dla = f1['DLA_META'].read()
nrows = len(dat_dla)
f1.close()
newdata = np.empty(nrows, dtype=final_dtype)
newdata['NHI'] = dat_dla['NHI']
newdata['Z'] = dat_dla['Z_DLA']
newdata['TARGETID'] = dat_dla['TARGETID']
newdata['DLAID'] = dat_dla['DLAID']
return newdata
def main():
args = parse()
all_truths = glob.glob(f"{args.Directory}/*/*/truth-*.fits*")
numpy_arrs = []
print(f"Iterating over files.")
with Pool(processes=args.nproc) as pool:
imap_it = pool.imap(_getDLACat, all_truths)
for arr in imap_it:
if arr is None:
continue
numpy_arrs.append(arr)
ndlas = 0
for arr in numpy_arrs:
ndlas += arr.size
print(f"There are {ndlas} DLAs.")
final_data = np.empty(ndlas, dtype=final_dtype)
ii = 0
for arr in numpy_arrs:
nrows = arr.size
if nrows == 0:
continue
final_data[ii:ii + nrows] = arr
ii += nrows
fdla = fitsio.FITS(
f"{args.SaveDirectory}/dla_cat.fits", 'rw', clobber=True)
fdla.write(final_data, extname='DLACAT')
fdla.close()
if __name__ == '__main__':
main()