[AstroPy] astropy.io.fits won't allow me to modify the NAXIS keywords

Howard Bushouse bushouse at stsci.edu
Mon Oct 23 09:52:29 EDT 2017


Regarding the updates to the NAXISn keywords, it's highly likely that NAXIS and NAXISn are determined automatically within the fits library by the shape and size of the data array and hence you can't change the keyword values without actually changing the shape of the data array itself. If the data array is currently 4-D, slice off the first 2 axes to convert it to 2-D and then resave the file. The NAXISn keywords should update automatically.

-Howard

> On Oct 23, 2017, at 9:47 AM, John ZuHone <jzuhone at gmail.com> wrote:
> 
> I’m a bit confused about the statement that “matplotlib returns an error”. Unless you are using APLpy or wcsaxes, Matplotlib doesn’t know anything about FITS file headers. Could you elaborate?
> 
> Sent from my iPhone
> 
>> On Oct 23, 2017, at 9:05 AM, Sébastien Bardeau <bardeau at iram.fr> wrote:
>> 
>> Dear astropy community,
>> 
>> I am facing a FITS image which contains the following keywords in its
>> primary header:
>> 
>> NAXIS   =                    4         /
>> NAXIS1  =                  256         /
>> NAXIS2  =                  256         /
>> NAXIS3  =                    1         /
>> NAXIS4  =                    1         /
>> 
>> As you can see it provides a 2D image, but there are 2 trailing
>> degenerate dimensions. Tools like ds9 have no problem understanding this
>> is a 2D image, but another user reports that matplotlib returns an
>> error. As an intermediate solution, I wanted to patch the header by
>> setting NAXIS to 2 and removing NAXIS3 and NAXIS4. You will find the
>> script below. Unfortunately, none of the modifications I will do on the
>> NAXIS* keywords will be saved in the result file, even if I keep these
>> keywords perfectly consistent with the actual binary image. On the other
>> hand, modifying, deleting, or adding other keywords works correctly.
>> 
>> I remember having the same problem with pyfits in the past. I can also
>> add that I can perfectly imaging doing the opposite transformation, i.e.
>> adding extra degenerate dimensions to transform an image into a cube or
>> more.
>> 
>> Is this a bug or a feature? I believe this is a kind of protection
>> against inconsistent modifications of the header, but can't we imagine
>> to relax a bit this protection, by e.g. checking "only" that the total
>> number of elements are preserved? I can provide a test file if needed.
>> 
>> Thanks,
>> 
>> Sebastien
>> 
>> 
>> #!/bin/env python
>> 
>> import astropy.io.fits
>> import sys
>> 
>> a = astropy.io.fits.open(sys.argv[1],mode='update')
>> 
>> print "Before: ",a[0].header['NAXIS']
>> a[0].header['NAXIS'] = 2  # Won't be saved
>> print "After:  ",a[0].header['NAXIS']
>> del a[0].header['NAXIS3']  # Won't be deleted
>> del a[0].header['NAXIS4']  # Won't be deleted
>> 
>> a[0].header['OBJECT'] = 'ANOTHERONE'  # Will be changed
>> del a[0].header['ORIGIN']  # Will be deleted
>> a[0].header['MYKEYWOR'] = 'Some string'  # Will be added
>> 
>> a.close()  # Save changes
>> del a
>> 
>> # Check
>> a = astropy.io.fits.open(sys.argv[1],mode='readonly')
>> print "Current:",a[0].header['NAXIS']  # Shows unmodified value
>> a.close()
>> del a
>> 
>> 
>> 
>> -- 
>>    Sebastien BARDEAU
>>          IRAM
>>  300 rue de la Piscine
>> F - 38406 Saint Martin d'Heres
>> Tel: (+33) 4 76 82 49 86
>> www.iram.fr/IRAMFR/GILDAS/
>> 
>> _______________________________________________
>> AstroPy mailing list
>> 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 --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1642 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/astropy/attachments/20171023/2546744f/attachment.bin>


More information about the AstroPy mailing list