Example #1
0
def run():
    if len(sys.argv) < 2:
        srcExposure = afwImage.ExposureF(InputExposurePath)
        if True:
            bbox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2000, 2000))
            srcExposure = afwImage.ExposureF(srcExposure, bbox, afwImage.LOCAL, False)
    else:
        srcExposure = afwImage.ExposureF(sys.argv[1])
    srcWcs = srcExposure.getWcs()
    srcDim = srcExposure.getDimensions()
    srcCtrInd = [int(d / 2) for d in srcDim]
    
    # make the destination exposure small enough that even after rotation and offset
    # (by reasonable amounts) there are no edge pixels
    destDim = afwGeom.Extent2I([int(sd * 0.5) for sd in srcDim])
    destExposure = afwImage.ExposureF(destDim)
    destCtrInd = [int(d / 2) for d in destDim]
    
    print "Warping", InputExposurePath
    print "Source (sub)image size:", srcDim
    print "Destination image size:", destDim
    print
    
    print "test# interp  scaleFac     skyOffset     rotAng   kernel   goodPix time/iter"
    print '       (pix)              (RA, Dec ")    (deg)                      (sec)'
    testNum = 1
    for interpLength in (0, 1, 5, 10):
        for scaleFac in (1.2,): # (1.0, 1.5):
            for skyOffsetArcSec in ((0.0, 0.0),): #  ((0.0, 0.0), (10.5, -5.5)):
                skyOffset = [so / 3600.0 for so in skyOffsetArcSec]
                for rotAng, kernelName in (
                    (0.0, "bilinear"),
                    (0.0, "lanczos2"),
                    (0.0, "lanczos3"),
                    (45.0, "lanczos3"),
                ):
                    destWcs = makeWcs(
                        projName = "TAN",
                        destCtrInd = destCtrInd,
                        skyOffset = skyOffset,
                        rotAng = rotAng,
                        scaleFac = scaleFac,
                        srcWcs = srcWcs,
                        srcCtrInd = srcCtrInd,
                    )
                    destExposure.setWcs(destWcs)
                    warpingKernel = afwMath.makeWarpingKernel(kernelName)
                    dTime, nIter, goodPix = timeWarp(destExposure, srcExposure, warpingKernel, interpLength)
                    print "%4d  %5d  %8.1f  %6.1f, %6.1f  %7.1f %10s %8d %6.2f" % (
                        testNum, interpLength, scaleFac, skyOffsetArcSec[0], skyOffsetArcSec[1],
                        rotAng, kernelName, goodPix, dTime/float(nIter))
                    
                    if SaveImages:
                        destExposure.writeFits("warpedExposure%03d.fits" % (testNum,))
                    testNum += 1
