Coverage for tests / test_translator_helpers.py: 29%

54 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-30 08:43 +0000

1# This file is part of astro_metadata_translator. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the LICENSE file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12import unittest 

13 

14from astropy.coordinates import AltAz, EarthLocation 

15from astropy.time import Time 

16 

17from astro_metadata_translator import StubTranslator 

18from astro_metadata_translator.translators.helpers import altaz_from_degree_headers 

19 

20 

21class HelperTranslator(StubTranslator): 

22 """Base class for testing shadowing.""" 

23 

24 def to_instrument(self) -> str: 

25 return "BaseInstrument" 

26 

27 def to_observation_type(self) -> str: 

28 return "flat" 

29 

30 def to_observation_id(self) -> str: 

31 return "OBSID" 

32 

33 def to_location(self) -> EarthLocation: 

34 return EarthLocation.from_geodetic(0.0, 0.0) 

35 

36 

37class ScienceTranslator(HelperTranslator): 

38 """Translator corresponding to science observation.""" 

39 

40 def to_observation_type(self) -> str: 

41 return "science" 

42 

43 

44class HelperTestCase(unittest.TestCase): 

45 """Test translator helpers.""" 

46 

47 def assert_azel(self, azel: AltAz, az: float, alt: float) -> None: 

48 self.assertAlmostEqual(float(azel.az.to_value("deg")), az) 

49 self.assertAlmostEqual(float(azel.alt.to_value("deg")), alt) 

50 

51 def test_altaz(self) -> None: 

52 """Test altaz extraction.""" 

53 translator = HelperTranslator({}) 

54 now = Time.now() 

55 

56 value = altaz_from_degree_headers(translator, [("ELSTART", "AZSTART")], now) 

57 self.assertIsNone(value) 

58 

59 translator._header["AZSTART"] = -400.0 

60 translator._header["ELSTART"] = 45.0 

61 

62 value = altaz_from_degree_headers(translator, [("ELSTART", "AZSTART")], now) 

63 self.assertIsNone(value) 

64 

65 for alt, az in ((80.0, 300.0), (-1.0, 200.0)): 

66 translator._header["AZSTART"] = az 

67 translator._header["ELSTART"] = alt 

68 value = altaz_from_degree_headers(translator, [("ELSTART", "AZSTART")], now, min_alt=-5.0) 

69 self.assert_azel(value, az, alt) 

70 

71 translator._header["AZSTART"] = 22.0 

72 translator._header["ELSTART"] = 34.0 

73 value = altaz_from_degree_headers(translator, [("AZ1", "EL1"), ("ELSTART", "AZSTART")], now) 

74 self.assert_azel(value, 22.0, 34.0) 

75 

76 value = altaz_from_degree_headers(translator, [("ELSTART", "AZSTART")], now, is_zd={"ELSTART"}) 

77 self.assert_azel(value, 22.0, 56.0) 

78 

79 translator._header["ELSTART"] = -1.0 

80 with self.assertLogs(): 

81 value = altaz_from_degree_headers(translator, [("ELSTART", "AZSTART")], now, min_alt=-0.5) 

82 self.assert_azel(value, 22.0, -0.5) 

83 

84 translator._header["ELSTART"] = 90.2 

85 with self.assertLogs(): 

86 value = altaz_from_degree_headers(translator, [("ELSTART", "AZSTART")], now, max_alt=95.0) 

87 self.assert_azel(value, 22.0, 90.0) 

88 

89 def test_science_altaz(self) -> None: 

90 """Test science altaz.""" 

91 translator = ScienceTranslator({}) 

92 now = Time.now() 

93 

94 with self.assertRaises(KeyError): 

95 altaz_from_degree_headers(translator, [("ELSTART", "AZSTART")], now) 

96 

97 

98if __name__ == "__main__": 

99 unittest.main()