Coverage for python/lsst/obs/fiberspectrograph/spectrum.py: 36%

49 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-19 11:19 +0000

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/>. 

21 

22__all__ = ("FiberSpectrum",) 

23 

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 

31 

32 

33class FiberSpectrum: 

34 """Define a spectrum from a fiber spectrograph. 

35 

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 """ 

47 

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 

52 

53 self.info = ObservationInfo(md) 

54 self.detector = FiberSpectrograph().getCamera()[detectorId] 

55 

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 

60 

61 def getDetector(self): 

62 """Get fiber spectrograph detector." 

63 """ 

64 return self.detector 

65 

66 def getInfo(self): 

67 """Get observation information." 

68 """ 

69 return self.info 

70 

71 def getMetadata(self): 

72 """Get the spectrum metadata." 

73 """ 

74 return self.metadata 

75 

76 def getFilter(self): 

77 """Get filter label." 

78 """ 

79 return FiberSpectrograph.filterDefinitions[0].makeFilterLabel() 

80 

81 def getBBox(self): 

82 """Get bounding box." 

83 """ 

84 return self.detector.getBBox() 

85 

86 @classmethod 

87 def readFits(cls, path): 

88 """Read a Spectrum from disk." 

89 

90 Parameters 

91 ---------- 

92 path : `str` 

93 The file to read 

94 

95 Returns 

96 ------- 

97 spectrum : `~lsst.obs.fiberspectrograph.FiberSpectrum` 

98 In-memory spectrum. 

99 """ 

100 

101 fitsfile = astropy.io.fits.open(path) 

102 md = dict(fitsfile[0].header) 

103 format_v = md["FORMAT_V"] 

104 

105 if format_v == 1: 

106 flux = fitsfile[0].data 

107 wavelength = fitsfile[md["PS1_0"]].data[md["PS1_1"]].flatten() 

108 

109 wavelength = u.Quantity(wavelength, u.Unit(md["CUNIT1"]), copy=False) 

110 

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}") 

119 

120 return cls(wavelength, flux, md=md, mask=mask, variance=variance) 

121 

122 def writeFits(self, path): 

123 """Write a Spectrum to disk. 

124 

125 Parameters 

126 ---------- 

127 path : `str` 

128 The file to write 

129 """ 

130 hdl = DataManager(self).make_hdulist() 

131 

132 hdl.writeto(path)