This web page goes through the steps involved in reducing GNIRS cross-dispersed (XD) spectra using the Gemini IRAF package (v1.11beta2 at the time of writing; check this page for the latest version). These spectra of a nearby galaxy nucleus were taken with the short (0.15 arcsec/pix) blue camera and 32 l/mm grating, the most commonly-used XD mode, but the data reduction steps will be broadly applicable to data taken in other XD configurations as well. The IRAF tasks can be used in several different ways according to the characteristics of the specific data set and the user's personal preference. The procedure shown here will not be optimal for all XD observations, but is intended as a basic guide for those new to GNIRS data.

For more information about GNIRS data reduction and how the tasks can be used, try the following:

The steps discussed on this page are:

  1. Inspecting and sorting the data
  2. Preparing the data reduction environment
  3. Pattern noise cleaning
  4. Dealing with radiation events in short camera data
  5. "Preparing" the data
  6. Making the flatfield
  7. Cutting, flatfielding and sky subtracting the science data
  8. Wavelength calibration and s-distortion correction
  9. Extracting spectra
  10. Telluric line cancellation and flux calibration



Sorting/inspecting the data

We will be working on data taken for GN-2011A-Q-126, a poor weather program designed to make use of the telescope in cloudy conditions. Usually the PI will download their data from the Gemini Science Archive at the CADC. Files retrieved from the archive may contain various types of data, depending on the search method: acquisition images, calibration data, science target spectroscopy, etc. Before starting the reduction, we need to work out which files are which. There at least three ways of doing this, and it can be helpful to combine a couple of them to get a good sense of the data.

  1. Use the OT

    - As observations in an observing programme are executed, their colour changes to reflect their status. Green, "ready" observations change to black on completion, so the above left screenshot shows that the acquisition and spectroscopy of NGC 4736 in the "Nucleus: IQ85 CCAny" group has been observed, along with a telluric standard and some daytime calibrations. The grey, "inactive" observations are no longer relevant and will not be scheduled.

    - The shaded steps in the "Sequence" view are those that have been completed. In this case, the first ABBA was executed, then the observer skipped ahead to take the flats and arcs and then move onto another programme as the weather was improving enough to allow regular queue observations.

    - The righthand screenshot shows the "Observation Log". This gives the filenames associated with the individual steps and also the "QA State". The QA state can be "Pass" (no known problems that would make the observations unsuitable for the intended science use), "Usable" (problems such as violation of the requested observing condition constraints, but possibly useful anyway), "Fail" (fatally compromised) or "Undefined". As Q126 was a low-priority "filler" programme, no quality assessment has been performed and all the QA states are undefined. Clicking on the "Comments" tab would show any comments left by the observer as they acquired the data.


  2. Use the observing log downloaded from the archive

    This extract gives a chronological record of the observations, showing the observer's comments as well as file numbers and details of the instrument configuration, object being observed, etc. In this case we can see that the observer noticed that the standard star spectra were saturated and reduced the exposure time to compensate.

  3. Use the file headers. For example:
     hselect N20110516*[0] field=$I,OBJECT,OBSTYPE,OBSCLASS expr=yes
    N20110516S0151.fits[0]  "NGC 4736"      OBJECT  acq
    N20110516S0152.fits[0]  "NGC 4736"      OBJECT  acq
    N20110516S0153.fits[0]  "NGC 4736"      OBJECT  acq
    N20110516S0154.fits[0]  "NGC 4736"      OBJECT  acq
    N20110516S0155.fits[0]  "NGC 4736"      OBJECT  science
    N20110516S0156.fits[0]  "NGC 4736"      OBJECT  science
    N20110516S0157.fits[0]  "NGC 4736"      OBJECT  science
    N20110516S0159.fits[0]  "NGC 4736"      OBJECT  science
    N20110516S0160.fits[0]  Ar      ARC     partnerCal
    N20110516S0161.fits[0]  Ar      ARC     partnerCal
    N20110516S0162.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0163.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0164.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0165.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0166.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0167.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0168.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0169.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0170.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0171.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0172.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0173.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0174.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0175.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0176.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0177.fits[0]  GCALflat        FLAT    partnerCal
    N20110516S0178.fits[0]  HIP65159        OBJECT  acqCal
    N20110516S0179.fits[0]  HIP65159        OBJECT  acqCal
    N20110516S0180.fits[0]  HIP65159        OBJECT  acqCal
    N20110516S0181.fits[0]  HIP65159        OBJECT  acqCal
    N20110516S0182.fits[0]  HIP65159        OBJECT  partnerCal
    N20110516S0183.fits[0]  HIP65159        OBJECT  partnerCal
    N20110516S0184.fits[0]  HIP65159        OBJECT  partnerCal
    N20110516S0185.fits[0]  HIP65159        OBJECT  partnerCal
    N20110516S0186.fits[0]  HIP65159        OBJECT  partnerCal
    N20110516S0187.fits[0]  HIP65159        OBJECT  partnerCal
    N20110516S0188.fits[0]  HIP65159        OBJECT  partnerCal
    N20110516S0189.fits[0]  HIP65159        OBJECT  partnerCal
    N20110516S0379.fits[0]  GCALflat        FLAT    dayCal
    N20110516S0380.fits[0]  GCALflat        FLAT    dayCal
    N20110516S0381.fits[0]  GCALflat        FLAT    dayCal
    N20110516S0382.fits[0]  GCALflat        FLAT    dayCal
    N20110516S0383.fits[0]  GCALflat        FLAT    dayCal
    N20110516S0408.fits[0]  Dark    DARK    dayCal
    N20110516S0409.fits[0]  Dark    DARK    dayCal
    N20110516S0410.fits[0]  Dark    DARK    dayCal
    N20110516S0411.fits[0]  Dark    DARK    dayCal
    N20110516S0412.fits[0]  Dark    DARK    dayCal
    N20110516S0413.fits[0]  Dark    DARK    dayCal
    N20110516S0414.fits[0]  Dark    DARK    dayCal
    N20110516S0415.fits[0]  Dark    DARK    dayCal
    N20110516S0416.fits[0]  Dark    DARK    dayCal
    N20110516S0417.fits[0]  Dark    DARK    dayCal

    Evidently the galaxy was acquired and then four spectra were taken. These were followed by arcs and flats, then a telluric standard was acquired and observed. More flats were taken the morning after the nighttime observations, along with a set of darks. (The darks were taken for test purposes and are not necessary for the reduction.) Note that comments from the observer or QA staff are not recorded in the headers, although the QA states are.

