The kmos_flat recipe
===============================================================

.. data:: kmos_flat

Synopsis
--------

Create master flatfield frame and badpixel map

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

This recipe creates the master flat field and calibration frames needed for
spatial calibration for all three detectors. It must be called after the 
kmo_dark-recipe, which generates a bad pixel mask (badpixel_dark.fits). The
bad pixel mask will be updated in this recipe.

As input at least 3 dark frames, 3 frames with the flat lamp on are
recommended. Additionally a badpixel mask from kmo_dark is required.


The badpixel mask contains 0 for bad pixels and 1 for good ones.


The structure of the resulting xcal and ycal frames is quite complex since
the arrangement of the IFUs isn't just linear on the detector. Basically the
integer part of the calibration data shows the offset of each pixels centre
in mas (Milli arcsec) from the field centre. The viewing of an IFU is
2800 mas (14pix*0.2arcsec/pix). So the values in these two frames will vary
between +/-1500 (One would expect 1400, but since the slitlets aren't
expected to be exactly vertical, the values can even go up to around 1500).

Additionally in the calibration data in y-direction the decimal part of the
data designates the IFU to which the slitlet corresponds to (for each
detector from 1 to 8).

Because of the irregular arrangement of the IFUs not all x-direction
calibration data is found in xcal and similarly not all y-direction
calibration data is located in ycal. For certain IFUs they are switched
 and/or flipped in x- or y-direction:
For IFUs 1,2,3,4,13,14,15,16:  x- and y- data is switched
For IFUs 17,18,19,20:          y-data is flipped 
For IFUs 21,22,23,24:          x-data is flipped 
For IFUs 5,6,7,8,9,10,11,12:   x- and y- data is switched and
                               x- and y- data is flipped

Furthermore frames can be provided for several rotator angles. In this case
the resulting calibration frames for each detector are repeatedly saved as 
extension for every angle.


Advanced features:
------------------
To create the badpixel mask the edges of all slitlets are fitted to a
polynomial. Since it can happen that some of these fits (3 detectors
8 IFUs * 14slitlets * 2 edges  (left and right edge of slitlet)= 672 edges)
fail, the fit parameters are themselves fitted again to detect any outliers.

By default, the parameters of all left and all right edges are grouped
individually and then fitted using chebyshev polynomials. The advantage of
a chebyshev polynomial is, that it consists in fact of a series of
orthogonal polynomials. This implies that the parameters of the polynomials
are independent. This fact predestines the use of chebyshev polynomials
for our case. So each individual parameter can be examined independently.

The reason why the left and right edges are fitted individually is that
there is a systematic pattern specific to these groups. The reason for
this pattern is probably to be found in the optical path the light is
traversing.


Input files
^^^^^^^^^^^^
::

   DO CATG           Type   Explanation                    Required #Frames
   -------           -----  -----------                    -------- -------
   FLAT_ON           RAW    Flatlamp-on exposures             Y       1-n  
                            (at least 3 frames recommended)                
   FLAT_OFF          RAW    Flatlamp-off exposures            Y       1-n  
                            (at least 3 frames recommended)                
   BADPIXEL_DARK     B2D    Bad pixel mask                    Y        1   

Output files
^^^^^^^^^^^^
::

   DO CATG           Type   Explanation
   -------           -----  -----------
   MASTER_FLAT       F2D    Normalised flat field
                            (6 extensions: alternating data & noise
   BADPIXEL_FLAT     B2D    Updated bad pixel mask (3 Extensions)
   XCAL              F2D    Calibration frame 1 (3 Extensions)
   YCAL              F2D    Calibration frame 2 (3 Extensions)
   FLAT_EDGE         F2L    Frame containing parameters of fitted 
                            slitlets of all IFUs of all detectors

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

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

   Create an object for the recipe kmos_flat.

::

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

Parameters
----------

.. py:attribute:: kmos_flat.param.badpix_thresh

    The threshold level to mark bad pixels [%]. (long; default: 35) [default=35].
.. py:attribute:: kmos_flat.param.surrounding_pixels

    The nb of bad surrounding pix to mark a pixel bad (long; default: 5) [default=5].
.. py:attribute:: kmos_flat.param.suppress_extension

    Suppress arbitrary filename extension (bool; default: False) [default=False].
.. py:attribute:: kmos_flat.param.cmethod

    Apply "average", "median", "sum", "min_max." or "ksigma". (str;  default: 'ksigma') [default="ksigma"].
.. py:attribute:: kmos_flat.param.cpos_rej

    The positive rejection threshold for kappa-sigma-clipping (sigma).  (float; default: 3.0) [default=3.0].
.. py:attribute:: kmos_flat.param.cneg_rej

    The negative rejection threshold for kappa-sigma-clipping (sigma).  (float; default: 3.0) [default=3.0].
.. py:attribute:: kmos_flat.param.citer

    The number of iterations for kappa-sigma-clipping. (long; default: 3) [default=3].
.. py:attribute:: kmos_flat.param.cmax

    The number of maximum pixel values to clip with min/max-clipping.  (long; default: 1) [default=1].
.. py:attribute:: kmos_flat.param.cmin

    The number of minimum pixel values to clip with min/max-clipping.  (long; default: 1) [default=1].
.. py:attribute:: kmos_flat.param.det

    Only reduce the specified detector (long; default: 0) [default=0].
.. py:attribute:: kmos_flat.param.angle

    Only reduce the specified angle (float; default: 370.0) [default=370.0].


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

::

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

   kmos_flat.param.badpix_thresh = 35
   kmos_flat.param.surrounding_pixels = 5
   kmos_flat.param.suppress_extension = False
   kmos_flat.param.cmethod = "ksigma"
   kmos_flat.param.cpos_rej = 3.0
   kmos_flat.param.cneg_rej = 3.0
   kmos_flat.param.citer = 3
   kmos_flat.param.cmax = 1
   kmos_flat.param.cmin = 1
   kmos_flat.param.det = 0
   kmos_flat.param.angle = 370.0


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

::

   import cpl
   kmos_flat = cpl.Recipe("kmos_flat")
   [...]
   res = kmos_flat( ..., param = {"badpix_thresh":35, "surrounding_pixels":5})


.. 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 `Alex Agudo Berbel, Yves Jung <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 CRIRES Instrument Pipeline
Copyright (C) 2002,2003 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., 59 Temple Place, Suite 330, Boston, 
MA  02111-1307  USA

.. codeauthor:: Alex Agudo Berbel, Yves Jung <usd-help@eso.org>
