168 """Load reference objects and mask bright stars on an exposure.
172 exposure : `lsst.afw.image.Exposure`
173 Science exposure to set the SPIKE plane. Will be modified in place.
177 spikeCat : `lsst.afw.table.SimpleCatalog`
178 The entries from the reference catalog selected as stars with
182 raise RuntimeError(
"Running diffraction spike mask task with no refObjLoader set in"
183 " __init__ or setRefObjLoader")
187 filterLabel = exposure.getFilter()
188 region =
getRegion(exposure, lsst.sphgeom.Angle.fromDegrees(self.config.angleMargin/3600.))
189 refCat = self.
refObjLoader.loadRegion(region, filterLabel.bandLabel).refCat
191 mask.addMaskPlane(self.config.spikeMask)
193 self.log.warning(
"Diffraction spike angle is not finite; check the boresight rotation and"
194 " parallactic angles. Not masking diffraction spikes.")
195 return afwTable.SimpleCatalog(refCat.schema)
202 bright = (magnitudes < self.config.magnitudeThreshold)
204 nBright = np.count_nonzero(bright)
207 xvals, yvals = exposure.wcs.skyToPixelArray(refCat[bright][self.config.raKey],
208 refCat[bright][self.config.decKey])
209 spikeCandidates = self.
selectSources(xvals, yvals, radii[bright], mask)
210 nSpike = np.count_nonzero(spikeCandidates)
214 self.log.info(
"Calculating mask for %d stars brighter than magnitude %f", nSpike,
215 self.config.magnitudeThreshold)
217 yvals[spikeCandidates],
218 radii[bright][spikeCandidates],
222 self.log.info(
"Not masking diffraction spikes; no bright stars found in the reference "
225 self.log.info(
"None of the %d stars brighter than magnitude %f are saturated on the image"
226 " or have spikes reaching it; not masking diffraction spikes.", nBright,
227 self.config.magnitudeThreshold)
228 return afwTable.SimpleCatalog(refCat.schema)
230 return refCat[bright][spikeCandidates].copy(deep=
True)
233 """Select saturated sources, and bright sources that are off the image.
237 xvals, yvals : `numpy.ndarray`
238 Array of x- and y-values of bright sources to mask.
239 mask : `lsst.afw.image.Mask`
240 The mask plane of the image to set the SPIKE mask plane.
241 spikeRadii : `numpy.ndarray`
242 Predicted lengths in pixels of the diffraction spikes for each
247 candidates : `numpy.ndarray`
248 Array of boolean flags indicating whether the given coordinates
249 should have a diffraction spike mask calculated.
251 candidates = np.zeros(len(xvals), dtype=bool)
252 saturatedBitMask = mask.getPlaneBitMask(self.config.saturatedMaskPlane)
253 bbox = mask.getBBox()
254 projection = np.max([abs(np.cos(np.deg2rad(self.
angles))),
255 abs(np.sin(np.deg2rad(self.
angles)))])
257 for i, (x, y, r)
in enumerate(zip(xvals, yvals, spikeRadii)):
259 if bbox.contains(srcBox):
260 candidates[i] = np.all((mask[srcBox].array & saturatedBitMask) > 0)
268 """Apply the SPIKE mask for a given set of coordinates. The mask plane
269 will be modified in place.
273 xvals, yvals : `numpy.ndarray`
274 Array of x- and y-values of bright sources to mask.
275 spikeRadii : `numpy.ndarray`
276 Array of radius values for each bright source.
277 mask : `lsst.afw.image.Mask`
278 The mask plane of the image to set the SPIKE mask plane.
280 bbox = mask.getBBox()
281 for x, y, r
in zip(xvals, yvals, spikeRadii):
283 singleBBox = maskSingle.getBBox()
284 if bbox.overlaps(singleBBox):
285 singleBBox = singleBBox.clippedTo(bbox)
286 mask[singleBBox] |= maskSingle[singleBBox]
289 """Create a mask plane centered on a single source with the BRIGHT mask
290 set. This mask does not have to be fully contained in the bounding box
291 of the mask of the science image.
296 Coordinates of the source to be masked.
298 Expected length of a diffraction spike for a source with a
303 mask : `lsst.afw.image.Mask`
304 A mask plane centered on the single source being modeled.
312 xLong = np.cos(np.deg2rad(angle))*r
313 yLong = np.sin(np.deg2rad(angle))*r
314 xShort = -np.sin(np.deg2rad(angle))*r/self.config.spikeAspectRatio
315 yShort = np.cos(np.deg2rad(angle))*r/self.config.spikeAspectRatio
321 polygons.append(afwGeom.Polygon(corners))
323 polygon = polygons[0]
324 for poly
in polygons[1:]:
325 polygon = polygon.unionSingle(poly)
327 polyImage = polygon.createImage(bbox).array
329 mask.array[polyImage > 0] = mask.getPlaneBitMask(self.config.spikeMask)
364 """Extract magnitude and magnitude error arrays from the given catalog.
368 refCat : `lsst.afw.table.SimpleCatalog`
369 The input reference catalog.
371 Label of filter being calibrated.
375 result : `lsst.pipe.base.Struct`
376 Results as a struct with attributes:
379 Reference magnitude (`np.array`).
381 Reference magnitude error (`np.array`).
383 A list of field names of the reference catalog used for fluxes (1 or 2 strings) (`list`).
385 refSchema = refCat.schema
387 if self.config.applyColorTerms:
388 self.log.info(
"Applying color terms for filter=%r, config.photoCatName=%s",
389 filterLabel.physicalLabel, self.config.photoCatName)
390 colorterm = self.config.colorterms.getColorterm(filterLabel.physicalLabel,
391 self.config.photoCatName,
394 refMagArr, refMagErrArr = colorterm.getCorrectedMagnitudes(refCat)
395 fluxFieldList = [getRefFluxField(refSchema, filt)
for filt
in (colorterm.primary,
396 colorterm.secondary)]
398 self.log.info(
"Not applying color terms.")
401 fluxFieldList = [getRefFluxField(refSchema, filterLabel.bandLabel)]
402 fluxField = getRefFluxField(refSchema, filterLabel.bandLabel)
403 fluxKey = refSchema.find(fluxField).key
404 refFluxArr = np.array(refCat.get(fluxKey))
407 fluxErrKey = refSchema.find(fluxField +
"Err").key
408 refFluxErrArr = np.array(refCat.get(fluxErrKey))
411 self.log.warning(
"Reference catalog does not have flux uncertainties for %s;"
412 " using sqrt(flux).", fluxField)
413 refFluxErrArr = np.sqrt(refFluxArr)
415 refMagArr = u.Quantity(refFluxArr, u.nJy).to_value(u.ABmag)
417 refMagErrArr = abMagErrFromFluxErr(refFluxErrArr*1e-9, refFluxArr*1e-9)
422 badMagnitudes = np.isnan(refMagArr)
423 if np.count_nonzero(badMagnitudes):
424 fluxName = f
"{self.config.fallbackFluxField}_flux"
427 fallbackFluxKey = refSchema.find(fluxName).key
429 self.log.warning(f
"Field {fluxName} not found in photometric reference catalog."
430 " Not masking bright stars with missing magnitudes.")
432 fallbackFluxArr = np.array(refCat.get(fallbackFluxKey))
433 fallbackMagArr = u.Quantity(fallbackFluxArr[badMagnitudes], u.nJy).to_value(u.ABmag)
434 refMagArr[badMagnitudes] = fallbackMagArr
438 refMagErr=refMagErrArr,
439 refFluxFieldList=fluxFieldList,