Coverage for python/lsst/obs/base/makeRawVisitInfoViaObsInfo.py: 15%

102 statements  

« prev     ^ index     » next       coverage.py v7.3.3, created at 2023-12-20 17:27 +0000

1# This file is part of obs_base. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

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

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22__all__ = ["MakeRawVisitInfoViaObsInfo"] 

23 

24import logging 

25import warnings 

26from typing import ClassVar 

27 

28import astropy.units 

29import astropy.utils.exceptions 

30from astropy.utils import iers 

31 

32# Prefer the standard pyerfa over the Astropy version 

33try: 

34 import erfa 

35 

36 ErfaWarning = erfa.ErfaWarning 

37except ImportError: 

38 import astropy._erfa as erfa 

39 

40 ErfaWarning = None 

41 

42from astro_metadata_translator import MetadataTranslator, ObservationInfo 

43from lsst.afw.coord import Observatory, Weather 

44from lsst.afw.image import RotType, VisitInfo 

45from lsst.daf.base import DateTime 

46from lsst.geom import SpherePoint, degrees, radians 

47 

48 

49class MakeRawVisitInfoViaObsInfo: 

50 """Base class functor to make a VisitInfo from the FITS header of a 

51 raw image using `~astro_metadata_translator.ObservationInfo` translators. 

52 

53 Subclasses can be used if a specific 

54 `~astro_metadata_translator.MetadataTranslator` translator should be used. 

55 

56 The design philosophy is to make a best effort and log warnings of 

57 problems, rather than raising exceptions, in order to extract as much 

58 VisitInfo information as possible from a messy FITS header without the 

59 user needing to add a lot of error handling. 

60 

61 Parameters 

62 ---------- 

63 log : `logging.Logger` or None 

64 Logger to use for messages. 

65 (None to use 

66 ``Log.getLogger("lsst.obs.base.makeRawVisitInfoViaObsInfo")``). 

67 doStripHeader : `bool`, optional 

68 Strip header keywords from the metadata as they are used? 

69 """ 

70 

71 metadataTranslator: ClassVar[MetadataTranslator | None] = None 

72 """Header translator to use to construct VisitInfo, defaulting to 

73 automatic determination.""" 

74 

75 def __init__(self, log=None, doStripHeader=False): 

76 if log is None: 

77 log = logging.getLogger(__name__) 

78 self.log = log 

79 self.doStripHeader = doStripHeader 

80 

81 def __call__(self, md): 

82 """Construct a VisitInfo and strip associated data from the metadata. 

83 

84 Parameters 

85 ---------- 

86 md : `lsst.daf.base.PropertyList` or `lsst.daf.base.PropertySet` 

87 Metadata to pull from. 

88 May be modified if ``stripHeader`` is ``True``. 

89 

90 Returns 

91 ------- 

92 visitInfo : `lsst.afw.image.VisitInfo` 

93 `~lsst.afw.image.VisitInfo` derived from the header using 

94 a `~astro_metadata_translator.MetadataTranslator`. 

95 """ 

96 obsInfo = ObservationInfo(md, translator_class=self.metadataTranslator) 

97 

98 if self.doStripHeader: 

99 # Strip all the cards out that were used 

100 for c in obsInfo.cards_used: 

101 del md[c] 

102 

103 return self.observationInfo2visitInfo(obsInfo, log=self.log) 

104 

105 @staticmethod 

106 def observationInfo2visitInfo(obsInfo, log=None): 

107 """Construct a `~lsst.afw.image.VisitInfo` from an 

108 `~astro_metadata_translator.ObservationInfo`. 

109 

110 Parameters 

111 ---------- 

112 obsInfo : `astro_metadata_translator.ObservationInfo` 

113 Information gathered from the observation metadata. 

114 log : `logging.Logger` or `lsst.log.Log`, optional 

115 Logger to use for logging informational messages. 

116 If `None` logging will be disabled. 

117 

118 Returns 

119 ------- 

120 visitInfo : `lsst.afw.image.VisitInfo` 

121 `~lsst.afw.image.VisitInfo` derived from the supplied 

122 `~astro_metadata_translator.ObservationInfo`. 

123 """ 

124 argDict = {} 

125 

126 # Map the translated information into a form suitable for VisitInfo 

127 if obsInfo.exposure_time is not None: 

128 argDict["exposureTime"] = obsInfo.exposure_time.to_value("s") 

129 if obsInfo.dark_time is not None: 

130 argDict["darkTime"] = obsInfo.dark_time.to_value("s") 

131 argDict["id"] = obsInfo.exposure_id 

132 argDict["instrumentLabel"] = obsInfo.instrument 

