Viewing and manipulating FITS images

Authors

Lia Corrales

Learning Goals

  • TODO

Keywords

matplotlib, FITS image, table

Summary

Demonstrates the use of astropy.utils.data to download a data file, uses astropy.io.fits to open the file, uses matplotlib to view the image with different color scales and stretches and to make histrograms. Also includes a demonstration of simple image stacking.

In[1]:

import numpy as np

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

from astropy.io import fits

The following cell is needed to download the example FITS files used here.

In[2]:

from astropy.utils.data import download_file
image_file = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True )

Out[2]:

Downloading http://data.astropy.org/tutorials/FITS-images/HorseHead.fits [Done]

Opening FITS files and loading the image data

I will open the FITS file and find out what it contains.

In[3]:

hdu_list = fits.open(image_file)
hdu_list.info()

Out[3]:

Filename: /home/circleci/.astropy/cache/download/py3/2c9202ae878ecfcb60878ceb63837f5f
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     161   (891, 893)   int16
  1  er.mask       1 TableHDU        25   1600R x 4C   [F6.2, F6.2, F6.2, F6.2]

Generally the image information is located in the PRIMARY block. The blocks are numbered and can be accessed by indexing hdu_list.

In[4]:

image_data = hdu_list[0].data

You data is now stored as a 2-D numpy array. Want to know the dimensions of the image? Just look at the shape of the array.

In[5]:

print(type(image_data))
print(image_data.shape)

Out[5]:

<class 'numpy.ndarray'>
(893, 891)

At this point, we can just close the FITS file. We have stored everything we wanted to a variable.

In[6]:

hdu_list.close()

SHORTCUT

If you don’t need to examine the FITS header, you can call fits.getdata to bypass the previous steps.

In[7]:

image_data = fits.getdata(image_file)
print(type(image_data))
print(image_data.shape)

Out[7]:

<class 'numpy.ndarray'>
(893, 891)

Viewing the image data and getting basic statistics

In[8]:

plt.imshow(image_data, cmap='gray')
plt.colorbar()

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

Out[8]:

<matplotlib.colorbar.Colorbar at 0x7f85d76acd30>
../_images/FITS-images_17_1.png

Let’s get some basic statistics about our image

In[9]:

print('Min:', np.min(image_data))
print('Max:', np.max(image_data))
print('Mean:', np.mean(image_data))
print('Stdev:', np.std(image_data))

Out[9]:

Min: 3759
Max: 22918
Mean: 9831.481676287574
Stdev: 3032.3927542049046

Plotting a histogram

To make a histogram with matplotlib.pyplot.hist(), I need to cast the data from a 2-D to array to something one dimensional.

In this case, I am using the ndarray.flatten() to return a 1-D numpy array.

In[10]:

print(type(image_data.flatten()))

Out[10]:

<class 'numpy.ndarray'>

In[11]:

histogram = plt.hist(image_data.flatten(), bins='auto')

Out[11]:

../_images/FITS-images_24_0.png

Displaying the image with a logarithmic scale

Want to use a logarithmic color scale? To do so we need to load the LogNorm object from matplotlib.

In[12]:

from matplotlib.colors import LogNorm

In[13]:

plt.imshow(image_data, cmap='gray', norm=LogNorm())

# I chose the tick marks based on the histogram above
cbar = plt.colorbar(ticks=[5.e3,1.e4,2.e4])
cbar.ax.set_yticklabels(['5,000','10,000','20,000'])

Out[13]:

[<matplotlib.text.Text at 0x7f85cf662c88>,
 <matplotlib.text.Text at 0x7f85cf670b70>,
 <matplotlib.text.Text at 0x7f85cf7f00f0>]
../_images/FITS-images_28_1.png

Basic image math: image stacking

You can perform math with the image data like any other numpy array. In this particular example, I will stack several images of M13 taken with a ~10’’ telescope.

I open a series of FITS files and store the data in a list, which I’ve named image_concat.

In[14]:

base_url = 'http://data.astropy.org/tutorials/FITS-images/M13_blue_{0:04d}.fits'

image_list = [download_file(base_url.format(n), cache=True)
              for n in range(1, 5+1)]
image_concat = [fits.getdata(image) for image in image_list]

Out[14]:

Downloading http://data.astropy.org/tutorials/FITS-images/M13_blue_0001.fits [Done]
Downloading http://data.astropy.org/tutorials/FITS-images/M13_blue_0002.fits [Done]
Downloading http://data.astropy.org/tutorials/FITS-images/M13_blue_0003.fits [Done]
Downloading http://data.astropy.org/tutorials/FITS-images/M13_blue_0004.fits [Done]
Downloading http://data.astropy.org/tutorials/FITS-images/M13_blue_0005.fits [Done]

Now I’ll stack the images by summing my concatenated list.

In[15]:

# The long way
final_image = np.zeros(shape=image_concat[0].shape)

for image in image_concat:
    final_image += image

# The short way
# final_image = np.sum(image_concat, axis=0)

I’m going to show the image, but I want to decide on the best stretch. To do so I’ll plot a histogram of the data.

In[16]:

image_hist = plt.hist(final_image.flatten(), bins='auto')

Out[16]:

../_images/FITS-images_36_0.png

I’ll use the keywords vmin and vmax to set limits on the color scaling for imshow.

In[17]:

plt.imshow(final_image, cmap='gray', vmin=2E3, vmax=3E3)
plt.colorbar()

Out[17]:

<matplotlib.colorbar.Colorbar at 0x7f85ce6f45c0>
../_images/FITS-images_38_1.png

Writing image data to a FITS file

This is easy to do with the writeto() method.

You will receive an error if the file you are trying to write already exists. That’s why I’ve set clobber=True.

In[18]:

outfile = 'stacked_M13_blue.fits'

hdu = fits.PrimaryHDU(final_image)
hdu.writeto(outfile, overwrite=True)

Exercises

Determine the mean, median, and standard deviation of a part of the stacked M13 image where there is not light from M13. Use those statistics with a sum over the part of the image that includes M13 to estimate the total light in this image from M13.

In[None]:

Show the image of the Horsehead Nebula, but in to units of surface brightness (magnitudes per square arcsecond). (Hint: the physical size of the image is 15x15 arcminutes.)

In[None]:

Now write out the image you just created, preserving the header the original image had, but add a keyword ‘UNITS’ with the value ‘mag per sq arcsec’. (Hint: you may need to read the astropy.io.fits documentation if you’re not sure how to include both the header and the data)

In[None]: