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:
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
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")
pylab.savefig("plot_spectra.png")
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).








