LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Background

Using the Background class; the code's in estimateBackground.py.

The basic strategy is

Start by importing needed packages

import os
import lsst.utils
import lsst.afw.image as afwImage
import lsst.afw.math as afwMath
Read an Image
def getImage():
imagePath = os.path.join(lsst.utils.getPackageDir("afwdata"),
"DC3a-Sim", "sci", "v5-e0", "v5-e0-c011-a00.sci_img.fits")
return afwImage.MaskedImageF(imagePath)

image = getImage()

We'll do the simplest case first. Start by creating a BackgroundControl object that's used to configure the algorithm that's used to estimate the background levels.

def simpleBackground(image):
binsize = 128
nx = int(image.getWidth()/binsize) + 1
ny = int(image.getHeight()/binsize) + 1
bctrl = afwMath.BackgroundControl(nx, ny)
Estimate the background levels
bkgd = afwMath.makeBackground(image, bctrl)

We can ask for the resulting heavily-binned image (but only after casting the base class Background to one that includes such an image, a BackgroundMI)

statsImage = afwMath.cast_BackgroundMI(bkgd).getStatsImage()

or subtract this background estimate from the input image, interpolating our estimated values using a NATURAL_SPLINE

image -= bkgd.getImageF(afwMath.Interpolate.NATURAL_SPLINE)

We actually have a lot more control over the whole process than that. We'll start by building a StatisticsControl object, and telling it our desires:

sctrl = afwMath.StatisticsControl()
sctrl.setNumSigmaClip(3)
sctrl.setNumIter(4)
sctrl.setAndMask(afwImage.MaskU.getPlaneBitMask(["INTRP", "EDGE"]))
sctrl.setNoGoodPixelsMask(afwImage.MaskU.getPlaneBitMask("BAD"))
sctrl.setNanSafe(True)
(actually I could have set most of those options in the ctor)

We then build the BackgroundControl object, passing it sctrl and also my desired statistic.

bctrl = afwMath.BackgroundControl(nx, ny, sctrl, afwMath.MEANCLIP)

Making the Background is the same as before

bkgd = afwMath.makeBackground(image, bctrl)

We can get the statistics image, and its variance:

statsImage = afwMath.cast_BackgroundMI(bkgd).getStatsImage()
ds9.mtv(statsImage.getVariance())

Finally, we can interpolate in a number of ways, e.g.

bkdgImages = dict(SPLINE = bkgd.getImageF(afwMath.Interpolate.NATURAL_SPLINE),
LINEAR = bkgd.getImageF(afwMath.Interpolate.LINEAR))

If we wish to use an approximation to the background (instead of interpolating the values) we proceed slightly differently. First we need an object to specify our interpolation strategy:

order = 2
actrl = afwMath.ApproximateControl(afwMath.ApproximateControl.CHEBYSHEV, order, order)
and then we can Approximate the Background with (in this case a Chebyshev polynomial)
approx = bkgd.getApproximate(actrl)

We can get an Image or MaskedImage from approx with

approx.getImage()
approx.getMaskedImage()
or truncate the expansion (as is often a good idea with a Chebyshev expansion); in this case to order one lower than the original fit.
approx.getImage(order - 1)