The muse_wavecal recipe
===============================================================

.. data:: muse_wavecal

Synopsis
--------

Detect arc emission lines and determine the wavelength solution for each slice.

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

This recipe detects arc emission lines and fits a wavelength solution to each slice of the instrument. The wavelength calibration table contains polynomials defining the wavelength solution of the slices on the CCD. Processing trims the raw data and records the overscan statistics, subtracts the bias (taking account of the overscan, if --overscan is not "none") and converts them from adu to count. Optionally, the dark can be subtracted and the data can be divided by the flat-field, but this is not recommended. The data is then combined using input parameters, into separate images for each lamp. To compute the wavelength solution, arc lines are detected at the center of each slice (using threshold detection on a S/N image) and subsequently assigned wavelengths, using pattern matching to identify lines from the input line catalog. Each line is then traced to the edges of the slice, using Gaussian centering in each CCD column. The Gaussians not only yield center, but also centering error, and line properties (e.g. FWHM). Deviant fits are detected using polynomial fits to each arc line (using the xorder parameter) and rejected. These analysis and measuring steps are carried out separately on images exposed by the different arc lamps, reducing the amount of blending, that can otherwise influence line identification and Gaussian centering. The final two-dimensional fit uses all positions (of all lamps), their wavelengths, and the given polynomial orders to compute the final wavelength solution for each slice, iteratively rejecting outliers. This final fit can be either unweighted (fitweighting="uniform", for fastest processing) or weighted (other values of fitweighting, for higher accuracy).


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

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

   Create an object for the recipe muse_wavecal.

::

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

Parameters
----------

.. py:attribute:: muse_wavecal.param.nifu

    IFU to handle. If set to 0, all IFUs are processed serially. If set to  -1, all IFUs are processed in parallel. (int; default: 0) [default=0].
.. py:attribute:: muse_wavecal.param.overscan

    If this is "none", stop when detecting discrepant overscan levels (see  ovscsigma), for "offset" it assumes that the mean overscan level  represents the real offset in the bias levels of the exposures  involved, and adjusts the data accordingly; for "vpoly", a polynomial  is fit to the vertical overscan and subtracted from the whole  quadrant. (str; default: 'vpoly') [default="vpoly"].
.. py:attribute:: muse_wavecal.param.ovscreject

    This influences how values are rejected when computing overscan  statistics. Either no rejection at all ("none"), rejection using the  DCR algorithm ("dcr"), or rejection using an iterative constant fit  ("fit"). (str; default: 'dcr') [default="dcr"].
.. py:attribute:: muse_wavecal.param.ovscsigma

    If the deviation of mean overscan levels between a raw input image and  the reference image is higher than |ovscsigma x stdev|, stop the  processing. If overscan="vpoly", this is used as sigma rejection level  for the iterative polynomial fit (the level comparison is then done  afterwards with |100 x stdev| to guard against incompatible settings).  Has no effect for overscan="offset". (float; default: 30.0) [default=30.0].
.. py:attribute:: muse_wavecal.param.ovscignore

    The number of pixels of the overscan adjacent to the data section of  the CCD that are ignored when computing statistics or fits. (int;  default: 3) [default=3].
.. py:attribute:: muse_wavecal.param.combine

    Type of lampwise image combination to use. (str; default: 'sigclip') [default="sigclip"].
.. py:attribute:: muse_wavecal.param.lampwise

    Identify and measure the arc emission lines on images separately for  each lamp setup. (bool; default: True) [default=True].
.. py:attribute:: muse_wavecal.param.sigma

    Sigma level used to detect arc emission lines above the median  background level in the S/N image of the central column of each slice  (float; default: 1.0) [default=1.0].
.. py:attribute:: muse_wavecal.param.dres

    The allowed range of resolutions for pattern matching (of detected arc  lines to line list) in fractions relative to the expected value  (float; default: 0.05) [default=0.05].
.. py:attribute:: muse_wavecal.param.tolerance

    Tolerance for pattern matching (of detected arc lines to line list)  (float; default: 0.1) [default=0.1].
.. py:attribute:: muse_wavecal.param.xorder

    Order of the polynomial for the horizontal curvature within each slice  (int; default: 2) [default=2].
.. py:attribute:: muse_wavecal.param.yorder

    Order of the polynomial used to fit the dispersion relation (int;  default: 6) [default=6].
.. py:attribute:: muse_wavecal.param.linesigma

    Sigma level for iterative rejection of deviant fits for each arc line  within each slice, a negative value means to use the default (2.5).  (float; default: -1.0) [default=-1.0].
.. py:attribute:: muse_wavecal.param.residuals

    Create a table containing residuals of the fits to the data of all arc  lines. This is useful to assess the quality of the wavelength solution  in detail. (bool; default: False) [default=False].
.. py:attribute:: muse_wavecal.param.fitsigma

    Sigma level for iterative rejection of deviant datapoints during the  final polynomial wavelength solution within each slice, a negative  value means to use the default (3.0). (float; default: -1.0) [default=-1.0].
.. py:attribute:: muse_wavecal.param.fitweighting

    Type of weighting to use in the final polynomial wavelength solution  fit, using centroiding error estimate and/or scatter of each single  line as estimates of its accuracy. (str; default: 'cerrscatter') [default="cerrscatter"].
.. py:attribute:: muse_wavecal.param.saveimages

    Save (bool; default: False) [default=False].
.. py:attribute:: muse_wavecal.param.resample

    Resample the input arc images onto 2D images for a visual check using  tracing and wavelength calibration solutions. Note that the image  produced will show small wiggles even when the calibration was  successful! (bool; default: False) [default=False].
.. py:attribute:: muse_wavecal.param.wavemap

    Create a wavelength map of the input images (bool; default: False) [default=False].
.. py:attribute:: muse_wavecal.param.merge

    Merge output products from different IFUs into a common file. (bool;  default: False) [default=False].


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

::

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

   muse_wavecal.param.nifu = 0
   muse_wavecal.param.overscan = "vpoly"
   muse_wavecal.param.ovscreject = "dcr"
   muse_wavecal.param.ovscsigma = 30.0
   muse_wavecal.param.ovscignore = 3
   muse_wavecal.param.combine = "sigclip"
   muse_wavecal.param.lampwise = True
   muse_wavecal.param.sigma = 1.0
   muse_wavecal.param.dres = 0.05
   muse_wavecal.param.tolerance = 0.1
   muse_wavecal.param.xorder = 2
   muse_wavecal.param.yorder = 6
   muse_wavecal.param.linesigma = -1.0
   muse_wavecal.param.residuals = False
   muse_wavecal.param.fitsigma = -1.0
   muse_wavecal.param.fitweighting = "cerrscatter"
   muse_wavecal.param.saveimages = False
   muse_wavecal.param.resample = False
   muse_wavecal.param.wavemap = False
   muse_wavecal.param.merge = False


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

::

   import cpl
   muse_wavecal = cpl.Recipe("muse_wavecal")
   [...]
   res = muse_wavecal( ..., param = {"nifu":0, "overscan":"vpoly"})


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

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

Please report any problems to `Peter Weilbacher <https://support.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 MUSE Instrument Pipeline
Copyright (C) 2005, 2019 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:: Peter Weilbacher <https://support.eso.org>