133 if obsInfo.focus_z is not None: 

134 argDict["focusZ"] = obsInfo.focus_z.to_value("mm") 

135 if obsInfo.observation_type is not None: 

136 argDict["observationType"] = obsInfo.observation_type 

137 if obsInfo.science_program is not None: 

138 argDict["scienceProgram"] = obsInfo.science_program 

139 if obsInfo.observation_reason is not None: 

140 argDict["observationReason"] = obsInfo.observation_reason 

141 if obsInfo.object is not None: 

142 argDict["object"] = obsInfo.object 

143 if obsInfo.has_simulated_content is not None: 

144 argDict["hasSimulatedContent"] = obsInfo.has_simulated_content 

145 

146 # VisitInfo uses the middle of the observation for the date 

147 if obsInfo.datetime_begin is not None and obsInfo.datetime_end is not None: 

148 tdelta = obsInfo.datetime_end - obsInfo.datetime_begin 

149 middle = obsInfo.datetime_begin + 0.5 * tdelta 

150 

151 # DateTime uses nanosecond resolution, regardless of the resolution 

152 # of the original date 

153 middle.precision = 9 

154 # isot is ISO8601 format with "T" separating date and time and no 

155 # time zone 

156 argDict["date"] = DateTime(middle.tai.isot, DateTime.TAI) 

157 

158 # Derive earth rotation angle from UT1 (being out by a second is 

159 # not a big deal given the uncertainty over exactly what part of 

160 # the observation we are needing it for). 

161 # ERFA needs a UT1 time split into two floats 

162 # We ignore any problems with DUT1 not being defined for now. 

163 try: 

164 # Catch any warnings about the time being in the future 

165 # since there is nothing we can do about that for simulated 

166 # data and it tells us nothing for data from the past. 

167 with warnings.catch_warnings(): 

168 # If we are using the real erfa it is not an AstropyWarning 

169 # During transition period filter both 

170 warnings.simplefilter("ignore", category=astropy.utils.exceptions.AstropyWarning) 

171 if ErfaWarning is not None: 

172 warnings.simplefilter("ignore", category=ErfaWarning) 

173 ut1time = middle.ut1 

174 except iers.IERSRangeError: 

175 ut1time = middle 

176 

177 era = erfa.era00(ut1time.jd1, ut1time.jd2) 

178 argDict["era"] = era * radians 

179 else: 

180 argDict["date"] = DateTime() 

181 

182 # Coordinates 

183 if obsInfo.tracking_radec is not None: 

184 icrs = obsInfo.tracking_radec.transform_to("icrs") 

185 argDict["boresightRaDec"] = SpherePoint(icrs.ra.degree, icrs.dec.degree, units=degrees) 

186 

187 altaz = obsInfo.altaz_begin 

188 if altaz is not None: 

189 argDict["boresightAzAlt"] = SpherePoint(altaz.az.degree, altaz.alt.degree, units=degrees) 

190 

191 argDict["boresightAirmass"] = obsInfo.boresight_airmass 

192 

193 if obsInfo.boresight_rotation_angle is not None: 

194 argDict["boresightRotAngle"] = obsInfo.boresight_rotation_angle.degree * degrees 

195 

196 if obsInfo.boresight_rotation_coord is not None: 

197 rotType = RotType.UNKNOWN 

198 if obsInfo.boresight_rotation_coord == "sky": 

199 rotType = RotType.SKY 

200 argDict["rotType"] = rotType 

201 

202 # Weather and Observatory Location 

203 temperature = float("nan") 

204 if obsInfo.temperature is not None: 

205 temperature = obsInfo.temperature.to_value("deg_C", astropy.units.temperature()) 

206 pressure = float("nan") 

207 if obsInfo.pressure is not None: 

208 pressure = obsInfo.pressure.to_value("Pa") 

209 relative_humidity = float("nan") 

210 if obsInfo.relative_humidity is not None: 

211 relative_humidity = obsInfo.relative_humidity 

212 argDict["weather"] = Weather(temperature, pressure, relative_humidity) 

213 

214 if obsInfo.location is not None: 

215 geolocation = obsInfo.location.to_geodetic() 

216 argDict["observatory"] = Observatory( 

217 geolocation.lon.degree * degrees, 

218 geolocation.lat.degree * degrees, 

219 geolocation.height.to_value("m"), 

220 ) 

221 

222 for key in list(argDict.keys()): # use a copy because we may delete items 

223 if argDict[key] is None: 

224 if log is not None: 

225 log.warning("argDict[%s] is None; stripping", key) 

226 del argDict[key] 

227 

228 return VisitInfo(**argDict)