PSF matching of ExposureF image cutouts -- problem with xy0

I was trying to do PSF matching of image cutouts that I made for HSC coadd images but I encountered some error message:

python modelPsfMatchTask.py --template 'stamp-i.fits' --science 'stamp-y.fits’

The error message.

psfMatch INFO: compute Psf-matching kernel
Traceback (most recent call last):
  File "modelPsfMatchTask.py", line 160, in <module>
    run(args)
  File "modelPsfMatchTask.py", line 130, in run
    result = psfMatchTask.run(templateExp, scienceExp)
  File "modelPsfMatchTask.py", line 45, in run
    return ModelPsfMatchTask.run(self, scienceExp, templateExp.getPsf())
  File "/Users/aisun/anaconda/envs/lsst/opt/lsst/pipe_base/python/lsst/pipe/base/timer.py", line 121, in wrapper
    res = func(self, *args, **keyArgs)
  File "/Users/aisun/anaconda/envs/lsst/opt/lsst/ip_diffim/python/lsst/ip/diffim/modelPsfMatch.py", line 278, in run
    psfAttr1 = measAlg.PsfAttributes(exposure.getPsf(), width//2, height//2)
  File "/Users/aisun/anaconda/envs/lsst/opt/lsst/meas_algorithms/python/lsst/meas/algorithms/algorithmsLib.py", line 788, in __init__
    this = _algorithmsLib.new_PsfAttributes(*args)
lsst.pex.exceptions.wrappers.InvalidParameterError: 
  File "src/CoaddPsf.cc", line 259, in virtual std::shared_ptr<afw::detection::Psf::Image> lsst::meas::algorithms::CoaddPsf::doComputeKernelImage(const afw::geom::Point2D &, const afw::image::Color &) const
    Cannot compute CoaddPsf at point (21, 21); no input images at that point. {0}
lsst::pex::exceptions::InvalidParameterError: 'Cannot compute CoaddPsf at point (21, 21); no input images at that point.' 

This is how I make the image cutouts:

import lsst.afw.image as afwImage
import lsst.afw.coord as afwCoord
import lsst.afw.geom as afwGeom

def make_cutout(exp, ra, dec, npix=128):
	""" 
	Params
	------
	exp (afwImage.ExposureF object)
	ra (float)
	dec (float)
	npix=128 (int)
	"""
	lsstwcs = exp.getWcs()
	pointCoord = afwCoord.IcrsCoord(afwGeom.Angle(ra, afwGeom.degrees), afwGeom.Angle(dec, afwGeom.degrees))
	x, y = lsstwcs.skyToPixel(pointCoord)
	corner = afwGeom.Point2I(int(x - npix // 2), int(y - npix // 2))
	bbox = afwGeom.Box2I(afwGeom.Point2I(corner), afwGeom.Extent2I(npix, npix))
	stamp = exp.Factory(exp, bbox, afwImage.PARENT, False)

	return stamp

It might be that the xy0 of the image cutouts is different from what the psfmatching task expects. Could someone point out how could one fix this? Any suggestion is greatly appreciated.

Is it possible that the coadd inputs haven’t been copied over by the Exposure constructor?

The short answer is that is an off-label use of ModelPsfMatchTask which currently can’t match to CoaddPsfs, but it should work soon.

More details:
The exact error message you are seeing is because meas.algorithms.PsfAttributes is deprecated and doesn’t work on CoaddPsfs. Your exposures bounding boxes are fine. I’ll file a ticket to update the remaining uses of PsfAttributes in the stack, but the script still will not work after that fix. I’ve sent you email on how to PSF-match your cutouts using imagePsfMatchTask in the meantime.

Aside: It has probably been confusing that you’ve been getting conflicting recommendations from me and Paul on whether to use ImagePsfMatchTask or ModelPsfMatchTask. Let me clarify what the difference is here:

  • ImagePsfMatchTask matches the sources in your image. This works on any image. But if you’re matching an image with one source, you’ll notice that it can work “too well,” because it can perfectly match a single source. If using ImagePsfMatchTask, I’d recommend increasing the size of your image to contain a few more stars.
  • ModelPsfMatchTask matches the Psf estimate attached to your image. It takes the Psf model, creates a grid of thumbnail images from that PSF model, and matches those images. So if your cutouts contain a galaxy and you want to match them based on the neighboring stars (which are not in your cutout), you’d want ModelPsfMatchTask.

Though I don’t know the details of your use case, ModelPsfMatchTask, is probably the better task. Unfortunately, at the moment ModelPsfMatchTask is limited in that it cannot not match a PSF to any arbitrary PSF model. The primary reason is that it doesn’t know until runtime what size PSF images to materialize. PSFs are immutable, and and not all Kernels/PSF can resize themselves based on their parameters. Crude padding/clipping is a bad idea for models where more information is available. I’m working on a ticket that will enable ModelPsfMatchTask on arbitrary Psf models, but it will not be ready before end of next week.

The images that I’m trying to match are galaxy images in different bands. ModelPsfMatchTask may indeed be better, because the source looks intrinsically different between the two bands, and how exactly are they different is the question to be studied. So preferably the PSF matching scheme should not depend on how the source look like but only on the PSF model.