def run(self, exposure, sensorRef, templateIdList=None): """Retrieve and mosaic a template coadd exposure that overlaps the exposure Parameters ---------- exposure: `lsst.afw.image.Exposure` an exposure for which to generate an overlapping template sensorRef : TYPE a Butler data reference that can be used to obtain coadd data templateIdList : TYPE, optional list of data ids (unused) Returns ------- result : `struct` return a pipeBase.Struct: - ``exposure`` : a template coadd exposure assembled out of patches - ``sources`` : None for this subtask """ skyMap = sensorRef.get(datasetType=self.config.coaddName + "Coadd_skyMap") expWcs = exposure.getWcs() expBoxD = afwGeom.Box2D(exposure.getBBox()) expBoxD.grow(self.config.templateBorderSize) ctrSkyPos = expWcs.pixelToSky(expBoxD.getCenter()) tractInfo = skyMap.findTract(ctrSkyPos) self.log.info("Using skyMap tract %s" % (tractInfo.getId(),)) skyCorners = [expWcs.pixelToSky(pixPos) for pixPos in expBoxD.getCorners()] patchList = tractInfo.findPatchList(skyCorners) if not patchList: raise RuntimeError("No suitable tract found") self.log.info("Assembling %s coadd patches" % (len(patchList),)) # compute coadd bbox coaddWcs = tractInfo.getWcs() coaddBBox = afwGeom.Box2D() for skyPos in skyCorners: coaddBBox.include(coaddWcs.skyToPixel(skyPos)) coaddBBox = afwGeom.Box2I(coaddBBox) self.log.info("exposure dimensions=%s; coadd dimensions=%s" % (exposure.getDimensions(), coaddBBox.getDimensions())) # assemble coadd exposure from subregions of patches coaddExposure = afwImage.ExposureF(coaddBBox, coaddWcs) coaddExposure.maskedImage.set(np.nan, afwImage.Mask.getPlaneBitMask("NO_DATA"), np.nan) nPatchesFound = 0 coaddFilter = None coaddPsf = None for patchInfo in patchList: patchSubBBox = patchInfo.getOuterBBox() patchSubBBox.clip(coaddBBox) patchArgDict = dict( datasetType=self.getCoaddDatasetName() + "_sub", bbox=patchSubBBox, tract=tractInfo.getId(), patch="%s,%s" % (patchInfo.getIndex()[0], patchInfo.getIndex()[1]), numSubfilters=self.config.numSubfilters, ) if patchSubBBox.isEmpty(): self.log.info("skip tract=%(tract)s, patch=%(patch)s; no overlapping pixels" % patchArgDict) continue if self.config.coaddName == 'dcr': if not sensorRef.datasetExists(subfilter=0, **patchArgDict): self.log.warn("%(datasetType)s, tract=%(tract)s, patch=%(patch)s," " numSubfilters=%(numSubfilters)s, subfilter=0 does not exist" % patchArgDict) continue self.log.info("Constructing DCR-matched template for patch %s" % patchArgDict) dcrModel = DcrModel.fromDataRef(sensorRef, **patchArgDict) # 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. patchInnerBBox = patchInfo.getInnerBBox() patchInnerBBox.clip(coaddBBox) dcrBBox = afwGeom.Box2I(patchSubBBox) dcrBBox.grow(-self.config.templateBorderSize) dcrBBox.include(patchInnerBBox) coaddPatch = dcrModel.buildMatchedExposure(bbox=dcrBBox, wcs=coaddWcs, visitInfo=exposure.getInfo().getVisitInfo()) else: if not sensorRef.datasetExists(**patchArgDict): self.log.warn("%(datasetType)s, tract=%(tract)s, patch=%(patch)s does not exist" % patchArgDict) continue self.log.info("Reading patch %s" % patchArgDict) coaddPatch = sensorRef.get(**patchArgDict) nPatchesFound += 1 coaddExposure.maskedImage.assign(coaddPatch.maskedImage, coaddPatch.getBBox()) if coaddFilter is None: coaddFilter = coaddPatch.getFilter() # Retrieve the PSF for this coadd tract, if not already retrieved if coaddPsf is None and coaddPatch.hasPsf(): coaddPsf = coaddPatch.getPsf() if nPatchesFound == 0: raise RuntimeError("No patches found!") if coaddPsf is None: raise RuntimeError("No coadd Psf found!") coaddExposure.setPsf(coaddPsf) coaddExposure.setFilter(coaddFilter) return pipeBase.Struct(exposure=coaddExposure, sources=None)
def run(self, exposure, sensorRef, templateIdList=None): """Retrieve and mosaic a template coadd exposure that overlaps the exposure Parameters ---------- exposure: `lsst.afw.image.Exposure` an exposure for which to generate an overlapping template sensorRef : TYPE a Butler data reference that can be used to obtain coadd data templateIdList : TYPE, optional list of data ids (unused) Returns ------- result : `struct` return a pipeBase.Struct: - ``exposure`` : a template coadd exposure assembled out of patches - ``sources`` : None for this subtask """ skyMap = sensorRef.get(datasetType=self.config.coaddName + "Coadd_skyMap") expWcs = exposure.getWcs() expBoxD = afwGeom.Box2D(exposure.getBBox()) expBoxD.grow(self.config.templateBorderSize) ctrSkyPos = expWcs.pixelToSky(expBoxD.getCenter()) tractInfo = skyMap.findTract(ctrSkyPos) self.log.info("Using skyMap tract %s" % (tractInfo.getId(),)) skyCorners = [expWcs.pixelToSky(pixPos) for pixPos in expBoxD.getCorners()] patchList = tractInfo.findPatchList(skyCorners) if not patchList: raise RuntimeError("No suitable tract found") self.log.info("Assembling %s coadd patches" % (len(patchList),)) # compute coadd bbox coaddWcs = tractInfo.getWcs() coaddBBox = afwGeom.Box2D() for skyPos in skyCorners: coaddBBox.include(coaddWcs.skyToPixel(skyPos)) coaddBBox = afwGeom.Box2I(coaddBBox) self.log.info("exposure dimensions=%s; coadd dimensions=%s" % (exposure.getDimensions(), coaddBBox.getDimensions())) # assemble coadd exposure from subregions of patches coaddExposure = afwImage.ExposureF(coaddBBox, coaddWcs) coaddExposure.maskedImage.set(np.nan, afwImage.Mask.getPlaneBitMask("NO_DATA"), np.nan) nPatchesFound = 0 coaddFilter = None coaddPsf = None for patchInfo in patchList: patchSubBBox = patchInfo.getOuterBBox() patchSubBBox.clip(coaddBBox) patchArgDict = dict( datasetType=self.getCoaddDatasetName() + "_sub", bbox=patchSubBBox, tract=tractInfo.getId(), patch="%s,%s" % (patchInfo.getIndex()[0], patchInfo.getIndex()[1]), numSubfilters=self.config.numSubfilters, ) if patchSubBBox.isEmpty(): self.log.info("skip tract=%(tract)s, patch=%(patch)s; no overlapping pixels" % patchArgDict) continue if self.config.coaddName == 'dcr': if not sensorRef.datasetExists(subfilter=0, **patchArgDict): self.log.warn("%(datasetType)s, tract=%(tract)s, patch=%(patch)s," " numSubfilters=%(numSubfilters)s, subfilter=0 does not exist" % patchArgDict) continue self.log.info("Constructing DCR-matched template for patch %s" % patchArgDict) dcrModel = DcrModel.fromDataRef(sensorRef, **patchArgDict) coaddPatch = dcrModel.buildMatchedExposure(bbox=patchSubBBox, wcs=coaddWcs, visitInfo=exposure.getInfo().getVisitInfo()) else: if not sensorRef.datasetExists(**patchArgDict): self.log.warn("%(datasetType)s, tract=%(tract)s, patch=%(patch)s does not exist" % patchArgDict) continue self.log.info("Reading patch %s" % patchArgDict) coaddPatch = sensorRef.get(**patchArgDict) nPatchesFound += 1 coaddExposure.maskedImage.assign(coaddPatch.maskedImage, coaddPatch.getBBox()) if coaddFilter is None: coaddFilter = coaddPatch.getFilter() # Retrieve the PSF for this coadd tract, if not already retrieved if coaddPsf is None and coaddPatch.hasPsf(): coaddPsf = coaddPatch.getPsf() if nPatchesFound == 0: raise RuntimeError("No patches found!") if coaddPsf is None: raise RuntimeError("No coadd Psf found!") coaddExposure.setPsf(coaddPsf) coaddExposure.setFilter(coaddFilter) return pipeBase.Struct(exposure=coaddExposure, sources=None)
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` of `int` : `lsst.daf.butler.DeferredDatasetHandle` (Gen3) `dict` (Gen2) Dictionary of spatially relevant retrieved coadd patches, indexed by their sequential patch number. In Gen3 mode, .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 coaddFilter = 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.warn( f"skip patch={patchNumber}; patch does not exist for this coadd" ) continue if patchSubBBox.isEmpty(): self.log.info( f"skip tract={availableCoaddRefs[patchNumber]['tract']}, " f"patch={patchNumber}; no overlapping pixels") 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]) if sensorRef: dcrModel = DcrModel.fromDataRef( sensorRef, self.config.effectiveWavelength, self.config.bandwidth, **availableCoaddRefs[patchNumber]) else: 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, wcs=coaddWcs, 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 coaddFilter is None: coaddFilter = 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(coaddFilter) return coaddExposure