{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Edit a FITS header\n\n\nThis example describes how to edit a value in a FITS header\nusing `astropy.io.fits`.\n\n-------------------\n\n*By: Adrian Price-Whelan*\n\n*License: BSD*\n\n-------------------\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from astropy.io import fits"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Download a FITS file:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from astropy.utils.data import get_pkg_data_filename\n\nfits_file = get_pkg_data_filename('tutorials/FITS-Header/input_file.fits')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Look at contents of the FITS file\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fits.info(fits_file)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Look at the headers of the two extensions:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(\"Before modifications:\")\nprint()\nprint(\"Extension 0:\")\nprint(repr(fits.getheader(fits_file, 0)))\nprint()\nprint(\"Extension 1:\")\nprint(repr(fits.getheader(fits_file, 1)))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "`astropy.io.fits` provides an object-oriented interface for reading and\ninteracting with FITS files, but for small operations (like this example) it\nis often easier to use the\n`convenience functions <http://docs.astropy.org/en/latest/io/fits/index.html#convenience-functions>`_.\n\nTo edit a single header value in the header for extension 0, use the\n`~astropy.io.fits.setval()` function. For example, set the OBJECT keyword\nto 'M31':\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fits.setval(fits_file, 'OBJECT', value='M31')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "With no extra arguments, this will modify the header for extension 0, but\nthis can be changed using the ``ext`` keyword argument. For example, we can\nspecify extension 1 instead:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fits.setval(fits_file, 'OBJECT', value='M31', ext=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This can also be used to create a new keyword-value pair (\"card\" in FITS\nlingo):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fits.setval(fits_file, 'ANEWKEY', value='some value')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Again, this is useful for one-off modifications, but can be inefficient\nfor operations like editing multiple headers in the same file\nbecause `~astropy.io.fits.setval()` loads the whole file each time it\nis called. To make several modifications, it's better to load the file once:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "with fits.open(fits_file, 'update') as f:\n    for hdu in f:\n        hdu.header['OBJECT'] = 'CAT'\n\nprint(\"After modifications:\")\nprint()\nprint(\"Extension 0:\")\nprint(repr(fits.getheader(fits_file, 0)))\nprint()\nprint(\"Extension 1:\")\nprint(repr(fits.getheader(fits_file, 1)))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}