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

Shortcuts 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

89 statements  

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 

26 

27import astropy.units 

28import astropy.utils.exceptions 

29from astropy.utils import iers 

30 

31# Prefer the standard pyerfa over the Astropy version 

32try: 

33 import erfa 

34 

35 ErfaWarning = erfa.ErfaWarning 

36except ImportError: 

37 import astropy._erfa as erfa 

38 

39 ErfaWarning = None 

40 

41from astro_metadata_translator import ObservationInfo 

42from lsst.afw.coord import Observatory, Weather 

43from lsst.afw.image import RotType, VisitInfo 

44from lsst.daf.base import DateTime 

45from lsst.geom import SpherePoint, degrees, radians 

46 

47 

48class MakeRawVisitInfoViaObsInfo: 

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

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

51 

52 Subclasses can be used if a specific 

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

54 

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

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

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

58 user needing to add a lot of error handling. 

59 

60 Parameters 

61 ---------- 

62 log : `logging.Logger` or None 

63 Logger to use for messages. 

64 (None to use 

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

66 doStripHeader : `bool`, optional 

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

68 """ 

69 

70 metadataTranslator = None 

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

72 automatic determination.""" 

73 

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

75 if log is None: 

76 log = logging.getLogger(__name__) 

77 self.log = log 

78 self.doStripHeader = doStripHeader 

79 

80 def __call__(self, md, exposureId=None): 

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

82 

83 Parameters 

84 ---------- 

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

86 Metadata to pull from. 

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

88 exposureId : `int`, optional 

89 Ignored. Here for compatibility with `MakeRawVisitInfo`. 

90 

91 Returns 

92 ------- 

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

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

95 a `~astro_metadata_translator.MetadataTranslator`. 

96 """ 

97 

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

99 

100 if self.doStripHeader: 

101 # Strip all the cards out that were used 

102 for c in obsInfo.cards_used: 

103 del md[c] 

104 

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

106 

107 @staticmethod 

108 def observationInfo2visitInfo(obsInfo, log=None): 

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

110 `~astro_metadata_translator.ObservationInfo` 

111 

112 Parameters 

113 ---------- 

114 obsInfo : `astro_metadata_translator.ObservationInfo` 

115 Information gathered from the observation metadata. 

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

117 Logger to use for logging informational messages. 

118 If `None` logging will be disabled. 

119 

120 Returns 

121 ------- 

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

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

124 `~astro_metadata_translator.ObservationInfo`. 

125 """ 

126 argDict = dict() 

127 

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

129 if obsInfo.exposure_time is not None: 

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

131 if obsInfo.dark_time is not None: 

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

133 argDict["exposureId"] = obsInfo.detector_exposure_id 

134 argDict["id"] = obsInfo.exposure_id 

135 argDict["instrumentLabel"] = obsInfo.instrument 

136 

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

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

139 tdelta = obsInfo.datetime_end - obsInfo.datetime_begin 

140 middle = obsInfo.datetime_begin + 0.5 * tdelta 

141 

142 # DateTime uses nanosecond resolution, regardless of the resolution 

143 # of the original date 

144 middle.precision = 9 

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

146 # time zone 

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

148 

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

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

151 # the observation we are needing it for). 

152 # ERFA needs a UT1 time split into two floats 

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

154 try: 

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

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

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

158 with warnings.catch_warnings(): 

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

160 # During transition period filter both 

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

162 if ErfaWarning is not None: 

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

164 ut1time = middle.ut1 

165 except iers.IERSRangeError: 

166 ut1time = middle 

167 

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

169 argDict["era"] = era * radians 

170 else: 

171 argDict["date"] = DateTime() 

172 

173 # Coordinates 

174 if obsInfo.tracking_radec is not None: 

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

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

177 

178 altaz = obsInfo.altaz_begin 

179 if altaz is not None: 

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

181 

182 argDict["boresightAirmass"] = obsInfo.boresight_airmass 

183 

184 if obsInfo.boresight_rotation_angle is not None: 

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

186 

187 if obsInfo.boresight_rotation_coord is not None: 

188 rotType = RotType.UNKNOWN 

189 if obsInfo.boresight_rotation_coord == "sky": 

190 rotType = RotType.SKY 

191 argDict["rotType"] = rotType 

192 

193 # Weather and Observatory Location 

194 temperature = float("nan") 

195 if obsInfo.temperature is not None: 

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

197 pressure = float("nan") 

198 if obsInfo.pressure is not None: 

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

200 relative_humidity = float("nan") 

201 if obsInfo.relative_humidity is not None: 

202 relative_humidity = obsInfo.relative_humidity 

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

204 

205 if obsInfo.location is not None: 

206 geolocation = obsInfo.location.to_geodetic() 

207 argDict["observatory"] = Observatory( 

208 geolocation.lon.degree * degrees, 

209 geolocation.lat.degree * degrees, 

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

211 ) 

212 

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

214 if argDict[key] is None: 

215 if log is not None: 

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

217 del argDict[key] 

218 

219 return VisitInfo(**argDict)