Coverage for tests / test_makeRawVisitInfoViaObsInfo.py: 31%
68 statements
« prev ^ index » next coverage.py v7.13.2, created at 2026-02-04 01:12 -0800
« prev ^ index » next coverage.py v7.13.2, created at 2026-02-04 01:12 -0800
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 <http://www.gnu.org/licenses/>.
22import unittest
24import astropy.units as u
25import numpy as np
26from astro_metadata_translator import FitsTranslator, ObservationInfo, StubTranslator
27from astropy.time import Time
29import lsst.afw.image
30from lsst.daf.base import DateTime
31from lsst.obs.base import MakeRawVisitInfoViaObsInfo
34class NewTranslator(FitsTranslator, StubTranslator):
35 """Metadata translator to use for tests."""
37 _trivial_map = {
38 "exposure_time": "EXPTIME",
39 "exposure_id": "EXP-ID",
40 "observation_type": "OBSTYPE",
41 "science_program": "PROGRAM",
42 "observation_reason": "REASON",
43 "object": "OBJECT",
44 "has_simulated_content": "SIMULATE",
45 }
47 def to_location(self):
48 return None
50 def to_detector_exposure_id(self):
51 return self.to_exposure_id()
53 def to_focus_z(self):
54 return self._header["FOCUSZ"]
57class MakeTestableVisitInfo(MakeRawVisitInfoViaObsInfo):
58 """Test class for VisitInfo construction."""
60 metadataTranslator = NewTranslator
63class TestMakeRawVisitInfoViaObsInfo(unittest.TestCase):
64 """Test VisitInfo construction."""
66 def setUp(self):
67 # Reference values
68 self.exposure_time = 6.2 * u.s
69 self.exposure_id = 54321
70 self.datetime_begin = Time("2001-01-02T03:04:05.123456789", format="isot", scale="utc")
71 self.datetime_begin.precision = 9
72 self.datetime_end = Time("2001-01-02T03:04:07.123456789", format="isot", scale="utc")
73 self.datetime_end.precision = 9
74 self.focus_z = 1.5 * u.mm
76 self.header = {
77 "DATE-OBS": self.datetime_begin.isot,
78 "DATE-END": self.datetime_end.isot,
79 "INSTRUME": "SomeCamera",
80 "TELESCOP": "LSST",
81 "TIMESYS": "UTC",
82 "EXPTIME": self.exposure_time,
83 "EXP-ID": self.exposure_id,
84 "FOCUSZ": self.focus_z,
85 "EXTRA1": "an abitrary key and value",
86 "EXTRA2": 5,
87 "OBSTYPE": "test type",
88 "PROGRAM": "test program",
89 "REASON": "test reason",
90 "OBJECT": "test object",
91 "SIMULATE": True,
92 }
94 def testMakeRawVisitInfoViaObsInfo(self):
95 maker = MakeTestableVisitInfo()
96 beforeLength = len(self.header)
98 # Capture the warnings from StubTranslator since they are
99 # confusing to people but irrelevant for the test.
100 with self.assertWarns(UserWarning):
101 with self.assertLogs(level="WARNING"):
102 visitInfo = maker(self.header)
104 self.assertAlmostEqual(visitInfo.getExposureTime(), self.exposure_time.to_value("s"))
105 self.assertEqual(visitInfo.id, self.exposure_id)
106 self.assertEqual(visitInfo.getDate(), DateTime("2001-01-02T03:04:06.123456789Z", DateTime.UTC))
107 # The header can possibly grow with header fix up provenance.
108 self.assertGreaterEqual(len(self.header), beforeLength)
109 self.assertEqual(visitInfo.getInstrumentLabel(), "SomeCamera")
110 # Check focusZ with default value from astro_metadata_translator
111 self.assertEqual(visitInfo.getFocusZ(), self.focus_z.to_value("mm"))
112 self.assertEqual(visitInfo.observationType, "test type")
113 self.assertEqual(visitInfo.scienceProgram, "test program")
114 self.assertEqual(visitInfo.observationReason, "test reason")
115 self.assertEqual(visitInfo.object, "test object")
116 self.assertEqual(visitInfo.hasSimulatedContent, True)
118 def testMakeRawVisitInfoViaObsInfo_empty(self):
119 """Test that empty metadata fields are set to appropriate defaults."""
120 maker = MakeTestableVisitInfo()
121 # Capture the warnings from StubTranslator since they are
122 # confusing to people but irrelevant for the test.
123 with self.assertWarns(UserWarning):
124 with self.assertLogs(level="WARNING"):
125 visitInfo = maker({})
126 self.assertTrue(np.isnan(visitInfo.focusZ))
127 self.assertEqual(visitInfo.observationType, "")
128 self.assertEqual(visitInfo.scienceProgram, "")
129 self.assertEqual(visitInfo.observationReason, "")
130 self.assertEqual(visitInfo.object, "")
131 self.assertEqual(visitInfo.hasSimulatedContent, False)
133 def testObservationInfo2VisitInfo(self):
134 with self.assertWarns(UserWarning):
135 obsInfo = ObservationInfo(self.header, translator_class=NewTranslator)
137 # No log specified so no log message should appear
138 visitInfo = MakeRawVisitInfoViaObsInfo.observationInfo2visitInfo(obsInfo)
139 self.assertIsInstance(visitInfo, lsst.afw.image.VisitInfo)
140 self.assertAlmostEqual(visitInfo.getExposureTime(), self.exposure_time.to_value("s"))
141 self.assertEqual(visitInfo.id, self.exposure_id)
142 self.assertEqual(visitInfo.getDate(), DateTime("2001-01-02T03:04:06.123456789Z", DateTime.UTC))
143 self.assertEqual(visitInfo.getInstrumentLabel(), "SomeCamera")
144 # Check focusZ with default value from astro_metadata_translator
145 self.assertEqual(visitInfo.getFocusZ(), self.focus_z.to_value("mm"))
148if __name__ == "__main__": 148 ↛ 149line 148 didn't jump to line 149 because the condition on line 148 was never true
149 unittest.main()