Example #2
0
def main():
    DefDataDir = eups.productDir("afwdata") or ""
    
    DefOriginalExposurePath = os.path.join(DefDataDir, "med")
    DefWcsImageOrExposurePath = os.path.join(DefDataDir, "medswarp1lanczos4.fits")
    DefOutputExposurePath = "warpedExposure"
    DefKernel = "lanczos4"
    DefVerbosity = 6 # change to 0 once this all works to hide all messages
    
    usage = """usage: %%prog [options] [originalExposure [warpedWcsImageOrExposure [outputExposure]]]

    Computes outputExposure = originalExposure warped to match warpedWcsExposure's WCS and size

    Note:
    - exposure arguments are paths to Exposure fits files;
      they must NOT include the final _img.fits|_var.fits|_msk.fits
      if warpedWcsImageOrExposure ends in .fits then it specifies an image
    - default originalExposure = %s
    - default warpedWcsImageOrExposure = %s
    - default outputExposure = %s
    """ % (DefOriginalExposurePath, DefWcsImageOrExposurePath, DefOutputExposurePath)
    
    parser = optparse.OptionParser(usage)
    parser.add_option("-k", "--kernel",
                      type=str, default=DefKernel,
                      help="kernel type: bilinear or lancszosN where N = order; default=%s" % (DefKernel,))
    parser.add_option("-v", "--verbosity",
                      type=int, default=DefVerbosity,
                      help="verbosity of diagnostic trace messages; 1 for just warnings, more for more" + \
                      " information; default=%s" % (DefVerbosity,))
    
    (opt, args) = parser.parse_args()
    
    kernelName = opt.kernel.lower()
    kernel = afwMath.makeWarpingKernel(kernelName)
    
    def getArg(ind, defValue):
        if ind < len(args):
            return args[ind]
        return defValue
    
    originalExposurePath = getArg(0, DefOriginalExposurePath)
    warpedWcsImageOrExposurePath = getArg(1, DefWcsImageOrExposurePath)
    outputExposurePath = getArg(2, DefOutputExposurePath)
    print "Remapping masked image  ", originalExposurePath
    print "to match wcs and size of", warpedWcsImageOrExposurePath
    print "using", kernelName, "kernel"
    
    originalExposure = afwImage.ExposureF(originalExposurePath)
    
    if warpedWcsImageOrExposurePath.lower().endswith(".fits"):
        # user specified an image, not an exposure
        warpedDI = afwImage.DecoratedImageF(warpedWcsImageOrExposurePath)
        warpedWcs = afwImage.makeWcs(warpedDI.getMetadata())
        warpedMI = afwImage.MaskedImageF(afwGeom.Extent2I(warpedDI.getWidth(), warpedDI.getHeight()))
        warpedExposure = afwImage.ExposureF(warpedMI, warpedWcs)
    else:
        warpedExposure = afwImage.ExposureF(warpedWcsImageOrExposurePath)
    
    if opt.verbosity > 0:
        print "Verbosity =", opt.verbosity
        lsst.pex.logging.Trace_setVerbosity("lsst.afw.math", opt.verbosity)
    
    numGoodPixels = afwMath.warpExposure(warpedExposure, originalExposure, kernel)
    print "Warped exposure has %s good pixels" % (numGoodPixels)
    
    print "Writing warped exposure to %s" % (outputExposurePath,)
    warpedExposure.writeFits(outputExposurePath)
