Coverage for tests/test_fitsRawFormatter.py: 40%

98 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-05 02:41 -0700

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 

24import astropy.units as u 

25import lsst.afw.geom 

26import lsst.afw.math 

27import lsst.daf.base 

28import lsst.daf.butler 

29import lsst.geom 

30import lsst.resources 

31import lsst.utils.tests 

32from astro_metadata_translator import FitsTranslator, StubTranslator 

33from astro_metadata_translator.translators.helpers import tracking_from_degree_headers 

34from astropy.coordinates import Angle 

35from lsst.afw.cameraGeom import makeUpdatedDetector 

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

37from lsst.obs.base import ( 

38 FilterDefinition, 

39 FilterDefinitionCollection, 

40 FitsRawFormatterBase, 

41 MakeRawVisitInfoViaObsInfo, 

42) 

43from lsst.obs.base.tests import make_ramp_exposure_untrimmed 

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

45 

46 

47class SimpleTestingTranslator(FitsTranslator, StubTranslator): 

48 _const_map = { 

49 "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", "observation_type": "OBSTYPE"} 

73 

74 def to_tracking_radec(self): 

75 radecsys = ("RADESYS",) 

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

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

78 

79 

80class MakeTestingRawVisitInfo(MakeRawVisitInfoViaObsInfo): 

81 metadataTranslator = SimpleTestingTranslator 

82 

83 

84class SimpleFitsRawFormatter(FitsRawFormatterBase): 

85 filterDefinitions = FilterDefinitionCollection( 

86 FilterDefinition(physical_filter="u", band="u", lambdaEff=300.0) 

87 ) 

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.0, 20.0, 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( 

152 instrument="Cam1", exposure=2, detector=10, physical_filter="u", band="u", universe=universe 

153 ) 

154 

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

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

157 self.formatter = SimpleFitsRawFormatter(fileDescriptor, dataId) 

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

159 self.formatter._metadata = self.metadata 

160 

161 def test_makeWcs(self): 

162 detector = self.formatter.getDetector(1) 

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

164 self.assertNotEqual(wcs, self.metadataSkyWcs) 

165 self.assertEqual(wcs, self.boresightSkyWcs) 

166 

167 def test_makeWcs_warn_if_metadata_is_bad(self): 

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

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 detector = self.formatter.getDetector(1) 

179 with self.warnContext: 

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

181 self.assertEqual(wcs, self.metadataSkyWcs) 

182 self.assertNotEqual(wcs, self.boresightSkyWcs) 

183 

184 def test_makeWcs_fail_if_visitInfo_is_None(self): 

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

186 detector = self.formatter.getDetector(1) 

187 self.metadata.remove("CTYPE1") 

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

189 self.formatter.makeWcs(None, detector) 

190 

191 def test_makeWcs_fail_if_detector_is_bad(self): 

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

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

194 # make a SkyWcs. 

195 detector = DetectorWrapper().detector 

196 with self.assertRaises(InitialSkyWcsError): 

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

198 

199 def test_amp_parameter(self): 

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

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

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

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

204 # it. 

205 detector = self.formatter.getDetector(1) 

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

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

208 full = make_ramp_exposure_untrimmed(detector) 

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

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

211 for n, amp in enumerate(detector): 

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

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

214 with self.subTest(parameters=parameters): 

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

216 # and has the right parameters. 

217 formatter = SimpleFitsRawFormatter( 

218 lsst.daf.butler.FileDescriptor( 

219 lsst.daf.butler.Location(None, path=lsst.resources.ResourcePath(tmpFile)), 

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

221 parameters=parameters, 

222 ), 

223 self.formatter.dataId, 

224 ) 

225 subexp = formatter.read() 

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

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

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

229 # We could try transformed amplifiers here that involve flips 

230 # and offsets, but: 

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

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

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

234 # test_butlerFits.py; 

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

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

237 # raw formatter implementation that we need to test there 

238 # anyway; 

239 # - these are kind of expensive tests. 

240 

241 

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

243 pass 

244 

245 

246def setup_module(module): 

247 lsst.utils.tests.init() 

248 

249 

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

251 lsst.utils.tests.init() 

252 unittest.main()