Skip to content

Commit

Permalink
Write and read mask and variance plane and change check on formatv.
Browse files Browse the repository at this point in the history
  • Loading branch information
aferte committed Feb 27, 2024
1 parent 891e0ac commit 572c817
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
22 changes: 20 additions & 2 deletions python/lsst/obs/fiberspectrograph/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ def make_hdulist(self):
"""
hdu1 = self.make_primary_hdu()
hdu2 = self.make_wavelength_hdu()
return astropy.io.fits.HDUList([hdu1, hdu2])
hdu3, hdu4 = self.make_maskvariance_hdu()
return astropy.io.fits.HDUList([hdu1, hdu2, hdu3, hdu4])

def make_fits_header(self):
"""Return a FITS header built from a Spectrum"""
hdr = astropy.io.fits.Header()

hdr["FORMAT_V"] = self.FORMAT_VERSION
hdr.update(self.spectrum.md)
hdr.update(self.spectrum.metadata)

# WCS headers - Use -TAB WCS definition
wcs_cards = [
Expand Down Expand Up @@ -88,6 +89,23 @@ def make_primary_hdu(self):
)
return hdu

def make_maskvariance_hdu(self):
"""Return the HDU for the mask and variance plane."""
hdr_mask = astropy.io.fits.Header()
hdr_mask["EXTTYPE"] = 'MASK '
hdr_mask["EXTNAME"] = 'MASK '
hdu_mask = astropy.io.fits.ImageHDU(
data=self.spectrum.mask, header=hdr_mask
)

hdr_variance = astropy.io.fits.Header()
hdr_variance["EXTTYPE"] = 'VARIANCE'
hdr_variance["EXTNAME"] = 'VARIANCE'
hdu_variance = astropy.io.fits.ImageHDU(
data=self.spectrum.variance, header=hdr_variance
)
return hdu_mask, hdu_variance

def make_wavelength_hdu(self):
"""Return the wavelength HDU built from a Spectrum."""

Expand Down
24 changes: 16 additions & 8 deletions python/lsst/obs/fiberspectrograph/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ class FiberSpectrum:
Optional Detector ID for this data.
"""

def __init__(self, wavelength, flux, md=None, detectorId=0):
def __init__(self, wavelength, flux, md=None, detectorId=0, mask=None, variance=None):
self.wavelength = wavelength
self.flux = flux
self.metadata = md

self.info = ObservationInfo(md)
self.detector = FiberSpectrograph().getCamera()[detectorId]

mask = afwImage.MaskX(1, 1)
self.getPlaneBitMask = mask.getPlaneBitMask # ughh, awful Mask API
self.mask = np.zeros(flux.shape, dtype=mask.array.dtype)
self.variance = np.zeros_like(flux)
mask_temp = afwImage.MaskX(1, 1)
self.getPlaneBitMask = mask_temp.getPlaneBitMask # ughh, awful Mask API
self.mask = mask
self.variance = variance

def getDetector(self):
"""Get fiber spectrograph detector."
Expand Down Expand Up @@ -100,16 +100,24 @@ def readFits(cls, path):

fitsfile = astropy.io.fits.open(path)
md = dict(fitsfile[0].header)
format_v = md["FORMAT_V"]

if md["FORMAT_V"] == 1:
if format_v == 1:
flux = fitsfile[0].data
wavelength = fitsfile[md["PS1_0"]].data[md["PS1_1"]].flatten()

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

mask_temp = afwImage.MaskX(1, 1)
mask = np.zeros(flux.shape, dtype=mask_temp.array.dtype)
variance = np.zeros_like(flux)
if len(fitsfile) == 4:
mask = fitsfile[2].data
variance = fitsfile[3].data
else:
raise ValueError(f"FORMAT_V has changed from 1 to {md["FORMAT_V"]}")
raise ValueError(f"FORMAT_V has changed from 1 to {format_v}")

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

def writeFits(self, path):
"""Write a Spectrum to disk.
Expand Down

0 comments on commit 572c817

Please sign in to comment.