594 def apply(self, catalog, exposure):
595 """Apply the mask plane requirements to a catalog.
596
597 Returns whether the sources were selected.
598
599 Parameters
600 ----------
601 catalog : `lsst.afw.table.SourceCatalog` or `pandas.DataFrame`
602 or `astropy.table.Table`
603 Catalog of sources to which the requirements will be applied.
604 exposure : `lsst.afw.image.Exposure` or None
605 The exposure whose mask plane is to be respected.
606
607
608 Returns
609 -------
610 selected : `numpy.ndarray`
611 Boolean array indicating for each source whether it is selected
612 (True means selected).
613
614 Raises
615 ------
616 RuntimeError
617 Raised if exposure passed is `None`.
618 """
619 if exposure is None:
620 raise RuntimeError("Must provide an exposure to CullFromMaskedRegion selection.")
621 xRefList = catalog[self.xColName]
622 yRefList = catalog[self.yColName]
623
624
625
626 x0, y0 = exposure.getXY0()
627 xMax, yMax = exposure.getDimensions()
628 xRefList = [int(min(max(0, xRef - x0), xMax - 1)) for xRef in xRefList]
629 yRefList = [int(min(max(0, yRef - y0), yMax - 1)) for yRef in yRefList]
630 badMaskNames = []
631 maskPlaneDict = exposure.getMask().getMaskPlaneDict()
632 for badName in self.badMaskNames:
633 if badName in maskPlaneDict:
634 badMaskNames.append(badName)
635 bitmask = exposure.mask.getPlaneBitMask(badMaskNames)
636 toKeep = ((exposure.mask.array & bitmask) == 0)
637 selected = toKeep[yRefList, xRefList]
638
639 return selected
640
641