The muse_lingain recipe
===============================================================

.. data:: muse_lingain

Synopsis
--------

Compute the gain and a model of the residual non-linearity for each detector quadrant

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

The recipe uses the bias and flat field images of a detector monitoring exposure sequence to determine the detector gain in counts/ADU and to model the residual non-linearity for each of the four detector quadrants of all IFUs. All measurements done by the recipe are done on the illuminated parts of the detector, i.e. on the slices. The location of the slices is taken from the given trace table, which is a mandatory input. Using the traces of the slices on the detector a set of measurement windows is placed along these traces. The data used for the determination of the gain and the residual non-linearity is the taken from these windows. Bad pixels indicated by an, optionally, provided bad pixel table, or flagged during the preprocessing (bias subtraction) of the input data are excluded from the measurements. Local measurements of the read-out-noise, the signal and the gain are calculated for each of the measurement windows. Using these measurements the gain for each detector quadrant is computed as the zero-order coefficient of a 1st order polynomial fitted to the binned gain measurements as a function of the signal level. The residual non-linearity is modelled by a (high) order polynomial which is fitted to the fractional percentage deviation of the count rate from an expected constant count rate (the linear case) as function of the signal level. (Not yet implemented!)

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

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

   Create an object for the recipe muse_lingain.

::

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

Parameters
----------

.. py:attribute:: muse_lingain.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_lingain.param.ybox

    Size of windows along the traces of the slices. (int; default: 50) [default=50].
.. py:attribute:: muse_lingain.param.xgap

    Extra offset from tracing edge. (int; default: 3) [default=3].
.. py:attribute:: muse_lingain.param.xborder

    Extra offset from the detector edge used for the selection of slices.  (int; default: 10) [default=10].
.. py:attribute:: muse_lingain.param.order

    Order of the polynomial used to fit the non-linearity residuals. (int;  default: 12) [default=12].
.. py:attribute:: muse_lingain.param.toffset

    Exposure time offset in seconds to apply to linearity flat fields.  (float; default: 0.018) [default=0.018].
.. py:attribute:: muse_lingain.param.fluxtol

    Tolerance value for the overall flux consistency check of a pair of  flat fields. The value is the maximum relative offset. (float;  default: 0.01) [default=0.01].
.. py:attribute:: muse_lingain.param.sigma

    Sigma value used for signal value clipping. (float; default: 3.0) [default=3.0].
.. py:attribute:: muse_lingain.param.signalmin

    Minimum signal value in log(ADU) used for the gain analysis and the  non-linearity polynomial model. (float; default: 0.0) [default=0.0].
.. py:attribute:: muse_lingain.param.signalmax

    Maximum signal value in log(ADU) used for the gain analysis and the  non-linearity polynomial model. (float; default: 4.9) [default=4.9].
.. py:attribute:: muse_lingain.param.signalbin

    Size of a signal bin in log10(ADU) used for the gain analysis and the  non-linearity polynomial model. (float; default: 0.1) [default=0.1].
.. py:attribute:: muse_lingain.param.gainlimit

    Minimum signal value [ADU] used for fitting the gain relation. (float;  default: 100.0) [default=100.0].
.. py:attribute:: muse_lingain.param.gainsigma

    Sigma value for gain value clipping. (float; default: 3.0) [default=3.0].
.. py:attribute:: muse_lingain.param.ctsmin

    Minimum signal value in log(counts) to consider for the non-linearity  analysis. (float; default: 3.0) [default=3.0].
.. py:attribute:: muse_lingain.param.ctsmax

    Maximum signal value in log(counts) to consider for the non-linearity  analysis. (float; default: 4.9) [default=4.9].
.. py:attribute:: muse_lingain.param.ctsbin

    Size of a signal bin in log10(counts) used for the non-linearity  analysis. (float; default: 0.1) [default=0.1].
.. py:attribute:: muse_lingain.param.linearmin

    Lower limit of desired linear range in log10(counts). (float; default:  2.5) [default=2.5].
.. py:attribute:: muse_lingain.param.linearmax

    Upper limit of desired linear range in log10(counts). (float; default:  3.0) [default=3.0].
.. py:attribute:: muse_lingain.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_lingain = cpl.Recipe("muse_lingain")

   muse_lingain.param.nifu = 0
   muse_lingain.param.ybox = 50
   muse_lingain.param.xgap = 3
   muse_lingain.param.xborder = 10
   muse_lingain.param.order = 12
   muse_lingain.param.toffset = 0.018
   muse_lingain.param.fluxtol = 0.01
   muse_lingain.param.sigma = 3.0
   muse_lingain.param.signalmin = 0.0
   muse_lingain.param.signalmax = 4.9
   muse_lingain.param.signalbin = 0.1
   muse_lingain.param.gainlimit = 100.0
   muse_lingain.param.gainsigma = 3.0
   muse_lingain.param.ctsmin = 3.0
   muse_lingain.param.ctsmax = 4.9
   muse_lingain.param.ctsbin = 0.1
   muse_lingain.param.linearmin = 2.5
   muse_lingain.param.linearmax = 3.0
   muse_lingain.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_lingain = cpl.Recipe("muse_lingain")
   [...]
   res = muse_lingain( ..., param = {"nifu":0, "ybox":50})


.. 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 `Ralf Palsa <usd-help@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:: Ralf Palsa <usd-help@eso.org>
