
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "generated/examples/io/create-mef.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_generated_examples_io_create-mef.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_generated_examples_io_create-mef.py:


=====================================================
Create a multi-extension FITS (MEF) file from scratch
=====================================================

This example demonstrates how to create a multi-extension FITS (MEF)
file from scratch using `astropy.io.fits`.


*By: Erik Bray*

*License: BSD*

.. GENERATED FROM PYTHON SOURCE LINES 17-20

.. code-block:: default


    import os








.. GENERATED FROM PYTHON SOURCE LINES 21-25

HDUList objects are used to hold all the HDUs in a FITS file. This
``HDUList`` class is a subclass of Python's builtin `list`. and can be
created from scratch. For example, to create a FITS file with
three extensions:

.. GENERATED FROM PYTHON SOURCE LINES 25-31

.. code-block:: default


    from astropy.io import fits
    new_hdul = fits.HDUList()
    new_hdul.append(fits.ImageHDU())
    new_hdul.append(fits.ImageHDU())








.. GENERATED FROM PYTHON SOURCE LINES 32-33

Write out the new file to disk:

.. GENERATED FROM PYTHON SOURCE LINES 33-36

.. code-block:: default


    new_hdul.writeto('test.fits')








.. GENERATED FROM PYTHON SOURCE LINES 37-43

Alternatively, the HDU instances can be created first (or read from an
existing FITS file).

Create a multi-extension FITS file with two empty IMAGE extensions (a
default PRIMARY HDU is prepended automatically if one is not specified;
we use ``overwrite=True`` to overwrite the file if it already exists):

.. GENERATED FROM PYTHON SOURCE LINES 43-49

.. code-block:: default


    hdu1 = fits.PrimaryHDU()
    hdu2 = fits.ImageHDU()
    new_hdul = fits.HDUList([hdu1, hdu2])
    new_hdul.writeto('test.fits', overwrite=True)








.. GENERATED FROM PYTHON SOURCE LINES 50-51

Finally, we'll remove the file we created:

.. GENERATED FROM PYTHON SOURCE LINES 51-53

.. code-block:: default


    os.remove('test.fits')








.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_generated_examples_io_create-mef.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: create-mef.py <create-mef.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: create-mef.ipynb <create-mef.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
