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

Sébastien Bardeau bardeau at iram.fr
Mon Oct 23 09:05:35 EDT 2017


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/



More information about the AstroPy mailing list