Coverage for tests/test_fitsRawFormatter.py: 43%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of obs_base.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
22import unittest
24import astropy.units as u
25import lsst.afw.geom
26import lsst.afw.math
27import lsst.daf.base
28import lsst.daf.butler
29import lsst.geom
30import lsst.utils.tests
31from astro_metadata_translator import FitsTranslator, StubTranslator
32from astro_metadata_translator.translators.helpers import tracking_from_degree_headers
33from astropy.coordinates import Angle
34from lsst.afw.cameraGeom import makeUpdatedDetector
35from lsst.afw.cameraGeom.testUtils import CameraWrapper, DetectorWrapper
36from lsst.obs.base import (
37 FilterDefinition,
38 FilterDefinitionCollection,
39 FitsRawFormatterBase,
40 MakeRawVisitInfoViaObsInfo,
41)
42from lsst.obs.base.tests import make_ramp_exposure_untrimmed
43from lsst.obs.base.utils import InitialSkyWcsError, createInitialSkyWcs
46class SimpleTestingTranslator(FitsTranslator, StubTranslator):
47 _const_map = {
48 "boresight_rotation_angle": Angle(90 * u.deg),
49 "boresight_rotation_coord": "sky",
50 "detector_exposure_id": 12345,
51 # The following are defined to prevent warnings about
52 # undefined translators
53 "dark_time": 0.0 * u.s,
54 "exposure_time": 0.0 * u.s,
55 "physical_filter": "u",
56 "detector_num": 0,
57 "detector_name": "0",
58 "detector_group": "",
59 "detector_unique_name": "0",
60 "detector_serial": "",
61 "observation_id": "--",
62 "science_program": "unknown",
63 "object": "unknown",
64 "exposure_id": 0,
65 "visit_id": 0,
66 "relative_humidity": 30.0,
67 "pressure": 0.0 * u.MPa,
68 "temperature": 273 * u.K,
69 "altaz_begin": None,
70 }
71 _trivial_map = {"boresight_airmass": "AIRMASS", "observation_type": "OBSTYPE"}
73 def to_tracking_radec(self):
74 radecsys = ("RADESYS",)
75 radecpairs = (("RA", "DEC"),)
76 return tracking_from_degree_headers(self, radecsys, radecpairs, unit=(u.deg, u.deg))
79class MakeTestingRawVisitInfo(MakeRawVisitInfoViaObsInfo):
80 metadataTranslator = SimpleTestingTranslator
83class SimpleFitsRawFormatter(FitsRawFormatterBase):
84 filterDefinitions = FilterDefinitionCollection(
85 FilterDefinition(physical_filter="u", band="u", lambdaEff=300.0)
86 )
88 @property
89 def translatorClass(self):
90 return SimpleTestingTranslator
92 def getDetector(self, id):
93 """Use CameraWrapper to create a fake detector that can map from
94 PIXELS to FIELD_ANGLE.
96 Always return Detector #10, so all the tests are self-consistent, and
97 make sure it is in "assembled" form, since that's what the base
98 formatter implementations assume.
99 """
100 return makeUpdatedDetector(CameraWrapper().camera.get(10))
103class FitsRawFormatterTestCase(lsst.utils.tests.TestCase):
104 def setUp(self):
105 # reset the filters before we test anything
106 FilterDefinitionCollection.reset()
108 # The FITS WCS and VisitInfo coordinates in this header are
109 # intentionally different, to make comparisons between them more
110 # obvious.
111 self.boresight = lsst.geom.SpherePoint(10.0, 20.0, lsst.geom.degrees)
112 self.header = {
113 "TELESCOP": "TEST",
114 "INSTRUME": "UNKNOWN",
115 "AIRMASS": 1.2,
116 "RADESYS": "ICRS",
117 "OBSTYPE": "science",
118 "EQUINOX": 2000,
119 "OBSGEO-X": "-5464588.84421314",
120 "OBSGEO-Y": "-2493000.19137644",
121 "OBSGEO-Z": "2150653.35350771",
122 "RA": self.boresight.getLatitude().asDegrees(),
123 "DEC": self.boresight.getLongitude().asDegrees(),
124 "CTYPE1": "RA---SIN",
125 "CTYPE2": "DEC--SIN",
126 "CRPIX1": 5,
127 "CRPIX2": 6,
128 "CRVAL1": self.boresight.getLatitude().asDegrees() + 1,
129 "CRVAL2": self.boresight.getLongitude().asDegrees() + 1,
130 "CD1_1": 1e-5,
131 "CD1_2": 0,
132 "CD2_2": 1e-5,
133 "CD2_1": 0,
134 }
135 # make a property list of the above, for use by the formatter.
136 self.metadata = lsst.daf.base.PropertyList()
137 self.metadata.update(self.header)
139 maker = MakeTestingRawVisitInfo()
140 self.visitInfo = maker(self.header)
142 self.metadataSkyWcs = lsst.afw.geom.makeSkyWcs(self.metadata, strip=False)
143 self.boresightSkyWcs = createInitialSkyWcs(self.visitInfo, CameraWrapper().camera.get(10))
145 # set this to `contextlib.nullcontext()` to print the log warnings
146 self.warnContext = self.assertLogs(level="WARNING")
148 # Make a data ID to pass to the formatter.
149 universe = lsst.daf.butler.DimensionUniverse()
150 dataId = lsst.daf.butler.DataCoordinate.standardize(
151 instrument="Cam1", exposure=2, detector=10, physical_filter="u", band="u", universe=universe
152 )
154 # We have no file in these tests, so make an empty descriptor.
155 fileDescriptor = lsst.daf.butler.FileDescriptor(None, None)
156 self.formatter = SimpleFitsRawFormatter(fileDescriptor, dataId)
157 # Force the formatter's metadata to be what we've created above.
158 self.formatter._metadata = self.metadata
160 def test_makeWcs(self):
161 detector = self.formatter.getDetector(1)
162 wcs = self.formatter.makeWcs(self.visitInfo, detector)
163 self.assertNotEqual(wcs, self.metadataSkyWcs)
164 self.assertEqual(wcs, self.boresightSkyWcs)
166 def test_makeWcs_warn_if_metadata_is_bad(self):
167 """If the metadata is bad, log a warning and use the VisitInfo WCS."""
168 detector = self.formatter.getDetector(1)
169 self.metadata.remove("CTYPE1")
170 with self.warnContext:
171 wcs = self.formatter.makeWcs(self.visitInfo, detector)
172 self.assertNotEqual(wcs, self.metadataSkyWcs)
173 self.assertEqual(wcs, self.boresightSkyWcs)
175 def test_makeWcs_warn_if_visitInfo_is_None(self):
176 """If VisitInfo is None, log a warning and use the metadata WCS."""
177 detector = self.formatter.getDetector(1)
178 with self.warnContext:
179 wcs = self.formatter.makeWcs(None, detector)
180 self.assertEqual(wcs, self.metadataSkyWcs)
181 self.assertNotEqual(wcs, self.boresightSkyWcs)
183 def test_makeWcs_fail_if_visitInfo_is_None(self):
184 """If VisitInfo is None and metadata failed, raise an exception."""
185 detector = self.formatter.getDetector(1)
186 self.metadata.remove("CTYPE1")
187 with self.warnContext, self.assertRaises(InitialSkyWcsError):
188 self.formatter.makeWcs(None, detector)
190 def test_makeWcs_fail_if_detector_is_bad(self):
191 """If Detector is broken, raise an exception."""
192 # This detector doesn't know about FIELD_ANGLE, so can't be used to
193 # make a SkyWcs.
194 detector = DetectorWrapper().detector
195 with self.assertRaises(InitialSkyWcsError):
196 self.formatter.makeWcs(self.visitInfo, detector)
198 def test_amp_parameter(self):
199 """Test loading subimages with the 'amp' parameter."""
200 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
201 # Get a detector; this must be the same one that's baked into the
202 # simple formatter at the top of this file, so that's how we get
203 # it.
204 detector = self.formatter.getDetector(1)
205 # Make full exposure with ramp values and save just the image to
206 # the temp file (with metadata), so it looks like a raw.
207 full = make_ramp_exposure_untrimmed(detector)
208 full.image.writeFits(tmpFile, metadata=self.metadata)
209 # Loop over amps and try to read them via the formatter.
210 for n, amp in enumerate(detector):
211 for amp_parameter in [amp, amp.getName(), n]:
212 for parameters in [{"amp": amp_parameter}, {"amp": amp_parameter, "detector": detector}]:
213 with self.subTest(parameters=parameters):
214 # Make a new formatter that points at the new file
215 # and has the right parameters.
216 formatter = SimpleFitsRawFormatter(
217 lsst.daf.butler.FileDescriptor(
218 lsst.daf.butler.Location(None, path=lsst.daf.butler.ButlerURI(tmpFile)),
219 lsst.daf.butler.StorageClassFactory().getStorageClass("ExposureI"),
220 parameters=parameters,
221 ),
222 self.formatter.dataId,
223 )
224 subexp = formatter.read()
225 self.assertImagesEqual(subexp.image, full[amp.getRawBBox()].image)
226 self.assertEqual(len(subexp.getDetector()), 1)
227 self.assertAmplifiersEqual(subexp.getDetector()[0], amp)
228 # We could try transformed amplifiers here that involve flips
229 # and offsets, but:
230 # - we already test the low-level code that does that in afw;
231 # - we test very similar high-level code (which calls that
232 # same afw code) in the non-raw Exposure formatter, in
233 # test_butlerFits.py;
234 # - the only instruments that actually have those kinds of
235 # amplifiers are those in obs_lsst, and that has a different
236 # raw formatter implementation that we need to test there
237 # anyway;
238 # - these are kind of expensive tests.
241class MemoryTester(lsst.utils.tests.MemoryTestCase):
242 pass
245def setup_module(module):
246 lsst.utils.tests.init()
249if __name__ == "__main__": 249 ↛ 250line 249 didn't jump to line 250, because the condition on line 249 was never true
250 lsst.utils.tests.init()
251 unittest.main()