Of course, it's always a good idea to look at each of the files and/or do some statistics before starting to reduce the data. The figure below shows some of the data taken for this program. Clockwise from top left:



Preparing the data reduction environment

Before starting to reduce the data we need to load the necessary IRAF packages, define various files and make some data lists. First, we load the gemini and gnirs packages and make sure the right defaults are set:

 gemini
 gnirs
 unlearn gemini
 unlearn gemtools
 unlearn gnirs

Optionally, we can define a database directory and log file to be used for this specific pass through this particular data set. If these are not set, the default is to use a "database" subdirectory in the working directory and to write the task output to a file named "gnirs.log". If using PyRAF, the user would prepend "iraf." to the following commands:

 gnirs.logfile = "Q126.log"
 gnirs.database = "Q126_database"

Before we create the data lists, a word on the XD flatfields. For reasons that are explained below, two sets of nighttime flats are taken using different lamps. The flats taken during the day are generally pinhole flats. These flats need to be processed separately, so we need to find out which belong to which set:

 hselect N20110516*[0] field=$I,GCALLAMP,SLIT expr='OBSTYPE=="FLAT"'
N20110516S0162.fits[0]  IRhigh  0.68arcsec_G5530
N20110516S0163.fits[0]  IRhigh  0.68arcsec_G5530
N20110516S0164.fits[0]  IRhigh  0.68arcsec_G5530
N20110516S0165.fits[0]  IRhigh  0.68arcsec_G5530
N20110516S0166.fits[0]  IRhigh  0.68arcsec_G5530
N20110516S0167.fits[0]  IRhigh  0.68arcsec_G5530
N20110516S0168.fits[0]  QH      0.68arcsec_G5530
N20110516S0169.fits[0]  QH      0.68arcsec_G5530
N20110516S0170.fits[0]  QH      0.68arcsec_G5530
N20110516S0171.fits[0]  QH      0.68arcsec_G5530
N20110516S0172.fits[0]  QH      0.68arcsec_G5530
N20110516S0173.fits[0]  QH      0.68arcsec_G5530
N20110516S0174.fits[0]  QH      0.68arcsec_G5530
N20110516S0175.fits[0]  QH      0.68arcsec_G5530
N20110516S0176.fits[0]  QH      0.68arcsec_G5530
N20110516S0177.fits[0]  QH      0.68arcsec_G5530
N20110516S0379.fits[0]  QH      LgPinholes_G5530
N20110516S0380.fits[0]  QH      LgPinholes_G5530
N20110516S0381.fits[0]  QH      LgPinholes_G5530
N20110516S0382.fits[0]  QH      LgPinholes_G5530
N20110516S0383.fits[0]  QH      LgPinholes_G5530

