-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostic.py
318 lines (252 loc) · 11.8 KB
/
diagnostic.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
"""
This script is a direct import from the `nustar_gen_utils` package
with a few import line changes.
"""
from nustar import *
import os
import astropy.units as u
ns = NuSTAR()
def goes_lightcurve(obs, show_sun=False, show_sky=False, show_impact=True):
"""
Module for producing a GOES lightcurve and the NuSTAR in/out of Sun times
Parameters
-----------
obs : Class
Output from nustar_gen.info.Observation() class
Other Parameters
----------------
show_sun : bool
Show just the Sun period (default is False)
show_sky : bool
Show the unocculted periods (default is false)
show_impact : bool
Show the periods where you are in Sun and the source is unocculted.
Default is True
Note that setting either show_sun or show_sky prevents show_impact
Returns
-------
None
"""
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.patches import Rectangle
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
from astropy.visualization import time_support
from astropy.io.fits import getheader, getdata
from astropy.time import Time
try:
from sunpy import timeseries as ts
from sunpy.net import Fido
from sunpy.net import attrs as a
except:
print('This module requires SunPy. Please install this first.')
return
# Get the header info
evf = obs.evtfiles['A'][0]
hdr = getheader(evf)
tstart = (ns.met_to_time(hdr['TSTART']))
tend = (ns.met_to_time(hdr['TSTOP']))
if tstart < Time('2019-01-01T01:00:00'):
gind = 15
else:
gind = 17
result = Fido.search(a.Time(tstart.fits, tend.fits), a.Instrument("XRS"),
a.goes.SatelliteNumber(gind))
files = Fido.fetch(result, progress=False)
goes_all = ts.TimeSeries(files, concatenate=True)
goes = goes_all.truncate(tstart.iso, tend.iso)
fig, ax = plt.subplots(figsize=(12, 8))
goes.plot(columns=["xrsb"])
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d %H:%M'))
fig.autofmt_xdate()
# Okay, now we want to add on some NuSTAR info.
attorb_file = os.path.join(obs.evdir, f'nu{obs.seqid}A.attorb')
assert os.path.isfile(attorb_file), f'make_goes_json_image.py: No attorb file {attorb_file}'
attorb, hdr = getdata(attorb_file, 1, header=True)
obs_start = ns.met_to_time(hdr['TSTART'])
obs_end = ns.met_to_time(hdr['TSTOP'])
ax.set_ylim(1e-7, 1e-3)
# Do the sunshine part
if show_sun:
show_impact=False
in_sun = (np.where(attorb['SUNSHINE'] ==1))[0]
diff = (np.diff(in_sun))
transition = (np.where(diff > 1))[0]
# Check to see if you started in sun:
lims = ax.get_ylim()
height = lims[1] - lims[0]
for ind, edge in enumerate(transition):
if (ind == 0):
if (in_sun[0] == 0):
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][0]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
else:
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[0]]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
else:
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][left_edge_ind]).datetime)
prev_right_edge = in_sun[edge]
left_edge_ind = prev_right_edge + diff[edge]
width = right_edge - left_edge
rect = Rectangle((left_edge, lims[0]), width, 1, color='yellow', alpha = 0.5)
ax.add_patch(rect)
# Now get the last one:
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][left_edge_ind]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun].max()).datetime)
rect = Rectangle((left_edge, lims[0]), width, 1, color='yellow', alpha = 0.5)
ax.add_patch(rect)
if show_sky:
show_impact=False
# Now do the occulted / unocculted part
## Add the unocculted periods ##
in_sun = (np.where(attorb['ELV'] > 3))[0]
diff = (np.diff(in_sun))
transition = (np.where(diff > 1))[0]
# Check to see if you started in sun:
lims = ax.get_ylim()
height = lims[1] - lims[0]
for ind, edge in enumerate(transition):
if (ind == 0):
if (in_sun[0] == 0):
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][0]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
else:
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[0]]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
else:
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][left_edge_ind]).datetime)
prev_right_edge = in_sun[edge]
left_edge_ind = prev_right_edge + diff[edge]
width = right_edge - left_edge
rect = Rectangle((left_edge, lims[0]), width, 1, color='blue', alpha = 0.2)
ax.add_patch(rect)
# Now get the last one:
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][left_edge_ind]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun].max()).datetime)
rect = Rectangle((left_edge, lims[0]), width, 1, color='blue', alpha = 0.2)
ax.add_patch(rect)
if show_impact:
## Show both in-sun and unocculted
in_sun = (np.where((attorb['ELV'] > 3)&(attorb['SUNSHINE'] ==1)))[0]
diff = (np.diff(in_sun))
transition = (np.where(diff > 1))[0]
# Check to see if you started in sun:
lims = ax.get_ylim()
height = lims[1] - lims[0]
for ind, edge in enumerate(transition):
if (ind == 0):
if (in_sun[0] == 0):
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][0]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
else:
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[0]]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
else:
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun[edge]]).datetime)
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][left_edge_ind]).datetime)
prev_right_edge = in_sun[edge]
left_edge_ind = prev_right_edge + diff[edge]
width = right_edge - left_edge
rect = Rectangle((left_edge, lims[0]), width, 1, color='orange', alpha = 0.2)
ax.add_patch(rect)
# Now get the last one:
left_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][left_edge_ind]).datetime)
right_edge = mdates.date2num(ns.met_to_time(attorb['TIME'][in_sun].max()).datetime)
rect = Rectangle((left_edge, lims[0]), width, 1, color='orange', alpha = 0.2)
ax.add_patch(rect)
plt.show()
def compare_sun_spec(obs,mod='A',src_rad = 2*u.arcmin,
en_range = (3, 20), en_bins = 34, ratio = False):
"""
Uses the attorb file to generate a spectrum when NuSTAR is in/out
of sunlight to check for solar activity.
Parameters
-----------
obs : Class
Output from nustar_gen.info.Observation() class
Other Parameters
----------------
mod : str
Which module, should 'A' or 'B'. Default is 'A'
src_rad : astropy unit
Radius to throw away source counts. Default is 2-arcmin
en_range : list
Energy range to use. Default is (3, 20).
en_bins : int
Number of bins to use for histogram (default is 34)
ratio : bool
Whether to plot the spectra or their ratio. Default is False.
Returns
-------
None
"""
import os
import warnings
from astropy.utils.exceptions import AstropyWarning
warnings.simplefilter('ignore', category=AstropyWarning)
warnings.simplefilter('ignore', category=RuntimeWarning)
from astropy.table import Table
from numpy import interp, histogram, sqrt
from astropy.io.fits import getheader
from utils import chan_to_energy
import matplotlib.pyplot as plt
# Load the 01 event file:
evf = obs.science_files[mod][0]
# In the future, maybe come back and do the thing where we get rid of the
# source and just look at the background. But we typically get rid of that info
#
limit_pix = ((src_rad / ns.pixel).cgs).value
# Check and see if nuskytodet has been run
sky2det = os.path.join(obs.evdir, f'nu{obs.seqid}_sky2det{mod}.fits')
if not os.path.isfile(sky2det):
# Spawn this in the background:
attfile = os.path.join(obs.evdir, f'nu{obs.seqid}_att.fits')
mastfile = os.path.join(obs.evdir, f'nu{obs.seqid}_mast.fits')
hdr = getheader(obs.evtfiles[mod][0])
cmdstring = f"nuskytodet pntra={hdr['RA_OBJ']} pntdec={hdr['DEC_OBJ']} "
cmdstring += f"attfile={attfile} instrument=FPM{mod} skydetfile={sky2det}"
cmdstring += f" mastaspectfile={mastfile}"
print('No sky2det file found, running nuskytodet')
sky2det_log = os.path.join(obs.evdir, f'nu{obs.seqid}_sky2det{mod}.log')
cmdstring += f'> {sky2det_log}'
print(cmdstring)
os.system(cmdstring)
detsrc = Table.read(sky2det, hdu =1)
attorbf = os.path.join(obs.evdir, f'nu{obs.seqid}{mod}.attorb')
assert os.path.isfile(attorbf), f'Attorb file not found: {attorbf}. Run nupipeline first.'
attorb = Table.read(attorbf, hdu=1, memmap=True)
evt = Table.read(evf, hdu='EVENTS', memmap=True)
evt['SUN'] = interp(evt['TIME'], attorb['TIME'], attorb['SUNSHINE'])
xr = interp(evt['TIME'], detsrc['TIME'], detsrc['DET1X'])
yr = interp(evt['TIME'], detsrc['TIME'], detsrc['DET1Y'])
evt['RAD'] = sqrt((evt['DET1X'] - xr)**2 + (evt['DET1Y'] - yr)**2)
no_sun = chan_to_energy(evt[(evt['SUN'] == 0)&(evt['RAD']>limit_pix)]['PI'])
sun = chan_to_energy(evt[(evt['SUN'] ==1)&(evt['RAD']>limit_pix)]['PI'])
scale_no_sun = (len(no_sun) / len(evt))
scale_sun = (len(sun) / len(evt))
hist_sun, edges = histogram(sun, range = en_range, bins = en_bins)
hist_nosun, edges = histogram(no_sun, range = en_range, bins = en_bins)
center = (edges[:-1] + edges[1:])/2
if ratio is False:
ax = plt.figure(figsize=(12, 8)).subplots()
ax.step(center, hist_nosun / scale_no_sun, label = 'No Sun')
ax.step(center, hist_sun / scale_sun, label = 'Sun')
ax.set_xscale('log')
ax.set_yscale('log')
ax.legend()
ax.set_xlabel('Energy (keV)', fontsize=12)
ax.set_ylabel('Scaled counts', fontsize=12)
plt.show()
else:
ax = plt.figure(figsize=(12, 8)).subplots()
ax.step(center, (hist_sun / scale_sun) / (hist_nosun / scale_no_sun),
label = 'Ratio (Sun / No Sun)')
ax.set_xscale('log')
ax.set_xlabel('Energy (keV)', fontsize=12)
ax.set_ylabel('Ratio', fontsize=12)
ax.legend()
ax.axhline(1.0, color = 'green')
plt.show()