Viewing and manipulating data from FITS tables

Authors

Lia Corrales

Learning Goals

  • TODO

Keywords

table, file input/output, matplotlib, FITS image

Summary

This tutorial demonstrates the use of astropy.utils.data to download a data file, then uses astropy.io.fits and astropy.table to open the file. Lastly, we’ll use matplotlib to visualize the data.

In[1]:

import numpy as np
from astropy.io import fits
from astropy.table import Table
from matplotlib.colors import LogNorm

# Set up matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

The following line is needed to download the example FITS files used in this tutorial.

In[2]:

from astropy.utils.data import download_file

FITS files often contain large amounts of multi-dimensional data and tables.

In this particular example, we’ll open a FITS file from a Chandra observation of the Galactic Center. The file contains a list of events with x and y coordinates, energy, and various other pieces of information.

In[3]:

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

Out[3]:

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

Opening the FITS file and viewing table contents

Since the file is big, let’s open it with memmap=True to prevent RAM storage issues.

In[4]:

hdu_list = fits.open(event_filename, memmap=True)

In[5]:

hdu_list.info()

Out[5]:

Filename: /home/circleci/.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]

In this case, we’re interested in reading EVENTS, which contains information about each X-ray photon that hit the detector.

To find out what information the table contains, let’s print the column names.

In[6]:

print(hdu_list[1].columns)

Out[6]:

ColDefs(
    name = 'time'; format = '1D'; unit = 's'
    name = 'ccd_id'; format = '1I'
    name = 'node_id'; format = '1I'
    name = 'expno'; format = '1J'
    name = 'chipx'; format = '1I'; unit = 'pixel'; coord_type = 'CPCX'; coord_unit = 'mm'; coord_ref_point = 0.5; coord_ref_value = 0.0; coord_inc = 0.023987
    name = 'chipy'; format = '1I'; unit = 'pixel'; coord_type = 'CPCY'; coord_unit = 'mm'; coord_ref_point = 0.5; coord_ref_value = 0.0; coord_inc = 0.023987
    name = 'tdetx'; format = '1I'; unit = 'pixel'
    name = 'tdety'; format = '1I'; unit = 'pixel'
    name = 'detx'; format = '1E'; unit = 'pixel'; coord_type = 'LONG-TAN'; coord_unit = 'deg'; coord_ref_point = 4096.5; coord_ref_value = 0.0; coord_inc = 0.00013666666666667
    name = 'dety'; format = '1E'; unit = 'pixel'; coord_type = 'NPOL-TAN'; coord_unit = 'deg'; coord_ref_point = 4096.5; coord_ref_value = 0.0; coord_inc = 0.00013666666666667
    name = 'x'; format = '1E'; unit = 'pixel'; coord_type = 'RA---TAN'; coord_unit = 'deg'; coord_ref_point = 4096.5; coord_ref_value = 266.41519201128; coord_inc = -0.00013666666666667
    name = 'y'; format = '1E'; unit = 'pixel'; coord_type = 'DEC--TAN'; coord_unit = 'deg'; coord_ref_point = 4096.5; coord_ref_value = -29.012248288366; coord_inc = 0.00013666666666667
    name = 'pha'; format = '1J'; unit = 'adu'; null = 0
    name = 'pha_ro'; format = '1J'; unit = 'adu'; null = 0
    name = 'energy'; format = '1E'; unit = 'eV'
    name = 'pi'; format = '1J'; unit = 'chan'; null = 0
    name = 'fltgrade'; format = '1I'
    name = 'grade'; format = '1I'
    name = 'status'; format = '32X'
)

Now we’ll take this data and convert it into an astropy table. While it’s possible to access FITS tables directly from the .data attribute, using Table tends to make a variety of common tasks more convenient.

In[7]:

evt_data = Table(hdu_list[1].data)

For example, a preview of the table is easily viewed by simply running a cell with the table as the last line:

In[8]:

evt_data

Out[8]:

