-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
234 lines (204 loc) · 7.9 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
"""
This file contains an assortment of different helper functions for
use in `nuanalysis`.
"""
import glob
import os
import subprocess
import random
from utils import energy_to_chan
def generate_directory(path, overwrite=False):
if os.path.isdir(path):
if overwrite:
clear_directory(path)
else:
os.mkdir(path)
def clear_directory(path):
if os.path.isdir(path):
for file in os.listdir(path):
os.remove(f"{path}/{file}")
else:
NotADirectoryError("This directory doesn't exist!")
def recover_events():
download_files = glob.glob("../../../../Downloads/*.tgz")
for file in download_files:
obsid = file.replace("../../../../Downloads/", '')[:11]
current_files = glob.glob("*/")
if len(current_files) == 0:
subprocess.run(["cp", file, "."])
subprocess.run(["tar", "-xf", file.replace("../../../../Downloads/", '')])
subprocess.run(["rm", file.replace("../../../../Downloads/", '')])
current_files = glob.glob("*/")
present_flag = False
for cur_file in current_files:
if obsid in cur_file:
present_flag = True
if not present_flag:
subprocess.run(["cp", file, "."])
subprocess.run(["tar", "-xf", file.replace("../../../../Downloads/", '')])
subprocess.run(["rm", file.replace("../../../../Downloads/", '')])
def read_detection_file(det_file):
with open(det_file) as detections:
for i in range(14):
detections.readline()
detect_info = {}
detect_info["INDEX"] = []
detect_info["COUNTS"] = []
detect_info["XPIX"] = []
detect_info["YPIX"] = []
detect_info["VIGCOR"] = []
detect_info["RA"] = []
detect_info["DEC"] = []
detect_info["ERR"] = []
detect_info["HBOX"] = []
detect_info["PROB"] = []
detect_info["SNR"] = []
for line in detections:
line_info = line.split()
detect_info["INDEX"].append(line_info[0])
detect_info["COUNTS"].append(line_info[1])
detect_info["XPIX"].append(line_info[2])
detect_info["YPIX"].append(line_info[3])
detect_info["VIGCOR"].append(line_info[4])
detect_info["RA"].append(f"{line_info[5]} {line_info[6]} {line_info[7]}")
detect_info["DEC"].append(f"{line_info[8]} {line_info[9]} {line_info[10]}")
detect_info["ERR"].append(line_info[11])
detect_info["HBOX"].append(line_info[12])
detect_info["PROB"].append(line_info[13])
detect_info["SNR"].append(line_info[14])
return detect_info
def read_detection_dir(path):
det_files = glob.glob(path + "/*.det")
detect_info = {}
for file in det_files:
with open(file) as detections:
for i in range(14):
detections.readline()
times = tuple(file.replace("./../data/30501002002/detections/559-1934_10000-3/", '').replace(".det", '').split(sep="-"))
detect_info[times] = {}
detect_info[times]["INDEX"] = []
detect_info[times]["COUNTS"] = []
detect_info[times]["XPIX"] = []
detect_info[times]["YPIX"] = []
detect_info[times]["VIGCOR"] = []
detect_info[times]["RA"] = []
detect_info[times]["DEC"] = []
detect_info[times]["ERR"] = []
detect_info[times]["HBOX"] = []
detect_info[times]["PROB"] = []
detect_info[times]["SNR"] = []
detect_info[times]["FILE"] = []
for line in detections:
line_info = line.split()
detect_info[times]["FILE"].append(file)
detect_info[times]["INDEX"].append(line_info[0])
detect_info[times]["COUNTS"].append(line_info[1])
detect_info[times]["XPIX"].append(line_info[2])
detect_info[times]["YPIX"].append(line_info[3])
detect_info[times]["VIGCOR"].append(line_info[4])
detect_info[times]["RA"].append(f"{line_info[5]} {line_info[6]} {line_info[7]}")
detect_info[times]["DEC"].append(f"{line_info[8]} {line_info[9]} {line_info[10]}")
detect_info[times]["ERR"].append(line_info[11])
detect_info[times]["HBOX"].append(line_info[12])
detect_info[times]["PROB"].append(line_info[13])
detect_info[times]["SNR"].append(line_info[14])
return detect_info
def generate_gti_files(path, tstart, tstop):
"""
Creates a Good Time Interval (GTI) file for temporal filtering calls using xselect and
nuproducts.
Parameters
----------
path : str
The path at which to execute the XSelect script
tstart : str or float
The starting time of the interval in SCC
tstop : str or float
The stopping time of the interval in SCC
"""
with open(path + "xselect.xco", 'w') as script:
script.write('\n')
script.write('\n')
script.write("filter time scc\n")
script.write(f"{tstart.replace('nu_', '')} , {tstop}\n")
script.write("x\n")
script.write("save time keyboard\n")
script.write(f"{tstart}_{tstop}_gti.fits\n")
script.write("exit no")
script.write('\n')
subprocess.run(["xselect", "@xselect.xco"], cwd=path, capture_output=False)
subprocess.run(["rm", "xselect.xco"], cwd=path, capture_output=False)
def remove_tmp_folders(path):
"""
Removes all of the nuproducts temporary files in a given directory
"""
tmp_dirs = glob.glob(path + "*tmp_nuproducts")
for dir in tmp_dirs:
subprocess.run(["rm", "-rf", dir])
def evt_to_fits_image(infile, outfile, dir, elow, ehigh, sessionid, usrgti=False):
"""
Helper script to generate the xselect commands to make an image in a given NuSTAR range
"""
xsel=open(f"{sessionid}xselect.xco","w")
xsel.write(f'{sessionid}\n')
xsel.write('\n')
xsel.write(f"{sessionid}\n")
xsel.write("read events \n")
xsel.write(f'{dir}\n')
xsel.write(f'{infile}\n ')
xsel.write('yes \n')
pi_low = energy_to_chan(elow)
pi_high = energy_to_chan(ehigh)
if usrgti is not False:
xsel.write(f'filter time \n')
xsel.write('file \n')
xsel.write(f'{usrgti}\n')
xsel.write('extract events\n')
xsel.write('filter pha_cutoff {} {} \n'.format(pi_low, pi_high))
xsel.write('set xybinsize 1\n')
xsel.write("extract image\n")
xsel.write("save image\n")
xsel.write("%s \n" % outfile)
xsel.write('exit\n')
xsel.write('n \n')
xsel.close()
return 'xsel.xco'
def evt_xselect_filter(infile, outfile, dir, elow, ehigh, sessionid, usrgti=False):
"""
Helper script to generate the xselect commands to make an image in a given NuSTAR range
"""
xsel=open(f"{sessionid}xselect.xco","w")
xsel.write(f'{sessionid}\n')
xsel.write('\n')
xsel.write(f"{sessionid}\n")
xsel.write("read events \n")
xsel.write(f'{dir}\n')
xsel.write(f'{infile}\n ')
xsel.write('yes \n')
pi_low = energy_to_chan(elow)
pi_high = energy_to_chan(ehigh)
if usrgti is not False:
xsel.write(f'filter time \n')
xsel.write('file \n')
xsel.write(f'{usrgti}\n')
xsel.write('extract events\n')
xsel.write('filter pha_cutoff {} {} \n'.format(pi_low, pi_high))
xsel.write('set xybinsize 1\n')
xsel.write("extract events\n")
xsel.write("\n")
xsel.write(f"save events {outfile}\n")
xsel.write('no\n')
xsel.write('no\n')
xsel.write('exit\n')
xsel.write('n \n')
xsel.close()
return 'xsel.xco'
def generate_random_id():
"""
Generates a float random value utilizing the product of four random numbers.
"""
n1 = random.random()
n2 = random.random()
n3 = random.random()
n4 = random.choice([100, 1000, 10000])
return n1 * n2 * n3 * n4