-
Notifications
You must be signed in to change notification settings - Fork 3
/
specind.py
299 lines (265 loc) · 9.47 KB
/
specind.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
"""
Simplest spectral indicator measurements
This code is originally from Sam Dixon (https://github.com/sam-dixon)
"""
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline as US
LIMITS = {
"CaIIHK": [3504, 3687, 3830, 3990],
"SiII4000": [3830, 3963, 4034, 4150],
"SiII5972": [5550, 5681, 5850, 6015],
"SiII6355": [5850, 6015, 6250, 6365],
}
def vel_space(lam, lam0):
"""
Convert a maximum absorption wavelength to a velocity
using the relativistic Doppler formula
Args:
lam: maximum absorption wavelength
lam0: rest-frame absorption wavelength
Returns:
v: ejecta velocity
"""
z = lam / lam0
return 3e5 * (z ** 2 - 1) / (z ** 2 + 1)
def wave_space(vel, lam0):
"""
Convert a velocity to a maximum absorption wavelength
Args:
vel: velocity
lam0: rest-frame absorption wavelength
Returns:
lam: maximum absorption wavelength
"""
beta = vel / 3e5
lam = lam0 * np.sqrt((1 + beta) / (1 - beta))
return lam
class Spectrum(object):
def __init__(self, wave, flux, var, smooth_type="spl"):
"""
Class for measuring the following spectral indicators as simply as
possible:
+ vSiII6355
+ EWSiII4000
+ EWCaIIHK
Args:
wave: wavelength the spectrum is observed at
flux: flux observed
var: flux variance
Attributes:
lamSiII6355: maximum absorption wavelength of SiII6355
EWSiII4000: equivalent width of SiII4000 absorption feature
EWCaIIHK: equivalent width of CaIIH&K absorption feature
"""
self.wave = wave
self.flux = flux
self.var = var
if self.var is None:
self.var = np.abs(self.flux / 1e3)
self.smoothed_wave = np.arange(min(wave), max(wave), 0.1)
if smooth_type == "spl":
self.smoothed_flux = self.smooth()
elif smooth_type == "gauss_filt":
self.smoothed_flux = self.gauss_filt()
self._lamSiII6355 = None
self._lamCaIIHK = None
self._EWSiII4000 = None
self._EWSiII5972 = None
self._EWSiII6355 = None
self._EWCaIIHK = None
def smooth(self):
"""
Smooth the input spectrum using a spline weighted by the variance
Args:
None
Returns:
smoothed_flux: spline evaluated along smoothed_wave
"""
w = 1.0 / np.sqrt(self.var)
spl = US(self.wave, self.flux, w=w)
smoothed_flux = spl(self.smoothed_wave)
return smoothed_flux
def gauss_filt(self, smooth_fac=0.02, n_l=15):
"""
Smooth the input spectrum using a inverse-variance weighted
Gaussian filter (like in Blondin 2007)
Args:
None
Returns:
smoothed_flux: spline evaluated along smoothed_wave
"""
y_smooth = []
x = self.wave
y = self.flux
v = self.var
for i in range(n_l, len(x) - n_l):
sig_i = x[i] * smooth_fac
g_i = np.array(
[
1 / np.sqrt(2 * np.pi) * np.exp((-1 / sig_i * (x[j] - x[i])) ** 2)
for j in range(i - n_l, i + n_l)
]
)
w_i = g_i / v[i - n_l : i + n_l]
y_smooth.append(np.dot(w_i, y[i - n_l : i + n_l]) / np.sum(w_i))
spl = US(x[n_l - 1 : -n_l - 1], np.array(y_smooth), s=0, k=4)
return spl(self.smoothed_wave)
def pseudo_continuum(self, l_max_ind, r_max_ind):
"""
Calculate the pseudo continuum
Args:
l_max_ind: index of maximum flux value in left region
r_max_ind: index of maximum flux value in right region
Returns:
pc_sub_flux: pseudo-continuum subtracted smoothed flux
"""
pc_delta_flux = self.smoothed_flux[r_max_ind] - self.smoothed_flux[l_max_ind]
pc_delta_wave = self.smoothed_wave[r_max_ind] - self.smoothed_wave[l_max_ind]
pseudo_cont_slope = pc_delta_flux / pc_delta_wave
pseudo_cont_int = (
self.smoothed_flux[r_max_ind]
- pseudo_cont_slope * self.smoothed_wave[r_max_ind]
)
pc_sub_flux = self.smoothed_flux / (
pseudo_cont_slope * self.smoothed_wave + pseudo_cont_int
)
return pc_sub_flux
def find_extrema(self, feature_name, return_pc=False):
"""
Find the extrema in the region defined by the given feature
Args:
feature_name: 'SiII6355', 'EWCaIIHK', or 'EWSiII4000'; feature
to find extrema for
return_pc: Return the pseudo-continuum subtracted flux if true
Returns:
l_max_ind: index of maximum flux value in left region
c_min_ind: index of minimum pseudo-continuum subtracted flux
value in center region
r_max_ind: index of maximum flux value in right region
"""
limit = LIMITS[feature_name]
l_region = self.smoothed_flux[
(limit[0] <= self.smoothed_wave) & (self.smoothed_wave < limit[1])
]
r_region = self.smoothed_flux[
(limit[2] <= self.smoothed_wave) & (self.smoothed_wave <= limit[3])
]
try:
l_max_ind = np.where(self.smoothed_flux == max(l_region))[0][0]
r_max_ind = np.where(self.smoothed_flux == max(r_region))[0][0]
except IndexError:
raise ValueError
# if return_pc:
# return None, None, None, None
# else:
# return None, None, None
pc_sub_flux = self.pseudo_continuum(l_max_ind, r_max_ind)
c_region = pc_sub_flux[
(limit[1] <= self.smoothed_wave) & (self.smoothed_wave < limit[2])
]
c_min_ind = np.where(pc_sub_flux == min(c_region))[0][0]
if return_pc:
return l_max_ind, c_min_ind, r_max_ind, pc_sub_flux
return l_max_ind, c_min_ind, r_max_ind
@property
def lamSiII6355(self):
"""
Maximum absorption wavelength of the SiII6355 feature
"""
if self._lamSiII6355 is None:
_, c_min_ind, _ = self.find_extrema("SiII6355")
if c_min_ind is not None:
self._lamSiII6355 = self.smoothed_wave[c_min_ind]
else:
self._lamSiII6355 = np.nan
return self._lamSiII6355
@property
def lamCaIIHK(self):
"""
Maximum absorption wavelength of the CaIIHK feature
"""
if self._lamCaIIHK is None:
_, c_min_ind, _ = self.find_extrema("CaIIHK")
if c_min_ind is not None:
self._lamCaIIHK = self.smoothed_wave[c_min_ind]
else:
self._lamCaIIHK = np.nan
return self._lamCaIIHK
@property
def EWSiII5972(self):
"""
Equivalent width of the SiII5972 feature
"""
if self._EWSiII5972 is None:
l_max_ind, c_min_ind, r_max_ind, pc_sub_flux = self.find_extrema(
"SiII5972", return_pc=True
)
if pc_sub_flux is not None:
c_region = pc_sub_flux[l_max_ind:r_max_ind]
self._EWSiII5972 = np.sum(1.0 - c_region) * 0.1
else:
self._EWSiII5972 = np.nan
return self._EWSiII5972
@property
def EWSiII6355(self):
"""
Equivalent width of the SiII6355 feature
"""
if self._EWSiII6355 is None:
l_max_ind, c_min_ind, r_max_ind, pc_sub_flux = self.find_extrema(
"SiII6355", return_pc=True
)
if pc_sub_flux is not None:
c_region = pc_sub_flux[l_max_ind:r_max_ind]
self._EWSiII6355 = np.sum(1.0 - c_region) * 0.1
else:
self._EWSiII6355 = np.nan
return self._EWSiII6355
@property
def EWSiII4000(self):
"""
Equivalent width of the SiII4000 feature
"""
if self._EWSiII4000 is None:
l_max_ind, c_min_ind, r_max_ind, pc_sub_flux = self.find_extrema(
"SiII4000", return_pc=True
)
if pc_sub_flux is not None:
c_region = pc_sub_flux[l_max_ind:r_max_ind]
self._EWSiII4000 = np.sum(1.0 - c_region) * 0.1
else:
self._EWSiII4000 = np.nan
return self._EWSiII4000
@property
def EWCaIIHK(self):
"""
Equivalent width of the CaIIH&K feature
"""
if self._EWCaIIHK is None:
l_max_ind, c_min_ind, r_max_ind, pc_sub_flux = self.find_extrema(
"CaIIHK", return_pc=True
)
if pc_sub_flux is not None:
c_region = pc_sub_flux[l_max_ind:r_max_ind]
self._EWCaIIHK = np.sum(1.0 - c_region) * 0.1
else:
self._EWCaIIHK = np.nan
return self._EWCaIIHK
def get_spin_dict(self):
"""
Get a dictionary with the available spectral indicators
"""
spin_dict = {
"lamSiII6355": self.lamSiII6355,
"lamCaIIHK": self.lamCaIIHK,
"vSiII6355": vel_space(self.lamSiII6355, 6355.0),
"vCaIIHK": vel_space(self.lamCaIIHK, 3934.0),
"EWSiII4000": self.EWSiII4000,
"EWSiII5972": self.EWSiII5972,
"EWSiII6355": self.EWSiII6355,
"EWCaIIHK": self.EWCaIIHK,
}
return spin_dict