The kmos_combine recipe
===============================================================

.. data:: kmos_combine

Synopsis
--------

Combine reconstructed cubes

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

This recipe shifts several exposures of an object and combines them. Diffe-
rent methods to match the exposures are described here (--method parameter).

The output cube is larger than the input cubes, according to the shifts to
be applied. Additionally a border of NaN values is added. The WCS is the
same as for the first exposure.

For each spatial/spectral pixel a new value is calculated (according the
--cmethod parameter) and written into the output cube.

Only exposures with the same WCS orientation can be combined (except
-–method=”none”), north must point to the same direction. It is recommended
to apply any rotation possibly after combining.


The behavior of the selection of IFUs to combine differs for some templates
and can be controlled with the parameters --name and --ifus.

If the input data cubes stem from templates KMOS_spec_obs_mapping8 or
KMOS_spec_obs_mapping24 all extensions from all input frames are combined
into a single map by default (like in recipe kmo_sci_red). If just the area
of a specific IFU should be combined, the parameter --ifus can be specified,
or more easily --name.

If the input data cubes stem from other templates like e.g.

KMOS_spec_obs_freedither all extensions of all input frames are combined
into several output frames by default. The input IFUs are grouped according
to their targeted object name stored in the keywords ESO OCS ARMx NAME. If 
just a specific object should be combined, its name can be specified with 
--name. If arbitrary IFUs shoukd be comined, one can specify these with the
parameter --ifus.


The default mapping mode is done via the --name parameter, where the name of
the object has to be provided. The recipe searches in input data cubes IFUs
pointing to that object.


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

   DO                  KMOS                                                
   category            Type   Explanation                  Required #Frames
   --------            -----  -----------                  -------- -------
   <none or any>       F3I    data frame                       Y      2-n  

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

   DO                      KMOS
   category                Type    Explanation
   --------                -----   -----------
   COMBINE_<ESO PRO CATG>  F3I     Combined data cube
   EXP_MASK_<ESO PRO CATG> F3I     Exposure time mask
   SCI_COMBINED_COLL               (optional) Collapsed combined cube
                                   (set --collapse_combined)


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

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

   Create an object for the recipe kmos_combine.

::

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

Parameters
----------

.. py:attribute:: kmos_combine.param.name

    Name of the object to combine. (str; default: '') [default=""].
.. py:attribute:: kmos_combine.param.ifus

    The indices of the IFUs to combine. "ifu1;ifu2;..." (str; default: '') [default=""].
.. py:attribute:: kmos_combine.param.method

    The shifting method:   'none': no shifting, combined directly,  'header': shift according to WCS (default), 'center': centering  algorithm, 'user': read shifts from file (str; default: 'none') [default="none"].
.. py:attribute:: kmos_combine.param.fmethod

    The fitting method (applies only when method='center'):   'gauss': fit  a gauss function to collapsed image (default), 'moffat': fit a moffat  function to collapsed image (str; default: 'gauss') [default="gauss"].
.. py:attribute:: kmos_combine.param.filename

    The path to the file with the shift vectors (method='user') (str;  default: '') [default=""].
.. py:attribute:: kmos_combine.param.flux

    Apply flux conservation: (TRUE (apply) or FALSE (don't apply) (bool;  default: False) [default=False].
.. py:attribute:: kmos_combine.param.edge_nan

    Set borders of cubes to NaN before combining them.(TRUE (apply) or  FALSE (don't apply) (bool; default: False) [default=False].
.. py:attribute:: kmos_combine.param.skipped_frames

    Comma separated List of IFUs to skip for the combination. An IFU is  specified with R:I.R is the index (starting at 1) of the reconstructed  frame, I the IFU number (str; default: '') [default=""].
.. py:attribute:: kmos_combine.param.suppress_extension

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

    Flag to collapse the combined images (bool; default: False) [default=False].
.. py:attribute:: kmos_combine.param.cmethod

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

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

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

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

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

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


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

::

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

   kmos_combine.param.name = ""
   kmos_combine.param.ifus = ""
   kmos_combine.param.method = "none"
   kmos_combine.param.fmethod = "gauss"
   kmos_combine.param.filename = ""
   kmos_combine.param.flux = False
   kmos_combine.param.edge_nan = False
   kmos_combine.param.skipped_frames = ""
   kmos_combine.param.suppress_extension = False
   kmos_combine.param.collapse_combined = False
   kmos_combine.param.cmethod = "ksigma"
   kmos_combine.param.cpos_rej = 3.0
   kmos_combine.param.cneg_rej = 3.0
   kmos_combine.param.citer = 3
   kmos_combine.param.cmax = 1
   kmos_combine.param.cmin = 1


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

::

   import cpl
   kmos_combine = cpl.Recipe("kmos_combine")
   [...]
   res = kmos_combine( ..., param = {"name":"", "ifus":""})


.. 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, Y. 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, Y. Jung <usd-help@eso.org>
