diff --git a/src/kbmod/standardizers/butler_standardizer.py b/src/kbmod/standardizers/butler_standardizer.py index 56e33a436..aff59386b 100644 --- a/src/kbmod/standardizers/butler_standardizer.py +++ b/src/kbmod/standardizers/butler_standardizer.py @@ -331,8 +331,13 @@ def toLayeredImage(self): psfs = self.standardizePSF() # guaranteed to exist, i.e. safe to access - mjds = meta["mjd"] + if isinstance(meta["mjd"], (list, tuple)): + mjds = meta["mjd"] + else: + mjds = (meta["mjd"] for e in self.processable) + imgs = [] for sci, var, mask, psf, t in zip(sciences, variances, masks, psfs, mjds): - imgs.append(LayeredImage(RawImage(sci), RawImage(var), RawImage(mask), t, psf)) + mask = mask.astype(np.float32) + imgs.append(LayeredImage(RawImage(sci, t), RawImage(var, t), RawImage(mask, t), psf)) return imgs diff --git a/src/kbmod/standardizers/fits_standardizers/fits_standardizer.py b/src/kbmod/standardizers/fits_standardizers/fits_standardizer.py index b80107781..1ba957906 100644 --- a/src/kbmod/standardizers/fits_standardizers/fits_standardizer.py +++ b/src/kbmod/standardizers/fits_standardizers/fits_standardizer.py @@ -441,6 +441,7 @@ def toLayeredImage(self): # copy. TODO: fix when/if CPP stuff is fixed. imgs = [] for sci, var, mask, psf, t in zip(sciences, variances, masks, psfs, mjds): - imgs.append(LayeredImage(RawImage(sci), RawImage(var), RawImage(mask.astype(np.float32)), psf)) - + # Converts nd array mask from bool to np.float32 + mask = mask.astype(np.float32) + imgs.append(LayeredImage(RawImage(sci, t), RawImage(var, t), RawImage(mask, t), psf)) return imgs diff --git a/tests/test_butlerstd.py b/tests/test_butlerstd.py index 4336d9f64..e8b6bdfe8 100644 --- a/tests/test_butlerstd.py +++ b/tests/test_butlerstd.py @@ -333,6 +333,32 @@ def test_psf(self): self.assertIsInstance(psf, PSF) self.assertEqual(psf.get_std(), std.config["psf_std"]) + def test_to_layered_image(self): + """Test ButlerStandardizer can create a LayeredImage.""" + std = Standardizer.get(DatasetId(8), butler=self.butler) + self.assertIsInstance(std, ButlerStandardizer) + + # Get the expected FITS files and extract the MJD from the header + fits = FitsFactory.get_fits(8, spoof_data=True) + hdr = fits["PRIMARY"].header + expected_mjd = Time(hdr["DATE-AVG"]).mjd + + # Get list of layered images froom the standardizer + butler_imgs = std.toLayeredImage() + self.assertEqual(1, len(butler_imgs)) + img = butler_imgs[0] + + # Compare standardized images + np.testing.assert_equal(fits["IMAGE"].data, img.get_science().image) + np.testing.assert_equal(fits["VARIANCE"].data, img.get_variance().image) + np.testing.assert_equal(fits["MASK"].data, img.get_mask().image) + + # Test that we correctly set metadata + self.assertEqual(expected_mjd, img.get_obstime()) + self.assertEqual(expected_mjd, img.get_science().obstime) + self.assertEqual(expected_mjd, img.get_variance().obstime) + self.assertEqual(expected_mjd, img.get_mask().obstime) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_standardizer.py b/tests/test_standardizer.py index dcff972e9..6b3db6518 100644 --- a/tests/test_standardizer.py +++ b/tests/test_standardizer.py @@ -13,8 +13,6 @@ KBMODV1, KBMODV1Config, FitsStandardizer, - SingleExtensionFits, - MultiExtensionFits, ) @@ -344,6 +342,31 @@ def test_psf(self): psf = next(std2.standardizePSF()) self.assertEqual(psf.get_std(), std2.config["psf_std"][0]) + def test_to_layered_image(self): + """Test that KBMODV1 standardizer can create LayeredImages.""" + std = Standardizer.get(self.fits, force=KBMODV1) + self.assertIsInstance(std, KBMODV1) + + # Get the expected FITS files and extract the MJD from the header + hdr = self.fits["PRIMARY"].header + expected_mjd = Time(hdr["DATE-AVG"], format="isot").mjd + + # Get list of layered images from the standardizer + layered_imgs = std.toLayeredImage() + self.assertEqual(1, len(layered_imgs)) + img = layered_imgs[0] + + # Compare standardized images + np.testing.assert_equal(self.fits["IMAGE"].data, img.get_science().image) + np.testing.assert_equal(self.fits["VARIANCE"].data, img.get_variance().image) + np.testing.assert_equal(self.fits["MASK"].data, img.get_mask().image) + + # Test that we correctly set metadata + self.assertEqual(expected_mjd, img.get_obstime()) + self.assertEqual(expected_mjd, img.get_science().obstime) + self.assertEqual(expected_mjd, img.get_variance().obstime) + self.assertEqual(expected_mjd, img.get_mask().obstime) + if __name__ == "__main__": unittest.main()