[ ]:
%matplotlib inline

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

[2]:
from astropy.utils.data import get_pkg_data_filename
from astropy.table import Table
from astropy.io import fits

Download a FITS file

[5]:
event_filename = get_pkg_data_filename('tutorials/FITS-tables/chandra_events.fits') #  filename('/data/ROSAT.fits')

Display information about the contents of the FITS file.

[6]:
fits.info(event_filename)
Filename: C:\Users\Kurt\.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

[7]:
events = Table.read(event_filename, hdu=1)

Print the column names of the Events Table.

[8]:
print(events.columns)
<TableColumns names=('time','ccd_id','node_id','expno','chipx','chipy','tdetx','tdety','detx','dety','x','y','pha','pha_ro','energy','pi','fltgrade','grade','status')>

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

[9]:
print(events['energy'].unit)
eV

Print the data stored in the Energy column.

[10]:
print(events['energy'])
  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
[ ]: