How to convert the WCS in the Calexp into a WCS inside a fits header

I would like to extract an Calexp image into a fits file. I can retrieve the “rubin-WCS” from the Calexp. However I don’t understand how to convert this WCS into information in the fits header of the fits image which can be used in a tool like DS9.

There is already infrastructure for sending an in-memory calexp directly to DS9 and there are instructions in the tutorial.

If you don’t wish to use this approach, if you only want a FITS file you can either call writeFits() method on the calexp and write it locally, or you can use the butler retrieve-artifacts command which will download the original FITS file.

That response was (reasonably) assuming you’re working in a notebook environment.

However, if you’re accessing the image via the RSP Portal you can just download it as a FITS file from the UI (there’s a “diskette” button in the image viewer.

If you are accessing it via the ObsTAP image service, please let us know. I’m not sure we have a completely worked-out tutorial available yet.

@sylvielsstfr, I just wanted to check to see if Tim and/or Gregory’s suggestions solved your question. Thanks!

I work in a notebook environnement.

I have found a trick like this:

Convert WCS-DM to astropy WCS

the_fits_WCS = WCS(calexp.getWcs().getFitsMetadata())
the_fits_WCS.to_header_string()

create the header

header = the_fits_WCS.to_header()

And then I fill the header with additional info I wih to keep and then

primary_hdu = fits.PrimaryHDU(header=header)
image_hdu = fits.ImageHDU(rotated_array)
hdu_list = fits.HDUList([primary_hdu, image_hdu])
hdu_list.writeto(filename_out,overwrite=True)

But when I open the fits file neither SAO-DS9 nor AstroImageJ are able to show the WCS I would say automatically.
(I had to go through astrometry.net).

However opening this fits file with astropy and getting the WCS as those were written make it possible to view the image in WCS.

Thanks for more hints .

Hi @sylvielsstfr, thanks for confirming you work in the Notebook Aspect.

Tim’s response (use the writeFits method) is the recommended way to save a calexp as a FITS file, which can then be downloaded and displayed in, e.g., DS9.

For example:

from lsst.daf.butler import Butler
butler = Butler('dp02', collections='2.2i/runs/DP0.2')
dataId = {'visit': 512055, 'detector': 75}
calexp = butler.get('calexp', dataId=dataId)
calexp.writeFits('my_calexp.fits')

The file my_calexp.fits can be downloaded and viewed in DS9 with the WCS applied.

Optionally, the Firefly interface exists within the Notebook Aspect for interactive image display, and it has many similar features to DS9.

import lsst.afw.display as afwDisplay
display = afwDisplay.getDisplay(backend='firefly')
display.image(calexp)

Hopefully this avoids the need for the workaround trick you mentioned above.

I’m going to mark Tim’s response as the solution for this topic, but as always if you run into other issues please just start a new topic and we’ll follow-up.