Edit a FITS header#

This example describes how to edit a value in a FITS header using astropy.io.fits.

By: Adrian Price-Whelan

License: BSD

from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

Download a FITS file:

fits_file = get_pkg_data_filename('tutorials/FITS-Header/input_file.fits')

Look at contents of the FITS file

fits.info(fits_file)
Filename: /home/user/.astropy/cache/download/url/519010d87325a22575dc1d16f3a05d26/contents
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU       7   (100, 100)   float64
  1                1 ImageHDU         7   (128, 128)   float64

Look at the headers of the two extensions:

print("Before modifications:")
print()
print("Extension 0:")
print(repr(fits.getheader(fits_file, 0)))
print()
print("Extension 1:")
print(repr(fits.getheader(fits_file, 1)))
Before modifications:

Extension 0:
SIMPLE  =                    T / conforms to FITS standard
BITPIX  =                  -64 / array data type
NAXIS   =                    2 / number of array dimensions
NAXIS1  =                  100
NAXIS2  =                  100
EXTEND  =                    T
OBJECT  = 'KITTEN  '

Extension 1:
XTENSION= 'IMAGE   '           / Image extension
BITPIX  =                  -64 / array data type
NAXIS   =                    2 / number of array dimensions
NAXIS1  =                  128
NAXIS2  =                  128
PCOUNT  =                    0 / number of parameters
GCOUNT  =                    1 / number of groups

astropy.io.fits provides an object-oriented interface for reading and interacting with FITS files, but for small operations (like this example) it is often easier to use the convenience functions.

To edit a single header value in the header for extension 0, use the setval() function. For example, set the OBJECT keyword to ‘M31’:

fits.setval(fits_file, 'OBJECT', value='M31')

With no extra arguments, this will modify the header for extension 0, but this can be changed using the ext keyword argument. For example, we can specify extension 1 instead:

fits.setval(fits_file, 'OBJECT', value='M31', ext=1)

This can also be used to create a new keyword-value pair (“card” in FITS lingo):

fits.setval(fits_file, 'ANEWKEY', value='some value')

Again, this is useful for one-off modifications, but can be inefficient for operations like editing multiple headers in the same file because setval() loads the whole file each time it is called. To make several modifications, it’s better to load the file once:

with fits.open(fits_file, 'update') as f:
    for hdu in f:
        hdu.header['OBJECT'] = 'CAT'

print("After modifications:")
print()
print("Extension 0:")
print(repr(fits.getheader(fits_file, 0)))
print()
print("Extension 1:")
print(repr(fits.getheader(fits_file, 1)))
After modifications:

Extension 0:
SIMPLE  =                    T / conforms to FITS standard
BITPIX  =                  -64 / array data type
NAXIS   =                    2 / number of array dimensions
NAXIS1  =                  100
NAXIS2  =                  100
EXTEND  =                    T
OBJECT  = 'CAT     '
ANEWKEY = 'some value'

Extension 1:
XTENSION= 'IMAGE   '           / Image extension
BITPIX  =                  -64 / array data type
NAXIS   =                    2 / number of array dimensions
NAXIS1  =                  128
NAXIS2  =                  128
PCOUNT  =                    0 / number of parameters
GCOUNT  =                    1 / number of groups
OBJECT  = 'CAT     '

Total running time of the script: (0 minutes 0.917 seconds)

Gallery generated by Sphinx-Gallery