Coverage for tests/test_makeRawVisitInfoViaObsInfo.py: 28%

68 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-02 10:58 +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 <http://www.gnu.org/licenses/>. 

21 

22import unittest 

23 

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 

31 

32 

33class NewTranslator(FitsTranslator, StubTranslator): 

34 """Metadata translator to use for tests.""" 

35 

36 _trivial_map = { 

37 "exposure_time": "EXPTIME", 

38 "exposure_id": "EXP-ID", 

39 "observation_type": "OBSTYPE", 

40 "science_program": "PROGRAM", 

41 "observation_reason": "REASON", 

42 "object": "OBJECT", 

43 "has_simulated_content": "SIMULATE", 

44 } 

45 

46 def to_location(self): 

47 return None 

48 

49 def to_detector_exposure_id(self): 

50 return self.to_exposure_id() 

51 

52 def to_focus_z(self): 

53 return self._header["FOCUSZ"] 

54 

55 

56class MakeTestableVisitInfo(MakeRawVisitInfoViaObsInfo): 

57 """Test class for VisitInfo construction.""" 

58 

59 metadataTranslator = NewTranslator 

60 

61 

62class TestMakeRawVisitInfoViaObsInfo(unittest.TestCase): 

63 """Test VisitInfo construction.""" 

64 

65 def setUp(self): 

66 # Reference values 

67 self.exposure_time = 6.2 * u.s 

68 self.exposure_id = 54321 

69 self.datetime_begin = Time("2001-01-02T03:04:05.123456789", format="isot", scale="utc") 

70 self.datetime_begin.precision = 9 

71 self.datetime_end = Time("2001-01-02T03:04:07.123456789", format="isot", scale="utc") 

72 self.datetime_end.precision = 9 

73 self.focus_z = 1.5 * u.mm 

74 

75 self.header = { 

76 "DATE-OBS": self.datetime_begin.isot, 

77 "DATE-END": self.datetime_end.isot, 

78 "INSTRUME": "SomeCamera", 

79 "TELESCOP": "LSST", 

80 "TIMESYS": "UTC", 

81 "EXPTIME": self.exposure_time, 

82 "EXP-ID": self.exposure_id, 

83 "FOCUSZ": self.focus_z, 

84 "EXTRA1": "an abitrary key and value", 

85 "EXTRA2": 5, 

86 "OBSTYPE": "test type", 

87 "PROGRAM": "test program", 

88 "REASON": "test reason", 

89 "OBJECT": "test object", 

90 "SIMULATE": True, 

91 } 

92 

93 def testMakeRawVisitInfoViaObsInfo(self): 

94 maker = MakeTestableVisitInfo() 

95 beforeLength = len(self.header) 

96 

97 # Capture the warnings from StubTranslator since they are 

98 # confusing to people but irrelevant for the test. 

99 with self.assertWarns(UserWarning): 

100 with self.assertLogs(level="WARNING"): 

101 visitInfo = maker(self.header) 

102 

103 self.assertAlmostEqual(visitInfo.getExposureTime(), self.exposure_time.to_value("s")) 

104 self.assertEqual(visitInfo.id, self.exposure_id) 

105 self.assertEqual(visitInfo.getDate(), DateTime("2001-01-02T03:04:06.123456789Z", DateTime.UTC)) 

106 # The header can possibly grow with header fix up provenance. 

107 self.assertGreaterEqual(len(self.header), beforeLength) 

108 self.assertEqual(visitInfo.getInstrumentLabel(), "SomeCamera") 

109 # Check focusZ with default value from astro_metadata_translator 

110 self.assertEqual(visitInfo.getFocusZ(), self.focus_z.to_value("mm")) 

111 self.assertEqual(visitInfo.observationType, "test type") 

112 self.assertEqual(visitInfo.scienceProgram, "test program") 

113 self.assertEqual(visitInfo.observationReason, "test reason") 

114 self.assertEqual(visitInfo.object, "test object") 

115 self.assertEqual(visitInfo.hasSimulatedContent, True) 

116 

117 def testMakeRawVisitInfoViaObsInfo_empty(self): 

118 """Test that empty metadata fields are set to appropriate defaults.""" 

119 maker = MakeTestableVisitInfo() 

120 # Capture the warnings from StubTranslator since they are 

121 # confusing to people but irrelevant for the test. 

122 with self.assertWarns(UserWarning): 

123 with self.assertLogs(level="WARNING"): 

124 visitInfo = maker({}) 

125 self.assertTrue(np.isnan(visitInfo.focusZ)) 

126 self.assertEqual(visitInfo.observationType, "") 

127 self.assertEqual(visitInfo.scienceProgram, "") 

128 self.assertEqual(visitInfo.observationReason, "") 

129 self.assertEqual(visitInfo.object, "") 

130 self.assertEqual(visitInfo.hasSimulatedContent, False) 

131 

132 def testObservationInfo2VisitInfo(self): 

133 with self.assertWarns(UserWarning): 

134 obsInfo = ObservationInfo(self.header, translator_class=NewTranslator) 

135 

136 # No log specified so no log message should appear 

137 visitInfo = MakeRawVisitInfoViaObsInfo.observationInfo2visitInfo(obsInfo) 

138 self.assertIsInstance(visitInfo, lsst.afw.image.VisitInfo) 

139 self.assertAlmostEqual(visitInfo.getExposureTime(), self.exposure_time.to_value("s")) 

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")) 

145 

146 

147if __name__ == "__main__": 147 ↛ 148line 147 didn't jump to line 148, because the condition on line 147 was never true

148 unittest.main()