Table length=483964
timeccd_idnode_idexpnochipxchipytdetxtdetydetxdetyxyphapha_roenergypifltgradegradestatus [32]
float64int16int16int32int16int16int16int16float32float32float32float32int32int32float32int32int16int16bool
238623220.909358333689208512439815095.6414138.9954168.07235087.7723548353413874.715951164False .. False
238623220.90935833168437237489534984865.5674621.18263662.19684915.93366676292621.1938180642False .. False
238623220.90935833268719289484337804814.8354340.2543935.22074832.5523033287512119.01883183False .. False
238623220.90935833068103295483731644807.36434954.3853324.46444897.27548317733253.036422300False .. False
238623220.90935833168498314481835594788.9874560.32763713.63434832.7353612343914214.382974642False .. False
238623220.90935833368791469466338524635.45264268.0533985.84964645.935004381952.723913400False .. False
238623220.90935833368894839429339554266.6424165.32034044.54694267.6058357133267.533422400False .. False
238623220.90935833368857941419139184164.8154202.22563995.93534170.8189758043817.036626200False .. False
238623220.90935833368910959417339714146.99374149.3644046.33764146.91065764462252.729515500False .. False
238623220.90935833368961962417040224144.12844098.49764096.5154138.09157213546154.109442200False .. False
.........................................................
238672393.549719331315723933199493350404902.9073082.49565212.49954766.2295122211814819.828633100False .. False
238672393.549719331215723596412472047034691.513418.98934853.51174595.80373142302012536.866859106False .. False
238672393.5497193313157231000608452451074494.7133015.71855230.8864353.0186585852599.565217900False .. False
238672393.549719331115723270917421543774188.33253743.59574472.074134.2213861346315535.7681024164False .. False
238672393.549719331015723232988414443394117.61473781.87744425.754068.4873168014996653.081545600False .. False
238672393.590759340115723366103316447663140.90483356.32084733.68163048.56643621360214362.48298400False .. False
238672393.590759340315723937646370741953681.21223925.54524231.83543651.97243717348614653.954100483False .. False
238672393.590759340115723406687374847263723.40143396.2524762.4213631.7224167615366652.82745600False .. False
238672393.590759340115723354870393147783906.073344.7754834.993807.0835243621659672.882663164False .. False
238672393.631799346115723384821325925233230.92045596.84962519.22023401.03274913561875.935912900False .. False

We can extract data from the table by referencing the column name. Let’s try making a histogram for the energy of each photon, which will give us a sense for the spectrum (folded with the detector’s efficiency).

In[9]:

energy_hist = plt.hist(evt_data['energy'], bins='auto')

Out[9]:

../_images/FITS-tables_18_0.png

Making a 2-D histogram with some table data

We’ll make an image by binning the x and y coordinates of the events into a 2-D histogram.

This particular observation spans five CCD chips. First, we determine the events that only fell on the main (ACIS-I) chips, which have number ids 0, 1, 2, and 3.

In[10]:

ii = np.in1d(evt_data['ccd_id'], [0, 1, 2, 3])
np.sum(ii)

Out[10]:

434858

Method 1: Use numpy to make a 2-D histogram and imshow to display it

This method allows us to create an image without stretching:

In[11]:

NBINS = (100,100)

img_zero, yedges, xedges = np.histogram2d(evt_data['x'][ii], evt_data['y'][ii], NBINS)

extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

plt.imshow(img_zero, extent=extent, interpolation='nearest', cmap='gist_yarg', origin='lower')

plt.xlabel('x')
plt.ylabel('y')

# To see more color maps
# http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps

Out[11]:

<matplotlib.text.Text at 0x7fc85d8826d8>
../_images/FITS-tables_25_1.png

Method 2: Use hist2d with a log-normal color scheme

In[12]:

NBINS = (100,100)
img_zero_mpl = plt.hist2d(evt_data['x'][ii], evt_data['y'][ii], NBINS,
                          cmap='viridis', norm=LogNorm())

cbar = plt.colorbar(ticks=[1.0,3.0,6.0])
cbar.ax.set_yticklabels(['1','3','6'])

plt.xlabel('x')
plt.ylabel('y')

Out[12]:

<matplotlib.text.Text at 0x7fc85d7940f0>
../_images/FITS-tables_27_1.png

Close the FITS file

When you’re done using a FITS file, it’s often a good idea to close it. That way you can be sure it won’t continue using up excess memory or file handles on your computer. (This happens automatically when you close Python, but you never know how long that might be…)

In[13]:

hdu_list.close()

Exercises

Make a scatter plot of the same data you histogrammed above. The plt.scatter function is your friend for this. What are the pros and cons of doing it this way?

In[None]:

Try the same with the plt.hexbin plotting function. Which do you think looks better for this kind of data?

In[None]:

Choose an energy range to make a slice of the FITS table, then plot it. How does the image change with different energy ranges?

In[None]: