Coverage for python/lsst/obs/lsst/translators/imsim.py: 36%

64 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-23 11:23 +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 LSSTCam imSim headers""" 

12 

13__all__ = ("LsstCamImSimTranslator", ) 

14 

15import logging 

16import astropy.units as u 

17from astropy.coordinates import Angle, AltAz 

18 

19try: 

20 import erfa 

21except ImportError: 

22 import astropy._erfa as erfa 

23 

24from astro_metadata_translator import cache_translation 

25from astro_metadata_translator.translators.helpers import tracking_from_degree_headers 

26 

27from .lsstsim import LsstSimTranslator 

28 

29log = logging.getLogger(__name__) 

30 

31 

32class LsstCamImSimTranslator(LsstSimTranslator): 

33 """Metadata translation class for LSSTCam imSim headers""" 

34 

35 name = "LSSTCam-imSim" 

36 """Name of this translation class""" 

37 

38 _const_map = { 

39 "instrument": "LSSTCam-imSim", 

40 "boresight_rotation_coord": "sky", 

41 "object": "UNKNOWN", 

42 "pressure": None, 

43 "temperature": None, 

44 "relative_humidity": 40.0, 

45 } 

46 

47 _trivial_map = { 

48 "detector_group": "RAFTNAME", 

49 "detector_name": "SENSNAME", 

50 "observation_id": "OBSID", 

51 "science_program": "RUNNUM", 

52 "exposure_id": "OBSID", 

53 "visit_id": "OBSID", 

54 "dark_time": ("DARKTIME", dict(unit=u.s)), 

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

56 "detector_serial": "LSST_NUM", 

57 } 

58 

59 cameraPolicyFile = "policy/imsim.yaml" 

60 

61 @classmethod 

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

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

64 supplied header. 

65 

66 There is no ``INSTRUME`` header in ImSim data. Instead we use 

67 the ``TESTTYPE`` header. 

68 

69 Parameters 

70 ---------- 

71 header : `dict`-like 

72 Header to convert to standardized form. 

73 filename : `str`, optional 

74 Name of file being translated. 

75 

76 Returns 

77 ------- 

78 can : `bool` 

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

80 otherwise. 

81 """ 

82 return cls.can_translate_with_options(header, {"TESTTYPE": "IMSIM"}, 

83 filename=filename) 

84 

85 @cache_translation 

86 def to_tracking_radec(self): 

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

88 radecsys = ("RADESYS",) 

89 radecpairs = (("RATEL", "DECTEL"),) 

90 return tracking_from_degree_headers(self, radecsys, radecpairs) 

91 

92 @cache_translation 

93 def to_boresight_airmass(self): 

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

95 for key in ("AIRMASS", "AMSTART"): 

96 if self.is_key_ok(key): 

97 return self._header[key] 

98 altaz = self.to_altaz_begin() 

99 if altaz is not None: 

100 return altaz.secz.to_value() 

101 return None 

102 

103 @cache_translation 

104 def to_boresight_rotation_angle(self): 

105 angle = Angle(90.*u.deg) - Angle(self.quantity_from_card("ROTANGLE", u.deg)) 

106 angle = angle.wrap_at("360d") 

107 return angle 

108 

109 @cache_translation 

110 def to_physical_filter(self): 

111 # Find throughputs version from imSim header data. For DC2 

112 # data, we used throughputs version 1.4. 

113 throughputs_version = None 

114 for key, value in self._header.items(): 

115 if key.startswith("PKG") and value == "throughputs": 

116 version_key = "VER" + key[len("PKG"):] 

117 throughputs_version = self._header[version_key].strip() 

118 break 

119 if throughputs_version is None: 

120 log.warning("%s: throughputs version not found. Using FILTER keyword value '%s'.", 

121 self._log_prefix, self._header["FILTER"]) 

122 return self._header["FILTER"] 

123 return "_".join((self._header["FILTER"], "sim", throughputs_version)) 

124 

125 @cache_translation 

126 def to_altaz_begin(self): 

127 # Calculate from the hour angle if available 

128 if self.to_observation_type() != "science": 

129 return None 

130 

131 if not self.are_keys_ok(["HASTART", "DECTEL"]): 

132 # Fallback to slow method 

133 return super().to_altaz_begin() 

134 

135 location = self.to_location() 

136 ha = Angle(self._header["HASTART"], unit=u.deg) 

137 

138 # For speed over accuracy, assume this is apparent Dec not ICRS 

139 dec = Angle(self._header["DECTEL"], unit=u.deg) 

140 

141 # Use erfa directly 

142 az, el = erfa.hd2ae(ha.radian, dec.radian, location.lat.radian) 

143 

144 return AltAz(az*u.radian, el*u.radian, 

145 obstime=self.to_datetime_begin(), location=location)