Spectral Extraction in Python

I’ve wrote about how to read in FITS files in Python before, but I thought I’d readdress as I’ve been writing lots about fitting and wanted to build up to fitting properly calibrated data. So in this example I will add how to extract a spectra too. For this example I’m going to use a calibration lamp taken at the University of Birmingham Observatory. Both the script described here and the data will be given below. The output looks like:
Spectral Plotting
So let’s start with reading in a FITS file. Firstly ensure you have the most useful per-requisites ([Pyfits], [numpy], [scipy]) and import them:

import pyfits, numpy, scipy

Now read in the file:

input_file = “A.fits”
hdulist = pyfits.open(input_file)

Get the data:

img_data = hdulist[0].data

Get the header:

img_header = hdulist[0].header

(more on FITS)
Now lets use some calibration data (which can be downloaded):

import pyfits, numpy, scipy, pylab
from scipy import *
from pylab import *
input_file = “Neon.fits”
hdulist = pyfits.open(input_file)
img_data = hdulist[0].data

We know that the spectra, by looking at the FITS file with DS9 has data in a small aperture. For now we will ignore any averaging. Lets take line 144 as we know there is data.

data_use = img_data[144]

Plot the spectra:

fig = figure(figsize=(9, 9)) #make a plot
ax1 = fig.add_subplot(111)
ax1.plot(img_data[144]) #spectrum
setp(gca(), ylabel=”Un-normalised power”, xlabel=”Pixel Position”)

We have now extracted the data for a spectral column. I will follow this post up in the not to distant future with how to now identify the lines and run spectral calibration. Once you have this you can apply to a target spectra. Of course I’ve so far also ignored dark/bias counts but these can be easily dealt with.
The above can be downloaded as extract_spectra.py and the data Neon.fits (this is zipped, use gunzip).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: