LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
Background

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

The basic strategy is

Start by importing needed packages

from __future__ import division
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)