Example #3
0
    def compareToSwarp(self, kernelName, 
                       useWarpExposure=True, useSubregion=False, useDeepCopy=False,
                       interpLength=10, cacheSize=100000,
                       rtol=4e-05, atol=1e-2):
        """Compare warpExposure to swarp for given warping kernel.
        
        Note that swarp only warps the image plane, so only test that plane.
        
        Inputs:
        - kernelName: name of kernel in the form used by afwImage.makeKernel
        - useWarpExposure: if True, call warpExposure to warp an ExposureF,
            else call warpImage to warp an ImageF
        - useSubregion: if True then the original source exposure (from which the usual
            test exposure was extracted) is read and the correct subregion extracted
        - useDeepCopy: if True then the copy of the subimage is a deep copy,
            else it is a shallow copy; ignored if useSubregion is False
        - interpLength: interpLength argument for lsst.afw.math.warpExposure
        - cacheSize: cacheSize argument for lsst.afw.math.SeparableKernel.computeCache;
            0 disables the cache
            10000 gives some speed improvement but less accurate results (atol must be increased)
            100000 gives better accuracy but no speed improvement in this test
        - rtol: relative tolerance as used by numpy.allclose
        - atol: absolute tolerance as used by numpy.allclose
        """
        warpingKernel = afwMath.makeWarpingKernel(kernelName)
        warpingKernel.computeCache(cacheSize)

        if useSubregion:
            originalFullExposure = afwImage.ExposureF(originalExposurePath)
            # "medsub" is a subregion of med starting at 0-indexed pixel (40, 150) of size 145 x 200
            bbox = afwGeom.Box2I(afwGeom.Point2I(40, 150), afwGeom.Extent2I(145, 200))
            originalExposure = afwImage.ExposureF(originalFullExposure, bbox, afwImage.LOCAL, useDeepCopy)
            swarpedImageName = "medsubswarp1%s.fits" % (kernelName,)
        else:
            originalExposure = afwImage.ExposureF(originalExposurePath)
            swarpedImageName = "medswarp1%s.fits" % (kernelName,)

        swarpedImagePath = os.path.join(dataDir, swarpedImageName)
        swarpedDecoratedImage = afwImage.DecoratedImageF(swarpedImagePath)
        swarpedImage = swarpedDecoratedImage.getImage()
        swarpedMetadata = swarpedDecoratedImage.getMetadata()
        warpedWcs = afwImage.makeWcs(swarpedMetadata)
        
        if useWarpExposure:
            # path for saved afw-warped image
            afwWarpedImagePath = "afwWarpedExposure1%s" % (kernelName,)
    
            afwWarpedMaskedImage = afwImage.MaskedImageF(swarpedImage.getDimensions())
            afwWarpedExposure = afwImage.ExposureF(afwWarpedMaskedImage, warpedWcs)
            afwMath.warpExposure(afwWarpedExposure, originalExposure, warpingKernel, interpLength)
            if SAVE_FITS_FILES:
                afwWarpedExposure.writeFits(afwWarpedImagePath)
            if display:
                ds9.mtv(afwWarpedExposure, frame=1, title="Warped")
    
            afwWarpedMask = afwWarpedMaskedImage.getMask()
            edgeBitMask = afwWarpedMask.getPlaneBitMask("EDGE")
            if edgeBitMask == 0:
                self.fail("warped mask has no EDGE bit")
            afwWarpedMaskedImageArrSet = afwWarpedMaskedImage.getArrays()
            afwWarpedMaskArr = afwWarpedMaskedImageArrSet[1]
    
            swarpedMaskedImage = afwImage.MaskedImageF(swarpedImage)
            swarpedMaskedImageArrSet = swarpedMaskedImage.getArrays()

            if display:
                ds9.mtv(swarpedMaskedImage, frame=2, title="SWarped")
            
            errStr = imageTestUtils.maskedImagesDiffer(afwWarpedMaskedImageArrSet, swarpedMaskedImageArrSet,
                doImage=True, doMask=False, doVariance=False, skipMaskArr=afwWarpedMaskArr,
                rtol=rtol, atol=atol)
            if errStr:
                if SAVE_FAILED_FITS_FILES:
                    afwWarpedExposure.writeFits(afwWarpedImagePath)
                    print "Saved failed afw-warped exposure as: %s" % (afwWarpedImagePath,)
                self.fail("afw and swarp %s-warped %s (ignoring bad pixels)" % (kernelName, errStr))
        else:
            # path for saved afw-warped image
            afwWarpedImagePath = "afwWarpedImage1%s.fits" % (kernelName,)
    
            afwWarpedImage = afwImage.ImageF(swarpedImage.getDimensions())
            originalImage = originalExposure.getMaskedImage().getImage()
            originalWcs = originalExposure.getWcs()
            afwMath.warpImage(afwWarpedImage, warpedWcs, originalImage,
                              originalWcs, warpingKernel, interpLength)
            if display:
                ds9.mtv(afwWarpedImage, frame=1, title="Warped")
                ds9.mtv(swarpedImage, frame=2, title="SWarped")
                diff = swarpedImage.Factory(swarpedImage, True)
                diff -= afwWarpedImage
                ds9.mtv(diff, frame=3, title="swarp - afw")
            if SAVE_FITS_FILES:
                afwWarpedImage.writeFits(afwWarpedImagePath)
            
            afwWarpedImageArr = afwWarpedImage.getArray()
            swarpedImageArr = swarpedImage.getArray()
            edgeMaskArr = numpy.isnan(afwWarpedImageArr)
            errStr = imageTestUtils.imagesDiffer(afwWarpedImageArr, swarpedImageArr,
                skipMaskArr=edgeMaskArr, rtol=rtol, atol=atol)
            if errStr:
                if SAVE_FAILED_FITS_FILES:
                    # save the image anyway
                    afwWarpedImage.writeFits(afwWarpedImagePath)
                    print "Saved failed afw-warped image as: %s" % (afwWarpedImagePath,)
                self.fail("afw and swarp %s-warped images do not match (ignoring NaN pixels): %s" % \
                    (kernelName, errStr))