[AstroPy] Saving Cutout2D image to FITS

Simon Conseil simon at sconseil.fr
Wed Mar 14 18:08:16 EDT 2018


Hi,

Le 13/03/2018 à 23:56, Ivan Valtchanov a écrit :
> The following attempt to save a Cutout2D object to a FITS file:
>
> ########
> from astropy.io <http://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.
>
Because you are overwriting the header and losing some useful keywords, 
when doing "hdu.header = f['image'].header".
Here is a revised version which works:

hdu = fits.PrimaryHDU(data=cutout.data, header=f['image'].header)
hdu.header.update(cutout.wcs.to_header())
hdu.writeto('test_cutout.fits')

Simon



> 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 
> <mailto: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 useastropy.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 list
>>     AstroPy at python.org <mailto:AstroPy at python.org>
>>     https://mail.python.org/mailman/listinfo/astropy
>
>     _______________________________________________
>     AstroPy mailing list
>     AstroPy at python.org <mailto:AstroPy at python.org>
>     https://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/20180314/2a221ba2/attachment.html>


More information about the AstroPy mailing list