Tutorial On Simple Spectrum Extraction


Amateur astronomers have increasingly better access to affordable spectrographs they can use with their telescope to take spectra of objects in the night sky. This opens up a whole new domain for citizen science. But to do real science, it helps to understand exactly how the data are gathered.

For most amateurs, their spectrograph might come with some canned software that is for all intents and purposes a black box that you put the CCD images from the spectrograph in one end and get one-dimensional spectra out the other with no hint of what happens in-between. One goal of this tutorial is to de-mystify that process and make amateurs as confortable with reducing spectra as they are with reducing CCD images or photometry. The other goal is to dymystify the reduction of spectra and unchain the enthusiastic amateur from their black-box to let them understand and take more control of how their data is reduced.

The Theory Behind Reducing Spectra

Many of the basic principles of CCD image reduction also apply to the reduction of spectra. The CCD is broken up into pixels. Each pixel can be thought as a mini-detector of light with its own calibration properties. In order to calibrate the pixel we need to know how much noise it would have gathered without the light falling on it and how it responds relative to the other pixels around it.

The Dark frame is used to tell how much noise there is in the pixel. The Flat frame is used to calibrate its relative response to other pixels.In spectroscopy the Dark is an important calibration. And we will apply the dark in very much the same way you would use a Dark frame for normal CCD imaging. The Dark will be subtracted from the spectrum to remove the background noise in each pixel.

The Flat plays a different roll in spectroscopy. The Flat is not directly divided into the CCD frame containing the spectra. Instead it is extracted identically to the spectra and modeled with a low-order polynomial. That polynomial of the extraction from the Flat is divided into the data. It is also important to understand that unless you are doing spectral photometry (not a recommended project for most amateurs) that the Flat is a completely unnecessary step in reducing spectra. If you only plan to measure the Doppler shifts of features or changes in them relative to the continuum, then the flat-field calibration, while it might make the spectra more presentable, is not needed.

In spectroscopy there is one additional calibration that is not done in normal CCD imaging. To analyze your spectrum you will need to map the row number on your CCD to wavelength. This is done with a wavelength calibration, also called a Comparison spectrum. The Comparison is reduced the same way as the spectrum of your target star and then you identify known features in the Comparison to map the CCD row number to the wavelength in both the Comparison and your target spectrum.

What You Need For The Tutorial

Below are the links to two FREE software packages that you will need to complete this tutorial. Please download them and install them on your computer before you begin.

