Personal tools
You are here: Home / COSMOS2 / COSMOS Cookbook

COSMOS Cookbook

Back to Table of Contents

COSMOS Cookbook

The following describes some procedures for handling data reduction tasks. For detailed information on each of the programs the reader should refer to the web pages for that program.

Reducing multislit spectra

Constructing bad pixel maps
Reducing Nod & Shuffle data
Reducing MOE data
Reducing LDSS3 data
Reducing Longslit data
Pipelining the reductions

Reducing multislit spectra

Let's assume that you have a mask named Mymask, and you have obtained the following set of observations with IMACS using the short camera in Nod&Shuffle; orientation, and the 200 line grism

ccd002direct mask image
ccd003spectroscopic flat
ccd004comparison arc
ccd006comparison arc
ccd008comparison arc
ccd009spectroscopic flat

Step 1: Setting up the environment

COSMOS assumes that all the data files except the FITS image files are in the current working directory, but that the FITS files are in the directory pointed to by the environment variable COSMOS_IMAGE_DIR. Everybody has their own preferences for arranging files; one convenient way is to have all the data files for an observing run in a directory dir, with subdirectories n1, n2, ... for each night's FITS files. You should cd to the directory in which you will keep and use the non-image files, then use setcosdir to specify the image directory. With the above suggested directory structure, to work with image files in the n1 subdirectory, you could either define COSMOS_IMAGE_DIR as ".", (i.e. the current directory) and refer to image files as n1/filename, or define COSMOS_IMAGE_DIR as "/home/joe-users/.../dir/n1", and refer to image files as filename.


Step 2: Setting up observation definition files

We need to set up an obsdef file for the spectroscopic observations. It also doesn't hurt to first check the direct image of the mask, just to ensure that there are no major problems. For this we need a direct image obsdef file as well.

We invoke defineobs and insert the following data for the direct image:

Dewar Offset FileSCdirect_N
Camera:SHORT f/2
Observation Definition FileMymask-direct

and for the spectroscopic observations, the following:

Dewar Offset FileSC200g_N
Camera:SHORT f/2
Observation Definition FileMymask

Note that as of COSMOS 2.20, there are now multiple versions of defineobs available. See the documentation for defineobs here for more details.

Because we have not yet used align-mask on any images, we have used the default dewar offset files (or "dewoff", for short), which are located in $COSMOS_HOME/examples/dewoff. When finished, we now have two obsdef files: Mymask-direct.obsdef, and Mymask.obsdef.
As of COSMOS 2.21, dewoff files made with newer data are also available in subdirectories within $COSMOS_HOME/examples/dewoff, with a name corresponding to the month they were added. Using these newer dewoff files for newer data could possibly bring you closer initially than using the other default dewoff files made with older data.


Step 3: Spot-check the features

One important step to take before doing any wavelength calibration is to check (and possibly adjust) your initial dewar offset file, as the initial offset may be too large for align-mask to detect:

  1. Run spectral-lines using your initial dewoff file (make sure the original dewoff file name is in the obsdef file you specify) and a line list of the features you want to use with align-mask (should be ONLY 1 to 3 bright, isolated features) to make sure that their predicted positions are relatively close to their actual positions:
    spectral-lines -o Mymask -l HNA.dat -b 1 -e
    The above command will generate a file called Mymask.xy. The -e flag is a powerful option because it will map the ends of the slit for each feature in the line list.
  2. You can then use the IRAF routines display8 and tvmark to check the initial positions.
    display8 ccd004 1 z1=0 z2=whatever
    tvmark 1 Mymask.xy
    This will display a mosaic of your lamp frames and mark the slit ends for each of the features in your line list file.
  3. Looking in your DS9 window, you then might notice either of the two following common problems:
    1. The features are shifted uniformly in one direction. In this case, align-mask can get confused because the shift is larger than the SEARCHBOX specified in the align-mask parameter file.
      Solution: Modify the initial dewoff file using the COSMOS routine adjust-offset.
    2. The predicted and actual positions of the features are rotated with respect to each other. In this case, align-mask would also get confused since the amount of rotation is larger than it can handle.
      Solution: Modify the value of the "Disperser Misalignment" (D_ALIGNROT) in the obsdef file using the COSMOS routine defineobs or your favorite text editor (see the defineobs page for more details about D_ALIGNROT).
  4. If necessary, repeat steps 1 and 2 with the new dewoff/obsdef file until step 2 shows a relatively good match. You can also run spectral-lines with a more complete line list (e.g. $COSMOS_HOME/examples/linelists/HeNeAr_short.dat) to make sure that there is no large offset or rotation. Don't worry about getting the features to match exactly, as align-mask is designed to get you an approximate initial map; adjust-map will do the fine-tuning, as far as wavelength calibration is concerned.

