[AstroPy] Saving Cutout2D image to FITS

Ivan Valtchanov ivvv68 at gmail.com
Tue Mar 13 18:56:46 EDT 2018


The following attempt to save a Cutout2D object to a FITS file:

########
from astropy.io import fits
from astropy import wcs
from astropy.nddata import Cutout2D

f = fits.open('fitfile.fits')
w = wcs.WCS(f['image'].header)
#
position = (168,155)
shape = (20, 20)
cutout = Cutout2D(f['image'].data, position, shape, wcs=w)
#
# block 2
#
hdu = fits.PrimaryHDU()
hdu.data = cutout.data
hdu.header = f['image'].header
hdu.header.update(cutout.wcs.to_header())
hdu.writeto('test_cutout.fits')
#########

fails with the following exception:

VerifyError:
Verification reported errors:
HDU 0:
    'SIMPLE' card does not exist.
Note: astropy.io.fits uses zero-based indexing.

While this one is OK:
#
# block 2
#
hdu = fits.PrimaryHDU()
hdu2 = fits.ImageHDU(cutout.data)
hdu2.header = f['image'].header
hdu2.header.update(cutout.wcs.to_header())
hdul = fits.HDUList([hdu,hdu2])
hdul.writeto('test_cutout.fits')
########

Problem solved but it is probably a good idea to have a utility method to
save directly a cutout object to a FITS...

Thanks,
Ivan


On Tue, 13 Mar 2018 at 16:38 Thomas Boch <thomas.boch at astro.unistra.fr>
wrote:

> Hi Ivan,
>
> the Cutout2D object has attributes *data* and *wcs* which can be used to
> create an astropy.io.fits.PrimaryHDU instance that can then be written to a
> file.
>
> Cheers,
>
> Thomas
>
>
> Le 13/03/2018 à 22:27, Ivan Valtchanov a écrit :
>
> Hi all,
>
> I've been trying to use astropy.nddata Cutout2D to crop a large image. I
> don't see how to save the cutout result to a FITS file.
> cutout.writeto('image.fits') does not work and in the API I cannot find a
> method in the Cutout2D class that saves the result to a FITS file.
>
> is there a way to do it?
>
> Thanks,
> Ivan Valtchanov
>
>
> _______________________________________________
> AstroPy mailing listAstroPy at python.orghttps://mail.python.org/mailman/listinfo/astropy
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20180313/a22abe4b/attachment.html>


More information about the AstroPy mailing list