Coverage for python/lsst/obs/fiberspectrograph/spectrum.py: 36%
49 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-09 04:35 -0700
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-09 04:35 -0700
1# This file is part of obs_fiberspectrograph
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22__all__ = ("FiberSpectrum",)
24import numpy as np
25import astropy.io.fits
26import astropy.units as u
27from ._instrument import FiberSpectrograph
28from .data_manager import DataManager
29import lsst.afw.image as afwImage
30from astro_metadata_translator import ObservationInfo
33class FiberSpectrum:
34 """Define a spectrum from a fiber spectrograph.
36 Parameters
37 ----------
38 wavelength : `numpy.ndarray`
39 Spectrum wavelength in units provided by the spectrum file.
40 flux: `numpy.ndarray`
41 Spectrum flux.
42 md: `dict`
43 Dictionary of the spectrum headers.
44 detectorId : `int`
45 Optional Detector ID for this data.
46 """
48 def __init__(self, wavelength, flux, md=None, detectorId=0, mask=None, variance=None):
49 self.wavelength = wavelength
50 self.flux = flux
51 self.metadata = md
53 self.info = ObservationInfo(md)
54 self.detector = FiberSpectrograph().getCamera()[detectorId]
56 mask_temp = afwImage.MaskX(1, 1)
57 self.getPlaneBitMask = mask_temp.getPlaneBitMask # ughh, awful Mask API
58 self.mask = mask
59 self.variance = variance
61 def getDetector(self):
62 """Get fiber spectrograph detector."
63 """
64 return self.detector
66 def getInfo(self):
67 """Get observation information."
68 """
69 return self.info
71 def getMetadata(self):
72 """Get the spectrum metadata."
73 """
74 return self.metadata
76 def getFilter(self):
77 """Get filter label."
78 """
79 return FiberSpectrograph.filterDefinitions[0].makeFilterLabel()
81 def getBBox(self):
82 """Get bounding box."
83 """
84 return self.detector.getBBox()
86 @classmethod
87 def readFits(cls, path):
88 """Read a Spectrum from disk."
90 Parameters
91 ----------
92 path : `str`
93 The file to read
95 Returns
96 -------
97 spectrum : `~lsst.obs.fiberspectrograph.FiberSpectrum`
98 In-memory spectrum.
99 """
101 fitsfile = astropy.io.fits.open(path)
102 md = dict(fitsfile[0].header)
103 format_v = md["FORMAT_V"]
105 if format_v == 1:
106 flux = fitsfile[0].data
107 wavelength = fitsfile[md["PS1_0"]].data[md["PS1_1"]].flatten()
109 wavelength = u.Quantity(wavelength, u.Unit(md["CUNIT1"]), copy=False)
111 mask_temp = afwImage.MaskX(1, 1)
112 mask = np.zeros(flux.shape, dtype=mask_temp.array.dtype)
113 variance = np.zeros_like(flux)
114 if len(fitsfile) == 4:
115 mask = fitsfile[2].data
116 variance = fitsfile[3].data
117 else:
118 raise ValueError(f"FORMAT_V has changed from 1 to {format_v}")
120 return cls(wavelength, flux, md=md, mask=mask, variance=variance)
122 def writeFits(self, path):
123 """Write a Spectrum to disk.
125 Parameters
126 ----------
127 path : `str`
128 The file to write
129 """
130 hdl = DataManager(self).make_hdulist()
132 hdl.writeto(path)