{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n=====================================================\nCreate a multi-extension FITS (MEF) file from scratch\n=====================================================\n\nThis example demonstrates how to create a multi-extension FITS (MEF)\nfile from scratch using `astropy.io.fits`.\n\n-------------------\n\n*By: Erik Bray*\n\n*License: BSD*\n\n-------------------\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "HDUList objects are used to hold all the HDUs in a FITS file. This\n``HDUList`` class is a subclass of Python's builtin `list`. and can be\ncreated from scratch. For example, to create a FITS file with\nthree extensions:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from astropy.io import fits\nnew_hdul = fits.HDUList()\nnew_hdul.append(fits.ImageHDU())\nnew_hdul.append(fits.ImageHDU())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Write out the new file to disk:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "new_hdul.writeto('test.fits')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Alternatively, the HDU instances can be created first (or read from an\nexisting FITS file).\n\nCreate a multi-extension FITS file with two empty IMAGE extensions (a\ndefault PRIMARY HDU is prepended automatically if one is not specified;\nwe use ``overwrite=True`` to overwrite the file if it already exists):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "hdu1 = fits.PrimaryHDU()\nhdu2 = fits.ImageHDU()\nnew_hdul = fits.HDUList([hdu1, hdu2])\nnew_hdul.writeto('test.fits', overwrite=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally, we'll remove the file we created:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "os.remove('test.fits')"
      ]
    }
  ],
  "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
}