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