-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec_io.py
266 lines (239 loc) · 7.9 KB
/
spec_io.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
"""
Utilities for reading in spectra from different filetypes and returning a
Spectrum class.
"""
import os
import numpy as np
import pandas as pd
from astropy.io import fits
from trm import molly
from .misc import Wave
__all__ = [
"spec_from_txt",
"spec_from_npy",
"model_from_dk",
"head_from_dk",
"spec_from_sdss_fits",
"subspectra_from_sdss_fits",
"spec_from_fits_generic",
"spec_list_from_molly",
]
#element dict for dk headers
el_dict = {
1:'H' , 2:'He', 3:'Li', 4:'Be', 6:'C' , 7:'N' , 8:'O' ,
9:'F' , 10:'Ne', 11:'Na', 12:'Mg', 13:'Al', 14:'Si', 15:'P' ,
16:'S' , 17:'Cl', 18:'Ar', 19:'K' , 20:'Ca', 21:'Sc', 22:'Ti',
23:'V' , 24:'Cr', 25:'Mn', 26:'Fe', 27:'Co', 28:'Ni', 29:'Cu',
30:'Zn', 31:'Ga', 32:'Ge', 38:'Sr', 56:'Ba',
}
def spec_from_txt(
Spectrum,
fname,
wave=None,
x_unit='AA',
y_unit='erg/(s cm2 AA)',
delimiter=r'\s+',
model=False,
**kwargs,
):
"""
Loads a text file as a Spectrum object. If model is set to True, only two
columns are read (wavelengths and fluxes) with errors set to zero,
otherwise errors are read from the third column. If wave is not explicitly
set, models are assumed to be 'vac', or 'air' for observed spectra with
errors. kwargs are passed to pd.read_csv.
"""
if 'usecols' not in kwargs:
ncols = 2 if model else 3
kwargs['usecols'] = range(ncols)
data = pd.read_csv(fname, delimiter=delimiter, **kwargs)
data = data.values.T
x, y, e = (*data, 0) if model else data
if wave is None:
wave = Wave.VAC if model else Wave.AIR
name, _ = os.path.splitext(os.path.basename(fname))
return Spectrum(x, y, e, name, wave, x_unit, y_unit)
def multi_model_from_txt(
Spectrum,
fname,
nfluxcols,
wave=Wave.VAC,
x_unit='AA',
y_unit='erg/(s cm2 AA)',
delimiter=r'\s+',
**kwargs,
):
"""
Read files with multiple flux columns as model spectra (no uncertainties).
This can be useful for loading model spectra with a single wavelength axis
and different model fluxes for each column.
"""
x, *yy = pd.read_csv(
fname,
delimiter=delimiter,
usecols=range(nfluxcols+2),
**kwargs
).values.T
name, _ = os.path.splitext(os.path.basename(fname))
return [Spectrum(x, y, 0, name, wave, x_unit, y_unit) for y in yy]
def head_from_dk(fname, return_skip=False):
"""
Return the header from a DK file, optionally the number of rows to skip for
reading the data after.
"""
hdr = {'el': {}}
with open(fname, 'r') as Fdk:
for skip, line in enumerate(Fdk, 1):
if line.startswith("TEFF"):
hdr['Teff'] = float(line.split()[2])
elif line.startswith("LOG_G"):
hdr['logg'] = float(line.split()[2])
elif line.startswith("COMMENT el"):
*_, Z, logZ = line.split()
Z = int(Z)
if Z >= 100: #compatability with older dk files
Z //= 100
hdr['el'][el_dict[Z]] = float(logZ)
elif line.startswith("NMU"):
#angular dependent fluxes
hdr['mu'], hdr['wmu'] = [], []
elif line.startswith("MU"):
hdr['mu'] += [float(x) for x in line.split()[2:]]
elif line.startswith("WMU"):
hdr['wmu'] += [float(x) for x in line.split()[2:]]
elif line.startswith("END"):
break
else:
continue
return (hdr, skip) if return_skip else hdr
def model_from_dk(
Spectrum,
fname,
x_unit='AA',
y_unit='erg/(s cm2 AA)',
use_Imu=False,
):
"""
Read Detlev Koester white dwarf models as spectra. Units are converted to
those specified, and DK header items placed in the Spectrum object header.
For models with angular dependent fluxes, if use_Imu is set to True a list
of models is returned, otherwise just the disc average flux is used.
"""
hdr, skip = head_from_dk(fname, True)
kwargs = {
'wave': Wave.VAC,
'x_unit': 'AA',
'y_unit': 'erg/(s cm3)',
'skiprows':skip,
}
if 'mu' in hdr and use_Imu:
#angular dependent fluxes
mus, wmus = hdr.pop('mu'), hdr.pop('wmu')
MM = multi_model_from_txt(Spectrum, fname, len(mus), **kwargs)
for M in MM:
M.x_unit_to(x_unit)
M.y_unit_to(y_unit)
M.head.update(hdr)
for M, mu, wmu in zip(MM[1:], mus, wmus):
M.name += f"_mu_{mu:f}"
M.head['mu'], M.head['wmu'] = mu, wmu
return MM
M = spec_from_txt(Spectrum, fname, model=True, **kwargs)
M.x_unit_to(x_unit)
M.y_unit_to(y_unit)
M.head.update(hdr)
return M
def spec_from_npy(
Spectrum,
fname,
wave=Wave.AIR,
x_unit='AA',
y_unit='erg/(s cm2 AA)'
):
"""
Loads a npy file with 2 or 3 columns as wavelengths, fluxes(, errors).
"""
data = np.load(fname)
if data.ndim != 2:
raise ValueError("Data must be 2D")
if data.shape[0] not in (2, 3):
raise ValueError("Data should have 2 or 3 columns")
x, y, e = (*data, 0) if data.shape[0] == 2 else data
name, _ = os.path.splitext(os.path.basename(fname))
return Spectrum(x, y, e, name, wave, x_unit, y_unit)
def spec_from_sdss_fits(Spectrum, fname, **kwargs):
"""
loads a sdss fits file as spectrum (result in vac wavelengths)
"""
hdulist = fits.open(fname, **kwargs)
S = _get_spec_from_hdu(Spectrum, hdulist[1])
name, _ = os.path.splitext(os.path.basename(fname))
S.name = name
hdr = hdulist[0].header
S.head['PLATE'] = hdr['PLATEID']
S.head['MJD'] = hdr['MJD']
#FIXME using wrong keyword
S.head['FIBER'] = hdr['PLATEID']
return S
def subspectra_from_sdss_fits(Spectrum, fname, **kwargs):
"""
loads a sdss fits file as spectrum (result in vac wavelengths)
"""
hdulist = fits.open(fname, **kwargs)
return [_get_spec_from_hdu(Spectrum, hdu) for hdu in hdulist[4:]]
def _get_spec_from_hdu(Spectrum, hdu):
keys = 'loglam flux ivar sky'.split()
loglam, flux, ivar, sky = [hdu.data[key] for key in keys]
lam = 10**loglam
ivar[ivar == 0.] = 0.001
err = 1/np.sqrt(ivar)
sky *= 1e17
name = hdu.header['EXTNAME'] if 'EXTNAME' in hdu.header else ""
head = {'sky': sky}
return Spectrum(lam, flux, err, name, Wave.VAC, head=head)*1e-17
def spec_from_fits_generic(
Spectrum,
fname,
wave=Wave.AIR,
x_unit="AA",
y_unit="erg/(s cm2 AA)",
):
"""
Load a spectrum from a generic fits file
"""
hdulist = fits.open(fname)
hdr = dict(hdulist[0].header)
data = hdulist[1].data
x, y, e = [data[col] for col in ("Wavelength", "Flux", "Error")]
e[e < 0] = np.inf
name = hdr['OBJECT'] if 'OBJECT' in hdr else fname
return Spectrum(x, y, e, name, wave, x_unit, y_unit, hdr)
def spec_list_from_molly(Spectrum, fname):
"""
Returns a list of spectra read in from a TRM molly file.
"""
return [_convert_mol(Spectrum, mol) for mol in molly.gmolly(fname)]
def _convert_mol(Spectrum, molsp):
x, y, e = molsp.wave, molsp.f, molsp.fe
name = molsp.head['Object']
S = Spectrum(x, y, np.abs(e), name, y_unit="mJy")
S.head = molsp.head
return S
def write(self, fname):
"""
Saves Spectrum to a text or npy file.
"""
if fname.endswith(".npy"):
cols = np.array(self.data)
np.save(fname, cols[:2] if self._model else cols)
return
#text files:
with open(fname, 'w') as F:
if self._model:
for px in self:
x, y, e = px
F.write(f"{x:9.3f} {y:12.5E}\n")
else:
for px in self:
x, y, e = px
F.write(f"{x:9.3f} {y:12.5E} {e:11.5E}\n")