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 Table from the astropy.table subpackage and astropy.io.fits

from astropy.utils.data import download_file
from astropy.table import Table
from astropy.io import fits

Download a FITS file

event_filename = download_file('http://data.astropy.org/tutorials/FITS-tables/chandra_events.fits',
                               cache=True)

Out:

Downloading http://data.astropy.org/tutorials/FITS-tables/chandra_events.fits [Done]

Display information about the contents of the FITS file.

fits.info(event_filename)

Out:

  Filename: /home/docs/.astropy/cache/download/py2/906ea1e642b1cb91dfa26ef2a2a935ba
No.    Name         Type      Cards   Dimensions   Format
0    PRIMARY     PrimaryHDU      32   ()
1    EVENTS      BinTableHDU    937   51850R x 26C   [1D, 1J, 1E, 1E, 1I, 1I, 1I, 1I, 1E, 1E, 1E, 1E, 1I, 1J, 1I, 1E, 1I, 1I, 1I, 1I, 1E, 1E, 1I, 1I, 1I, 32X]
2    GTI         BinTableHDU     28   1R x 2C      [1D, 1D]
3    GTI         BinTableHDU     28   1R x 2C      [1D, 1D]
4    GTI         BinTableHDU     28   1R x 2C      [1D, 1D]
5    GTI         BinTableHDU     28   1R x 2C      [1D, 1D]
6    REGION      BinTableHDU    264   3R x 9C      [1I, 16A, 1E, 1E, 2E, 1E, 16A, 1I, 1I]

Extension 1, EVENTS, is a Table that contains information about each X-ray photon that hit Chandra’s HETG-S detector.

Use Table to read the table

events = Table.read(event_filename, hdu=1)

Print the column names of the Events Table.

print(events.columns)

Out:

<TableColumns names=('time','expno','tg_r','tg_d','chipx','chipy','tdetx','tdety','detx','dety','x','y','ccd_id','pha','pi','energy','grade','fltgrade','node_id','tg_m','tg_lam','tg_mlam','tg_srcid','tg_part','tg_smap','status')>

If a column contains unit information, it will have an associated astropy.units object.

print(events['energy'].unit)

Out:

eV

Print the data stored in the Energy column.

print(events['energy'])

Out:

  energy
   eV
-------
5567.74
4567.13
5397.79
4964.62
 4600.1
1873.79
5399.74
7463.69
3636.53
3578.51
    ...
2105.02
2039.68
2435.06
2494.82
5204.24
1625.02
3549.63
5152.93
2807.87
3086.63
Length = 51850 rows

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

Download Python source code: fits-tables.py
Download IPython notebook: fits-tables.ipynb