Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

# This file is part of astro_metadata_translator. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

# license that can be found in the LICENSE file. 

 

import unittest 

from astropy.time import Time 

 

from astro_metadata_translator import FitsTranslator, StubTranslator, ObservationInfo 

 

 

class InstrumentTestTranslator(FitsTranslator, StubTranslator): 

"""Simple FITS-like translator to test the infrastructure""" 

 

# Needs a name to be registered 

name = "TestTranslator" 

 

# Indicate the instrument this class understands 

supported_instrument = "SCUBA_test" 

 

# Some new mappings, including an override 

_trivial_map = {"telescope": "TELCODE", 

"exposure_id": "EXPID", 

"relative_humidity": "HUMIDITY", 

"detector_name": "DETNAME", 

"observation_id": "OBSID"} 

 

# Add translator method to test joining 

def to_physical_filter(self): 

return self._join_keyword_values(["DETNAME", "HUMIDITY"], delim="_") 

 

 

class MissingMethodsTranslator(FitsTranslator): 

"""Translator class that does not implement all the methods.""" 

pass 

 

 

class TranslatorTestCase(unittest.TestCase): 

 

def setUp(self): 

# Known simple header 

self.header = {"TELESCOP": "JCMT", 

"TELCODE": "LSST", 

"INSTRUME": "SCUBA_test", 

"DATE-OBS": "2000-01-01T01:00:01.500", 

"DATE-END": "2000-01-01T02:00:01.500", 

"OBSGEO-X": "-5464588.84421314", 

"OBSGEO-Y": "-2493000.19137644", 

"OBSGEO-Z": "2150653.35350771", 

"OBSID": "20000101_00002", 

"EXPID": "22", # Should cast to a number 

"DETNAME": 76, # Should cast to a string 

"HUMIDITY": "55", # Should cast to a float 

"BAZ": "bar"} 

 

def test_manual_translation(self): 

 

header = self.header 

translator = FitsTranslator(header) 

 

# Treat the header as standard FITS 

self.assertFalse(FitsTranslator.can_translate(header)) 

self.assertEqual(translator.to_telescope(), "JCMT") 

self.assertEqual(translator.to_instrument(), "SCUBA_test") 

self.assertEqual(translator.to_datetime_begin(), 

Time(header["DATE-OBS"], format="isot")) 

 

# This class will issue warnings 

with self.assertLogs("astro_metadata_translator") as cm: 

class InstrumentTestTranslatorExtras(InstrumentTestTranslator): 

"""Version of InstrumentTestTranslator with unexpected 

fields.""" 

_trivial_map = {"foobar": "BAZ"} 

_const_map = {"format": "HDF5"} 

 

self.assertIn("Unexpected trivial", cm.output[0]) 

self.assertIn("Unexpected constant", cm.output[1]) 

 

# Use the special test translator instead 

translator = InstrumentTestTranslatorExtras(header) 

self.assertTrue(InstrumentTestTranslator.can_translate(header)) 

self.assertEqual(translator.to_telescope(), "LSST") 

self.assertEqual(translator.to_instrument(), "SCUBA_test") 

self.assertEqual(translator.to_format(), "HDF5") 

self.assertEqual(translator.to_foobar(), "bar") 

 

def test_translator(self): 

header = self.header 

 

# Specify a translation class 

with self.assertWarns(UserWarning): 

# Since the translator is incomplete it should issue warnings 

v1 = ObservationInfo(header, translator_class=InstrumentTestTranslator) 

self.assertEqual(v1.instrument, "SCUBA_test") 

self.assertEqual(v1.telescope, "LSST") 

self.assertEqual(v1.exposure_id, 22) 

self.assertIsInstance(v1.exposure_id, int) 

self.assertEqual(v1.detector_name, "76") 

self.assertEqual(v1.relative_humidity, 55.0) 

self.assertIsInstance(v1.relative_humidity, float) 

self.assertEqual(v1.physical_filter, "76_55") 

 

# Now automated class 

with self.assertWarns(UserWarning): 

# Since the translator is incomplete it should issue warnings 

v1 = ObservationInfo(header) 

self.assertEqual(v1.instrument, "SCUBA_test") 

self.assertEqual(v1.telescope, "LSST") 

 

location = v1.location.to_geodetic() 

self.assertAlmostEqual(location.height.to("m").to_value(), 4123.0, places=1) 

 

# Check that headers have been removed 

new_hdr = v1.stripped_header() 

self.assertNotIn("INSTRUME", new_hdr) 

self.assertNotIn("OBSGEO-X", new_hdr) 

self.assertIn("TELESCOP", new_hdr) 

 

# Check the list of cards that were used 

used = v1.cards_used 

self.assertIn("INSTRUME", used) 

self.assertIn("OBSGEO-Y", used) 

self.assertNotIn("TELESCOP", used) 

 

# Stringification 

summary = str(v1) 

self.assertIn("datetime_begin", summary) 

 

def test_failures(self): 

header = {} 

 

with self.assertRaises(TypeError): 

ObservationInfo(header, translator_class=ObservationInfo) 

 

with self.assertLogs("astro_metadata_translator"): 

with self.assertWarns(UserWarning): 

ObservationInfo(header, translator_class=InstrumentTestTranslator, pedantic=False) 

 

with self.assertRaises(KeyError): 

with self.assertWarns(UserWarning): 

ObservationInfo(header, translator_class=InstrumentTestTranslator, pedantic=True) 

 

with self.assertLogs("astro_metadata_translator"): 

with self.assertWarns(UserWarning): 

ObservationInfo(header, translator_class=InstrumentTestTranslator, pedantic=False, 

filename="testfile1") 

 

with self.assertRaises(KeyError): 

with self.assertWarns(UserWarning): 

ObservationInfo(header, translator_class=InstrumentTestTranslator, pedantic=True, 

filename="testfile2") 

 

with self.assertRaises(NotImplementedError): 

with self.assertLogs("astro_metadata_translator", level="WARN"): 

ObservationInfo(header, translator_class=MissingMethodsTranslator) 

 

 

164 ↛ 165line 164 didn't jump to line 165, because the condition on line 164 was never trueif __name__ == "__main__": 

unittest.main()