-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
hst_cos.py
55 lines (43 loc) · 1.8 KB
/
hst_cos.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
from astropy.units import Unit
from astropy.nddata import StdDevUncertainty
from ...spectra import Spectrum1D
from ..registers import data_loader
from ..parsing_utils import read_fileobj_or_hdulist
__all__ = ['cos_identify', 'cos_spectrum_loader']
def cos_identify(origin, *args, **kwargs):
"""Check whether given file contains HST/COS spectral data."""
with read_fileobj_or_hdulist(*args, **kwargs) as hdulist:
return (hdulist[0].header['TELESCOP'] == 'HST' and hdulist[0].header['INSTRUME'] == 'COS')
@data_loader(
label="HST/COS", identifier=cos_identify,
extensions=['FITS', 'FIT', 'fits', 'fit'], priority=10,
)
def cos_spectrum_loader(file_obj, **kwargs):
"""
Load COS spectral data from the MAST archive into a spectrum object.
Parameters
----------
file_obj: str, file-like, or HDUList
FITS file name, object (provided from name by Astropy I/O Registry),
or HDUList (as resulting from astropy.io.fits.open()).
Returns
-------
data: Spectrum1D
The spectrum that is represented by the data in this table.
"""
with read_fileobj_or_hdulist(file_obj, **kwargs) as hdulist:
header = hdulist[0].header
meta = {'header': header}
unit = Unit("erg/cm**2 Angstrom s")
disp_unit = Unit('Angstrom')
data = hdulist[1].data['FLUX'].flatten() * unit
dispersion = hdulist[1].data['wavelength'].flatten() * disp_unit
uncertainty = StdDevUncertainty(hdulist[1].data["ERROR"].flatten() * unit)
sort_idx = dispersion.argsort()
dispersion = dispersion[sort_idx]
data = data[sort_idx]
uncertainty = uncertainty[sort_idx]
return Spectrum1D(flux=data,
spectral_axis=dispersion,
uncertainty=uncertainty,
meta=meta)