.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/examples/io/modify-fits-header.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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* .. GENERATED FROM PYTHON SOURCE LINES 16-20 .. code-block:: Python from astropy.io import fits from astropy.utils.data import get_pkg_data_filename .. GENERATED FROM PYTHON SOURCE LINES 21-22 Download a FITS file: .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python fits_file = get_pkg_data_filename('tutorials/FITS-Header/input_file.fits') .. GENERATED FROM PYTHON SOURCE LINES 26-27 Look at contents of the FITS file .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: Python fits.info(fits_file) .. rst-class:: sphx-glr-script-out .. code-block:: none Filename: /home/user/.astropy/cache/download/url/519010d87325a22575dc1d16f3a05d26/contents No. Name Ver Type Cards Dimensions Format 0 PRIMARY 1 PrimaryHDU 8 (100, 100) float64 1 1 ImageHDU 8 (128, 128) float64 .. GENERATED FROM PYTHON SOURCE LINES 31-32 Look at the headers of the two extensions: .. GENERATED FROM PYTHON SOURCE LINES 32-41 .. 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 .. 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 = '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 ' .. GENERATED FROM PYTHON SOURCE LINES 42-50 `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': .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python fits.setval(fits_file, 'OBJECT', value='M31') .. GENERATED FROM PYTHON SOURCE LINES 54-57 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: .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python fits.setval(fits_file, 'OBJECT', value='M31', ext=1) .. GENERATED FROM PYTHON SOURCE LINES 61-63 This can also be used to create a new keyword-value pair ("card" in FITS lingo): .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python fits.setval(fits_file, 'ANEWKEY', value='some value') .. GENERATED FROM PYTHON SOURCE LINES 67-71 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: .. GENERATED FROM PYTHON SOURCE LINES 71-83 .. 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 .. 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 ' .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.036 seconds) .. _sphx_glr_download_generated_examples_io_modify-fits-header.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: modify-fits-header.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: modify-fits-header.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_