def getDcrModel(self, patchList, coaddRefs, visitInfo): """Build DCR-matched coadds from a list of exposure references. Parameters ---------- patchList : `dict` Dict of the patches containing valid data for each tract coaddRefs : `list` of elements of type `lsst.daf.butler.DeferredDatasetHandle` of `lsst.afw.image.Exposure` Data references to DcrModels that overlap the detector. visitInfo : `lsst.afw.image.VisitInfo` Metadata for the science image. Returns ------- `list` of elements of type `lsst.afw.image.Exposure` Coadd exposures that overlap the detector. """ coaddExposureList = [] for tract in patchList: for patch in set(patchList[tract]): coaddRefList = [ coaddRef for coaddRef in coaddRefs if _selectDataRef(coaddRef, tract, patch) ] dcrModel = DcrModel.fromQuantum( coaddRefList, self.config.effectiveWavelength, self.config.bandwidth, self.config.numSubfilters) coaddExposureList.append( dcrModel.buildMatchedExposure(visitInfo=visitInfo)) return coaddExposureList
def run(self, tractInfo, patchList, skyCorners, availableCoaddRefs, sensorRef=None, visitInfo=None): """Gen2 and gen3 shared code: determination of exposure dimensions and copying of pixels from overlapping patch regions. Parameters ---------- skyMap : `lsst.skymap.BaseSkyMap` SkyMap object that corresponds to the template coadd. tractInfo : `lsst.skymap.TractInfo` The selected tract. patchList : iterable of `lsst.skymap.patchInfo.PatchInfo` Patches to consider for making the template exposure. skyCorners : list of `lsst.geom.SpherePoint` Sky corner coordinates to be covered by the template exposure. availableCoaddRefs : `dict` [`int`] Dictionary of spatially relevant retrieved coadd patches, indexed by their sequential patch number. In Gen3 mode, values are `lsst.daf.butler.DeferredDatasetHandle` and ``.get()`` is called, in Gen2 mode, ``sensorRef.get(**coaddef)`` is called to retrieve the coadd. sensorRef : `lsst.daf.persistence.ButlerDataRef`, Gen2 only Butler data reference to get coadd data. Must be `None` for Gen3. visitInfo : `lsst.afw.image.VisitInfo`, Gen2 only VisitInfo to make dcr model. Returns ------- templateExposure: `lsst.afw.image.ExposureF` The created template exposure. """ coaddWcs = tractInfo.getWcs() # compute coadd bbox coaddBBox = geom.Box2D() for skyPos in skyCorners: coaddBBox.include(coaddWcs.skyToPixel(skyPos)) coaddBBox = geom.Box2I(coaddBBox) self.log.info("coadd dimensions=%s", coaddBBox.getDimensions()) coaddExposure = afwImage.ExposureF(coaddBBox, coaddWcs) coaddExposure.maskedImage.set(np.nan, afwImage.Mask.getPlaneBitMask("NO_DATA"), np.nan) nPatchesFound = 0 coaddFilterLabel = None coaddPsf = None coaddPhotoCalib = None for patchInfo in patchList: patchNumber = tractInfo.getSequentialPatchIndex(patchInfo) patchSubBBox = patchInfo.getOuterBBox() patchSubBBox.clip(coaddBBox) if patchNumber not in availableCoaddRefs: self.log.warning( "skip patch=%d; patch does not exist for this coadd", patchNumber) continue if patchSubBBox.isEmpty(): if isinstance(availableCoaddRefs[patchNumber], DeferredDatasetHandle): tract = availableCoaddRefs[patchNumber].dataId['tract'] else: tract = availableCoaddRefs[patchNumber]['tract'] self.log.info("skip tract=%d patch=%d; no overlapping pixels", tract, patchNumber) continue if self.config.coaddName == 'dcr': patchInnerBBox = patchInfo.getInnerBBox() patchInnerBBox.clip(coaddBBox) if np.min(patchInnerBBox.getDimensions() ) <= 2 * self.config.templateBorderSize: self.log.info( "skip tract=%(tract)s, patch=%(patch)s; too few pixels.", availableCoaddRefs[patchNumber]) continue self.log.info("Constructing DCR-matched template for patch %s", availableCoaddRefs[patchNumber]) dcrModel = DcrModel.fromQuantum( availableCoaddRefs[patchNumber], self.config.effectiveWavelength, self.config.bandwidth) # The edge pixels of the DcrCoadd may contain artifacts due to missing data. # Each patch has significant overlap, and the contaminated edge pixels in # a new patch will overwrite good pixels in the overlap region from # previous patches. # Shrink the BBox to remove the contaminated pixels, # but make sure it is only the overlap region that is reduced. dcrBBox = geom.Box2I(patchSubBBox) dcrBBox.grow(-self.config.templateBorderSize) dcrBBox.include(patchInnerBBox) coaddPatch = dcrModel.buildMatchedExposure(bbox=dcrBBox, visitInfo=visitInfo) else: if sensorRef is None: # Gen3 coaddPatch = availableCoaddRefs[patchNumber].get() else: # Gen2 coaddPatch = sensorRef.get( **availableCoaddRefs[patchNumber]) nPatchesFound += 1 # Gen2 get() seems to clip based on bbox kwarg but we removed bbox # calculation from caller code. Gen3 also does not do this. overlapBox = coaddPatch.getBBox() overlapBox.clip(coaddBBox) coaddExposure.maskedImage.assign( coaddPatch.maskedImage[overlapBox], overlapBox) if coaddFilterLabel is None: coaddFilterLabel = coaddPatch.getFilter() # Retrieve the PSF for this coadd tract, if not already retrieved if coaddPsf is None and coaddPatch.hasPsf(): coaddPsf = coaddPatch.getPsf() # Retrieve the calibration for this coadd tract, if not already retrieved if coaddPhotoCalib is None: coaddPhotoCalib = coaddPatch.getPhotoCalib() if coaddPhotoCalib is None: raise RuntimeError("No coadd PhotoCalib found!") if nPatchesFound == 0: raise RuntimeError("No patches found!") if coaddPsf is None: raise RuntimeError("No coadd Psf found!") coaddExposure.setPhotoCalib(coaddPhotoCalib) coaddExposure.setPsf(coaddPsf) coaddExposure.setFilter(coaddFilterLabel) return coaddExposure