Step 4: Aligning masks

It's usually not even necessary to look at the direct mask images, but let's do that anyway just to make sure everything is OK (useful to do, for example, at the beginning of a run). First, we set the parameters in align-mask

editpar align-mask

type q to end

SEARCHBOX                   10       half width of search range
MAGFACTOR                  100       magnification factor for offset vectors
LAMFILE                HNA.dat       line list file
NAVER                        1       number of neighboring points to average is

Change parameter:

A SEARCHBOX of 10 is usually sufficient; if most apertures cannot be found, increase it to 20. The LAMFILE is only needed for the spectroscopic image alignment.

Now, run align-mask to check and correct the alignment.

align-mask -o Mymask-direct -f ccd002

After the first iteration, assuming that it is successful, rerun defineobs and insert ccd002 as the name of the dewar offset file. The second iteration should show quite good agreement.

Assuming that this worked well, we are ready to align the spectroscopic images.

If it didn't work:

If it did not work well, something may be very wrong. In that case we should generate a list of predicted positions using apertures and overlay it in IRAF with the mosaiced image.

apertures -o Mymask-direct -b 1 (assuming you used 1x1 binning)


display8 ccd001 1 z1=0 z2=whatever
tvmark 1 Mymask.xy

A comparison of predicted and observed apertures positions should allow you to understand the problem.

Assuming it did work:

Now we need to align the spectroscopic mask. We only do this once, using one of the exposures, for a set of spectroscopic exposures that we want to reduce as a group and combine (even though flexure may result in slight shifts from one exposure to the next.) The reason for this will be explained later. We need to specify a line list file in the align-mask parameter file. This list should consist of a small number (no more than 1 to 3)of clean, well-isolated comparison lines. In this example, we have called it HNA.dat, and specified that in the align-mask parameter file. Having done so, we can execute align-mask:

align-mask -o Mymask -f ccd004

Again, change the name of the dewar offset file in Mymask.obsdef to ccd004 after the first iteration. The second iteration should look pretty good, like this:


The typical residual is about a pixel, and is dominated, as is obvious, by systematic errors. These are due to imperfectly-mapped distortions in the IMACS optics. These small errors will be removed later when we construct and adjust the spectral map.


Step 5: Constructing the spectral map

The spectral map is a file which contains the information necessary to transform from CCD coordinates to the space of wavelength vs slit position into which we want to transform our data. The information necessary to construct a first, approximate map (which should be good to a few pixels) is all contained in the observation definition file. The map is constructed using the program map-spectra. So:

map-spectra Mymask

We will do this only once for each set of data which we wish to combine. Because this map is approximate, we use the comparison arc exposures to adjust it in the most suitable way for each science spectral exposure, using adjust-map. It's best to use a single arc exposure. adjust-map works best with bias-subtracted frames, so we can do:

biasflat ccd004


adjust-map -m Mymask -f ccd004_b

which will produce a new map file to use with the science exposure ccd005, and so forth. To repeat what was stated earlier, if we wish to later combine ccd005, and ccd007, with cosmic ray rejection, we should start with the same basic map file,, created from one observation definition file, Mymask.obsdef.

