Coverage for python/lsst/obs/lsst/translators/ts3.py: 67%

53 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-15 01:31 +0000

1# This file is currently part of obs_lsst but is written to allow it 

2# to be migrated to the astro_metadata_translator package at a later date. 

3# 

4# This product includes software developed by the LSST Project 

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

6# See the LICENSE file in this directory for details of code ownership. 

7# 

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

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

10 

11"""Metadata translation code for LSST BNL TestStand 3 headers""" 

12 

13__all__ = ("LsstTS3Translator", ) 

14 

15import logging 

16import re 

17import os.path 

18 

19import astropy.units as u 

20from astropy.time import Time, TimeDelta 

21 

22from astro_metadata_translator import cache_translation 

23 

24from .lsst import LsstBaseTranslator 

25 

26log = logging.getLogger(__name__) 

27 

28# There is only a single sensor at a time so define a 

29# fixed sensor name 

30_DETECTOR_NAME = "S00" 

31 

32 

33class LsstTS3Translator(LsstBaseTranslator): 

34 """Metadata translator for LSST BNL Test Stand 3 data. 

35 """ 

36 

37 name = "LSST-TS3" 

38 """Name of this translation class""" 

39 

40 _const_map = { 

41 # TS3 is not attached to a telescope so many translations are null. 

42 "instrument": "LSST-TS3", 

43 "telescope": None, 

44 "location": None, 

45 "boresight_rotation_coord": None, 

46 "boresight_rotation_angle": None, 

47 "boresight_airmass": None, 

48 "tracking_radec": None, 

49 "altaz_begin": None, 

50 "object": "UNKNOWN", 

51 "relative_humidity": None, 

52 "temperature": None, 

53 "pressure": None, 

54 "detector_name": _DETECTOR_NAME, # Single sensor 

55 } 

56 

57 _trivial_map = { 

58 "detector_serial": "LSST_NUM", 

59 "physical_filter": "FILTER", 

60 "exposure_time": ("EXPTIME", dict(unit=u.s)), 

61 } 

62 

63 DETECTOR_NAME = _DETECTOR_NAME 

64 """Fixed name of single sensor.""" 

65 

66 cameraPolicyFile = "policy/ts3.yaml" 

67 

68 _ROLLOVER_TIME = TimeDelta(8*60*60, scale="tai", format="sec") 

69 """Time delta for the definition of a Rubin Test Stand start of day.""" 

70 

71 @classmethod 

72 def can_translate(cls, header, filename=None): 

73 """Indicate whether this translation class can translate the 

74 supplied header. 

75 

76 There is no usable ``INSTRUME`` header in TS3 data. Instead we use 

77 the ``TSTAND`` header. 

78 

79 Parameters 

80 ---------- 

81 header : `dict`-like 

82 Header to convert to standardized form. 

83 filename : `str`, optional 

84 Name of file being translated. 

85 

86 Returns 

87 ------- 

88 can : `bool` 

89 `True` if the header is recognized by this class. `False` 

90 otherwise. 

91 """ 

92 return cls.can_translate_with_options(header, {"TSTAND": "BNL-TS3-2-Janeway"}, filename=filename) 

93 

94 @staticmethod 

95 def compute_exposure_id(dateobs, seqnum=0, controller=None): 

96 """Helper method to calculate the TS3 exposure_id. 

97 

98 Parameters 

99 ---------- 

100 dateobs : `str` 

101 Date of observation in FITS ISO format. 

102 seqnum : `int`, unused 

103 Sequence number. Ignored. 

104 controller : `str`, unused 

105 Controller type. Ignored. 

106 

107 Returns 

108 ------- 

109 exposure_id : `int` 

110 Exposure ID. 

111 """ 

112 # There is worry that seconds are too coarse so use 10th of second 

113 # and read the first 21 characters. 

114 exposure_id = re.sub(r"\D", "", dateobs[:21]) 

115 return int(exposure_id) 

116 

117 @cache_translation 

118 def to_datetime_begin(self): 

119 # Docstring will be inherited. Property defined in properties.py 

120 self._used_these_cards("MJD-OBS") 

121 return Time(self._header["MJD-OBS"], scale="utc", format="mjd") 

122 

123 def to_exposure_id(self): 

124 """Generate a unique exposure ID number 

125 

126 Note that SEQNUM is not unique for a given day in TS3 data 

127 so instead we convert the ISO date of observation directly to an 

128 integer. 

129 

130 Returns 

131 ------- 

132 exposure_id : `int` 

133 Unique exposure number. 

134 """ 

135 iso = self._header["DATE-OBS"] 

136 self._used_these_cards("DATE-OBS") 

137 

138 return self.compute_exposure_id(iso) 

139 

140 # For now assume that visit IDs and exposure IDs are identical 

141 to_visit_id = to_exposure_id 

142 

143 @cache_translation 

144 def to_science_program(self): 

145 """Calculate the science program information. 

146 

147 There is no header recording this in TS3 data so instead return 

148 the observing day in YYYY-MM-DD format. 

149 

150 Returns 

151 ------- 

152 run : `str` 

153 Observing day in YYYY-MM-DD format. 

154 """ 

155 # Get a copy so that we can edit the default formatting 

156 date = self.to_datetime_begin().copy() 

157 date.format = "iso" 

158 date.out_subfmt = "date" # YYYY-MM-DD format 

159 return str(date) 

160 

161 @cache_translation 

162 def to_observation_id(self): 

163 # Docstring will be inherited. Property defined in properties.py 

164 filename = self._header["FILENAME"] 

165 self._used_these_cards("FILENAME") 

166 return os.path.splitext(filename)[0] 

167 

168 @cache_translation 

169 def to_detector_group(self): 

170 # Docstring will be inherited. Property defined in properties.py 

171 serial = self.to_detector_serial() 

172 detector_info = self.compute_detector_info_from_serial(serial) 

173 return detector_info[0]