Issues with Image Subtraction

Hi everyone,

So I think I’ve figured it out. I wanted to share my solution in case anyone else wants to do this. The first step involves obtaining the sources in the visit image and making sure that the final SourceCatalog has the right columns.

from lsst.meas.algorithms.detection import SourceDetectionTask
from lsst.meas.deblender import SourceDeblendTask
from lsst.meas.base import SingleFrameMeasurementTask
import lsst.daf.base as dafBase


calexp.mask.removeAndClearMaskPlane('DETECTED')
schema = afwTable.SourceTable.makeMinimalSchema()
raerr = schema.addField("coord_raErr", type="F")
decerr = schema.addField("coord_decErr", type="F")
isPrimary = schema.addField("detect_isPrimary", type = 'F')
skySource = schema.addField("sky_source", type = 'Flag')
algMetadata = dafBase.PropertyList()

config = SourceDetectionTask.ConfigClass()
config.thresholdValue = 5
config.thresholdType = "stdev"
sourceDetectionTask = SourceDetectionTask(schema=schema, config=config)
sourceDeblendTask = SourceDeblendTask(schema=schema)
config = SingleFrameMeasurementTask.ConfigClass()
sourceMeasurementTask = SingleFrameMeasurementTask(schema=schema,
                                                   config=config,
                                                   algMetadata=algMetadata)
tab = afwTable.SourceTable.make(schema)

result = sourceDetectionTask.run(tab, calexp)
sources = result.sources
sourceDeblendTask.run(calexp, sources)
sourceMeasurementTask.run(measCat=sources, exposure=calexp)
sources = sources.copy(True)

sky_source_key = schema["sky_source"].asKey()
num_sources = len(sources)
sky_indices = np.random.choice(num_sources, size=int(0.5 * num_sources), replace=False)
for i, record in enumerate(sources):
    record.set(sky_source_key, i in sky_indices)

One caveat is that in the last bit, I set some sources as a sky_source. I’m currently doing this at random, so this probably can be improved… The second step involves warping the template so that it is aligned with the visit image.

import lsst.afw.geom as afwGeom
import lsst.afw.math as afwMath
import lsst.meas.algorithms as measAlgorithms
from lsst.ip.diffim import GetTemplateTask


config = afwMath.Warper.ConfigClass()
config.warpingKernelName = "lanczos5"
warper = afwMath.Warper.fromConfig(config)


bbox = calexp.getBBox()
xyTransform = afwGeom.makeWcsPairTransform(template.wcs,calexp.wcs)
warpedPsf = measAlgorithms.WarpedPsf(template.psf, xyTransform)
warped = warper.warpExposure(calexp.wcs, template, destBBox = bbox)
warped.setPsf(warpedPsf)
warped = copy.deepcopy(warped)

And then finally, do the actual subtraction:

from lsst.ip.diffim.subtractImages import AlardLuptonSubtractTask, AlardLuptonSubtractConfig

config = AlardLuptonSubtractConfig()
alTask = AlardLuptonSubtractTask(config=config)
diff = alTask.run(warped, calexp, sources)

One last note is that, for some reason I don’t fully understand, it is important to find the sources first, and then warp the template. Otherwise, when subtracting, it will complain that the warped template has the OTHERDET mask plane (see the error a few posts above). This code seems to run without errors, although it does feel a bit hacky. And I’m not super confident it actually reproduces the same difference image, but I wanted to share what I had nonetheless. Any comments or suggestions to improve this code are very welcome.