Care must be taken to pick a good list of comparison lines. Make sure that all chosen lines are clean and well-isolated, lines near to stronger lines are particularly to be avoided. Particularly when using a new line list, or a new instrumental setup, it is wise to first run adjust-map in debugging mode (add the -d flag and a slit number), so that you can see how the dispersion fits are working: you may find that one line is consistently not behaving well, or that the order of the adjustment is too high or low. It's also not a bad idea to check the map file output created by adjust-map, by using spectral-map to create a list of expected spectral line positions, then overlaying them on the image of the arc frame.

spectral-map -m ccd004_b -l linelist.dat -b 1

where linelist.dat is a file containing a comparison arc line list. Then, in IRAF:

display8 ccd004 1 z1=0 z1=whatever
tvmark 1 ccd004_b.xy

The marked positions should align virtually perfectly with the slit centers (to within the pixelization). If they're still off, try repeating adjust-map:

adjust-map -m ccd004_b -f ccd004

That should do it quite well. If it doesn't, it's probably due to a bad line fit. adjust-map produces a file called adjust-map.dat, which contains details of the line offset determinations. See the web page for adjust-map for details. Look at the values of delta(x) for the spectrum which is giving problems. There will probably be one discrepant value. Chances are that inspection will show that this line is being influenced by a neighboring line; if so, delete it from the line list and repeat the fit.

In this case, adjust-map produces a postscript file called A quicker, alternative way to check the results of adjust-map is by looking at the plots in this file. The first plot in each row is the dispersion fit for that particular slit, and can tell you if there were any problems with any of the features.


Step 6: Preparing the frames

We now need to do bias subtraction and flat-fielding of the science exposures, using biasflat. Since we will use the same bias frame for all science exposures, we can identify it in the biasflat parameter file, but we will need to specify different flat-field frames on the command line for each science exposure. We create spectroscopic flat fields for the science exposures using Sflats, in our case:

Sflats -m ccd004_b -f ccd003

will create a set of image files ccd003_flat which we will use with ccd005. Sflats normally uses the standard bad pixel file for each dewar, to mask out bad regions of the chips. If you have created your own custom bad pixel file for a mask, for example to mask out zero order grism images, you would say:

Sflats -m ccd004_b -f ccd003 -z mybadfile

Here's what a section of one of these frames looks like:

Sflat image

Now we are ready to biasflat ccd005:

biasflat -f ccd003_flat ccd005

The output will be a set of images files ccd005_f. Here's what the same section of a flattened spectrum frame looks like:

flattened spectrums

Step 7: Processing the spectra

The next step is to subtract the sky using the routine subsky.

subsky -m ccd004_b -f ccd005_f


subsky -m ccd004_b -f ccd005_f -z mybadfile

which produces a set of image files ccd005_s, a section of which looks like this:

sky subtracted frame

If the comparison arcs are well-matched to the object frames, the sky subtraction should usually be this clean, but it might not for several reasons:

  • If the spline fit parameters are not set properly, the spine fit can become unstable, creating ringing. The result will be an image which looks like this:
  • For reducing IMACS exposures with short slits, a 1-d spline fit is usually more than adequate, and is much faster than the 2-d spline fit. However, if your slits are long, and particularly if the objects sit on a variable background, you may see a significant slope across the sky subtracted spectra. In that case, use a 2-d spline.
  • Another time when 2-d splines are useful is when the spectral map does not perfectly describe the tilt of slits, resulting in residuals which look like this:

    This seldom occurs with IMACS, but does seem to be more common with LDSS3. In this case, switching to a 2-d fit should solve the problem.

    Occasionally, the spline fit for a spectrum does not converge. In this case, you will get a message like this:

    Problem fitting sky of slit 2, chip 6 0 (1)
    and this spectrum will be set to zeroes.

We are now ready to extract the spectra. You have three choices:

  1. Do a 2-d extraction on each object exposure, using extract-2dspec, then use sumspec to combine the frames with cosmic-ray rejection.
  2. Do a 1-d or 2-d combining + extraction + CR rejection using extract
  3. Using the spectrum mapping in the map file plus the sky subtracted spectral frames, design a custom extraction procedure. See Writing routines for COSMOS for more details on this.

