def addSnaps(self, snap0, snap1): """Add two snap exposures together, returning a new exposure @param[in] snap0 snap exposure 0 @param[in] snap1 snap exposure 1 @return combined exposure """ self.log.info("snapCombine addSnaps") combinedExp = snap0.Factory(snap0, True) combinedMi = combinedExp.getMaskedImage() combinedMi.set(0) weightMap = combinedMi.getImage().Factory(combinedMi.getBBox()) weight = 1.0 badPixelMask = afwImage.Mask.getPlaneBitMask(self.config.badMaskPlanes) addToCoadd(combinedMi, weightMap, snap0.getMaskedImage(), badPixelMask, weight) addToCoadd(combinedMi, weightMap, snap1.getMaskedImage(), badPixelMask, weight) # pre-scaling the weight map instead of post-scaling the combinedMi saves a bit of time # because the weight map is a simple Image instead of a MaskedImage weightMap *= 0.5 # so result is sum of both images, instead of average combinedMi /= weightMap setCoaddEdgeBits(combinedMi.getMask(), weightMap) # note: none of the inputs has a valid PhotoCalib object, so that is not touched # Filter was already copied combinedMetadata = combinedExp.getMetadata() metadata0 = snap0.getMetadata() metadata1 = snap1.getMetadata() self.fixMetadata(combinedMetadata, metadata0, metadata1) return combinedExp
def simpleAdd(exp0, exp1, badPixelMask): """Add two exposures, avoiding bad pixels """ imArr0, maskArr0, varArr0 = exp0.getMaskedImage().getArrays() imArr1, maskArr1, varArr1 = exp1.getMaskedImage().getArrays() expRes = exp0.Factory(exp0, True) miRes = expRes.getMaskedImage() imArrRes, maskArrRes, varArrRes = miRes.getArrays() weightMap = afwImage.ImageF(exp0.getDimensions()) weightArr = weightMap.getArray() good0 = numpy.bitwise_and(maskArr0, badPixelMask) == 0 good1 = numpy.bitwise_and(maskArr1, badPixelMask) == 0 imArrRes[:, :] = numpy.where(good0, imArr0, 0) + numpy.where( good1, imArr1, 0) varArrRes[:, :] = numpy.where(good0, varArr0, 0) + numpy.where( good1, varArr1, 0) maskArrRes[:, :] = numpy.bitwise_or(numpy.where(good0, maskArr0, 0), numpy.where(good1, maskArr1, 0)) weightArr[:, :] = numpy.where(good0, 1, 0) + numpy.where(good1, 1, 0) miRes /= weightMap miRes *= 2 # want addition, not mean, where both pixels are valid setCoaddEdgeBits(miRes.getMask(), weightMap) return expRes
def addSnaps(self, snap0, snap1): """Add two snap exposures together, returning a new exposure @param[in] snap0 snap exposure 0 @param[in] snap1 snap exposure 1 @return combined exposure """ self.log.info("snapCombine addSnaps") combinedExp = snap0.Factory(snap0, True) combinedMi = combinedExp.getMaskedImage() combinedMi.set(0) weightMap = combinedMi.getImage().Factory(combinedMi.getBBox(afwImage.PARENT)) weight = 1.0 badPixelMask = afwImage.MaskU.getPlaneBitMask(self.config.badMaskPlanes) addToCoadd(combinedMi, weightMap, snap0.getMaskedImage(), badPixelMask, weight) addToCoadd(combinedMi, weightMap, snap1.getMaskedImage(), badPixelMask, weight) # pre-scaling the weight map instead of post-scaling the combinedMi saves a bit of time # because the weight map is a simple Image instead of a MaskedImage weightMap *= 0.5 # so result is sum of both images, instead of average combinedMi /= weightMap setCoaddEdgeBits(combinedMi.getMask(), weightMap) # note: none of the inputs has a valid Calib object, so that is not touched # Filter was already copied combinedMetadata = combinedExp.getMetadata() metadata0 = snap0.getMetadata() metadata1 = snap1.getMetadata() self.fixMetadata(combinedMetadata, metadata0, metadata1) return combinedExp
def testRandomMap(self): """Test setCoaddEdgeBits using a random depth map """ imDim = afwGeom.Extent2I(50, 55) coaddMask = afwImage.MaskU(imDim) numpy.random.seed(12345) depthMapArray = numpy.random.randint(0, 3, list((imDim[1], imDim[0]))).astype(numpy.uint16) depthMap = afwImage.makeImageFromArray(depthMapArray) refCoaddMaskArray = coaddMask.getArray() edgeMask = afwImage.MaskU.getPlaneBitMask("NO_DATA") refCoaddMaskArray |= numpy.array(numpy.where(depthMapArray > 0, 0, edgeMask), dtype=refCoaddMaskArray.dtype) coaddUtils.setCoaddEdgeBits(coaddMask, depthMap) coaddMaskArray = coaddMask.getArray() if numpy.any(refCoaddMaskArray != coaddMaskArray): errMsgList = ( "Coadd mask does not match reference:", "computed= %s" % (coaddMaskArray,), "reference= %s" % (refCoaddMaskArray,), ) errMsg = "\n".join(errMsgList) self.fail(errMsg)
def simpleAdd(exp0, exp1, badPixelMask): """Add two exposures, avoiding bad pixels """ imArr0, maskArr0, varArr0 = exp0.getMaskedImage().getArrays() imArr1, maskArr1, varArr1 = exp1.getMaskedImage().getArrays() expRes = exp0.Factory(exp0, True) miRes = expRes.getMaskedImage() imArrRes, maskArrRes, varArrRes = miRes.getArrays() weightMap = afwImage.ImageF(exp0.getDimensions()) weightArr = weightMap.getArray() good0 = np.bitwise_and(maskArr0, badPixelMask) == 0 good1 = np.bitwise_and(maskArr1, badPixelMask) == 0 imArrRes[:, :] = np.where(good0, imArr0, 0) + np.where(good1, imArr1, 0) varArrRes[:, :] = np.where(good0, varArr0, 0) + np.where(good1, varArr1, 0) maskArrRes[:, :] = np.bitwise_or(np.where(good0, maskArr0, 0), np.where(good1, maskArr1, 0)) weightArr[:, :] = np.where(good0, 1, 0) + np.where(good1, 1, 0) miRes /= weightMap miRes *= 2 # want addition, not mean, where both pixels are valid setCoaddEdgeBits(miRes.getMask(), weightMap) return expRes
def run(self, identMatrix, butler, ra, dec, scale, xSize, ySize, ignore=False): """Warp and stack images @param[in] identMatrix Matrix of warp identifiers @param[in] butler Data butler @param[in] ra Right Ascension (radians) of skycell centre @param[in] dec Declination (radians) of skycell centre @param[in] scale Scale (arcsec/pixel) of skycell @param[in] xSize Size in x @param[in] ySize Size in y @param[in] ignore Ignore missing files? @output Stacked exposure """ assert identMatrix, "No identMatrix provided" skycell = self.skycell(ra, dec, scale, xSize, ySize) coaddDim = afwGeom.Extent2I(xSize, ySize) coadd = afwImage.ExposureF(coaddDim, skycell.getWcs()) weight = afwImage.ImageF(coaddDim) badpix = afwImage.MaskU.getPlaneBitMask( "EDGE") # Allow everything else through for identList in identMatrix: warp = self.warp(identList, butler, skycell, ignore=ignore) # XXX Save for later? coaddUtils.addToCoadd(coadd.getMaskedImage(), weight, warp.getMaskedImage(), badpix, 1.0) coaddUtils.setCoaddEdgeBits(coadd.getMaskedImage().getMask(), weight) coaddImage = coadd.getMaskedImage() coaddImage /= weight # XXX Coadd has NANs where weight=0 return coadd
def testRandomMap(self): """Test setCoaddEdgeBits using a random depth map """ imDim = afwGeom.Extent2I(50, 55) coaddMask = afwImage.Mask(imDim) np.random.seed(12345) depthMapArray = np.random.randint(0, 3, list((imDim[1], imDim[0]))).astype(np.uint16) depthMap = afwImage.makeImageFromArray(depthMapArray) refCoaddMask = afwImage.Mask(imDim) refCoaddMaskArray = refCoaddMask.getArray() edgeMask = afwImage.Mask.getPlaneBitMask("NO_DATA") refCoaddMaskArray |= np.array(np.where(depthMapArray > 0, 0, edgeMask), dtype=refCoaddMaskArray.dtype) coaddUtils.setCoaddEdgeBits(coaddMask, depthMap) self.assertMasksEqual(coaddMask, refCoaddMask)
def testRandomMap(self): """Test setCoaddEdgeBits using a random depth map """ imDim = geom.Extent2I(50, 55) coaddMask = afwImage.Mask(imDim) np.random.seed(12345) depthMapArray = np.random.randint(0, 3, list( (imDim[1], imDim[0]))).astype(np.uint16) depthMap = afwImage.makeImageFromArray(depthMapArray) refCoaddMask = afwImage.Mask(imDim) refCoaddMaskArray = refCoaddMask.getArray() edgeMask = afwImage.Mask.getPlaneBitMask("NO_DATA") refCoaddMaskArray |= np.array(np.where(depthMapArray > 0, 0, edgeMask), dtype=refCoaddMaskArray.dtype) coaddUtils.setCoaddEdgeBits(coaddMask, depthMap) self.assertMasksEqual(coaddMask, refCoaddMask)
def getCoadd(self): """Get the coadd exposure for all exposures you have coadded so far If all exposures in this coadd have the same-named filter then that filter is set in the coadd. Otherwise the coadd will have the default unknown filter. @warning: the Calib is not be set. """ # make a deep copy so I can scale it coaddMaskedImage = self._coadd.getMaskedImage() scaledMaskedImage = coaddMaskedImage.Factory(coaddMaskedImage, True) # set the edge pixels coadd_utils.setCoaddEdgeBits(scaledMaskedImage.getMask(), self._weightMap) # scale non-edge pixels by weight map scaledMaskedImage /= self._weightMap scaledExposure = afwImage.makeExposure(scaledMaskedImage, self._wcs) if len(self._filterDict) == 1: scaledExposure.setFilter(list(self._filterDict.values())[0]) return scaledExposure
def assemble(self, skyInfo, tempExpRefList, imageScalerList, weightList, bgInfoList=None, altMaskList=None, doClip=False, mask=None): """Assemble a coadd from input warps The assembly is performed over small areas on the image at a time, to conserve memory usage. @param skyInfo: Patch geometry information, from getSkyInfo @param tempExpRefList: List of data references to tempExp @param imageScalerList: List of image scalers @param weightList: List of weights @param bgInfoList: List of background data from background matching, or None @param altMaskList: List of alternate masks to use rather than those stored with tempExp, or None @param doClip: Use clipping when codding? @param mask: Mask to ignore when coadding @return coadded exposure """ tempExpName = self.getTempExpDatasetName() self.log.info("Assembling %s %s" % (len(tempExpRefList), tempExpName)) if mask is None: mask = self.getBadPixelMask() statsCtrl = afwMath.StatisticsControl() statsCtrl.setNumSigmaClip(self.config.sigmaClip) statsCtrl.setNumIter(self.config.clipIter) statsCtrl.setAndMask(mask) statsCtrl.setNanSafe(True) statsCtrl.setWeighted(True) statsCtrl.setCalcErrorFromInputVariance(True) for plane, threshold in self.config.maskPropagationThresholds.items(): bit = afwImage.MaskU.getMaskPlane(plane) statsCtrl.setMaskPropagationThreshold(bit, threshold) if doClip: statsFlags = afwMath.MEANCLIP else: statsFlags = afwMath.MEAN if bgInfoList is None: bgInfoList = [None]*len(tempExpRefList) if altMaskList is None: altMaskList = [None]*len(tempExpRefList) coaddExposure = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs) coaddExposure.setCalib(self.scaleZeroPoint.getCalib()) coaddExposure.getInfo().setCoaddInputs(self.inputRecorder.makeCoaddInputs()) self.assembleMetadata(coaddExposure, tempExpRefList, weightList) coaddMaskedImage = coaddExposure.getMaskedImage() subregionSizeArr = self.config.subregionSize subregionSize = afwGeom.Extent2I(subregionSizeArr[0], subregionSizeArr[1]) for subBBox in _subBBoxIter(skyInfo.bbox, subregionSize): try: self.assembleSubregion(coaddExposure, subBBox, tempExpRefList, imageScalerList, weightList, bgInfoList, altMaskList, statsFlags, statsCtrl) except Exception as e: self.log.fatal("Cannot compute coadd %s: %s" % (subBBox, e,)) coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), coaddMaskedImage.getVariance()) return coaddExposure
coaddExposure = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs) coaddExposure.setCalib(self.scaleZeroPoint.getCalib()) coaddExposure.getInfo().setCoaddInputs(self.inputRecorder.makeCoaddInputs()) self.assembleMetadata(coaddExposure, tempExpRefList, weightList) coaddMaskedImage = coaddExposure.getMaskedImage() subregionSizeArr = self.config.subregionSize subregionSize = afwGeom.Extent2I(subregionSizeArr[0], subregionSizeArr[1]) for subBBox in _subBBoxIter(skyInfo.bbox, subregionSize): try: self.assembleSubregion(coaddExposure, subBBox, tempExpRefList, imageScalerList, weightList, bgInfoList, statsFlags, statsCtrl) except Exception, e: self.log.fatal("Cannot compute coadd %s: %s" % (subBBox, e,)) coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), coaddMaskedImage.getVariance()) return coaddExposure def assembleMetadata(self, coaddExposure, tempExpRefList, weightList): """Set the metadata for the coadd This basic implementation simply sets the filter from the first input. @param coaddExposure: The target image for the coadd @param tempExpRefList: List of data references to tempExp @param weightList: List of weights """ tempExpName = self.getTempExpDatasetName() # We load a single pixel of each coaddTempExp, because we just want to get at the metadata
def main(): # try out one exposure #visits = ["0288935","0288976"] #,"0289893","0289913","0289931","0289614","0289818","0289820", "0289850","0289851","0289871","0289892", "0288935","0288976","0289016","0289056","0289161","0289202","0289243","0289284","0289368","0289409","0289450","0289493","0289573","0289656"] visits = ["0288976", "0288935"] ccds = [] exit(0) for i in range(1, 61): ccds.append(i) filterName = 'g' DATA_PATH = "/root/extra_home/lsst_data/" #spathprefix = "/home/dongfang/download/lsst_data/" spathprefix = DATA_PATH + "raw/" #calexpsloc = "/home/dongfang/download/lsst_data/calexps/" calexpsloc = DATA_PATH + "calexps/" #coaddloc = "/home/dongfang/download/lsst_data/coadds/" coaddloc = DATA_PATH + "coadds/" #mergecoaddloc = "/home/dongfang/download/lsst_data/merge/" mergecoaddloc = DATA_PATH + "merge/" # Characterize Image charImageConfig = CharacterizeImageConfig() charImage = CharacterizeImageTask() calibrateConfig = CalibrateConfig(doPhotoCal=False, doAstrometry=False) calibrateTask = CalibrateTask(config=calibrateConfig) makeCTEConfig = MakeCoaddTempExpConfig() makeCTE = MakeCoaddTempExpTask(config=makeCTEConfig) newSkyMapConfig = skymap.discreteSkyMap.DiscreteSkyMapConfig( projection='STG', decList=[-4.9325280994132905], patchInnerDimensions=[2000, 2000], radiusList=[4.488775723429071], pixelScale=0.333, rotation=0.0, patchBorder=100, raList=[154.10660740464786], tractOverlap=0.0) hits_skymap = skymap.discreteSkyMap.DiscreteSkyMap(config=newSkyMapConfig) tract = hits_skymap[0] coaddTempDict = {} calibResDict = {} f = open("log.txt", 'wb') start = datetime.datetime.now() #process CCDs to create calexps. for v in visits: for ccd in ccds: visit = int(v) filename = "instcal" + v + "." + str(ccd) + ".fits" calexpfn = calexpsloc + v + "/" + filename source = spathprefix + v + "/" + filename exposure = afwImg.ExposureF(source) try: # Characterize Image charRes = charImage.characterize(exposure, exposureIdInfo=None, background=None) except: f.write("DFZ DEBUG at charRes: errors in visit " + v + ", ccd " + str(ccd) + "\n") try: # Caliberate Image calibRes = calibrateTask.calibrate( charRes.exposure, exposureIdInfo=None, background=charRes.background, icSourceCat=None) except: f.write("DFZ DEBUG at calibRes: errors in visit " + v + ", ccd " + str(ccd) + "\n") try: #write out calexps calibRes.exposure.writeFits(calexpfn) #calbresDict.append((v,ccd),calibRes) except: f.write("DFZ DEBUG at calibRes.exposure: errors in visit " + v + ", ccd " + str(ccd) + "\n") end = datetime.datetime.now() d = end - start f.write("time for creating calexps: ") f.write(str(d.total_seconds())) f.write("\n") #time for creating co-add tempexps. start = datetime.datetime.now() # map calexps to patch-ids visit = visits[0] ccdsPerPatch = [] for ccd in ccds: filename = "instcal" + visit + "." + str(ccd) + ".fits" source = calexpsloc + visit + "/" + filename exposure = afwImg.ExposureF(source) bbox = exposure.getBBox() wcs = exposure.getWcs() corners = bbox.getCorners() xIndexMax, yIndexMax = tract.findPatch( wcs.pixelToSky(corners[0][0], corners[0][1])).getIndex() xIndexMin, yIndexMin = tract.findPatch( wcs.pixelToSky(corners[2][0], corners[2][1])).getIndex() yy = range(yIndexMin, yIndexMax + 1) xx = range(xIndexMin, xIndexMax + 1) for yIdx in yy: for xIdx in xx: ccdsPerPatch.append((ccd, (xIdx, yIdx))) print len(ccdsPerPatch) #import cPickle #cPickle.dump(open("ccdsinpatch.p",'wb'),ccdsPerPatch) # import cPickle # f = open("ccdsInPatch.p",'wb') # cPickle.dump(ccdsInPatch,f) #import cPickle #ccdsInPatch = cPickle.load(open("ccdsInPatch.p",'rb')) df = pd.DataFrame(ccdsPerPatch) dfgby = df.groupby(1) makeCTEConfig = MakeCoaddTempExpConfig() makeCTE = MakeCoaddTempExpTask(config=makeCTEConfig) coaddTempExpDict = {} for visit in visits: for a in dfgby.indices: coaddTempExpDict[a] = {} xInd = a[0] yInd = a[1] skyInfo = getSkyInfo(hits_skymap, xInd, yInd) v = int(visit) coaddTempExp = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs) coaddTempExp.getMaskedImage().set( numpy.nan, afwImage.MaskU.getPlaneBitMask("NO_DATA"), numpy.inf) totGoodPix = 0 didSetMetadata = False modelPsf = makeCTEConfig.modelPsf.apply( ) if makeCTEConfig.doPsfMatch else None setInputRecorder = False for b in dfgby.get_group(a)[0].ravel(): print a print b if not setInputRecorder: ccdsinPatch = len(dfgby.get_group(a)[0].ravel()) try: inputRecorder = makeCTE.inputRecorder.makeCoaddTempExpRecorder( v, ccdsinPatch) except: f.write("DFZ DEBUG at inputRecorder\n") setInputRecorder = True numGoodPix = 0 ccd = b filename = "instcal" + visit + "." + str(ccd) + ".fits" source = calexpsloc + visit + "/" + filename calExp = afwImg.ExposureF(source) ccdId = calExp.getId() warpedCcdExp = makeCTE.warpAndPsfMatch.run( calExp, modelPsf=modelPsf, wcs=skyInfo.wcs, maxBBox=skyInfo.bbox).exposure if didSetMetadata: mimg = calExp.getMaskedImage() mimg *= (coaddTempExp.getCalib().getFluxMag0()[0] / calExp.getCalib().getFluxMag0()[0]) del mimg numGoodPix = coaddUtils.copyGoodPixels( coaddTempExp.getMaskedImage(), warpedCcdExp.getMaskedImage(), makeCTE.getBadPixelMask()) totGoodPix += numGoodPix if numGoodPix > 0 and not didSetMetadata: coaddTempExp.setCalib(warpedCcdExp.getCalib()) coaddTempExp.setFilter(warpedCcdExp.getFilter()) didSetMetadata = True inputRecorder.addCalExp(calExp, ccdId, numGoodPix) ##### End loop over ccds here: inputRecorder.finish(coaddTempExp, totGoodPix) if totGoodPix > 0 and didSetMetadata: coaddTempExp.setPsf( modelPsf if makeCTEConfig.doPsfMatch else CoaddPsf( inputRecorder.coaddInputs.ccds, skyInfo.wcs)) coaddTempExpDict[a][v] = coaddTempExp coaddfilename = coaddloc + visit + "/" + "instcal" + visit + "." + str( xInd) + "_" + str(yInd) + ".fits" coaddTempExp.writeFits(coaddfilename) end = datetime.datetime.now() d = end - start f.write("time for creating co-add tempexps:\n ") f.write(str(d.total_seconds())) f.write("\n") #DFZ: stop here exit(0) start = datetime.datetime.now() config = AssembleCoaddConfig() assembleTask = AssembleCoaddTask(config=config) mergcoadds = {} for a in dfgby.indices: ccdsinPatch = len(dfgby.get_group(a)[0].ravel()) xInd = a[0] yInd = a[1] imageScalerRes = prepareInputs(coaddTempExpDict[a].values(), coaddTempExpDict[a].keys(), assembleTask) mask = None doClip = False if mask is None: mask = assembleTask.getBadPixelMask() statsCtrl = afwMath.StatisticsControl() statsCtrl.setNumSigmaClip(assembleTask.config.sigmaClip) statsCtrl.setNumIter(assembleTask.config.clipIter) statsCtrl.setAndMask(mask) statsCtrl.setNanSafe(True) statsCtrl.setWeighted(True) statsCtrl.setCalcErrorFromInputVariance(True) for plane, threshold in assembleTask.config.maskPropagationThresholds.items( ): bit = afwImage.MaskU.getMaskPlane(plane) statsCtrl.setMaskPropagationThreshold(bit, threshold) if doClip: statsFlags = afwMath.MEANCLIP else: statsFlags = afwMath.MEAN coaddExposure = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs) coaddExposure.setCalib(assembleTask.scaleZeroPoint.getCalib()) coaddExposure.getInfo().setCoaddInputs( assembleTask.inputRecorder.makeCoaddInputs()) #remember to set metadata if you want any hope of running detection and measurement on this coadd: #self.assembleMetadata(coaddExposure, tempExpRefList, weightList) #most important thing is the psf coaddExposure.setFilter(coaddTempExpDict[a].values()[0].getFilter()) coaddInputs = coaddExposure.getInfo().getCoaddInputs() for tempExp, weight in zip(coaddTempExpDict[a].values(), imageScalerRes.weightList): assembleTask.inputRecorder.addVisitToCoadd(coaddInputs, tempExp, weight) #takes numCcds as argument coaddInputs.ccds.reserve(ccdsinPatch) coaddInputs.visits.reserve(len(imageScalerRes.dataIdList)) psf = measAlg.CoaddPsf(coaddInputs.ccds, coaddExposure.getWcs()) coaddExposure.setPsf(psf) maskedImageList = afwImage.vectorMaskedImageF() coaddMaskedImage = coaddExposure.getMaskedImage() for dataId, imageScaler, exposure in zip( imageScalerRes.dataIdList, imageScalerRes.imageScalerList, coaddTempExpDict[a].values()): print dataId, imageScaler, exposure maskedImage = exposure.getMaskedImage() imageScaler.scaleMaskedImage(maskedImage) maskedImageList.append(maskedImage) maskedImage = afwMath.statisticsStack(maskedImageList, statsFlags, statsCtrl, imageScalerRes.weightList) coaddMaskedImage.assign(maskedImage, skyInfo.bbox) coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), coaddMaskedImage.getVariance()) # write out Coadd! mergefilename = mergecoaddloc + str(xInd) + "_" + str(yInd) + ".fits" mergcoadds[a] = coaddExposure coaddExposure.writeFits(mergefilename) end = datetime.datetime.now() d = end - start f.write("time for creating merged co-adds:\n ") f.write(str(d.total_seconds())) f.write("\n") start = datetime.datetime.now() config = DetectCoaddSourcesConfig() detectCoaddSources = DetectCoaddSourcesTask(config=config) for a in dfgby.indices: # Detect on Coadd exp = mergcoadds[a] detRes = detectCoaddSources.runDetection(exp, idFactory=None) end = datetime.datetime.now() d = end - start f.write("time for detecting sources:\n ") f.write(str(d.total_seconds())) f.close()
print " Compute coadd component" coaddComponent = coaddKaiser.CoaddComponent(exposure, psfKernel, normalizePsf) print " Divide exposure by sigma squared = %s" % (coaddComponent.getSigmaSq(),) blurredExposure = coaddComponent.getBlurredExposure() blurredMaskedImage = blurredExposure.getMaskedImage() sigmaSq = coaddComponent.getSigmaSq() if saveImages: blurredExposure.writeFits("blurred%s" % (fileName,)) blurredMaskedImage /= sigmaSq if saveImages: blurredExposure.writeFits("scaledBlurred%s" % (fileName,)) print " Remap blurred exposure to match coadd WCS" remappedBlurredMaskedImage = afwImage.MaskedImageD( coaddExposure.getWidth(), coaddExposure.getHeight()) remappedBlurredExposure = afwImage.ExposureD(remappedBlurredMaskedImage, coaddWcs) if saveImages: remappedBlurredExposure.writeFits("remappedBlurred%s" % (fileName,)) nGoodPix = afwMath.warpExposure(remappedBlurredExposure, blurredExposure, afwMath.LanczosWarpingKernel(3)) nPix = coaddExposure.getWidth() * coaddExposure.getHeight() print " Remapped image has %d good pixels (%0.0f %%)" % (nGoodPix, 100 * nGoodPix / float(nPix)) print " Add remapped blurred exposure to coadd and save updated coadd exposure" coaddUtils.addToCoadd(coaddMaskedImage, depthMap, remappedBlurredExposure.getMaskedImage(), coaddMask) coaddExposure.writeFits(outName) depthMap.writeFits(depthOutName) coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), depthMap) coaddExposure.writeFits(outName)
def mergeCoadd(a, dfgby_raveled, coaddTempExpDict_a, hits_skymap): from lsst.pipe.tasks.assembleCoadd import AssembleCoaddTask, AssembleCoaddConfig import lsst.afw.math as afwMath import lsst.afw.image as afwImage import lsst.coadd.utils as coaddUtils import lsst.meas.algorithms as measAlg config = AssembleCoaddConfig() assembleTask = AssembleCoaddTask(config=config) ccdsinPatch = len(dfgby_raveled) # len(dfgby.get_group(a)[0].ravel()) xInd = a[0] yInd = a[1] print xInd, yInd if xInd == 0 and yInd == 0: return None imageScalerRes = prepareInputs(coaddTempExpDict_a.values(), coaddTempExpDict_a.keys(), assembleTask) if imageScalerRes is None: return None mask = assembleTask.getBadPixelMask() statsCtrl = afwMath.StatisticsControl() statsCtrl.setNumSigmaClip(assembleTask.config.sigmaClip) statsCtrl.setNumIter(assembleTask.config.clipIter) statsCtrl.setAndMask(mask) statsCtrl.setNanSafe(True) statsCtrl.setWeighted(True) statsCtrl.setCalcErrorFromInputVariance(True) for plane, threshold in assembleTask.config.maskPropagationThresholds.items( ): bit = afwImage.MaskU.getMaskPlane(plane) statsCtrl.setMaskPropagationThreshold(bit, threshold) statsFlags = afwMath.MEAN skyInfo = getSkyInfo(hits_skymap, xInd, yInd) coaddExposure = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs) coaddExposure.setCalib(assembleTask.scaleZeroPoint.getCalib()) coaddExposure.getInfo().setCoaddInputs( assembleTask.inputRecorder.makeCoaddInputs()) # remember to set metadata if you want any hope of running detection and measurement on this coadd: # self.assembleMetadata(coaddExposure, tempExpRefList, weightList) # most important thing is the psf coaddExposure.setFilter(coaddTempExpDict_a.values()[0].getFilter()) coaddInputs = coaddExposure.getInfo().getCoaddInputs() for tempExp, weight in zip(coaddTempExpDict_a.values(), imageScalerRes.weightList): assembleTask.inputRecorder.addVisitToCoadd(coaddInputs, tempExp, weight) # takes numCcds as argument coaddInputs.ccds.reserve(ccdsinPatch) coaddInputs.visits.reserve(len(imageScalerRes.dataIdList)) psf = measAlg.CoaddPsf(coaddInputs.ccds, coaddExposure.getWcs()) coaddExposure.setPsf(psf) maskedImageList = afwImage.vectorMaskedImageF() coaddMaskedImage = coaddExposure.getMaskedImage() for dataId, imageScaler, exposure in zip(imageScalerRes.dataIdList, imageScalerRes.imageScalerList, coaddTempExpDict_a.values()): print dataId, imageScaler, exposure maskedImage = exposure.getMaskedImage() imageScaler.scaleMaskedImage(maskedImage) maskedImageList.append(maskedImage) maskedImage = afwMath.statisticsStack(maskedImageList, statsFlags, statsCtrl, imageScalerRes.weightList) coaddMaskedImage.assign(maskedImage, skyInfo.bbox) coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), coaddMaskedImage.getVariance()) return coaddExposure
maskedImageView <<= afwImage.MaskedImageF( expMeta.path, 0, dumPS, overlapBBox, afwImage.PARENT) maskedImageList.append(maskedImage) weightList.append(expMeta.weight) try: coaddSubregion = afwMath.statisticsStack(maskedImageList, afwMath.MEANCLIP, statsCtrl, weightList) coaddView <<= coaddSubregion except Exception, e: print "Outlier rejection failed; setting EDGE mask: %s" % (e, ) raise # setCoaddEdgeBits does this later, so no need to do it here coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), coaddMaskedImage.getVariance()) return coaddExposure if __name__ == "__main__": algName = "outlierRejectedCoadd" pexLog.Trace.setVerbosity('lsst.coadd', 3) pexLog.Trace.setVerbosity('lsst.ip.diffim', 1) parser = lsst.pipette.coaddOptions.CoaddOptionParser() parser.add_option( "--fwhm", dest="fwhm", type="float", default=0.0,
blurredMaskedImage = blurredExposure.getMaskedImage() sigmaSq = coaddComponent.getSigmaSq() if saveImages: blurredExposure.writeFits("blurred%s" % (fileName, )) blurredMaskedImage /= sigmaSq if saveImages: blurredExposure.writeFits("scaledBlurred%s" % (fileName, )) print " Remap blurred exposure to match coadd WCS" remappedBlurredMaskedImage = afwImage.MaskedImageD( coaddExposure.getWidth(), coaddExposure.getHeight()) remappedBlurredExposure = afwImage.ExposureD( remappedBlurredMaskedImage, coaddWcs) if saveImages: remappedBlurredExposure.writeFits("remappedBlurred%s" % (fileName, )) nGoodPix = afwMath.warpExposure(remappedBlurredExposure, blurredExposure, afwMath.LanczosWarpingKernel(3)) nPix = coaddExposure.getWidth() * coaddExposure.getHeight() print " Remapped image has %d good pixels (%0.0f %%)" % ( nGoodPix, 100 * nGoodPix / float(nPix)) print " Add remapped blurred exposure to coadd and save updated coadd exposure" coaddUtils.addToCoadd(coaddMaskedImage, depthMap, remappedBlurredExposure.getMaskedImage(), coaddMask) coaddExposure.writeFits(outName) depthMap.writeFits(depthOutName) coaddUtils.setCoaddEdgeBits(coaddMaskedImage.getMask(), depthMap) coaddExposure.writeFits(outName)