.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_examples_io_modify-fits-header.py: ================== 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* ------------------- .. code-block:: python from astropy.io import fits Download a FITS file: .. code-block:: python from astropy.utils.data import get_pkg_data_filename fits_file = get_pkg_data_filename('tutorials/FITS-Header/input_file.fits') Look at contents of the FITS file .. code-block:: python fits.info(fits_file) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Filename: /home/docs/.astropy/cache/download/py3/ba96b4b76d8eff8a716f3a8aeb814ace 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: .. code-block:: python 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))) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 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 `~astropy.io.fits.setval()` function. For example, set the OBJECT keyword to 'M31': .. code-block:: python 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: .. code-block:: python 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): .. code-block:: python 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 `~astropy.io.fits.setval()` loads the whole file each time it is called. To make several modifications, it's better to load the file once: .. code-block:: python 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))) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 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.504 seconds) .. _sphx_glr_download_generated_examples_io_modify-fits-header.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: modify-fits-header.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: modify-fits-header.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_