Method 1 is the most robust at the present time, so we'll do that.

First, we extract each object exposure

extract-2dspec -m ccd004_b -f ccd005_s
extract-2dspec -m ccd006_b -f ccd007_s

If the search parameter has been set to a non-zero value, extract-2dspec will present various plots of spectrum shape and offset.

Now, having turned on cosmic-ray rejection in the sumspec parameter file, we do:

sumspec -o Mymask_2spec ccd005 ccd007

Note that we don't specify any spectrum type (like _2spec) for the input files, but must for the output file.


Constructing Bad Pixel Maps

A bad pixel map exists for each dewar, in the directory $COSMOS_HOME/sdata, with the name dewar.badpix. This file maps the location of known bad features on each chip. These files contain, the coordinates of bad pixel groups, one per line. The format is:

chip      xmin     xmax    ymin    ymax

Unless another bad pixel map is specified on the command line, Sflats and subsky use the standard files, however the user can construct and use his/her own bad pixel file, either from scratch, or using the standard files as a starting point.

In addition to bad pixels on the chips, it is often useful to block regions where unwanted spectral orders overlie the desired spectrum. This can be done by constructing a new bad pixel file for each observing setup, using the program badorders. badorders requires, as input, an observation definition file specific to each spectral order to be blocked. These files will differ from the standard observation definition file for the observing setup in the spectral order specified, and probably also in the dewar offset file, since there are usually small order-to-order differences offsets between the true positions of spectral features and those predicted by the optical model.

Constructing an appropriate dewar offset file for each order may take some work. Since the orders that one wants to block are those which lie on top of the desired order, align-mask may or may not be successful in determining a correct alignment because of the confused image. If it does not work well, it will be necessary to create one for the order by adjusting the dewar offset file created for the primary order, using adjust-offset.

Don't forget:

  • if you create the new dewar offset file with align-mask: align-mask names the dewar offset file from the arc file, so you must take care to rename each dewoff file to specify the order to which is applies, and take care that the original primary order offset file doesn't get overwritten.
  • if you create the new dewar offset file with adjust-offset: first rename the primary order offset file to something specific to each order before running adjust-offset.


Reducing Nod&Shuffle; data

Although the COSMOS routines handle most aspects of Nod&Shuffle; data automatically, there are some aspects of N&S; data reduction that must be noted:

  • Comparison arcs should not be shuffled. adjust-map does not use the shuffled images, and their proximity to the primary comparison lines may cause problems for the fit.
  • Spectral flats may either be shuffled or not shuffled. If not shuffled, the shuffled parameter in Sflats should be set to the shuffle distance of the data, so that a shuffled flat field file is produced.
  • N&S; spectra should be processed through subsky, even though no sky subtraction is done to the data, so that subsky can produce the data plane with pixel errors that are needed in the later reductions.
  • extract-2dspec extracts the shuffled regions along with the primary spectra. If the parameter sub_ns is set to "yes", the shuffled regions are subtracted from the primary spectra to subtract sky. This should be done if spectra are later to be combined, with cosmic ray rejection, using sumspec.
  • viewspectra can handle 2-dimensional N&S; data produced by extract-2dspec that either has or has not had the shuffled region subtracted. If the region has not been subtracted, the shuffle parameter should be set to "yes", and the shuffled region is subtracted before display and analysis. The 1-d spectral plots show the spectrum after subtraction of the (negative) nodded spectrum.


Reducing MOE data

Data taking with the IMACS and the MOE echelle grating is reduced like any other IMACS data. To set up MOE reductions, in defineobs select MOE (for old- MOE data) or MOE2 (for new-MOE data) from the list of gratings, and select the spectral order to be reduced. (Note that each MOE order must be reduced separately.) The grating angle should be set to zero. Template dewar offset files for MOE and MOE2 can be found in the $COSMOS_HOME/examples/dewoff directory.

