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