Coverage for tests/test_fitsRawFormatter.py: 38%

97 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-12 01:53 -0800

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 <https://www.gnu.org/licenses/>. 

21 

22import unittest 

23 

24from astropy.coordinates import Angle 

25import astropy.units as u 

26 

27import lsst.utils.tests 

28 

29from astro_metadata_translator import FitsTranslator, StubTranslator 

30from astro_metadata_translator.translators.helpers import tracking_from_degree_headers 

31from lsst.afw.cameraGeom import makeUpdatedDetector 

32from lsst.afw.cameraGeom.testUtils import CameraWrapper, DetectorWrapper 

33import lsst.afw.geom 

34import lsst.afw.math 

35import lsst.daf.base 

36import lsst.daf.butler 

37import lsst.geom 

38from lsst.obs.base import ( 

39 FilterDefinition, 

40 FilterDefinitionCollection, 

41 FitsRawFormatterBase, 

42 MakeRawVisitInfoViaObsInfo, 

43) 

44from lsst.obs.base.utils import createInitialSkyWcs, InitialSkyWcsError 

45from lsst.obs.base.tests import make_ramp_exposure_untrimmed 

46 

47 

48class SimpleTestingTranslator(FitsTranslator, StubTranslator): 

49 _const_map = {"boresight_rotation_angle": Angle(90*u.deg), 

50 "boresight_rotation_coord": "sky", 

51 "detector_exposure_id": 12345, 

52 # The following are defined to prevent warnings about 

53 # undefined translators 

54 "dark_time": 0.0*u.s, 

55 "exposure_time": 0.0*u.s, 

56 "physical_filter": "u", 

57 "detector_num": 0, 

58 "detector_name": "0", 

59 "detector_group": "", 

60 "detector_unique_name": "0", 

61 "detector_serial": "", 

62 "observation_id": "--", 

63 "science_program": "unknown", 

64 "object": "unknown", 

65 "exposure_id": 0, 

66 "visit_id": 0, 

67 "relative_humidity": 30.0, 

68 "pressure": 0.0*u.MPa, 

69 "temperature": 273*u.K, 

70 "altaz_begin": None, 

71 } 

72 _trivial_map = {"boresight_airmass": "AIRMASS", 

73 "observation_type": "OBSTYPE"} 

74 

75 def to_tracking_radec(self): 

76 radecsys = ("RADESYS", ) 

77 radecpairs = (("RA", "DEC"),) 

78 return tracking_from_degree_headers(self, radecsys, radecpairs, unit=(u.deg, u.deg)) 

79 

80 

81class MakeTestingRawVisitInfo(MakeRawVisitInfoViaObsInfo): 

82 metadataTranslator = SimpleTestingTranslator 

83 

84 

85class SimpleFitsRawFormatter(FitsRawFormatterBase): 

86 filterDefinitions = FilterDefinitionCollection(FilterDefinition(physical_filter="u", band="u", 

87 lambdaEff=300.0)) 

88 

89 @property 

90 def translatorClass(self): 

91 return SimpleTestingTranslator 

92 

93 def getDetector(self, id): 

94 """Use CameraWrapper to create a fake detector that can map from 

95 PIXELS to FIELD_ANGLE. 

96 

97 Always return Detector #10, so all the tests are self-consistent, and 

98 make sure it is in "assembled" form, since that's what the base 

99 formatter implementations assume. 

100 """ 

101 return makeUpdatedDetector(CameraWrapper().camera.get(10)) 

102 

103 

104class FitsRawFormatterTestCase(lsst.utils.tests.TestCase): 

105 def setUp(self): 

106 # reset the filters before we test anything 

107 FilterDefinitionCollection.reset() 

108 

109 # The FITS WCS and VisitInfo coordinates in this header are 

110 # intentionally different, to make comparisons between them more 

111 # obvious. 

112 self.boresight = lsst.geom.SpherePoint(10., 20., lsst.geom.degrees) 

113 self.header = { 

114 "TELESCOP": "TEST", 

115 "INSTRUME": "UNKNOWN", 

116 "AIRMASS": 1.2, 

117 "RADESYS": "ICRS", 

118 "OBSTYPE": "science", 

119 "EQUINOX": 2000, 

120 "OBSGEO-X": "-5464588.84421314", 

121 "OBSGEO-Y": "-2493000.19137644", 

122 "OBSGEO-Z": "2150653.35350771", 

123 "RA": self.boresight.getLatitude().asDegrees(), 

124 "DEC": self.boresight.getLongitude().asDegrees(), 

125 "CTYPE1": "RA---SIN", 

126 "CTYPE2": "DEC--SIN", 

127 "CRPIX1": 5, 

128 "CRPIX2": 6, 

129 "CRVAL1": self.boresight.getLatitude().asDegrees() + 1, 

130 "CRVAL2": self.boresight.getLongitude().asDegrees() + 1, 

131 "CD1_1": 1e-5, 

132 "CD1_2": 0, 

133 "CD2_2": 1e-5, 

134 "CD2_1": 0 

135 } 

136 # make a property list of the above, for use by the formatter. 

137 self.metadata = lsst.daf.base.PropertyList() 

138 self.metadata.update(self.header) 

139 

140 maker = MakeTestingRawVisitInfo() 

141 self.visitInfo = maker(self.header) 

142 

143 self.metadataSkyWcs = lsst.afw.geom.makeSkyWcs(self.metadata, strip=False) 

144 self.boresightSkyWcs = createInitialSkyWcs(self.visitInfo, CameraWrapper().camera.get(10)) 

