Coverage for tests/test_makeRawVisitInfoViaObsInfo.py: 36%

55 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-08-31 04:25 -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/>. 

21 

22import unittest 

23 

24import astropy.units as u 

25import lsst.afw.image 

26from astro_metadata_translator import FitsTranslator, ObservationInfo, StubTranslator 

27from astropy.time import Time 

28from lsst.daf.base import DateTime 

29from lsst.obs.base import MakeRawVisitInfoViaObsInfo 

30 

31 

32class NewTranslator(FitsTranslator, StubTranslator): 

33 _trivial_map = { 

34 "exposure_time": "EXPTIME", 

35 "exposure_id": "EXP-ID", 

36 } 

37 

38 def to_location(self): 

39 return None 

40 

41 def to_detector_exposure_id(self): 

42 return self.to_exposure_id() 

43 

44 def to_focus_z(self): 

45 return self._header["FOCUSZ"] 

46 

47 

48class MakeTestableVisitInfo(MakeRawVisitInfoViaObsInfo): 

49 metadataTranslator = NewTranslator 

50 

51 

52class TestMakeRawVisitInfoViaObsInfo(unittest.TestCase): 

53 def setUp(self): 

54 # Reference values 

55 self.exposure_time = 6.2 * u.s 

56 self.exposure_id = 54321 

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

58 self.datetime_begin.precision = 9 

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

60 self.datetime_end.precision = 9 

61 self.focus_z = 1.5 * u.mm 

62 

63 self.header = { 

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

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

66 "INSTRUME": "SomeCamera", 

67 "TELESCOP": "LSST", 

68 "TIMESYS": "UTC", 

69 "EXPTIME": self.exposure_time, 

70 "EXP-ID": self.exposure_id, 

71 "FOCUSZ": self.focus_z, 

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

73 "EXTRA2": 5, 

74 } 

75 

76 def testMakeRawVisitInfoViaObsInfo(self): 

77 maker = MakeTestableVisitInfo() 

78 beforeLength = len(self.header) 

79 

80 # Capture the warnings from StubTranslator since they are 

81 # confusing to people but irrelevant for the test. 

82 with self.assertWarns(UserWarning): 

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

84 visitInfo = maker(self.header) 

85 

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

87 with self.assertWarns(FutureWarning): 

88 # TODO: tested for backward-compatibility; remove on DM-32138 

89 self.assertEqual(visitInfo.getExposureId(), self.exposure_id) 

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

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

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

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

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

95 # Check focusZ with default value from astro_metadata_translator 

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

97 

98 def testObservationInfo2VisitInfo(self): 

99 

100 with self.assertWarns(UserWarning): 

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

102 

103 # No log specified so no log message should appear 

104 visitInfo = MakeRawVisitInfoViaObsInfo.observationInfo2visitInfo(obsInfo) 

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

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

107 with self.assertWarns(FutureWarning): 

108 # TODO: tested for backward-compatibility; remove on DM-32138 

109 self.assertEqual(visitInfo.getExposureId(), self.exposure_id) 

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

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

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

113 # Check focusZ with default value from astro_metadata_translator 

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

115 

116 

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

118 unittest.main()