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

56 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-26 11:36 +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, compute_detector_exposure_id_generic 

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 @classmethod 

118 def compute_detector_exposure_id(cls, exposure_id, detector_num): 

119 # Docstring inherited from LsstBaseTranslator. 

120 return compute_detector_exposure_id_generic(exposure_id, detector_num, max_num=cls.DETECTOR_MAX) 

121 

122 @cache_translation 

123 def to_datetime_begin(self): 

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

125 self._used_these_cards("MJD-OBS") 

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

127 

128 def to_exposure_id(self): 

129 """Generate a unique exposure ID number 

130 

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

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

133 integer. 

134 

135 Returns 

136 ------- 

137 exposure_id : `int` 

138 Unique exposure number. 

139 """ 

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

141 self._used_these_cards("DATE-OBS") 

142 

143 return self.compute_exposure_id(iso) 

144 

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

146 to_visit_id = to_exposure_id 

147 

148 @cache_translation 

149 def to_science_program(self): 

150 """Calculate the science program information. 

151 

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

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

154 

155 Returns 

156 ------- 

157 run : `str` 

158 Observing day in YYYY-MM-DD format. 

159 """ 

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

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

162 date.format = "iso" 

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

164 return str(date) 

165 

166 @cache_translation 

167 def to_observation_id(self): 

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

169 filename = self._header["FILENAME"] 

170 self._used_these_cards("FILENAME") 

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

172 

173 @cache_translation 

174 def to_detector_group(self): 

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

176 serial = self.to_detector_serial() 

177 detector_info = self.compute_detector_info_from_serial(serial) 

178 return detector_info[0]