So, we have six flats taken with the IR lamp, 10 with the QH lamp, and five daytime pinhole flats. We can now delete any existing list files and construct lists containing the various kinds of data:

 delete *.lis ver- >& dev$null
 gemlist N20110516S 155-157,159 > obj.lis
 gemlist N20110516S 186-189 > telluric.lis
 gemlist N20110516S 168-177 > QHflats.lis
 gemlist N20110516S 162-167 > IRflats.lis
 gemlist N20110516S 160-161 > arcs.lis
 gemlist N20110516S 379-383 > pinholes.lis
 gemlist N20110516S 408-417 > darks.lis
 concat *lis > all.lis
 gemlist N20110516S 168-177,162-167,160-161,379-383 > cals.lis

Finally, we use the nsheaders command to set the header keywords to be used by subsequent tasks:

 nsheaders gnirs


Pattern noise cleaning

GNIRS spectra are frequently affected by striping caused by the detector controller. This can be removed using the cleanir script (a standalone pyraf script, not part of the IRAF package) either at the start of the reduction or after running nsprepare. The vertical pattern is not prominent in this particular data set, however, and running the cleanir script has little effect. If we were to use it, the syntax would be something like this (run outside IRAF):

cleanir -fq N20110516S\*

See the cleanir web page for more options and advice on using the script. The nvnoise task in the GNIRS IRAF package is also available for modelling and subtracting electronic striping.



Dealing with radiation events in short camera data

