Coverage for python/lsst/obs/lsst/rawFormatter.py: 97%

101 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-06 22:08 +0000

1# This file is part of obs_lsst. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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"""Gen3 Butler Formatters for LSST raw data. 

23""" 

24 

25__all__ = ( 

26 "LsstCamRawFormatter", 

27 "LatissRawFormatter", 

28 "LsstCamImSimRawFormatter", 

29 "LsstCamPhoSimRawFormatter", 

30 "LsstTS8RawFormatter", 

31 "LsstTS3RawFormatter", 

32 "LsstComCamRawFormatter", 

33 "LsstUCDCamRawFormatter", 

34) 

35 

36import numpy as np 

37from astro_metadata_translator import fix_header, merge_headers 

38 

39import lsst.afw.fits 

40from lsst.obs.base import FitsRawFormatterBase 

41from lsst.obs.base.formatters.fitsExposure import standardizeAmplifierParameters 

42from lsst.afw.cameraGeom import makeUpdatedDetector 

43 

44from ._instrument import LsstCam, Latiss, \ 

45 LsstCamImSim, LsstCamPhoSim, LsstTS8, \ 

46 LsstTS3, LsstUCDCam, LsstComCam 

47from .translators import LatissTranslator, LsstCamTranslator, \ 

48 LsstUCDCamTranslator, LsstTS3Translator, LsstComCamTranslator, \ 

49 LsstCamPhoSimTranslator, LsstTS8Translator, LsstCamImSimTranslator 

50from .assembly import fixAmpsAndAssemble, fixAmpGeometry, readRawAmps, warn_once 

51 

52 

53class LsstCamRawFormatter(FitsRawFormatterBase): 

54 translatorClass = LsstCamTranslator 

55 filterDefinitions = LsstCam.filterDefinitions 

56 _instrument = LsstCam 

57 

58 def readMetadata(self): 

59 """Read all header metadata directly into a PropertyList. 

60 

61 Specialist version since some of our data does not 

62 set INHERIT=T so we have to merge the headers manually. 

63 

64 Returns 

65 ------- 

66 metadata : `~lsst.daf.base.PropertyList` 

67 Header metadata. 

68 """ 

69 file = self.fileDescriptor.location.path 

70 phdu = lsst.afw.fits.readMetadata(file, 0) 

71 if "INHERIT" in phdu: 71 ↛ 73line 71 didn't jump to line 73, because the condition on line 71 was never true

72 # Trust the inheritance flag 

73 return super().readMetadata() 

74 

75 # Merge ourselves 

76 md = merge_headers([phdu, lsst.afw.fits.readMetadata(file)], 

77 mode="overwrite") 

78 fix_header(md) 

79 return md 

80 

81 def getDetector(self, id): 

82 in_detector = self._instrument.getCamera()[id] 

83 # The detectors attached to the Camera object represent the on-disk 

84 # amplifier geometry, not the assembled raw. But Butler users 

85 # shouldn't know or care about what's on disk; they want the Detector 

86 # that's equivalent to `butler.get("raw", ...).getDetector()`, so we 

87 # adjust it accordingly. This parallels the logic in 

88 # fixAmpsAndAssemble, but that function and the ISR AssembleCcdTask it 

89 # calls aren't set up to handle bare bounding boxes with no pixels. We 

90 # also can't remove those without API breakage. So this is fairly 

91 # duplicative, and hence fragile. 

92 # We start by fixing amp bounding boxes based on the size of the amp 

93 # images themselves, because the camera doesn't have the right overscan 

94 # regions for all images. 

95 filename = self.fileDescriptor.location.path 

96 temp_detector = in_detector.rebuild() 

97 temp_detector.clear() 

98 with warn_once(filename) as logCmd: 

99 for n, in_amp in enumerate(in_detector): 

100 reader = lsst.afw.image.ImageFitsReader(filename, hdu=(n + 1)) 

101 out_amp, _ = fixAmpGeometry(in_amp, 

102 bbox=reader.readBBox(), 

103 metadata=reader.readMetadata(), 

104 logCmd=logCmd) 

105 temp_detector.append(out_amp) 

106 adjusted_detector = temp_detector.finish() 

107 # Now we need to apply flips and offsets to reflect assembly. The 

108 # function call that does this in fixAmpsAndAssemble is down inside 

109 # ip.isr.AssembleCcdTask. 

110 return makeUpdatedDetector(adjusted_detector) 

111 

112 def readImage(self): 

113 # Docstring inherited. 

114 return self.readFull().getImage() 

115 

116 def readFull(self): 

117 # Docstring inherited. 

118 rawFile = self.fileDescriptor.location.path 

119 amplifier, detector, _ = standardizeAmplifierParameters( 

120 self.checked_parameters, 

121 self._instrument.getCamera()[self.observationInfo.detector_num], 

122 ) 

123 if amplifier is not None: 

124 # LSST raws are already per-amplifier on disk, and in a different 

125 # assembly state than all of the other images we see in 

126 # DM-maintained formatters. And we also need to deal with the 

127 # on-disk image having different overscans from our nominal 

128 # detector. So we can't use afw.cameraGeom.AmplifierIsolator for 

129 # most of the implementation (as other formatters do), but we can 

130 # call most of the same underlying code to do the work. 

131 

132 def findAmpHdu(name): 

133 """Find the HDU for the amplifier with the given name, 