145 

146 # set this to `contextlib.nullcontext()` to print the log warnings 

147 self.warnContext = self.assertLogs(level="WARNING") 

148 

149 # Make a data ID to pass to the formatter. 

150 universe = lsst.daf.butler.DimensionUniverse() 

151 dataId = lsst.daf.butler.DataCoordinate.standardize(instrument="Cam1", exposure=2, detector=10, 

152 physical_filter="u", band="u", universe=universe) 

153 

154 # We have no file in these tests, so make an empty descriptor. 

155 fileDescriptor = lsst.daf.butler.FileDescriptor(None, None) 

156 self.formatter = SimpleFitsRawFormatter(fileDescriptor, dataId) 

157 # Force the formatter's metadata to be what we've created above. 

158 self.formatter._metadata = self.metadata 

159 

160 def test_makeWcs(self): 

161 detector = self.formatter.getDetector(1) 

162 wcs = self.formatter.makeWcs(self.visitInfo, detector) 

163 self.assertNotEqual(wcs, self.metadataSkyWcs) 

164 self.assertEqual(wcs, self.boresightSkyWcs) 

165 

166 def test_makeWcs_warn_if_metadata_is_bad(self): 

167 """If the metadata is bad, log a warning and use the VisitInfo WCS. 

168 """ 

169 detector = self.formatter.getDetector(1) 

170 self.metadata.remove("CTYPE1") 

171 with self.warnContext: 

172 wcs = self.formatter.makeWcs(self.visitInfo, detector) 

173 self.assertNotEqual(wcs, self.metadataSkyWcs) 

174 self.assertEqual(wcs, self.boresightSkyWcs) 

175 

176 def test_makeWcs_warn_if_visitInfo_is_None(self): 

177 """If VisitInfo is None, log a warning and use the metadata WCS. 

178 """ 

179 detector = self.formatter.getDetector(1) 

180 with self.warnContext: 

181 wcs = self.formatter.makeWcs(None, detector) 

182 self.assertEqual(wcs, self.metadataSkyWcs) 

183 self.assertNotEqual(wcs, self.boresightSkyWcs) 

184 

185 def test_makeWcs_fail_if_visitInfo_is_None(self): 

186 """If VisitInfo is None and metadata failed, raise an exception. 

187 """ 

188 detector = self.formatter.getDetector(1) 

189 self.metadata.remove("CTYPE1") 

190 with self.warnContext, self.assertRaises(InitialSkyWcsError): 

191 self.formatter.makeWcs(None, detector) 

192 

193 def test_makeWcs_fail_if_detector_is_bad(self): 

194 """If Detector is broken, raise an exception. 

195 """ 

196 # This detector doesn't know about FIELD_ANGLE, so can't be used to 

197 # make a SkyWcs. 

198 detector = DetectorWrapper().detector 

199 with self.assertRaises(InitialSkyWcsError): 

200 self.formatter.makeWcs(self.visitInfo, detector) 

201 

202 def test_amp_parameter(self): 

203 """Test loading subimages with the 'amp' parameter. 

204 """ 

205 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: 

206 # Get a detector; this must be the same one that's baked into the 

207 # simple formatter at the top of this file, so that's how we get 

208 # it. 

209 detector = self.formatter.getDetector(1) 

210 # Make full exposure with ramp values and save just the image to 

211 # the temp file (with metadata), so it looks like a raw. 

212 full = make_ramp_exposure_untrimmed(detector) 

213 full.image.writeFits(tmpFile, metadata=self.metadata) 

214 # Loop over amps and try to read them via the formatter. 

215 for n, amp in enumerate(detector): 

216 for amp_parameter in [amp, amp.getName(), n]: 

217 for parameters in [{"amp": amp_parameter}, {"amp": amp_parameter, "detector": detector}]: 

218 with self.subTest(parameters=parameters): 

219 # Make a new formatter that points at the new file 

220 # and has the right parameters. 

221 formatter = SimpleFitsRawFormatter( 

222 lsst.daf.butler.FileDescriptor( 

223 lsst.daf.butler.Location(None, path=lsst.daf.butler.ButlerURI(tmpFile)), 

224 lsst.daf.butler.StorageClassFactory().getStorageClass("ExposureI"), 

225 parameters=parameters, 

226 ), 

227 self.formatter.dataId, 

228 ) 

229 subexp = formatter.read() 

230 self.assertImagesEqual(subexp.image, full[amp.getRawBBox()].image) 

231 self.assertEqual(len(subexp.getDetector()), 1) 

232 self.assertAmplifiersEqual(subexp.getDetector()[0], amp) 

233 # We could try transformed amplifiers here that involve flips 

234 # and offsets, but: 

235 # - we already test the low-level code that does that in afw; 

236 # - we test very similar high-level code (which calls that 

237 # same afw code) in the non-raw Exposure formatter, in 

238 # test_butlerFits.py; 

239 # - the only instruments that actually have those kinds of 

240 # amplifiers are those in obs_lsst, and that has a different 

241 # raw formatter implementation that we need to test there 

242 # anyway; 

243 # - these are kind of expensive tests. 

244 

245 

246class MemoryTester(lsst.utils.tests.MemoryTestCase): 

247 pass 

248 

249 

250def setup_module(module): 

251 lsst.utils.tests.init() 

252 

253 

254if __name__ == '__main__': 254 ↛ 255line 254 didn't jump to line 255, because the condition on line 254 was never true

255 lsst.utils.tests.init() 

256 unittest.main()