Skip to content

Commit

Permalink
Merge pull request #1865 from pypeit/mosaic_try_old
Browse files Browse the repository at this point in the history
Fix bug when reading mosaic data in files from previous pypeit versions.
  • Loading branch information
kbwestfall authored Oct 31, 2024
2 parents 1b7f032 + c97ec40 commit e35362a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
2 changes: 2 additions & 0 deletions doc/releases/1.16.1dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Bug Fixes
the 'BOXSLIT' flag.
- Fix a bug in `pypeit_coadd_2d` related to how the binning was taken into account
in the mask definition, and in the calculation of the offset between frames.
- Fix bug when trying to open mosaic data from previous versions; version
checking flag was not being propagated.



Expand Down
23 changes: 17 additions & 6 deletions pypeit/images/mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class Mosaic(datamodel.DataContainer):
'tform': dict(otype=np.ndarray, atype=float,
descr='The full transformation matrix for each detector used to '
'construct the mosaic.'),
'msc_ord': dict(otype=int, descr='Order of the interpolation used to construct the mosaic.')}
'msc_ord': dict(otype=int,
descr='Order of the interpolation used to construct the mosaic.')}

name_prefix = 'MSC'
"""
Expand Down Expand Up @@ -159,10 +160,18 @@ def _parse(cls, hdu, hdu_prefix=None, **kwargs):
hdr = fits.Header()
hdr['DMODCLS'] = DetectorContainer.__name__
hdr['DMODVER'] = _hdu.header['DETMODV']
d['detectors'] = np.array([DetectorContainer.from_hdu(
fits.BinTableHDU(data=table.Table(tbl[i]),
name='DETECTOR', header=hdr))
for i in range(ndet)])
d['detectors'] = []
for i in range(ndet):
_hdu = fits.BinTableHDU(data=table.Table(tbl[i]), name='DETECTOR', header=hdr)
# NOTE: I'm using _parse() to ensure that I keep the result of the
# version and type checking.
_d, vp, tp, ph = DetectorContainer._parse(_hdu)
if not vp:
msgs.warn('Detector datamodel version is incorrect. May cause a fault.')
version_passed &= vp
d['detectors'] += [DetectorContainer.from_dict(d=_d) if tp else None]
type_passed &= tp
d['detectors'] = np.array(d['detectors'], dtype=object)

return d, version_passed, type_passed, parsed_hdus

Expand Down Expand Up @@ -213,5 +222,7 @@ def copy(self):
"""
Return a (deep) copy of the object.
"""
return Mosaic(id=self.id, detectors=np.array([det.copy() for det in self.detectors]), shape=self.shape, shift=self.shift.copy(), rot=self.rot.copy(), tform=self.tform.copy(), msc_ord=self.msc_ord)
return Mosaic(id=self.id, detectors=np.array([det.copy() for det in self.detectors]),
shape=self.shape, shift=self.shift.copy(), rot=self.rot.copy(),
tform=self.tform.copy(), msc_ord=self.msc_ord)

2 changes: 1 addition & 1 deletion pypeit/specobjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def from_fitsfile(cls, fits_file, det=None, chk_version=True):
# from_hdu method, and the name of the HDU must have a known format
# (e.g., 'DET01-DETECTOR').
_det = hdu.name.split('-')[0]
detector_hdus[_det] = dmodcls.from_hdu(hdu)
detector_hdus[_det] = dmodcls.from_hdu(hdu, chk_version=chk_version)

# Now the objects
for hdu in hdul[1:]:
Expand Down
41 changes: 41 additions & 0 deletions pypeit/tests/test_mosaic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pathlib import Path
from IPython import embed

import pytest

from astropy.io import fits

from pypeit.pypmsgs import PypeItDataModelError
from pypeit.tests.tstutils import data_output_path
from pypeit.images.mosaic import Mosaic
from pypeit.spectrographs.util import load_spectrograph


def test_io():
# Create the mosaic
spec = load_spectrograph('keck_deimos')
mpar = spec.get_mosaic_par((1,5))

# Write it
ofile = data_output_path('tmp_mosaic.fits')
mpar.to_file(ofile, overwrite=True)

# Try to read it
_mpar = Mosaic.from_file(ofile)

# Change the version
_ofile = data_output_path('tmp_mosaic_wrongver.fits')
with fits.open(ofile) as hdu:
hdu['MOSAIC'].header['DMODVER'] = '1.0.0'
hdu.writeto(_ofile, overwrite=True)

# Reading should fail because version is checked by default
with pytest.raises(PypeItDataModelError):
_mpar = Mosaic.from_file(_ofile)

# Should not fail because skipping the version check
_mpar = Mosaic.from_file(_ofile, chk_version=False)

# Remove files
Path(ofile).unlink()
Path(_ofile).unlink()

0 comments on commit e35362a

Please sign in to comment.