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

.. only:: html

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

        Click :ref:`here <sphx_glr_download_generated_gallery_creating_ndcube_from_fitsfile.py>`
        to download the full example code

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

.. _sphx_glr_generated_gallery_creating_ndcube_from_fitsfile.py:


=======================================================
How to create an NDCube from data stored in a FITS file
=======================================================

This example shows how you load in data from a FITS file to create an `~ndcube.NDCube`.
Here we will use an example of a single image.

.. GENERATED FROM PYTHON SOURCE LINES 9-17

.. code-block:: default


    import matplotlib.pyplot as plt
    from astropy.io import fits
    from astropy.utils.data import get_pkg_data_filename
    from astropy.wcs import WCS

    from ndcube import NDCube








.. GENERATED FROM PYTHON SOURCE LINES 18-21

We first download the example file that we will use here to show how to create an
`~ndcube.NDCube` from data stored in a FITS file.
Here we are using an example file from ``astropy``.

.. GENERATED FROM PYTHON SOURCE LINES 21-24

.. code-block:: default


    image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')



.. rst-class:: sphx-glr-script-out

.. code-block:: pytb

    Traceback (most recent call last):
      File "/build/ndcube-QTi9TK/ndcube-2.2.0/examples/creating_ndcube_from_fitsfile.py", line 22, in <module>
        image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/astropy/utils/data.py", line 752, in get_pkg_data_filename
        return download_file(
               ^^^^^^^^^^^^^^
      File "/usr/lib/python3/dist-packages/astropy/utils/data.py", line 1563, in download_file
        raise urllib.error.URLError(
    urllib.error.URLError: <urlopen error Unable to open any source! Exceptions were {'http://data.astropy.org/tutorials/FITS-images/HorseHead.fits': URLError(ConnectionRefusedError(111, 'Connection refused')), 'http://www.astropy.org/astropy-data/tutorials/FITS-images/HorseHead.fits': URLError(ConnectionRefusedError(111, 'Connection refused'))}>




.. GENERATED FROM PYTHON SOURCE LINES 25-28

Lets extract the image data and the header information from the FITS file.
This can be achieved by using the functionality within `astropy.io.fits`.
In this file the image information is located in the Primary HDU (extension 0).

.. GENERATED FROM PYTHON SOURCE LINES 28-32

.. code-block:: default


    image_data = fits.getdata(image_file)
    image_header = fits.getheader(image_file)


.. GENERATED FROM PYTHON SOURCE LINES 33-36

To create an `~ndcube.NDCube` object, we need both the data array and a
WCS object (e.g. an `~astropy.wcs.WCS`). Here the data WCS information is
within the header, which we can pass to :class:`~astropy.wcs.WCS` to create a WCS object.

.. GENERATED FROM PYTHON SOURCE LINES 36-39

.. code-block:: default


    example_ndcube = NDCube(image_data, WCS(image_header))


.. GENERATED FROM PYTHON SOURCE LINES 40-42

Now we have created an `~ndcube.NDCube` from this data.
We can inspect the `~ndcube.NDCube`, such as the WCS.

.. GENERATED FROM PYTHON SOURCE LINES 42-45

.. code-block:: default


    print(example_ndcube.wcs)


.. GENERATED FROM PYTHON SOURCE LINES 46-47

and we can also inspect the dimensions.

.. GENERATED FROM PYTHON SOURCE LINES 47-50

.. code-block:: default


    print(example_ndcube.dimensions)


.. GENERATED FROM PYTHON SOURCE LINES 51-52

We can also quickly visualize the data using the :meth:`~ndcube.NDCube.plot` method.

.. GENERATED FROM PYTHON SOURCE LINES 52-56

.. code-block:: default


    example_ndcube.plot()

    plt.show()


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

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


.. _sphx_glr_download_generated_gallery_creating_ndcube_from_fitsfile.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: creating_ndcube_from_fitsfile.py <creating_ndcube_from_fitsfile.py>`



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

     :download:`Download Jupyter notebook: creating_ndcube_from_fitsfile.ipynb <creating_ndcube_from_fitsfile.ipynb>`


.. only:: html

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

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