151 def cosmicRay(self, exposure, keepCRs=None):
152 """Mask cosmic rays.
153
154 Parameters
155 ----------
156 exposure : `lsst.afw.image.Exposure`
157 Exposure to process.
158 keepCRs : `Unknown` or `None`, optional
159 Don't interpolate over the CR pixels (defer to ``pex_config`` if `None`).
160 """
161 import lsstDebug
164
165 assert exposure, "No exposure provided"
166 psf = exposure.getPsf()
167 assert psf, "No psf provided"
168
169
170 try:
171 mask = exposure.getMaskedImage().getMask()
172 crBit = mask.getMaskPlane("CR")
173 mask.clearMaskPlane(crBit)
174 except Exception:
175 pass
176
177 exposure0 = exposure
178 binSize = self.config.cosmicray.background.binSize
179 nx, ny = exposure.getWidth()/binSize, exposure.getHeight()/binSize
180
181
182 if nx*ny <= 1:
184 modelBg = None
185 else:
186
187
188
189 exposure = exposure.Factory(exposure, True)
190 subtractBackgroundTask = measAlg.SubtractBackgroundTask(config=self.config.cosmicray.background)
191 modelBg = subtractBackgroundTask.run(exposure).background
192 medianBg = 0.0
193
194 if keepCRs is None:
195 keepCRs = self.config.cosmicray.keepCRs
196 try:
197 crs = measAlg.findCosmicRays(exposure.getMaskedImage(), psf, medianBg,
198 pexConfig.makePropertySet(self.config.cosmicray), keepCRs)
199 if modelBg:
200
201 img = exposure.getMaskedImage()
202 img += modelBg.getImageF()
203 del img
204
205 exposure0.setMaskedImage(exposure.getMaskedImage())
206
207 except Exception:
208 if display:
209 afwDisplay.Display().mtv(exposure0, title="Failed CR")
210 raise
211
212 num = 0
213 if crs is not None:
214 mask = exposure0.getMaskedImage().getMask()
215 crBit = mask.getPlaneBitMask("CR")
216 afwDet.setMaskFromFootprintList(mask, crs, crBit)
217 num = len(crs)
218
219 if display and displayCR:
220 disp = afwDisplay.Display()
221 disp.incrDefaultFrame()
222 disp.mtv(exposure0, title="Post-CR")
223
224 with disp.Buffering():
225 for cr in crs:
226 afwDisplay.utils.drawBBox(cr.getBBox(), borderWidth=0.55)
227
228 text = "kept" if keepCRs else "interpolated over"
229 self.log.info("Identified and %s %s cosmic rays.", text, num)
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)