The only complication in reducing MOE data is that the high density of thorium-argon comparison lines plus the as-yet-imperfect mapping of the MOE optical distortions complicate the tasks of align-mask and adjust-map: the danger of zeroing in on the wrong line is significant. To deal with this align-mask should be run using a small number of isolated comparison lines. Note that the dewar offset file produced by this process should work equally well for at least several adjacent spectral orders.

adjust-map should be run in two steps. In the first step, the same short list of isolated comparison lines should be used and the ORD_DISP and ORD_SAG parameters should be set to 1. In the second step, the map produced by the first step can be refined using a more complete list of thorium-argon lines, with ORD_DISP and ORD_SAG set to appropriately higher values.

The most important advice for reducing MOE data is to check every step of the process: do not assume that MOE data can be reduced in a hands-off, pipelined manner. Use the position lists produced by spectral-lines and spectral-map, together with the IRAF routine tvmark, to check the accuracy of the predictions of the dewoff and map files at each stage of the reductions. Check the graph plots produced by adjust-map to ensure that the fit to the comparison arcs is reliable. Inspect the sky-subtracted files to ensure that this step went well, etc.


Reducing LDSS3 data

Reducing LDSS3 data is very similar to IMACS with a few minor changes:

  • In defineobs select LDSS3.
  • To remove intermittent pattern noise for LDSS3, use the routine ldss3-pattern.
  • All data frames need to be stitched together before the alignment using the COSMOS routine stitch. The IRAF routine lstitch will not work with COSMOS.
  • Prior to running map-spectra, the parameter chipnum must be set to 1.
  • The biasflat parameter DEWAR needs to be changed to "LDSS3", since stitch takes care of all the gains and biases.
  • In subsky and Sflats, you need to change the NOISE and GAIN values, which can be found here.
  • Dewoff files can be converted from Normal orientation to Nod&Shuffle; orientation by subtracting 90 degrees. This is important for instruments such as IMACS in which the default orientation can be changed.
  • For newer LDSS3-C data (data taken after March 2014), make sure you set DEWAR to "LDSS3C-2". All of the other programs are run the same way as you would on older LDSS3 data.
  • Note that LDSS3-C data taken in Normal mode is in Normal orientation (spectra dispersed vertically), so be sure to use a dewoff file ending in "_N.dewoff". Newer dewoff files are located in $COSMOS_HOME/examples/dewoff/Aug2014/


Reducing Longslit data

Longslit data can be taken for IMACS or LDSS3. COSMOS can correct for line-curvature to produce rectified longslit data.

  • The initial dewoff alignment must be done with adjust-offset instead of align-mask. Refer to Creating and Modifying Dewoff Files documentation for specifics of modifying dewoff files.
  • Use the advanced adjust-map parameter file instead of the normal one. Make sure to rename it to adjust-map.par so adjust-map can find it.
  • Turn on the fit_curv parameter in the adjust-map parameter file in order to enable fitting for the curvature.
  • If there is a lot of scatter in the curvature plot, remove blended or confused lines from the line list or decrease the line rejection threshold.
  • By running extract-2dspec on a lamp frame, using the map from adjust-map, you can inspect how well the line-curvature is being fit.


Pipelining the reductions

Once all this becomes routine, i.e. you know how all the programs behave and how your data behaves, you can put everything from the map-spectra procedure onward into a script, using process-2spec. Note the restrictions listed in the web page before running.


Spectrum set:           Mymask_night1
Associated obsdef file: Mymask
Bad pixel file:         Mybadfile
Science frame #         005
    Bias frame:         bias
    Comparison arcs:    004 006
    Flat frames:        005

Science frame #         007
    Bias frame:         bias
    Comparison arcs:    006 008
    Flat frames:        009 010 011

Science frame #

Spectrum reduction makefile Mymask_night1.make created

The script Mymask_night1.make can be executed using the standard UNIX command make.

make -f Mymask_night1.make


Next: Creating and Modifying Dewoff Files

Back to Table of Contents