.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_examples_io_fits-tables.py: ===================================================================== Accessing data stored as a table in a multi-extension FITS (MEF) file ===================================================================== FITS files can often contain large amount of multi-dimensional data and tables. This example opens a FITS file with information from Chandra's HETG-S instrument. The example uses `astropy.utils.data` to download multi-extension FITS (MEF) file, `astropy.io.fits` to investigate the header, and `astropy.table.Table` to explore the data. ------------------- *By: Lia Corrales, Adrian Price-Whelan, and Kelle Cruz* *License: BSD* ------------------- Use `astropy.utils.data` subpackage to download the FITS file used in this example. Also import `~astropy.table.Table` from the `astropy.table` subpackage and `astropy.io.fits` .. code-block:: python from astropy.utils.data import get_pkg_data_filename from astropy.table import Table from astropy.io import fits Download a FITS file .. code-block:: python event_filename = get_pkg_data_filename('tutorials/FITS-tables/chandra_events.fits') Display information about the contents of the FITS file. .. code-block:: python fits.info(event_filename) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Filename: /home/docs/.astropy/cache/download/py3/26e9900d731d08997d99ada3973f4592 No. Name Ver Type Cards Dimensions Format 0 PRIMARY 1 PrimaryHDU 30 () 1 EVENTS 1 BinTableHDU 890 483964R x 19C [1D, 1I, 1I, 1J, 1I, 1I, 1I, 1I, 1E, 1E, 1E, 1E, 1J, 1J, 1E, 1J, 1I, 1I, 32X] 2 GTI 3 BinTableHDU 28 1R x 2C [1D, 1D] 3 GTI 2 BinTableHDU 28 1R x 2C [1D, 1D] 4 GTI 1 BinTableHDU 28 1R x 2C [1D, 1D] 5 GTI 0 BinTableHDU 28 1R x 2C [1D, 1D] 6 GTI 6 BinTableHDU 28 1R x 2C [1D, 1D] Extension 1, EVENTS, is a Table that contains information about each X-ray photon that hit Chandra's HETG-S detector. Use `~astropy.table.Table` to read the table .. code-block:: python events = Table.read(event_filename, hdu=1) Print the column names of the Events Table. .. code-block:: python print(events.columns) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none If a column contains unit information, it will have an associated `astropy.units` object. .. code-block:: python print(events['energy'].unit) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none eV Print the data stored in the Energy column. .. code-block:: python print(events['energy']) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none energy eV --------- 13874.715 2621.1938 12119.018 3253.0364 14214.382 1952.7239 3267.5334 3817.0366 2252.7295 6154.1094 ... 4819.8286 12536.866 2599.5652 15535.768 6653.0815 14362.482 14653.954 6652.827 9672.882 1875.9359 Length = 483964 rows **Total running time of the script:** ( 0 minutes 3.404 seconds) .. _sphx_glr_download_generated_examples_io_fits-tables.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: fits-tables.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: fits-tables.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_