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# 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 

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 DETECTOR_MAX = 999 

67 """Maximum number of detectors to use when calculating the 

68 detector_exposure_id.""" 

69 

70 cameraPolicyFile = "policy/ts3.yaml" 

71 

72 @classmethod 

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

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

75 supplied header. 

76 

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

78 the ``TSTAND`` header. 

79 

80 Parameters 

81 ---------- 

82 header : `dict`-like 

83 Header to convert to standardized form. 

84 filename : `str`, optional 

85 Name of file being translated. 

86 

87 Returns 

88 ------- 

89 can : `bool` 

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

91 otherwise. 

92 """ 

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

94 

95 @staticmethod 

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

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

98 

99 Parameters 

100 ---------- 

101 dateobs : `str` 

102 Date of observation in FITS ISO format. 

103 seqnum : `int`, unused 

104 Sequence number. Ignored. 

105 controller : `str`, unused 

106 Controller type. Ignored. 

107 

108 Returns 

109 ------- 

110 exposure_id : `int` 

111 Exposure ID. 

112 """ 

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

114 # and read the first 21 characters. 

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

116 return int(exposure_id) 

117 

118 @cache_translation 

119 def to_datetime_begin(self): 

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

121 self._used_these_cards("MJD-OBS") 

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

123 

124 def to_exposure_id(self): 

125 """Generate a unique exposure ID number 

126 

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

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

129 integer. 

130 

131 Returns 

132 ------- 

133 exposure_id : `int` 

134 Unique exposure number. 

135 """ 

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

137 self._used_these_cards("DATE-OBS") 

138 

139 return self.compute_exposure_id(iso) 

140 

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

142 to_visit_id = to_exposure_id 

143 

144 @cache_translation 

145 def to_science_program(self): 

146 """Calculate the science program information. 

147 

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

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

150 

151 Returns 

152 ------- 

153 run : `str` 

154 Observing day in YYYY-MM-DD format. 

155 """ 

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

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

158 date.format = "iso" 

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

160 return str(date) 

161 

162 @cache_translation 

163 def to_observation_id(self): 

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

165 filename = self._header["FILENAME"] 

166 self._used_these_cards("FILENAME") 

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

168 

169 @cache_translation 

170 def to_detector_group(self): 

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

172 serial = self.to_detector_serial() 

173 detector_info = self.compute_detector_info_from_serial(serial) 

174 return detector_info[0]