-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdph_gen.py
More file actions
executable file
·189 lines (151 loc) · 6.48 KB
/
dph_gen.py
File metadata and controls
executable file
·189 lines (151 loc) · 6.48 KB
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
from matplotlib.gridspec import GridSpec
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib
import argparse
from yaml import load, Loader
matplotlib.use('Agg')
def resample(image, pixsize):
"""
Take a 128 x 128 pixel image, and rebin it such that
new pixels = pixsize x pixsize old pixels
"""
assert pixsize in [1, 2, 4, 8, 16] # return error and exit otherwise
imsize = int(128/pixsize)
newimage = np.zeros((imsize, imsize))
for xn, x in enumerate(np.arange(0, 128, pixsize)):
for yn, y in enumerate(np.arange(0, 128, pixsize)):
newimage[xn, yn] = np.nansum(image[x:x+pixsize, y:y+pixsize]) # Nansum is important as sum of masked array can be nan
return newimage
def plot_binned_dph(fig,ax,ax_title,image,pixbin,colormap):
"""
Plots a dph of a given binning
Inputs:
fig = figure to which the ax object belongs
ax = axis object
ax_title = title required
image = the image to be plotted
pixbin = the resampling bin size
"""
im = ax.imshow(resample(image,pixbin),cmap=colormap, interpolation='none')
ax.set_title(ax_title,fontsize=8)
ax.set_xlim(-1,128/pixbin - 0.5)
ax.axvline(x=-0.75,ymin=0,ymax=64,linewidth=5,color='k')
ax.spines['left'].set_position(('data',-0.5))
ax.set_yticklabels([])
ax.xaxis.set_ticks(np.arange(0,128/pixbin,16/pixbin))
ax.set_xticklabels(np.arange(0,128,16))
cb = fig.colorbar(im,ax=ax,cmap=colormap, fraction=0.046, pad=0.04)
cb.ax.tick_params(labelsize=8)
return 0
def evt2image(infile, tstart, tend, e_low, e_high):
hdu = fits.open(infile)
pixel_edges = np.arange(-0.5, 63.6)
# e_low = 200 # Energy cut lower bound
# e_high = 2000 # Energy cut upper bound
data = hdu[1].data[np.where( (hdu[1].data['Time'] >= tstart) & (hdu[1].data['Time'] <= tend) )]
sel = (data['energy'] > e_low) & (data['energy'] < e_high)
data = data[sel]
im1 = np.histogram2d(data['detx'], data['dety'], bins=(pixel_edges, pixel_edges))
im1 = np.transpose(im1[0])
data = hdu[2].data[np.where( (hdu[2].data['Time'] >= tstart) & (hdu[2].data['Time'] <= tend) )]
sel = (data['energy'] > e_low) & (data['energy'] < e_high)
data = data[sel]
im2 = np.histogram2d(data['detx'], data['dety'], bins=(pixel_edges, pixel_edges))
im2 = np.transpose(im2[0])
data = hdu[3].data[np.where( (hdu[3].data['Time'] >= tstart) & (hdu[3].data['Time'] <= tend) )]
sel = (data['energy'] > e_low) & (data['energy'] < e_high)
data = data[sel]
im3 = np.histogram2d(data['detx'], data['dety'], bins=(pixel_edges, pixel_edges))
im3 = np.transpose(im3[0])
data = hdu[4].data[np.where( (hdu[4].data['Time'] >= tstart) & (hdu[4].data['Time'] <= tend) )]
sel = (data['energy'] > e_low) & (data['energy'] < e_high)
data = data[sel]
im4 = np.histogram2d(data['detx'], data['dety'], bins=(pixel_edges, pixel_edges))
im4 = np.transpose(im4[0])
image = np.zeros((128,128))
image[0:64,0:64] = im4
image[0:64,64:128] = im3
image[64:128,0:64] = im1
image[64:128,64:128] = im2
image = np.flip(image,0)
# plt.imshow(image, origin="lower")
# plt.show()
return image
def data_bkgd_image(infile,pre_tstart,pre_tend,grb_tstart,grb_tend,post_tstart,post_tend,e_low,e_high):
"""
Creates source and background dph.
"""
predph = evt2image(infile,pre_tstart,pre_tend,e_low,e_high)
grbdph = evt2image(infile,grb_tstart,grb_tend,e_low,e_high)
postdph = evt2image(infile,post_tstart,post_tend,e_low,e_high)
bkgddph = predph+postdph
# oneD_grbdph = grbdph.flatten()
# oneD_bkgddph = bkgddph.flatten()
t_src = grb_tend - grb_tstart
t_total = (pre_tend-pre_tstart)+(post_tend-post_tstart)
sourcedph = grbdph - bkgddph * t_src/t_total
return sourcedph
# Make Parser
parser = argparse.ArgumentParser()
parser.add_argument('infile', help='Event file to process', type=str)
parser.add_argument('config', help='Config file containing windows', type=str)
parser.add_argument('--no_plotlc', help='If provided, will not make an lc plot. Only provide if window is set', dest='plotlc', action='store_false')
args = parser.parse_args()
with open(args.config, 'r') as f:
config = load(f, Loader=Loader)
# Window Times
pre_tstart = config['window']['pre_tstart']
pre_tend = config['window']['pre_tend']
grb_tstart = config['window']['grb_tstart']
grb_tend = config['window']['grb_tend']
post_tstart = config['window']['post_tstart']
post_tend = config['window']['post_tend']
# Time to specify as 'middle' of the burst
nominal_grb_time = 0.5*(grb_tstart+grb_tend)
# Get the DPH image
image = data_bkgd_image(args.infile, pre_tstart, pre_tend, grb_tstart,grb_tend, post_tstart, post_tend, config['dph']['emin'], config['dph']['emax'])
#DPH Plotting begins
plotfile = PdfPages(config['outname']['dph'])
fig = plt.figure(figsize=(6,6))
gs = GridSpec(nrows=2, ncols=2)
binnings = [1,4,8,16]
for i in range(4):
ax = plt.subplot(gs[i])
ax_title = f"{binnings[i]} Pixel Binning"
pixbin = binnings[i]
plot_binned_dph(fig, ax, ax_title, image, pixbin, 'viridis')
plt.text(0.5, 0.95, "Detector Plane Histogram", ha='center', va='top', transform=fig.transFigure)
plt.text(0.5, 0.9, f"at time {nominal_grb_time:2.2f}", ha='center', va='bottom', transform=fig.transFigure)
plotfile.savefig()
plotfile.close()
#DPH Plotting ends
if args.plotlc:
#LC Plotting begins
plotfile = PdfPages(config['outname']['lc'])
plt.figure()
data = fits.getdata(args.infile, config['lc']['quad'])
t, e = data['time'], data['energy']
#Apply energy cuts
sel = (e>config['dph']['emin']) & (e<config['dph']['emax'])
tmin = nominal_grb_time - config['lc']['interval']
tmax = nominal_grb_time + config['lc']['interval']
#Histogram to get light curve
counts, bins = np.histogram(t[sel], bins=np.arange(tmin, tmax, config['lc']['tbin']))
bins = 0.5*(bins[1:] + bins[:-1])
#Actual Plotting
plt.plot(bins, counts, 'k')
plt.axvspan(grb_tstart,grb_tend,color='red',alpha=0.3,label='GRB')
plt.axvspan(pre_tstart,pre_tend,color='orange',alpha=0.5)
plt.axvspan(post_tstart,post_tend,color='orange',alpha=0.5,label='Background')
plt.legend()
plt.suptitle(f"Light Curve binned at {config['lc']['tbin']} s")
plt.title("with transient and background windows")
plt.ylabel("Counts")
plt.xlabel("Time [s]")
# plt.show()
plotfile.savefig()
plotfile.close()
#LC plotting ends