134 according to cameraGeom. 

135 """ 

136 for hdu, amp in enumerate(detector): 136 ↛ 139line 136 didn't jump to line 139, because the loop on line 136 didn't complete

137 if amp.getName() == name: 

138 return hdu + 1 

139 raise LookupError(f"Could not find HDU for amp with name {name}.") 

140 

141 reader = lsst.afw.image.ImageFitsReader(rawFile, hdu=findAmpHdu(amplifier.getName())) 

142 image = reader.read(dtype=np.dtype(np.int32), allowUnsafe=True) 

143 with warn_once(rawFile) as logCmd: 

144 # Extract an amplifier from the on-disk detector and fix its 

145 # overscan bboxes as necessary to match the on-disk bbox. 

146 adjusted_amplifier_builder, _ = fixAmpGeometry( 

147 detector[amplifier.getName()], 

148 bbox=image.getBBox(), 

149 metadata=reader.readMetadata(), 

150 logCmd=logCmd, 

151 ) 

152 on_disk_amplifier = adjusted_amplifier_builder.finish() 

153 # We've now got two Amplifier objects in play: 

154 # A) 'amplifier' is what the user wants 

155 # B) 'on_disk_amplifier' represents the subimage we have. 

156 # The one we want has the orientation/shift state of (A) with 

157 # the overscan regions of (B). 

158 comparison = amplifier.compareGeometry(on_disk_amplifier) 

159 # If the flips or origins differ, we need to modify the image 

160 # itself. 

161 if comparison & comparison.FLIPPED: 

162 from lsst.afw.math import flipImage 

163 image = flipImage( 

164 image, 

165 comparison & comparison.FLIPPED_X, 

166 comparison & comparison.FLIPPED_Y, 

167 ) 

168 if comparison & comparison.SHIFTED: 

169 image.setXY0(amplifier.getRawBBox().getMin()) 

170 # Make a single-amplifier detector that reflects the image we're 

171 # returning. 

172 detector_builder = detector.rebuild() 

173 detector_builder.clear() 

174 detector_builder.unsetCrosstalk() 

175 if comparison & comparison.REGIONS_DIFFER: 

176 # We can't just install the amplifier the user gave us, because 

177 # that has the wrong overscan regions; instead we transform the 

178 # on-disk amplifier to have the same orientation and offsets as 

179 # the given one. 

180 adjusted_amplifier_builder.transform( 

181 outOffset=on_disk_amplifier.getRawXYOffset(), 

182 outFlipX=amplifier.getRawFlipX(), 

183 outFlipY=amplifier.getRawFlipY(), 

184 ) 

185 detector_builder.append(adjusted_amplifier_builder) 

186 detector_builder.setBBox(adjusted_amplifier_builder.getBBox()) 

187 else: 

188 detector_builder.append(amplifier.rebuild()) 

189 detector_builder.setBBox(amplifier.getBBox()) 

190 exposure = lsst.afw.image.makeExposure(lsst.afw.image.makeMaskedImage(image)) 

191 exposure.setDetector(detector_builder.finish()) 

192 else: 

193 ampExps = readRawAmps(rawFile, detector) 

194 exposure = fixAmpsAndAssemble(ampExps, rawFile) 

195 self.attachComponentsFromMetadata(exposure) 

196 return exposure 

197 

198 

199class LatissRawFormatter(LsstCamRawFormatter): 

200 translatorClass = LatissTranslator 

201 _instrument = Latiss 

202 filterDefinitions = Latiss.filterDefinitions 

203 wcsFlipX = True 

204 

205 

206class LsstCamImSimRawFormatter(LsstCamRawFormatter): 

207 translatorClass = LsstCamImSimTranslator 

208 _instrument = LsstCamImSim 

209 filterDefinitions = LsstCamImSim.filterDefinitions 

210 

211 

212class LsstCamPhoSimRawFormatter(LsstCamRawFormatter): 

213 translatorClass = LsstCamPhoSimTranslator 

214 _instrument = LsstCamPhoSim 

215 filterDefinitions = LsstCamPhoSim.filterDefinitions 

216 

217 

218class LsstTS8RawFormatter(LsstCamRawFormatter): 

219 translatorClass = LsstTS8Translator 

220 _instrument = LsstTS8 

221 filterDefinitions = LsstTS8.filterDefinitions 

222 

223 

224class LsstTS3RawFormatter(LsstCamRawFormatter): 

225 translatorClass = LsstTS3Translator 

226 _instrument = LsstTS3 

227 filterDefinitions = LsstTS3.filterDefinitions 

228 

229 

230class LsstComCamRawFormatter(LsstCamRawFormatter): 

231 translatorClass = LsstComCamTranslator 

232 _instrument = LsstComCam 

233 filterDefinitions = LsstComCam.filterDefinitions 

234 

235 

236class LsstUCDCamRawFormatter(LsstCamRawFormatter): 

237 translatorClass = LsstUCDCamTranslator 

238 _instrument = LsstUCDCam 

239 filterDefinitions = LsstUCDCam.filterDefinitions