-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightcurves.py
327 lines (247 loc) · 8.44 KB
/
lightcurves.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
print
print
print
print
print
print
print
print
print
print
print
print
print "Start"
from pylab import *
from decimal import Decimal
import os
import subprocess
from fnmatch import fnmatch
from scipy import fftpack
from scipy import optimize
from datetime import datetime
print "Done importing stuff."
#subprocess.call("start_xray")
def getFilePaths(superpath = "/scratch/janna/project_data", pattern = 'FS46*.gz', get_obs_ids=False):
# FS46*,gz for standard 1 data, FS37*.gz for event data, *.lc.txt for lightcurve ascii files
# NOTE: filepaths now without file at the end, only directory.
filepaths = []
filenames = []
if get_obs_ids:
obs_ids = []
for dirpath, dirnames, files in os.walk(superpath):
for f in files:
if fnmatch(f, pattern):
filepath = os.path.join(superpath, dirpath)
filepaths.append(filepath)
filenames.append(f)
if get_obs_ids:
obs_ids.append(dirpath[len(superpath) + 1:-4])
print len(filepaths), "files found in ", superpath
if get_obs_ids:
return filepaths, filenames, obs_ids
else:
return filepaths, filenames
def runsaextrct(filepath, filename, binwidth = "0.1"):
os.chdir(str(filepath))
print "changed directory to: "
os.system('pwd')
full_filepath = os.path.join(filepath, filename)
extract_filename = filename + "_extract"
extract_args = ["saextrct", full_filepath, "APPLY", "-", extract_filename, "ONE", "TIME", "GOOD", binwidth, "lightcurve", "RATE", "SUM", \
"INDEF", "INDEF", "INDEF", "INDEF", "INDEF", "INDEF", "INDEF"]
subprocess.call(extract_args)
extract_out = os.path.join(filepath, extract_filename)
extract_out += ".lc"
columns_in = extract_out
columns_args = ["flcol", columns_in]
subprocess.call(columns_args)
#plot_in = extract_out
#plot_args = ["fplot", plot_in, "TIME", "RATE", "-", "/XW", "offset=yes", "NONE"]
#subprocess.call(plot_args)
dump_in = extract_out
dump_out = dump_in + ".txt"
dump_args = ["fdump", dump_in, dump_out, "TIME, RATE", "-", "prhead=no", "showcol=no", "showunit=no", "showrow=no"]
subprocess.call(dump_args)
def runseextrct(filepath, filename, binwidth = "0.1"):
os.chdir(str(filepath))
print "changed directory to: "
os.system('pwd')
full_filepath = os.path.join(filepath, filename)
extract_filename = filename + "_extract"
extract_args = ["seextrct", full_filepath, "APPLY", "-", extract_filename, "TIME", "Event", binwidth, "lightcurve", "RATE", "SUM", \
"INDEF", "INDEF", "INDEF", "INDEF", "INDEF", "INDEF", "INDEF"]
subprocess.call(extract_args)
extract_out = os.path.join(filepath, extract_filename)
extract_out += ".lc"
columns_in = extract_out
columns_args = ["flcol", columns_in]
subprocess.call(columns_args)
dump_in = extract_out
dump_out = dump_in + ".txt"
dump_args = ["fdump", dump_in, dump_out, "TIME, RATE", "-", "prhead=no", "showcol=no", "showunit=no", "showrow=no"]
subprocess.call(dump_args)
def getData(filepath, filename, starttime):
print "loading data from file: ", filename, "..."
full_filepath = os.path.join(filepath, filename)
datafile = open(full_filepath, 'rb')
TIME = []
RATE = []
for row in datafile:
entries = row.split()
if len(entries) > 0:
#print "entries ", entries
#print "Time ", entries[0], type(entries[0])
TIME.append(Decimal(entries[0]) - starttime)
#print "Rate ", entries[1], type(entries[1])
#print Decimal(entries[1])
RATE.append(Decimal(entries[1]))
return TIME, RATE
# TAI-UTC dictionary:
tai_utc_dict = {}
tai_utc_filepath = "/scratch/janna/tai-utc.dat"
tai_utc_file = open(tai_utc_filepath, 'rb')
for row_t in tai_utc_file:
entries_t = row_t.split()
JD_date = Decimal(entries_t[4])
number1 = Decimal(entries_t[6])
number2 = Decimal(entries_t[11][:-2])
number3 = Decimal(entries_t[13])
MJD_date = JD_date - Decimal("2400000.5")
tai_min_utc = number1 + (MJD_date - number2) * number3
tai_utc_dict[MJD_date] = tai_min_utc
def MJD_UTC_to_MET_TT(MJD_UTC):
# Tested this function, it's ok!
MJD_UTC = Decimal(str(MJD_UTC))
MJD_dates = array(sorted(tai_utc_dict.keys()))
MJD_date = MJD_dates[MJD_dates.searchsorted(MJD_UTC) - 1]
tai_min_utc = tai_utc_dict[MJD_date]
RXTE_zero = Decimal("49353.00073567628")
TT_TAI = Decimal("32.184")
MET_TT = (MJD_UTC - RXTE_zero) * 3600 * 24 + tai_min_utc + TT_TAI
return MET_TT
# obs-ids dictionary:
obs_ids_dict = {}
obs_ids_filepath = "/scratch/janna/obs_ids_Tullio.txt"
obs_ids_file = open(obs_ids_filepath, 'rb')
for row_o in obs_ids_file:
entries_o = row_o.split()
obs_id = entries_o[0]
starttime_MJD = Decimal(entries_o[1])
starttime_MET = MJD_UTC_to_MET_TT(starttime_MJD)
obs_ids_dict[obs_id] = starttime_MET
def plotLightcurves(filepath, filename, obs_id, binwidth = "0.1", make_figs = True):
starttime = Decimal(obs_ids_dict[obs_id])
print "start time: ", starttime, type(starttime)
TIME, RATE = getData(filepath, filename, starttime)
if make_figs:
title_ = "Lightcurve of burst " + str(obs_id) + ", file " + str(filename[:-17])
x_label = "time (s) rel. to starttime " + str(starttime) + " MET"
y_label = "photon counts per " + binwidth + " s"
figpath = os.path.join(filepath, filename)
figpath1 = figpath[:-7] + "_plot.png"
print figpath
plot(TIME, RATE)
title(title_)
xlabel(x_label)
ylabel(y_label)
ylabel(y_label)
savefig(figpath1)
close()
start_index = searchsorted(array(TIME), -50)
end_index = searchsorted(array(TIME), 200)
if make_figs:
title2_ = "Zoomed l" + title_[1:]
figpath2 = figpath[:-7] + "_zoom_plot.png"
plot(TIME[start_index:end_index], RATE[start_index:end_index])
title(title_)
xlabel(x_label)
ylabel(y_label)
ylabel(y_label)
savefig(figpath2)
close()
zoom_filepath = os.path.join(filepath, filename)
zoom_filepath = zoom_filepath[:-4] + "_zoom.txt"
zoom_file = open(zoom_filepath, 'w+')
for i in range(start_index, end_index):
row = str(TIME[i]) + " " + str(RATE[i])
zoom_file.write(row)
zoom_file.close()
def do_stuff(datatype, make_figures = True):
if datatype == 1:
gz_pattern = "FS46*.gz"
txt_pattern = "FS46*.txt"
binwidth = "0.1"
elif datatype == 2:
gz_pattern = "FS37*.gz"
txt_pattern = "FS37*.txt"
binwidth = "0.1"
else:
print "Invalid datatype, choose 1 for standard1 data and 2 for event data."
return
extract_errorlist = []
extract_errorlist_paths = []
obs_id_errorlist = []
plot_errorlist = []
plot_errorlist_obs_ids = []
filepaths_gz, filenames_gz = getFilePaths(pattern = gz_pattern)
assert len(filepaths_gz) == len(filenames_gz)
for i in range(len(filepaths_gz)):
filepath = filepaths_gz[i]
filename = filenames_gz[i]
try:
if datatype == 1:
runsaextrct(filepath, filename, binwidth)
elif datatype == 2:
runseextrct(filepath, filename, binwidth)
except:
print "An error occured in file ", filename
extract_errorlist.append(filename)
extract_errorlist_paths.append(filepath)
filepaths_txt, filenames_txt, obs_ids = getFilePaths(pattern = txt_pattern, get_obs_ids=True)
assert len(filepaths_txt) == len(filenames_txt) == len(obs_ids)
for i in range(len(filepaths_txt)):
filepath = filepaths_txt[i]
filename = filenames_txt[i]
try:
obs_id = obs_ids[i]
except:
print "obs id ", obs_id, " not in dictionary"
obs_id_errorlist.append(obs_id)
#plotLightcurves(filepath, filename, obs_id, make_figs = make_figures)
try:
plotLightcurves(filepath, filename, obs_id, make_figs = make_figures)
except:
print "An error occured in plotting file ", filename
plot_errorlist.append(filename)
plot_errorlist_obs_ids.append(obs_id)
print "Done :D"
print
print "Errors in saextrct part in files: "
for i in range(len(extract_errorlist)):
print "file: ", extract_errorlist[i]
print "in directory: ", extract_errorlist_paths[i]
print
print "obs-ids not in list: "
for obs_id_err in obs_id_errorlist:
print obs_id_err
print
print "Errors in plot part in files: "
for i in range(len(plot_errorlist)):
print "File: ", plot_errorlist[i]
print " with obs id: ", plot_errorlist_obs_ids[i]
def do_stuff_event_data():
filepaths, filenames = getFilePaths(pattern = "FS37*.gz")
assert len(filepaths) == len(filenames)
maxfrequency = 1000
binwidth = 1 / Decimal(2 * maxfrequency)
binwidth = str(binwidth)
for i in range(len(filepaths)):
runsaextrct(filepaths[i], filenames[i], binwidth)
do_stuff(2)
def remove_stuff():
filepaths_rm, filenames_rm = getFilePaths(pattern = "*.gz_extract*")
print filenames_rm
for i in range(len(filepaths_rm)):
full_path_rm = os.path.join(filepaths_rm[i], filenames_rm[i])
rm_args = ["rm", full_path_rm]
subprocess.call(rm_args)