Coverage for python/lsst/obs/lsst/translators/lsstCam.py: 43%

64 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-27 04:15 -0700

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 the main LSST Camera""" 

12 

13__all__ = ("LsstCamTranslator", ) 

14 

15import logging 

16 

17import pytz 

18import astropy.time 

19import astropy.units as u 

20 

21from astro_metadata_translator import cache_translation 

22from astro_metadata_translator.translators.helpers import is_non_science 

23 

24from .lsst import LsstBaseTranslator, SIMONYI_TELESCOPE 

25 

26log = logging.getLogger(__name__) 

27 

28# Normalized name of the LSST Camera 

29LSST_CAM = "LSSTCam" 

30 

31_LSST_CAM_SHIP_DATE = 202406 

32 

33 

34def is_non_science_or_lab(self): 

35 """Pseudo method to determine whether this is a lab or non-science 

36 header. 

37 

38 Raises 

39 ------ 

40 KeyError 

41 If this is a science observation and on the mountain. 

42 """ 

43 # Return without raising if this is not a science observation 

44 # since the defaults are fine. 

45 try: 

46 # This will raise if it is a science observation. 

47 is_non_science(self) 

48 return 

49 except KeyError: 

50 pass 

51 

52 # We are still in the lab, return and use the default. 

53 if not self._is_on_mountain(): 

54 return 

55 

56 # This is a science observation on the mountain so we should not 

57 # use defaults. 

58 raise KeyError(f"{self._log_prefix}: Required key is missing and this is a mountain science observation") 

59 

60 

61class LsstCamTranslator(LsstBaseTranslator): 

62 """Metadata translation for the main LSST Camera.""" 

63 

64 name = LSST_CAM 

65 """Name of this translation class""" 

66 

67 supported_instrument = LSST_CAM 

68 """Supports the lsstCam instrument.""" 

69 

70 _const_map = { 

71 "instrument": LSST_CAM, 

72 "telescope": SIMONYI_TELESCOPE, 

73 } 

74 

75 _trivial_map = { 

76 "detector_group": "RAFTBAY", 

77 "detector_name": "CCDSLOT", 

78 "observation_id": "OBSID", 

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

80 "detector_serial": "LSST_NUM", 

81 "object": ("OBJECT", dict(default="UNKNOWN")), 

82 "science_program": (["PROGRAM", "RUNNUM"], dict(default="unknown")), 

83 "boresight_rotation_angle": (["ROTPA", "ROTANGLE"], dict(checker=is_non_science_or_lab, 

84 default=0.0, unit=u.deg)), 

85 } 

86 

87 # Use Imsim raft definitions until a true lsstCam definition exists 

88 cameraPolicyFile = "policy/lsstCam.yaml" 

89 

90 @classmethod 

91 def fix_header(cls, header, instrument, obsid, filename=None): 

92 """Fix LSSTCam headers. 

93 

94 Notes 

95 ----- 

96 See `~astro_metadata_translator.fix_header` for details of the general 

97 process. 

98 """ 

99 

100 modified = False 

101 

102 # Calculate the standard label to use for log messages 

103 log_label = cls._construct_log_prefix(obsid, filename) 

104 

105 if "FILTER" not in header and header.get("FILTER2") is not None: 

106 ccdslot = header.get("CCDSLOT", "unknown") 

107 raftbay = header.get("RAFTBAY", "unknown") 

108 

109 log.warning("%s %s_%s: No FILTER key found but FILTER2=\"%s\" (removed)", 

110 log_label, raftbay, ccdslot, header["FILTER2"]) 

111 header["FILTER2"] = None 

112 modified = True 

113 

114 if header.get("DAYOBS") in ("20231107", "20231108") and header["FILTER"] == "ph_05": 

115 header["FILTER"] = "ph_5" 

116 modified = True 

117 

118 return modified 

119 

120 @classmethod 

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

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

123 supplied header. 

124 

125 Parameters 

126 ---------- 

127 header : `dict`-like 

128 Header to convert to standardized form. 

129 filename : `str`, optional 

130 Name of file being translated. 

131 

132 Returns 

133 ------- 

134 can : `bool` 

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

136 otherwise. 

137 """ 

138 # INSTRUME keyword might be of two types 

139 if "INSTRUME" in header: 

140 instrume = header["INSTRUME"].lower() 

141 if instrume == cls.supported_instrument.lower(): 

142 return True 

143 return False 

144 

145 @cache_translation 

146 def to_physical_filter(self): 

147 """Calculate the physical filter name. 

148 

149 Returns 

150 ------- 

151 filter : `str` 

152 Name of filter. Can be a combination of FILTER, FILTER1, and 

153 FILTER2 headers joined by a "~". Trailing "~empty" components 

154 are stripped. 

155 Returns "unknown" if no filter is declared. 

156 """ 

157 joined = super().to_physical_filter() 

158 while joined.endswith("~empty"): 

159 joined = joined.removesuffix("~empty") 

160 

161 return joined 

162 

163 @classmethod 

164 def observing_date_to_offset(cls, observing_date: astropy.time.Time) -> astropy.time.TimeDelta | None: 

165 """Return the offset to use when calculating the observing day. 

166 

167 Parameters 

168 ---------- 

169 observing_date : `astropy.time.Time` 

170 The date of the observation. Unused. 

171 

172 Returns 

173 ------- 

174 offset : `astropy.time.TimeDelta` 

175 The offset to apply. During lab testing the offset is Pacific 

176 Time which can mean UTC-7 or UTC-8 depending on daylight savings. 

177 In Chile the offset is always UTC-12. 

178 """ 

179 # Timezone calculations are slow. Only do this if the instrument 

180 # is in the lab. 

181 if int(observing_date.strftime("%Y%m")) > _LSST_CAM_SHIP_DATE: 

182 return cls._ROLLOVER_TIME # 12 hours in base class 

183 

184 # Convert the date to a datetime UTC. 

185 pacific_tz = pytz.timezone("US/Pacific") 

186 pacific_time = observing_date.utc.to_datetime(timezone=pacific_tz) 

187 

188 # We need the offset to go the other way. 

189 offset = pacific_time.utcoffset() * -1 

190 return astropy.time.TimeDelta(offset)