The flames_cal_wavecal recipe
===============================================================

.. data:: flames_cal_wavecal

Synopsis
--------

Performs the wavelength calibration

Description
-----------

The recipe performs a wavelength calibration for each extraction window.

Conceptually, each chip contains a number of order lines, each of which
contains a number of fibre traces, each of which contains a number of
extraction windows. For FLAMES/UVES data there
are multiple traces per order but only one extraction window per trace.

The number of traces is defined in the order table while the geometry of
the extraction windows is specified by recipe parameters (see below).


Expected input for this recipe is an arc lamp frame, FIB_ARC_LAMP_RED,
FIB_ORDEF_TABLE_(REDL|REDU), 'guess' line table(s)
for each chip, FIB_LIN_GUE_(REDL|REDU), a wavelength catalogue table, 
LINE_REFER_TABLE, and optionally a wavelength table of bright lines,
LINE_INTMON_TABLE, used only for computing Quality Control parameters.


The output line table(s), FIB_LINE_TABLE_(REDL|REDU), contains the columns
::

  X            : Horizontal position (from Gaussian fit) of detected line
  dX           : Uncertainty (one sigma) of X
  Ynew         : Vertical position of detected line
  XWidth       : Width (in pixels) of detected line from Gaussian fit
  Peak         : Intensity of detected line
  Background   : Fitted background (ADU) of detected line
  Slope        : Linear background slope (ADU/pixel) of detected line
                 from Gaussian fit
  Intensity    : Intensity of detected line scaled to unit exposure
                 time. (This column only present if a LINE_INTMON_TABLE
                 is provided.)
  Order        : Absolute order number of detected line
  Y            : Relative order number of detected line
                 (it's not a very descriptive column name)
  WaveC        : Wavelength of this line (computed using the resulting
                 dispersion relation)
  dLambdaC     : Uncertainty (one sigma) of 'WaveC'.
  Pixel        : The width in w.l.u. of a pixel (computed locally).
  Residual     : Residual (in w.l.u.) of this line
  Residual_pix : Residual (in pixels) of this line
  Lambda_candidate : Nearest line in catalogue
  dLambda_cat_sq   : Squared distance to nearest catalogue line
  dLambda_nn_sq    : Squared distance to nearest neighbour multiplied by ALPHA
  Ident        : The wavelength associated with this emission line,
                 or invalid if this line was not identified
  dIdent       : Uncertainty of catalogue wavelength
  Select       : 1 if the line was identified, 0 otherwise
  NLinSol      : 1 if the line was identified and accepted for the
                 polynomial fit, 0 otherwise
  Intensity    : Intensity of detected line scaled to unit exposure
                 time. (This column is present only if a LINE_INTMON_TABLE
                 is provided.)

The 2nd table extension contains the dispersion relation (a 2d polynomial).
The 3rd table extension contains the map from (pixel, pixel)-space to
 physical order numbers (used internally by the calibration recipe; 
another 2d polynomial).


If there is more than one extraction window, the results of each calibration
is stored in subsequent table extensions of the same FITS file. For 
example, extensions 4, 5 and 6 would contain the resulting line table 
(and its two associated polynomials) for the second extraction window. 
The results for the calibration of the n'th extraction window is stored 
in extensions (3*n - 2) to 3*n.


Constructor
-----------

.. method:: cpl.Recipe("flames_cal_wavecal")
   :noindex:

   Create an object for the recipe flames_cal_wavecal.

::

   import cpl
   flames_cal_wavecal = cpl.Recipe("flames_cal_wavecal")

Parameters
----------

.. py:attribute:: flames_cal_wavecal.param.debug

    Whether or not to save intermediate results to local directory (bool;  default: False) [default=False].
.. py:attribute:: flames_cal_wavecal.param.plotter

    Any plots produced by the recipe are redirected to the command  specified by this parameter. The plotting command must contain the  substring 'gnuplot' and must be able to parse gnuplot syntax on its  standard input. Valid examples of such a command may include 'gnuplot  -persist' and 'cat > mygnuplot$$.gp'. A finer control of the plotting  options can be obtained by writing an executable script, e.g.  my_gnuplot.pl, that executes gnuplot after setting the desired gnuplot  options (e.g. set terminal pslatex color). To turn off plotting, set  this parameter to 'no' (str; default: 'no') [default="no"].
.. py:attribute:: flames_cal_wavecal.param.process_chip

    For RED arm data process the redl, redu, or both chip(s) (str;  default: 'both') [default="both"].
.. py:attribute:: flames_cal_wavecal.param.nwindows

    Number of extraction windows per trace. The windows will be aligned  (i.e. no overlap and no spacing between adjacent windows). Unless an  offset is specified, the middle window(s) is centered on the trace  (long; default: 1) [default=1].
.. py:attribute:: flames_cal_wavecal.param.length

    Length (in pixels) of each extraction window. This parameter is also  equal to the seperation of adjacent window centers, causing the  extraction windows to always be aligned. The parameter is  automatically adjusted according to the binning of the input raw  frame. If negative, the extraction window length is determined  automatically to cover the full slit (float; default: 7.0) [default=7.0].
.. py:attribute:: flames_cal_wavecal.param.offset

    A global offset (in pixels) of all extraction windows (float; default:  0.0) [default=0.0].
.. py:attribute:: flames_cal_wavecal.param.range

    Width (pix) of search window is 2*range + 1. This parameter is  automatically adjusted according to binning. (long; default: 8) [default=8].
.. py:attribute:: flames_cal_wavecal.param.minlines

    Minimum number of lines to detect. If zero, the default value (1100  for BLUE/REDL chips; 1000 for REDU chip) is used. (long; default: 0) [default=0].
.. py:attribute:: flames_cal_wavecal.param.maxlines

    Maximum number of lines to detect. If zero, the default value (1600  for BLUE/REDL chip; 1400 for REDU chip) is used. (long; default: 0) [default=0].
.. py:attribute:: flames_cal_wavecal.param.shiftmax

    The maximum shift (pix) in either direction compared to guess  solution. This parameter is automatically corrected for binning  (float; default: 10.0) [default=10.0].
.. py:attribute:: flames_cal_wavecal.param.shiftstep

    The step size (pix) used when searching for the optimum shift. This  parameter is automatically corrected for binning (float; default: 0.1) [default=0.1].
.. py:attribute:: flames_cal_wavecal.param.shifttoler

    Tolerance (pix) when matching shifted lines. This parameter is not  adjusted according to binning (float; default: 0.05) [default=0.05].
.. py:attribute:: flames_cal_wavecal.param.alpha

    The parameter that controls the distance to the nearest neighbours  (float; default: 0.1) [default=0.1].
.. py:attribute:: flames_cal_wavecal.param.maxerror

    This parameter controls the graceful exit of the identification loop.  If the RMS of the global fit exceeds this value (pix) the iteration  stops (float; default: 20.0) [default=20.0].
.. py:attribute:: flames_cal_wavecal.param.degree

    Degrees of the global 2d dispersion polynomial. If a negative number  is specified, the polynomial degrees are automatically selected by  starting from (1, 1) and inreasing the degrees as long as the RMS  residual decreases significantly (long; default: 4) [default=4].
.. py:attribute:: flames_cal_wavecal.param.tolerance

    Tolerance of fit. If positive, the tolerance is in pixel units. If  negative, abs(tolerance) is in wavelength units. Lines with residuals  worse than the tolerance are excluded from the final fit. Unlike in  previous versions, this parameter is not corrected for CCD binning.  This rejection based on the absolute residual in pixel can be  effectively disabled by setting the tolerance to a very large number  (e.g. 9999). In that case outliers will be rejected using only kappa  sigma clipping. (float; default: 0.6) [default=0.6].
.. py:attribute:: flames_cal_wavecal.param.kappa

    Lines with residuals more then kappa stdev are rejected from the final  fit (float; default: 4.0) [default=4.0].
.. py:attribute:: flames_cal_wavecal.param.rebin.wavestep

    The bin size (in w.l.u.) in wavelength space. If negative, a step size  of 2/3 * ( average pixel size ) is used. (float; default: -1.0) [default=-1.0].
.. py:attribute:: flames_cal_wavecal.param.rebin.scale

    Whether or not to multiply by the factor dx/dlambda (pixels per  wavelength) during the rebinning. This option is disabled as default  in concordance with the method used in the MIDAS pipeline. This option  should be set to true to convert the observed flux (in pixel-space) to  a flux per wavelength (in wavelength-space). (bool; default: False) [default=False].
.. py:attribute:: flames_cal_wavecal.param.extract.method

    Extraction method. (2d/optimal not supported by uves_cal_wavecal,  weighted supported only by uves_cal_wavecal, 2d not supported by  uves_cal_response) (str; default: 'average') [default="average"].
.. py:attribute:: flames_cal_wavecal.param.extract.kappa

    In optimal extraction mode, this is the threshold for bad (i.e.  hot/cold) pixel rejection. If a pixel deviates more than kappa*sigma  (where sigma is the uncertainty of the pixel flux) from the inferred  spatial profile, its weight is set to zero. Range: [-1,100]. If this  parameter is negative, no rejection is performed. (float; default:  10.0) [default=10.0].
.. py:attribute:: flames_cal_wavecal.param.extract.chunk

    In optimal extraction mode, the chunk size (in pixels) used for  fitting the analytical profile (a fit of the analytical profile to  single bins would suffer from low statistics). (long; default: 32) [default=32].
.. py:attribute:: flames_cal_wavecal.param.extract.profile

    In optimal extraction mode, the kind of profile to use. 'gauss' gives  a Gaussian profile, 'moffat' gives a Moffat profile with beta=4 and a  possible linear sky contribution. 'virtual' uses a virtual resampling  algorithm (i.e. measures and uses the actual object profile).  'constant' assumes a constant spatial profile and allows optimal  extraction of wavelength calibration frames. 'auto' will automatically  select the best method based on the estimated S/N of the object. For  low S/N, 'moffat' or 'gauss' are recommended (for robustness). For  high S/N, 'virtual' is recommended (for accuracy). In the case of  virtual resampling, a precise determination of the order positions is  required; therefore the order-definition is repeated using the  (assumed non-low S/N) science frame (str; default: 'auto') [default="auto"].
.. py:attribute:: flames_cal_wavecal.param.extract.skymethod

    In optimal extraction mode, the sky subtraction method to use.  'median' estimates the sky as the median of pixels along the slit  (ignoring pixels close to the object), whereas 'optimal' does a chi  square minimization along the slit to obtain the best combined object  and sky levels. The optimal method gives the most accurate sky  determination but is also a bit slower than the median method (str;  default: 'optimal') [default="optimal"].
.. py:attribute:: flames_cal_wavecal.param.extract.oversample

    The oversampling factor used for the virtual resampling algorithm. If  negative, the value 5 is used for S/N <=200, and the value 10 is used  if the estimated S/N is > 200 (long; default: -1) [default=-1].
.. py:attribute:: flames_cal_wavecal.param.extract.best

    (optimal extraction only) If false (fastest), the spectrum is  extracted only once. If true (best), the spectrum is extracted twice,  the second time using improved variance estimates based on the first  iteration. Better variance estimates slightly improve the obtained  signal to noise but at the cost of increased execution time (bool;  default: True) [default=True].


The following code snippet shows the default settings for the available 
parameters.

::

   import cpl
   flames_cal_wavecal = cpl.Recipe("flames_cal_wavecal")

   flames_cal_wavecal.param.debug = False
   flames_cal_wavecal.param.plotter = "no"
   flames_cal_wavecal.param.process_chip = "both"
   flames_cal_wavecal.param.nwindows = 1
   flames_cal_wavecal.param.length = 7.0
   flames_cal_wavecal.param.offset = 0.0
   flames_cal_wavecal.param.range = 8
   flames_cal_wavecal.param.minlines = 0
   flames_cal_wavecal.param.maxlines = 0
   flames_cal_wavecal.param.shiftmax = 10.0
   flames_cal_wavecal.param.shiftstep = 0.1
   flames_cal_wavecal.param.shifttoler = 0.05
   flames_cal_wavecal.param.alpha = 0.1
   flames_cal_wavecal.param.maxerror = 20.0
   flames_cal_wavecal.param.degree = 4
   flames_cal_wavecal.param.tolerance = 0.6
   flames_cal_wavecal.param.kappa = 4.0
   flames_cal_wavecal.param.rebin.wavestep = -1.0
   flames_cal_wavecal.param.rebin.scale = False
   flames_cal_wavecal.param.extract.method = "average"
   flames_cal_wavecal.param.extract.kappa = 10.0
   flames_cal_wavecal.param.extract.chunk = 32
   flames_cal_wavecal.param.extract.profile = "auto"
   flames_cal_wavecal.param.extract.skymethod = "optimal"
   flames_cal_wavecal.param.extract.oversample = -1
   flames_cal_wavecal.param.extract.best = True


You may also set or overwrite some or all parameters by the recipe 
parameter `param`, as shown in the following example:

::

   import cpl
   flames_cal_wavecal = cpl.Recipe("flames_cal_wavecal")
   [...]
   res = flames_cal_wavecal( ..., param = {"debug":False, "plotter":"no"})


.. seealso:: `cpl.Recipe <http://packages.python.org/python-cpl/recipe.html>`_
   for more information about the recipe object.

Bug reports
-----------

Please report any problems to `Jonas M. Larsen <cpl@eso.org>`_. Alternatively, you may 
send a report to the `ESO User Support Department <usd-help@eso.org>`_.

Copyright
---------

This file is part of the FLAMES/UVES Pipeline
Copyright (C) 2004, 2005, 2006, 2007 European Southern Observatory

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
MA  02111-1307  USA

.. codeauthor:: Jonas M. Larsen <cpl@eso.org>
