How to get images from calexp centered around an ra,dec

Hi,

We are trying to get calexp images of a variable star. We could get the visited and the detector.
However, we are getting an error when we try to get a cutout around an RA, DEC using butler.get.

Here is our code:

def get_cutout_image(ra_deg,dec_deg,visit,detector,band,datasetType='calexp',cutoutSideLength=image_size):
    """
    get the tract and patch of a source. currently retrieves i band only. 

    Parameters
    ----------
    ra : ra of source in degrees
    dec : dec of source in degrees
    
    """
    cutoutSize = geom.ExtentI(cutoutSideLength, cutoutSideLength)
    
    spherePoint = geom.SpherePoint(ra_deg*geom.degrees, dec_deg*geom.degrees)
    tract = skymap.findTract(spherePoint)
    patch = tract.findPatch(spherePoint)
    xy = geom.PointI(tract.getWcs().skyToPixel(spherePoint))
    bbox = geom.BoxI(xy - cutoutSize // 2, cutoutSize)
    
    my_tract = tract.getId()
    my_patch = tract.getSequentialPatchIndex(patch)
    
    parameters = {'bbox': bbox}
    dataId = {'band': band, 'tract': my_tract, 'patch': my_patch, 'visit': visit, 'detector':detector}
    
    cutout_image = butler.get(datasetType, dataId=dataId, parameters=parameters)
    #cutout_image = butler.get(datasetType, dataId=dataId)
    
    return cutout_image
cutout_image = get_cutout_image(63.292031,-44.508647,466723,102, 'r',datasetType='calexp',cutoutSideLength=100)

We get the following error:

ValueError: Failure from formatter 'lsst.obs.base.formatters.fitsExposure.FitsExposureFormatter' for dataset 15c0c7f3-331a-4b8e-8b4c-62a4b868590c (calexp from file:///tmp/butler-cache-dir-si0gs3ov/exempt/986945d9-15c0c7f3-331a-4b8e-8b4c-62a4b868590c.fits): 
  File "src/image/ImageBaseFitsReader.cc", line 179, in ndarray::Array<T, 2, 2> lsst::afw::image::ImageBaseFitsReader::readArray(const lsst::geom::Box2I&, lsst::afw::image::ImageOrigin, bool) [with T = float]
    Subimage box (10853,2706) 100x100 doesn't fit in image (0,0) 4072x4000 in HDU 1 {0}
lsst::pex::exceptions::LengthError: 'Subimage box (10853,2706) 100x100 doesn't fit in image (0,0) 4072x4000 in HDU 1'

We are referring to Melissa Graham’s notebook:

We also found a relevant community forum related to this topic:

Any help is appreciated.

Thanks!

Your bounding box has to be relative to the patch pixel coordinates not the tract coordinates.

That (10853, 2706) is the coordinate in tract coordinates but your image is a patch.

I’m traveling and can’t really dig in here but my suspicion is that the RA,Dec doesn’t match the visit and detector and that’s why the box didn’t fit in the image. Perhaps someone can double check that and report back?

But also, that notebook was not intended to be circulated or widely used, and was created with an older version of the LSST Science Pipelines. For an up-to-date demo of how to make cutouts from calexps, see the DP0.2 tutorial notebook “03a_Image_Display_and_Manipulation”.

Let us know if the issue persists…

@timj @MelissaGraham
Thanks!

Following the code from DP0.2 tutorial notebook “03a_Image_Display_and_Manipulation” works.