Pyspec – a python spectroscopy tool

This document lays out the basics of installing and using the Python tool pyspec (currently under testing and so is version 0, an example final plot can be found below). The installation steps are given for Ubuntu but all of the tools used here are standard python tools and can be install on a variety of different systems, given is the current webpage for each package.

PySpec Calibration Example

Getting pyspec

The latest version of pyspec, along with some example data can be found [here] (it is a 1.4MB download).

To take this a bit further, I’ve not built this into the main package yet though I’ve put together another python script that allows you to perform flux calibrations – see [pyspec_flux] (it is a 10.7kB download). For this you need to have observed a standard star over the same wavelength range and to have the theoretical data (well the properly calibrated flux data and this can be found at [vizier] for most calibration stars. This tool works just like pyspec – you should run ./pyspec_flux.pyc and input your data tables (wavelength, flux).

PySpec with the flux calibrated

Spectroscopy with pyspec a short manual

A short manual describing the functions of pyspec in more detail than on this page (though the below instructions gives you all you need to know) is available: pyspec_man.pdf.

Installing dependencies

Python (at least Python 2.5.2)

sudo apt-get install python

You may also need to install Ipython:


sudo apt-get install python-pyfits



gunzip asciidata-1.1.1.tar.gz
tar -xvf asciidata-1.1.1.tar
cd asciidata-1.1.1
sudo python install


sudo apt-get install python-scipy


sudo apt-get install python-matplotlib


sudo apt-get install python-tk


DS9 is not required for running the software but is used for visualisation of the FITS file such that the correct calibration can be made.

tar -zxf ds9.linux.5.6.tar.gz
sudo mv ds9 /usr/local/bin
sudo chmod +x /usr/local/bin/ds9

So you want a fit? Well there is a bit of preparation todo…

Firstly before you run the script you need to ensure that all of your files follow certain conventions:

Bias files should be named bias*.fit where the * represents any postive interger. The same is said for your dark*.fit files.

Your input reference lamp spectra/target observation can have any file name, but for consistancy it is suggested you have them named

You should now open up your reference lamp fits file and using the calibration data (see: ) find the lines and record the results in a table ref.txt (you can use pico or any other text editor todo this). The table is just a table of pixel and wavelength values (see ref_example.txt for the structure). You also need to look at your dataset and find the correct row that you would like to extra and the number of rows over you wish to sum, for weakly exposed spectra you might want to make this large (however, for spectra with rotation structure, e.g. Saturn you will want todo this over a narrow range).

Running the program

Move to the directory you extracted pyspec to and simply type:


You will be asked if you want to apply a previous calibration. If you have already determined the calibration of a reference lamp and want to apply this to a secondary dataset, hit Y. This will then guide you through the steps to calibration a target (currently the software, if you hit N will only calibrate only target). If you choose N then you will go through the whole process of calibration, if is a fairly simple process.

Outputted data (if option N, and full calibration is undertaken):

cal_out.txt – a data table of pixel vs wavelenght for your final calibrated dataset, useful for reploting data / reading into fitting tools (you can read this data into IRAF or anything else really).

output.txt – a line of numbers, these represent the values determine of the median values of bias and the dark-bias, c and m from the linear regression. This is used if you choose not to repeat the whole process for another dataset.

fig_Ia.png – pixel / intensity plot for reference lamp.
fig_Ib.png – the wavlenght calibration regression, completed by python tool polyfit.
fig_Ic.png – the wavelength calibrated reference lamp.
fig_Id.png – the calibrated target dataset.
fig_out.png – the calibrated target dataset (if reapplying the calibrated dataset).


That’s it really, its a pretty simple task…

Example Data

If you wish to test pyspec some example data taken with the University of Birmingham Observatory can be found in the tar file, the data is in the directory example_data – just move the data into the same directory as pyspec.pyc and you can use this data.

Source code: will be released once this is past basic debugging.

Comments are welcome… please contact me through this site.

5 responses to “PySpec”

  1. Melissa says :

    Hello, I am just beginning a masters in astronomy and I am looking for an alternative to iraf in python. Even though pyspec is still in it’s infancy, I am greatly interested. May I be so bold as to enquire, can I have a look at the source code?

    • starrydude says :

      Hi Melissa, the code used to be just up on the page. I’ll check and if not try and dig you out the latest copy. I’m currently on holiday so might not be straight away. Don’t forget their is also pyraf a much more mature and sophisitcated system:

      • starrydude says :

        Ah I see I must have not packed up the original source and just put up the .pyc file.. the code is probably quite a mess given I’ve tinkered with it since this posting. I found the original code, with some messing – should all work. This can be retireved from Dropbox –

      • Melissa says :

        Thank you very much for the code, it’s greatly appreciated. I have also heard of pyraf, but I haven’t tried it yet.

      • starrydude says :

        I’d very much suggest giving pyraf a go. I guess it really depends what you want to do. If you want all the functionality of iraf callable in python then it makes sense. Personally I’ve always been happy to just run iraf when needed. As you’ll see on my other pages I quite often would call my python scripts and iraf from the command line. Then I’d combine it into a little shell script for easy parallelism.

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: