Introduction of a first Spectroscopic Program

As a first task we want to look at an already reduced 1d-spectrum in the usual .fit-format graphically. Therefore we have to write a script which contains the necessary instructions for Python3. We assume that the user knows the most important Python commands, i.e. how to handle files and variables, how to create loops and embedded instructions, i.e. minimal programming knowledge with Python3. And that the modules to be imported are also installed (with ).1d_Spectrum_plot

In the figure we see in the left window the script 1d_Spectrum_plot.py (download here). This is a simple text file that can be displayed and manipulated in any editor. We express that it is a Python program with the extension .py. Let’s have a look at this text. In green a descriptive text is enclosed in triple “. This is the text that is displayed when the help for the file is called. It should be as informative and short as possible.

Below this there are three import commands. Here the modules ( = program packages) are called, which cause the following:

numpy is a module for faster processing of numeric data. In particular, it provides matrices in which data can be stored and then quickly regrouped, modified, and processed. numpy is abbreviated np and is used under this alias (nickname).

astropy.io is part of the big astronomical package Astropy . It is used to read and write data, e.g. fits files. Of course, this package must first be installed in our Python3 before we can import and use it:

pip install astropy

In the script we only import the fits package from astropy.io because we don’t need  more.

– The matplotlib module contains a treasure trove of graphical possibilities. It must also be installed before we can import the submodule matplotlib.pyplot under the alias plt.

This gives us everything we need to start the actual program.

Of course we have to tell the program which spectrum we want to look at in fits format. This is done with the statement

file = input(‚Enter path and file name:  ‚)

We define a string variable named file, to which we assign the string  that we enter manually in the console after the prompt ‚Enter path and file name: ‚. The complete relative or absolute path to the fits file must be entered here if it is not in the current working directory (which is selected and displayed in the top right corner of the Spyder window).

In line 24 we afford a luxury: We want to know what is in the header of the spectrum, i.e. which object it is, when the recording took place, etc. First we have to open the file (line 23). Here we see the typical formulation of an object-oriented language. We call the fits.open() method from the fits instance (which we imported in line 16), which then opens the file previously defined in the file (makes it readable). We give this opened object any identifier, here sp.

In the next line 24 we cause the program to print the header of sp in the console (with the command print() ). The 1d-spectrum contains only one dimension (along the dispersion), which becomes accessible by sp[0]. And since we want to see the header, the method sp[0].header gives exactly the complete content of the header, which is printed out in the console after the manually programmed explanation ‚Header of the spectrum : ‚. If we run the program from the editor in the console (key F5 or green arrow), we get the header expression as in next figure. 1d_Spectrum_plot_spyder.png

All keywords of the header are displayed in the bottom right corner of the console. And also all variables are listed in the variable explorer, incl. variable type, size and the first values. Following header values are important for further calculations:

1. NAXIS1 = 3362 = Axis length = number of pixels / wavelength points

2. CRVAL1 = 6464.09665924841 = Start of wavelength scale in Angstrom

3. CDELT1 = 0.0683598419022372 = step size, i.e. the wavelength interval of the individual pixels.

The fits files contain the header and the actual data, here in sp[0].data. This is simply a line with NAXIS1 values, namely the spectral intensities (Flux). With the above 3 header values the corresponding wavelengths can be calculated. For example for the 100th pixel by

Wavelength[99] = CRVAL1 + 99 * CDELT1

The index count in Python starts with 0, so wavelength[0] = CRVAL1 + 0 * CDELT1 = the start wavelength of the spectrum on the short wavelength side.

In the upper right corner of Fig. the variables already calculated are listed in the variable explorer after this program run. You should always take a look at this and check the variables for plausibility and the correct type.

In the further course of the program text we start in line 26 with the calculation of the wavelengths, which belong to the flux values, so that we can represent them afterwards graphically as spectrum.

Let us remember: sp[0].data contains the 3362 flux values. We pass them into a numpy object, namely a numpyarray (a special matrix) and call this variable flux. In line 28 we create a numpyarray called wave with loud ones (1.), with exactly NAXIS1 (here 3362) pieces. In the following we fill it with the wavelengths of the pixels. For this purpose we form the wavelength in the loop in lines 29 and 30 pixels for each pixel and store it in the wave element i. After this has been done 3362 times, our matrix wave is filled with the wavelengths (= numbers in float format).

In line 35 we close the fits file, we don’t need it anymore.

Then we start programming the spectrum plot in line 38. This is done according to the rules described in the matplotlib documentation, .

Let’s run the program  and look at the plot of the spectrum that our program has now created. Note that we have selected the settings for the IPython console so that the graphic is displayed in a separate window. This has the advantage that this window is active. We can manipulate the graphic in the window (zoom, move axes, change labels and curve styles) and save the desired result manually, for example as PNG. This would not work if we integrated the graphic „inline“ into the console.Figure_1