In addition to those software packages you will need to download the following four FITS files that you will need to follow along with the tutorial.

  • spectrum.fit
  • dark.fit
  • comp.fit
  • flat.fit

  • The Tutorial

    1. Getting Started

    Start up DS9 and open up the file spectrum.fit using the "Open" option under the "File" menu.

    To make the spectrum easier to see go under the "Zoom" menu and zoom the image so that it can fit on your screen. Also go to the "Sclae" menu and select "Log" to display the brightness as a logrithmic scale. You should see something similar to the picture below.

    Screen shot #1

    2. Selecting the Projection Tool

    The Projection region tool in DS9 can be used to make one-dimensional (1D) extractions out of the two-dimensional (2D) CCD images. Select the Projection tool from the "Regions" menu in DS9 as shown below.

    Screen shot #2

    3. Select the region to extract

    Click and drag the mouse across the spectrum.fit image and it will leave behind a green line that traces out the projection that is extracted and graphed in a small secondary window that pops up. Draw your projection region so that it goes across one order of the spectrum. You can expand the width of the box by "grabbing" the dot at the center with your mouse and pulling it. See below for an example.

    Screen shot #3

    Another more precise way to determine the location of the projection region is to double click on it and pop-up the projection control box. In that window you can set the coordinates of the end-points of the projection and its thickness. This will come in very handy when we want to extract exactly the same pixels from the Dark, Flat, and Comp that we extracted from the spectrum.

    If you want to follow along endter the numbers shown in the box below into your projection region control box AND click "Apply."

    Screen shot #4

    3. Saving the 1D extraction

    Select the projection region graph window and while in that window select "Save As" from the "File" menu. For the purpose of this tutorial we will save the 1D extraction from spectrum.fit as spectrum.txt.

    Screen shot #5

    4. Extract the Dark, Flat, and Comparison

    Next follow steps 1 through 3 to extract the same set of pixels from the Dark (dark.fit), Flat (flat.fit), and Comparison (comp.fit) that you extracted from spectrum.fit. IT IS CRITICAL that the extractions of the Dark and Comparison have the same parameters (see the screenshot of the window in step #2) that you used for spectrum.fit. You want to make sure that the first pixel in each extraction refers to the same pixel on the CCD in the spectrum, Dark, and Comparison images.

    Below are the extractions from each image that I got. You can compare these with your files to make sure that you are on the right track.

    5. Subtract the Dark

    The next step should be familiar to those of you who have done a lot of CCD imaging or photometry. The spectrum.txt extraction still contains the "dark current" for the CCD pixels. We need to subtract that using the dark.txt extraction.

    To do this just load spectrum.txt and dark.txt into a spreadsheet program (i.e. Microsoft Excel) and subtract the values in dark.txt from the cooresponding rows in spectrum.txt. Save this file as spectrum.darksub.txt. An example of the result is below.

    The dark has already been subtracted from the Flat and Comparison provided in this tutorial. This demonstrates and excellent point. If the software you use to take the CCD images with your spectrograph allows you to subtract a dark from your spectrum.fits, then you can go ahead and apply the dark that way. That will yeild exactly the same results as the procedure that we have followed here.

    6. *OPTIONAL* Flat-fielding *OPTIONAL*

    This step is completely optional. As explained before, you do not need to flat-field your spectrum unless you need to do an flux calibration (which is not necessary for most measurements).

    If you were doing normal CCD image reductions you would divide the Flat image (that is also dark subtracted) directly into your object image at this point. With spectroscopy we do not divide flat.txt directly into spectrum.darksub.txt. Doing that directly could increase the noise in your reduced spectrum by up to a factor of 1.4.

    Instead you should fit a low-order polynomial to flat.txt as a function of CCD row in the extraction. How you fit this function is up to you. If I was doing this, I would use the "fit" function in gnuplot. But you could just as easilly read it into Microsoft Excel and fit a polynomial to it. When you do that, it is worthwhile to check (and fine tune) the fit of your ploynomial by plotting it with the flat.txt data and seeing how well it matches.

    Comparison plot of flat.txt and polynomial fit

    For this flat, a best fit of a sixth order polynomial yeilds the equation: f(x) = (3922.31) + (11.1949)*x + (0.0328195)*x^2 + (-7.287e-5)*x^3 + (6.3027e-8)*x^4 + (-2.5858e-11)*x^5 + (3.9247e-15)*x^6

    The values in this function have to be normalized before you do anything with it. If you divide the unmornalized function above into your numbers in spectrum.darksub.txt, it will make all your y-axis values ridiculously small. There are many ways to normalize a function like this. What I'll do here is take the value of flat.txt at the mid point (around column 1009) and divide that into the function (f(x)). The normalization value for this example is 16579.

    Once you have the ploynomial fit, comput its value in each row and divide that number into spectrum.darksub.txt and produce the file spectrum.flat.txt. The you can check you own results against the file linked below.


    The data used in this example is flawed to illustrate a point. It shows that flattening a spectrum might not be helpful. Not that the dip in the flat between column 500 and 1000 is not fit by the polynomial. This means that dip (which is probably due to contamination on the CCD, i.e. ice crystals condensing) is not removed from the spectrum by flattening. However, notice that the flat might be helpful for identifying these "dents" in the spectrum that are instrumental rather than in your observed source. When you start measuring your spectrum you are going to have to keep this in mind: which features in it are instrumental effects.

    7. Calibrate the Wavelength Scale

    Download and install Specview (see above). Specview is free software to display and measure spectra. Really all you need to display your spectrum is a plotting program. But to accurately measure line centers and equivalent widths you need the tools included in Specview. Specview has a lot of bells and whistles you may never use. In this tutorial the focus is on just the basic functions.

    You are going to have to edit comp.txt in order to read it into specview. Specview expects information about the title of the spectrum and the wavelength and flux units at the top of the file. Open up comp.txt in a text editor and insert the following three lines at the top of the file:


    Save and close the file. For a full explanation of what you did by adding these see the specview documentation page:


    Open up specview and use the "Open" option under the "File" menu to open up comp.txt. Note that to get the X-axis to display correctly you should click on the "Plot" button at the top of the window that opens with the spectrum and select "Linear" from the drop-down menu. You should see a window similar to the screenshot shown below. This is a typical Mercury-Argon spectrum that the Barber Observatory uses to calibrate our spectra.

    Screen shot #6

    To zoom in on part of the spectrum, click on the spectrum and drag the mouse (holding the mouse button down). To return to the full view click on the botton at the top of the window that looks like this:Rest Button

    For a full explanation of the features on this screen seen the specview documentation.


    Your next task is going to be to identify the lines in this spectrum and measure their line centers (in CCD row number). You can do this by zooming in on each line and using the cursor to measure the peak of each feature. The spectrum below has been annotated with the wavelengths of each line.

    Annotated graph of comparison spectrum

    It may take some practice with your spectrograph and comparison source to learn how to identify the lines in your comparison spectrum. But once you have done it well once the comparison spectrum will only shift around on the CCD, it will not change too much.

    Here is a table of the expected wavelengths and CCD column where they appear for the example in this tutorial:

    Wavelength (nm)Column

    Fit a function to the pairs of rows you measured and known wavelengths. Normally a second or third order polynomial fit is sufficient. Now you know which row number corresponds to which wavelength. A linear fit to the data in the table above gives: (wave) = 746.503 + 0.03259

    You can use this function in a spreadsheet to change the row numbers in spectrum.darksub.txt into wavelengths. An example of the finished file (spectrum.final.txt) is here.


    8. Making your Spectrum Specview Readable

    As a last step you need to add three lines to the top of spectrum.final.txt so that specview can read it. Open up spectrum.final.txt in a text editor and add the following lines:


    A full list of the keywords that specview accepts in the header for the second two lines (units for the wavelength and flux axis) is given here:


    The wavelength units as we reduced them are nanometers (nm). However, note that we have not actually calibrated our spectrum to be direcly in units of erg/s/cm2/Angstrom. However the counts in your spectrum are direcly proporotional to those values and with those units invoked, specview will correctly compute equivalent widths for features you select.

    Congratulations! You can now load your reduced spectrum into specview and start doing science!

    Wrap Up

    To recap, this tutorial is intended to cover only the basics of what needs to be done to a CCD image from your spectrograph in order to get it ready to do science. You will probably find this procedure too cumbersome and interactive to do it with each and every one of your spectra but it should help you understand the flow and method enough to write your own software or start using the canned software that came with your spectrograph more effectively. Hopefully there is also value in understanding how to do it yourself bare-knuckled if you had to.

    Return to the SAS Spectroscopy workshop webpage.

    Last Modified: May 17, 2012