GNIRS spectra taken with the short cameras are affected by radiation events from the radioactive short camera lens coatings. A number of these can be seen in the galaxy spectra shown above. The radiation events, which bear some resemblance to cosmic ray hits, can be dealt with in several ways depending on the characteristics of the data. For example:

  1. Cosmic ray detection algorithms (e.g. IRAF's crutil package) can be used to remove the radiation events
  2. Masks generated by cosmic ray detection algorithms can be passed to nsprepare using the "bpm" parameter, causing them to be written into the prepared file's DQ array.
  3. Setting the fl_cravg flag in nsprepare causes the task to attempt to identify radiation events in each file
  4. Pixel rejection algorithms can be employed when combining files
  5. In some circumstances the radiation events can simply be ignored
  6. The user could identify and interpolate over spikes in the extracted, 1D spectra

For the calibration data we will use option (4). Normally, this should also work for the science data. However with this particular data set we only have two files at each nod position, so the data aren't well-suited to the use of sigma-clipping algorithms. Some experimentation shows that (3) doesn't give good results for this data set, so we will instead use option (1) later in the reduction.



Preparing the data

At this point we can start preparing the data for further reduction. The nsprepare task can perform various operations such as adding variance and data quality extensions and attaching the mask definition file (MDF) to the data. The MDF gives the location of each order on the detector, so that the orders can eventually be "cut" and written into separate file extensions. The MDFs were defined during commissioning and are supplied with the Gemini IRAF package (in the gnirs$data directory). However, the exact location of the orders can change from observation to observation because of, for example, the inexact repeatability of the prism turret, deliberate recalibrations of the prism turret, or flexure. By setting shiftx=INDEF and shifty=INDEF, we make the nsprepare task use a nighttime flatfield to measure the location of the orders with respect to the pre-defined MDF. With the default parameter settings, the task will also check and attempt to correct the world coordinate system, flag saturated pixels and add variance and data quality extensions. At the time of writing we do not recommend using the nonlinearity correction provided by nsprepare on data taken with GNIRS at Gemini North, so we disable the fl_correct flag.

A table called array.fits is used by nsprepare to obtain information about saturation etc. Some of this information was incorrect in previous versions of the package and a new version of the table has been released. Users should download the table and either use it to replace gnirs$data/array.fits in their Gemini installation, or use the arraytable parameter in nsprepare to point to the new version of the table. The corrected array.fits will be included in v1.11 of the Gemini IRAF package.

Not sure of the best setting for the offsetsec parameter; the default is "none" but the description suggests it's probably a good idea to subtract the offset. I guess it's only important for the variance estimates?

 imdelete n@all.lis ver- >& dev$null
 nsprepare @all.lis shiftx=INDEF shifty=INDEF fl_correct- arraytable="myarray.fits"
.
.
NSPREPARE: Will measure MDF offset using N20110516S0162
NSOFFSET: Integer offset for gnirs$data/gnirsn-sxd-short-32-mdf is 36.
NSOFFSET: Final offset for gnirs$data/gnirsn-sxd-short-32-mdf is 36.0.
NSOFFSET: Shifting gnirsn-sxd-short-32-mdf-1508_12.fits[1] in X by 36.0 pixels.
.
.

The files output by nsprepare include extensions containing the MDF, science data, and variance and data quality arrays:

 gemextn N20110516S0168
N20110516S0168[0]
N20110516S0168[1]

 gemextn nN20110516S0168
nN20110516S0168[0]
nN20110516S0168[1][MDF]
nN20110516S0168[2][SCI,1]
nN20110516S0168[3][VAR,1]
nN20110516S0168[4][DQ,1]


Making the flatfield

Before creating the master flatfield we need to "cut" the individual flat files so that each order is written into a separate extension, as described in the previous section. We will also cut the arcs and pinholes for use later in the reduction. We use the nsreduce task for this (nscut could also be used):

 imdelete rn@cals.lis ver- >& dev$null
 nsreduce n@cals.lis fl_sky- fl_nscut+ fl_flat- fl_dark- fl_nsappwave- fl_corner+

The cut data have the following file format, with the science data for order 3 in extension [sci,1], order 4 in [sci,2], etc.:

 gemextn rnN20110516S0168
rnN20110516S0168[0]
rnN20110516S0168[1][MDF]
rnN20110516S0168[2][SCI,1]
rnN20110516S0168[3][VAR,1]
rnN20110516S0168[4][DQ,1]
rnN20110516S0168[5][SCI,2]
rnN20110516S0168[6][VAR,2]
rnN20110516S0168[7][DQ,2]
rnN20110516S0168[8][SCI,3]
rnN20110516S0168[9][VAR,3]
rnN20110516S0168[10][DQ,3]
rnN20110516S0168[11][SCI,4]
rnN20110516S0168[12][VAR,4]
rnN20110516S0168[13][DQ,4]
rnN20110516S0168[14][SCI,5]
rnN20110516S0168[15][VAR,5]
rnN20110516S0168[16][DQ,5]
rnN20110516S0168[17][SCI,6]
rnN20110516S0168[18][VAR,6]
rnN20110516S0168[19][DQ,6]

The orders can be inspected using a single call to the nxdisplay task, or just by displaying each science extension in turn:

 nxdisplay rnN20110516S0168
 display rnN20110516S0168[SCI,1]
 display rnN20110516S0168[SCI,2]

...etc. The image below shows order 5 (extension 3) in one of the QH flats cut using the shifted MDF obtained above (left) and the original, unshifted MDF (right). As in the left-hand image, the whole length of the slit (approx 47 pix for the 0.15 arcsec/pixel camera) should be visible and only one order included in the cut data.

Before making the flat, it can be useful to examine the image statistics for the individual flatfield exposures to look for abnormal count levels or unusually high standard deviations that may indicate data that should be excluded from the final flat. For example, these commands run imstat on some orders of the reduced flats and print the results to the logfile we defined earlier:

 printlog "--------" Q126.log ver+
 delete tmpflat

 gemextn rn@IRflats.lis proc="expand" extname="SCI" extver="1" > tmpflat
 printlog "IR order 3 Flats"  Q126.log ver+
 imstatistic @tmpflat | tee (gnirs.log, append+) 
 delete tmpflat

 gemextn rn@QHflats.lis proc="expand" extname="SCI" extver="2" > tmpflat
 printlog "QH order 4 Flats"  Q126.log ver+
 imstatistic @tmpflat | tee (gnirs.log, append+) 
 delete tmpflat

As explained below, only order 3 (extension 1) of the IR flats is relevant, and only orders 4-8 of the QH flats.

We now use the nsflat task to construct two sets of flats. The IR flats are used for order 3, and the QH flats for the higher orders. This is because the QH lamp contains a strong spectral feature in the K band which can only be fit by a spline of very high order. If for some reason IR flats are not available or the user does not wish to use them, the QH lamp can be used with a low-order fit but the flatfielded spectra will contain a prominent artefact in order 3 until divided by the (flatfielded) telluric standard. Both lamps have other spectral features but they are weaker and/or can be fit by a lower-order spline and can be ignored.

The short exposure times used for the flats (typically just a few seconds at low-moderate spectral resolution) mean that they are affected by few radiation events. By default nsflat applies a pixel rejection algorithm which screens out such deviant pixels in any case (see the "rejtype" and associated parameters).

It is not usually necessary to run nsflat interactively on the IR flats, but it can be a good idea to check the fitting of the QH flats interactively. As the IR flat is only used for order 3, there is little signal in the higher orders, so the warnings given by nsflat about those orders can be ignored.

 imdelete IRflat,IRflat_bpm.pl ver- >& dev$null
 nsflat rn@IRflats.lis flatfile="IRflat.fits" fl_corner+ process="fit" order=10 lthresh=100. thr_flo=0.35 thr_fup=1.5 

 imdelete QHflat,QHflat_bpm.pl ver- >& dev$null
 nsflat rn@QHflats.lis flatfile="QHflat.fits" fl_corner+ process="fit" order=5 lthresh=50. thr_flo=0.35 thr_fup=4.0 fl_inter+

When run interactively, nsflat should show a fairly smooth and well-fit curve for each order (below). The higher orders only have signal over a portion of the array. The "fuzz" in the fitted data is due to the "odd-even effect". This is present in all data from GNIRS at Gemini North and will have divided out by the end of the reduction.

We now need to combine order 3 of the IR flat and orders 4-8 of the QH flat. For this to work as shown below, the data format must be as shown above (this should come automatically out of the above reduction steps). To combine the normalised IR and QH flats into a single master flat:

 imdelete final_flat.fits ver- >& dev$null
 fxcopy IRflat.fits final_flat.fits "0-3" new_file+
 fxinsert QHflat.fits final_flat.fits[3]    groups="4-9"
 fxinsert QHflat.fits final_flat.fits[9]    groups="10-18"

The final flat from this process is shown below, using nxdisplay. Some features are visible, such as the dark "scratches" at the top of order 5. These are genuine features of the detector and have been there since the array was received. A vertical cut through a portion of the flat shows it to be well-normalised, and the "odd-even effect" is present at about the 10% level.



Reducing the science data

We start by cleaning the science data of radiation events. As discussed above, if we had several images per offset position we could construct median images to detect and replace the events, or take advantage of one of the pixel rejection algorithms in nscombine or nsstack (below). With just two images at each offset position, though, we will do the following:

Create a relatively clean comparison frame using the minimum of the two images at each position:

 imdel min155,min156
 gemcombine nN20110516S0155,nN20110516S0159 output=min155 combine=median reject=minmax nhigh=1 nlow=0
 gemcombine nN20110516S0156,nN20110516S0157 output=min156 combine=median reject=minmax nhigh=1 nlow=0

Estimate the noise at that pixel using the RDNOISE and GAIN header keywords (written into the headers by nsprepare), and identify pixels deviating by some amount above the noise in the minimum frame. These spectra were taken through variable cloud cover and have rather different flux levels, so the thresholds used in imexpr are not the same for all files. Some experimentation was needed to find thresholds that effectively identified radiation events with few or no false positives at the location of the galaxy spectrum or at the edges of the spectral orders:

 hselect n@obj.lis field=$I,RDNOISE,GAIN expr=yes
 nN20110516S0155 6.19    13.50
 ...
 imdel mask15?.pl
 imexpr expr= "(a-b)>30*sqrt(6.19**2+2*b/13.50) ? 1 : 0" output=mask155.pl a=nN20110516S0155.fits[sci] b=min155.fits[sci]
 imexpr expr= "(a-b)>100*sqrt(6.19**2+2*b/13.50) ? 1 : 0" output=mask159.pl a=nN20110516S0159.fits[sci] b=min155.fits[sci]
 imexpr expr= "(a-b)>20*sqrt(6.19**2+2*b/13.50) ? 1 : 0" output=mask156.pl a=nN20110516S0156.fits[sci] b=min156.fits[sci]
 imexpr expr= "(a-b)>20*sqrt(6.19**2+2*b/13.50) ? 1 : 0" output=mask157.pl a=nN20110516S0157.fits[sci] b=min156.fits[sci]

"Grow" the masked areas to cover the halos as well as the bright central pixels

 delete masks.lis >& dev$null
 files mask15?.pl > masks.lis
 imdel g@masks.lis
 crgrow @masks.lis g@masks.lis rad=1.5

Replace the masked values with unaffected values from the "minimum" image:

 imdel cln@obj.lis
 copy nN20110516S0155.fits clnN20110516S0155.fits 
 copy nN20110516S0156.fits clnN20110516S0156.fits 
 copy nN20110516S0157.fits clnN20110516S0157.fits 
 copy nN20110516S0159.fits clnN20110516S0159.fits 
 imexpr "c=1 ? b : a   " output=clnN20110516S0155.fits[sci,overwrite] a=nN20110516S0155.fits[sci] b=min155.fits[sci] c=gmask155.pl
 imexpr "c=1 ? b : a   " output=clnN20110516S0159.fits[sci,overwrite] a=nN20110516S0159.fits[sci] b=min155.fits[sci] c=gmask159.pl
 imexpr "c=1 ? b : a   " output=clnN20110516S0156.fits[sci,overwrite] a=nN20110516S0156.fits[sci] b=min156.fits[sci] c=gmask156.pl
 imexpr "c=1 ? b : a   " output=clnN20110516S0157.fits[sci,overwrite] a=nN20110516S0157.fits[sci] b=min156.fits[sci] c=gmask157.pl

"Before" and "after" images are shown below; the mask images can also be inspected to ensure that the right pixels have been identified as radiation events. These particular spectra were taken under variable cloud cover, meaning that the flux level varies significantly between the spectra. Replacing affected pixels in one file by the same pixel from the other file therefore leaves a few subtle artefacts in the data. The procedure should work well for many other data sets, however. In general, dealing with the radiation events is more effective when more individual exposures are available. Although the short exposure times for the standard star mean that many fewer pixels are affected, the same steps can also be carried out on those spectra.

The next step is to cut, flatfield and sky-subtract the cleaned galaxy data using nsreduce:

 imdelete rcln@obj.lis ver- >& dev$null
 nsreduce cln@obj.lis fl_nsappwave- fl_sky+ skyrange=INDEF fl_flat+ flatimage="final_flat.fits" 
...
------------------------------------------------------------------------------
NSREDUCE: Generating the sky frame(s) using the other nod positions
          (separations greater than 1.5 arcsec) from neighbouring
          exposures.

------------------------------------------------------------------------------
NSSKY -- Wed Oct 19 16:05:38 HST 2011
 
WARNING - NSSKY: Will take sky from input images
NSSKY: Obs time for clnN20110516S0155-67773_1409.fits[0]: 10:02:46.4
NSSKY: Obs time for clnN20110516S0156-67773_1410.fits[0]: 10:08:26.9  0:05:40.5
NSSKY: Obs time for clnN20110516S0157-67773_1411.fits[0]: 10:14:03.9  0:05:37.0
NSSKY: Obs time for clnN20110516S0159-67773_1412.fits[0]: 10:22:53.9  0:08:50.0
NSSKY: Min/max:  0:05:37.0/ 0:08:50.0
NSSKY: Using observations within 530.0s as sky.
NSSKY: Grouping images (please wait).
 
NSSKY exit status:  good.
NSREDUCE: Processing 24 extension(s) from 4 file(s).
NSREDUCE: slit: 0.68arcsec_G5530
NSREDUCE: grism: XD_G0526

   n input                   --> output                  sky image
 dark                    flat                    scale

   1 clnN20110516S0155       --> rclnN20110516S0155      clnN20110516S0156-67773_1410
 none                    final_flat              1.0000 
   2 clnN20110516S0156       --> rclnN20110516S0156      clnN20110516S0155-67773_1409
 none                    final_flat              1.0000 
   3 clnN20110516S0157       --> rclnN20110516S0157      clnN20110516S0159-67773_1412
 none                    final_flat              1.0000 
   4 clnN20110516S0159       --> rclnN20110516S0159      clnN20110516S0157-67773_1411
 none                    final_flat              1.0000 
 
NSREDUCE exit status:  good.
------------------------------------------------------------------------------

With skyrange set to "INDEF", nodsize=3 arcsec (the default) and no sky images explicitly defined, the task attempted to find sky images in the correct nod position and within a reasonable length of time from the on-source images. The output shows that the correct sky frames were indeed identified (this information is also added to the file headers). See the nsreduce help and gemini.gnirs.gnirsexamples files for more sky subtraction options.

The same procedure is applied to the prepared telluric standard spectra, cleaned in the same way as the galaxy data:

 imdelete rcln@telluric.lis ver- >& dev$null
 nsreduce cln@telluric.lis fl_nsappwave- fl_sky+ skyrange=INDEF fl_flat+ flatimage="final_flat.fits"

At this stage we have a couple of sky-subtracted, flatfielded galaxy and standard star spectra at each nod position. These now need to be combined. For the galaxy, we have two on-object spectra at the same nod position, and two blank sky spectra. We will use nscombine to simply average the two sky-subtracted, on-source files. For the telluric standard we will use nscombine to shift and average the nod beams. Because we have already cleaned the files of radiation events, we won't attempt any pixel rejection in nscombine or nsstack. If that had not been done, we could investigate using the "rejtype" and associated parameters to reject affected pixels.

 imdelete rclnN20110516S0155_comb ver- >& dev$null
 nscombine rclnN20110516S0155,rclnN20110516S0159 rejtype=none masktype=none

 imdelete rclnN20110516S0186_comb ver- >& dev$null
 nscombine rcln@telluric.lis rejtype=none masktype=none


S-distortion correction and wavelength calibration

As the spectral orders are tilted and curved, the next step in the reduction is to rectify them. We also measure and apply the wavelength calibration at this stage.

The nssdist task is used to measure the spectral distortion. It does this by tracing the pinhole flatfield spectra across the array. In principle, all five pinholes could be used to map the distortion in both the spectral and spatial dimensions. However, the distortion in fact varies little along the slit, so we will only use a single pinhole in this example (this means that the standard star could also be used).

Used interactively, nssdist presents a cross-section of the pinholes for each order. The "m" key should be used to mark one or more of the pinholes (not the faint one on the right as it's vignetted by the decker), and then the user should hit enter to accept the pinhole position given by nssdist. The task will give warnings about losing the trace in the higher orders; this is simply because there is little signal at the short wavelength ends of these orders.

 nssdist rnN20110516S0379 section="[*,100]" coordlist=gnirs$data/pinholes-short-dense-north.lis function="legendre" order=5  minsep=5 thresh=1000 nlost=0 fl_inter+

We now run nswavelength on the arc(s) to establish the pixel-wavelength relation. Only one arc spectrum is required, but it is also possible to combine several arcs first to improve the S/N. Nswavelength appears to work well on XD arcs, but it is a good idea to run the task interactively, at least initially, to check the results:

 imdelete arc_comb ver- >& dev$null
 nscombine rn@arcs.lis output="arc_comb"
 imdelete warc_comb ver- >& dev$null
 nswavelength arc_comb coordlist="gnirs$data/lowresargon.dat" fl_median+ fl_inter+ refit- threshold=300. nlost=10 fwidth=5.

After applying a crude wavelength solution based on information in the file headers, nswavelength asks the user whether they want examine line identifications interactively. Answering "yes" brings up the plot shown below. Using "m" to mark a line will bring up its fitted wavelength and the wavelength expected from the line list, so we can check whether the line identification and fitting if working well. Arc line plots are available on the GNIRS (at R~5000) and UKIRT (at lower resolution) web pages and can serve as a useful sanity check at this stage. If the line wavelengths are acceptable, hit "q" to quit. Nswavelength then steps along the slit and fits the arc spectrum at those locations. It should not be necessary to examine the lines during this step (answer "NO"). This is repeated for each of the 6 orders. Finally, for reasons related to compatibility with other instruments and modes, nswavelength re-enters the "reidentify" step; the user can again answer "NO".

(Will add a short note about the fl_median option when have compared the calibration with and without it more thoroughly.)

To actually apply the distortion correction and wavelength calibration to the data, it is necessary to run the galaxy and standard star files through nsfitcoords and nstransform. Running nsfitcoords interactively is recommended.

 imdelete frclnN20110516S0155_comb,frclnN20110516S0186_comb  ver- >& dev$null
 nsfitcoords rclnN20110516S0155_comb lamptrans="warc_comb" sdisttrans="rnN20110516S0379" fl_inter+ lxorder=1 lyorder=3 sxorder=4 syorder=4
 nsfitcoords rclnN20110516S0186_comb lamptrans="warc_comb" sdisttrans="rnN20110516S0379" fl_inter+ lxorder=1 lyorder=3 sxorder=4 syorder=4
 imdelete tfrclnN20110516S0155_comb,tfrclnN20110516S0186_comb ver- >& dev$null
 nstransform frclnN20110516S0155_comb,frclnN20110516S0186_comb

For each order, nsfitcoords presents arc lamp and pinhole plots (left and middle images, below). It can be necessary to delete outlying points in the pinhole plots and then hit the "f" key to redo the fit (right image, below), especially for the highter orders in which the residuals can be several pixels (note the y scale on the plots presented when running the task!).

By this stage in the reduction we have a single straightened, wavelength-calibrated file for the galaxy and the same for the telluric standard. After running nstransform on the data, the wavelength scale is inverted compared to the raw files. To check that the wavelength calibration is acceptably accurate, we can run nsfitcoords and nstransform on the arc itself, use nsextract or imcopy and splot to extract and plot a spectrum, and compare the line wavelengths against a reference spectrum such as those on the web pages mentioned above. To check the rectification of the spectra, we could use imexam's "j" command to fit Gaussians at various points and check that the fitted centre does not change appreciably along the spectrum.



Extracting spectra

The spectra are now ready to be extracted. The nsextract task has many options, and the most suitable ones will depend on the characteristics of the data set and the purpose(s) the spectra are to be used for. The spectra should be very close to vertical after the rectification performed above, so one possibility is to simply extract and sum a few columns. For bright objects like these, nsextract can automatically locate the peak of the spectrum and the task can be run non-interactively. The version of nsextract in the beta2 version of the package contains a bug that can cause it to crash when used with the fl_apall- option, so please download and use this version of the script (which will be in the next package release) if you are having trouble.

 #if using the version of nsextract linked from this page:
 #task nsextract=./nsextract.cl
 imdelete vtfrclnN20110516S0155_comb,vtfrclnN20110516S0186_comb ver- >& dev$null
 nsextract tfrclnN20110516S0155_comb up=6 low=-6 ylevel=INDEF fl_apall- fl_project+ background=none outprefix=v
 nsextract tfrclnN20110516S0186_comb up=6 low=-6 ylevel=INDEF fl_apall- fl_project+ background=none outprefix=v 

Looking at the aperture files in the database directory, it seems that nsextract is subtracting some kind of background even though it's told not to. I don't want to do that because the galaxy fills the slit. Any comments, Emma?

We could also extract the data using apall. Apall's tracing functionality allows us to verify that the spectra were indeed straightened correctly in previous steps, and correct for any small deviations if desired. We can then extract the galaxy spectra using the trace measured for the standard star:

 imdelete xtfrclnN20110516S0186_comb ver- >& dev$null
 nsextract tfrclnN20110516S0186_comb up=6 low=-6 background="none" fl_addvar- fl_trace+ fl_inter+
 imdelete xtfrclnN20110516S0155_comb ver- >& dev$null
 nsextract tfrclnN20110516S0155_comb ylevel=INDEF up=6 low=-6 background="none" fl_addvar- fl_trace- trace=tfrclnN20110516S0186_comb fl_inter+ 

When extracting the galaxy in this way, we say "no" when asked if we want to resize the aperture and then use the "s" key to shift the aperture so that it is properly centred on the emission peak. Is the background subtraction really disabled? The database files suggest that some kind of background is in fact being subtracted.

The various extraction parameters can be changed order-by-order interactively. Alternatively the user could create a copy of the gnirs$data/apertures.fits file and edit it to contain values optimised for their particular data set. This table would then be passed to nsextract using the "aptable" parameter. See the nsextract help for more information.

The extracted galaxy and standard star spectra are shown below.



Telluric line cancellation

Several options are available at this stage. The nstelluric task can be used to shift and scale the standard star for (in theory) the best telluric line cancellation. Use of nstelluric is somewhat involved; the "msabsflux" section of this web page gives some hints that are also applicable to nstelluric. Alternatively, the native IRAF telluric and calibrate tasks can be run on each extension to shift, scale and ratio the spectra and apply a spectrophotometric calibration to the data.

For this example, we will simply divide the galaxy by the standard star and then multiply by a blackbody of appropriate temperature. The blackbody spectra will need to span the same range of wavelengths as the real data. This can be determined using the hselect command to show the starting wavelength and wavelength increment, or by writing the spectra into text files and examining the start and end wavelengths in a text editor. We also need to know the spectral type and temperature of the standard star. The spectral type can be found using SIMBAD, for example, and this web page is a useful source of stellar temperatures. This particular standard star, HIP 65159, is of spectral type F8V with a temperature of approximately 6100 K.

So, to conclude the reduction, we perform something like this set of steps on each extension of the extracted galaxy file. Because of the way the spectra are flipped by nstransform, it is necessary to set the "wstart" parameter in mk1dspec to the longest wavelength and "wend" to the shortest wavelength, otherwise the final galaxy spectrum turns out to have a very strange shape.

delete N4736_?.fits ver- >& dev$null
imarith vtfrclnN20110516S0155_comb[sci,1]  / vtfrclnN20110516S0186_comb[SCI,1] result=N4736_3

hselect vtfrclnN20110516S0155_comb[sci,1] field=$I,CRVAL1,CD1_1 expr="yes"
#vtfrclnN20110516S0155_comb[sci,1]       18938.51171875  6.43956995010377

onedspec
delete temp*
wspectext vtfrclnN20110516S0155_comb[sci,1] temp1 header-
#Starting wavelength = 18939 Angstroms, ending wavelength = 25513 Angstroms

imdel order3_6100K ver- >& dev$null
mk1dspec order3_6100K ncols=1022 wstart=25513 wend=18939 temperature=6100
imdel N4736_3_bb ver- >& dev$null
imarith N4736_3 * order3_6100K result=N4736_3_bb

delete bb_orders.txt ver- >& dev$null
files N4736_*bb.fits > bb_orders.txt
delete txt*bb.fits ver- >& dev$null
wspectext @bb_orders.txt txt@bb_orders.txt header=no

The resulting galaxy spectrum is shown below, with the orders plotted in different colours to illustrate the consistency between the overlap regions. The grey bars show regions of very low atmospheric transparency, judged using these sky transmission spectra. The increased noise around 1.15 microns and 0.94 microns probably also result from imperfectly cancelled telluric lines, as do some of the "spikes" in the spectrum, e.g. around 0.85 microns and between 1.25 and 1.3 microns. Some of the "noise" and features in the spectrum, though, especially in orders 3 and 4, are probably in fact real, distinct absorption features in cool stars in the target galaxy itself.

To improve the telluric cancellation, the fxcor and imshift tasks in the noao.rv and images.imgeom packages could be used on each extension to cross-correlate and shift the spectra before dividing. Users familiar with the Starlink software could instead use Figaro's scross, ishift and irflux tasks on each galaxy extension. Depending on the spectral type of the standard star, it may also be desirable to model out lines originating in the standard star itself during this process (see this page). Given the narrow slit and non-photometric conditions under which these observations were obtained, no flux calibration was attempted during this reduction. This could be accomplished by scaling the spectrum to existing photometry of the galaxy, if data in a similar aperture is available.