Astropy: Handling FITS files
Documentation
For more information about the features presented below, you can read the astropy.io.fits docs.
Data
The data used in this page is a model of the gamma-ray sky background used for the LAT instrument on the Fermi telescope, as well as the Fermi/LAT point source catalog.
Reading FITS files and accessing data
Opening a FITS file is relatively straightforward. We can open the background model included in the tutorial files:
The returned object, hdulist, behaves like a Python list, and each element maps to a Header-Data Unit (HDU) in the FITS file. You can view more information about the FITS file with:
As we can see, this file contains two HDUs. To access the primary HDU, which contains the main data, you can then do:
The hdu object then has two important attributes: data, which behaves like a Numpy array, can be used to access the data, and header, which behaves like a dictionary, can be used to access the header information. First, we can take a look at the data:
This tells us that it is a 3-d cube. We can now take a peak at the header:
which shows that this is a Plate Carrée (-CAR) projection in Galactic Coordinates, and the third axis is photon energy. We can access individual header keywords using standard item notation:
Provided that we started up ipython with the --pylab flag, we can plot one of the slices in photon energy:
Note that this is just a plot of an array, so the coordinates are just pixel coordinates at this stage. The data is stored with longitude increasing to the right (the opposite of the normal convention), but the Level 3 problem at the bottom of this page shows how to correctly flip the image.
Modifying data or header information in a FITS file object is easy. We can update existing header keywords:
or add new ones:
and we can also change the data, for example extracting only the first slice in photon energy:
Note that this does not change the original FITS file, simply the FITS file object in memory. Since the data is now 2-dimensional, we can remove the WCS keywords for the third dimension:
You can write the FITS file object to a file with:
if you want to simply write out this HDU to a file, or:
if you want to write out all of the original HDUs, including the modified one, to a file.
Creating a FITS file from scratch
If you want to create a FITS file from scratch, you need to start off by creating an HDU object:
and you can then populate the data and header attributes with whatever information you like:
Note that setting the data automatically populates the header with basic information:
and you should never have to set header keywords such as NAXIS, NAXIS1, and so on manually. We can then set additional header keywords:
and we can then write out the FITS file to disk:
Convenience functions
In cases where you just want to access the data or header in a specific HDU, you can use the following convenience functions:
To get the data or header for an HDU other than the first, you can specify the extension name or index. The second HDU is called energies, so we can do:
or:
and similarly for getheader.
Accessing Tabular Data
Tabular data behaves very similarly to image data such as that shown above, but the data array is a structured Numpy array which requires column access via the item notation:
However, as we saw on the notes on the Table class, it is often easier to simply read in FITS tables using Table.read:
Practical Exercises
Level 1
Try and read in one of your own FITS files using astropy.io.fits, and see if you can also plot the array values in Matplotlib. Also, examine the header, and try and extract individual values. You can even try and modify the data/header and write the data back out - but take care not to write over the original file!
Level 2
Read in the LAT Point Source Catalog (data/gll_psc_v08.fit) and make a scatter plot of the Galactic Coordinates of the sources (complete with axis labels). Bonus points if you can make the plot go between -180 and 180 instead of 0 and 360 degrees. Note that the Point Source Catalog contains the Galactic Coordinates, so no need to convert them.
Level 3
Using Matplotlib, make an all-sky plot of the LAT Background Model in the Plate Carée projection showing the LAT Point Source Catalog overlaid with markers, and with the correct coordinates on the axes. You should do this using only astropy.io.fits, Numpy, and Matplotlib (no WCS or coordinate conversion library). Hint: the -CAR projection is such that the x pixel position is proportional to longitude, and the y pixel position to latitude. Bonus points for a pretty colormap.