The kmo_sci_red recipe
===============================================================

.. data:: kmo_sci_red

Synopsis
--------

Reconstruct obj/sky-pairs individually and combine them afterwards

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

Ideally at least two data frames have to be provided since we need for each IFU
pointing to an object as well a sky frame for the same IFU.

If an OH spectrum is given in the SOF file the lambda axis will be corrected
using the OH lines as reference.

Every IFU containing an object will be reconstructed and divided by telluric
and illumination correction. By default these intermediate cubes are saved to
disk. Frames just containing skies won’t produce an output here, so the number
of output frames can be smaller than the number of input frames.

Then the reconstructed objects with the same object name are combined. These
outputs are also saved to disk, the number of created files depends on the
number of reconstructed objects of different name. If the user just wants to
combine a certain object, the parameters --name or --ifus can be used.

For exposures taken with the templates KMOS_spec_obs_mapping8 and
KMOS_spec_obs_mapping24 the recipe behaves a bit different: All active IFUs
will be combined, regardless of the object names.


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

   DO                    KMOS                                                  
   category              Type   Explanation                   Required #Frames
   --------              -----  -----------                   -------- -------
   SCIENCE               RAW    The science frames                Y      >=1  
   XCAL                  F2D    x calibration frame               Y       1   
   YCAL                  F2D    y calibration frame               Y       1   
   LCAL                  F2D    Wavelength calib. frame           Y       1   
   WAVE_BAND             F2L    Table with start-/end-wavelengths Y       1   
   MASTER_FLAT           F2D    Master flat                       Y      0,1  
   ILLUM_CORR            F2I    Illumination correction           N      0,1  
   TELLURIC              F1I    normalised telluric spectrum      N      0,1  
   OH_SPEC               F1S    Vector holding OH lines           N      0,1  

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

   DO                    KMOS
   category              Type   Explanation
   --------              -----  -----------
   SCI_COMBINED          F3I    Combined cubes with noise
   SCI_RECONSTRUCTED     F3I    Reconstructed cube with noise
   EXP_MASK              F3I    Exposure time mask (not for mapping-templates!)
   SCI_INTERIM_OBJECT    F3I    (optional) Intermediate reconstructed object 
                                cubes used for sky tweaking, no noise 
                                (set --sky_tweak and --save_interims)
   SCI_INTERIM_SKY       F3I    (optional) Intermediate reconstructed sky 
                                cubes used for sky tweaking, no noise
                                (set --sky_tweak and --save_interims)


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

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

   Create an object for the recipe kmo_sci_red.

::

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

Parameters
----------

.. py:attribute:: kmo_sci_red.param.imethod

    Method to use for interpolation during reconstruction. ["NN" (nearest  neighbour), "lwNN" (linear weighted nearest neighbor), "swNN" (square  weighted nearest neighbor), "MS" (Modified Shepard's method)"CS"  (Cubic spline)] (str; default: 'CS') [default="CS"].
.. py:attribute:: kmo_sci_red.param.smethod

    Method to use for interpolation during shifting. ["NN" (nearest  neighbour), "CS" (Cubic spline)] (str; default: 'CS') [default="CS"].
.. py:attribute:: kmo_sci_red.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: 'header') [default="header"].
.. py:attribute:: kmo_sci_red.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:: kmo_sci_red.param.name

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

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

    Change the pixel scale [arcsec]. Default of 0.2" results into cubes of  14x14pix, a scale of 0.1" results into cubes of 28x28pix, etc. (float;  default: 0.2) [default=0.2].
.. py:attribute:: kmo_sci_red.param.suppress_extension

    Suppress arbitrary filename extension.(TRUE (apply) or FALSE (don't  apply) (bool; default: False) [default=False].
.. py:attribute:: kmo_sci_red.param.neighborhoodRange

    Defines the range to search for neighbors in pixels (float; default:  1.001) [default=1.001].
.. py:attribute:: kmo_sci_red.param.filename

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

    TRUE: Apply flux conservation. FALSE: otherwise (bool; default: False) [default=False].
.. py:attribute:: kmo_sci_red.param.background

    TRUE: Apply background removal. FALSE: otherwise (bool; default:  False) [default=False].
.. py:attribute:: kmo_sci_red.param.fast_mode

    FALSE: cubes are shifted and combined,TRUE: cubes are collapsed and  then shifted and combined (bool; default: False) [default=False].
.. py:attribute:: kmo_sci_red.param.extrapolate

    Applies only to 'smethod=CS' when doing sub-pixel shifts: FALSE:  shifted IFU will be filled with NaN's at the borders,TRUE: shifted IFU  will be extrapolated at the borders (bool; default: False) [default=False].
.. py:attribute:: kmo_sci_red.param.xcal_interpolation

    TRUE: Interpolate xcal between rotator angles. FALSE: otherwise (bool;  default: True) [default=True].
.. py:attribute:: kmo_sci_red.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:: kmo_sci_red.param.no_combine

    Don't combine cubes after reconstruction.(TRUE (apply) or FALSE (don't  apply) (bool; default: False) [default=False].
.. py:attribute:: kmo_sci_red.param.no_subtract

    Don't sky subtract object and references.(TRUE (apply) or FALSE (don't  apply) (bool; default: False) [default=False].
.. py:attribute:: kmo_sci_red.param.sky_tweak

    Use modified sky cube for sky subtraction.(TRUE (apply) or FALSE  (don't apply) (bool; default: False) [default=False].
.. py:attribute:: kmo_sci_red.param.tbsub

    Subtract thermal background from input cube.(TRUE (apply) or FALSE  (don't apply) (bool; default: True) [default=True].
.. py:attribute:: kmo_sci_red.param.b_samples

    The number of samples in wavelength for the reconstructed cube (long;  default: 2048) [default=2048].
.. py:attribute:: kmo_sci_red.param.b_start

    The lowest wavelength [um] to take into account when reconstructing  (default of -1 sets the proper value for the actual band  automatically) (float; default: -1.0) [default=-1.0].
.. py:attribute:: kmo_sci_red.param.b_end

    The highest wavelength [um] to take into account when reconstructing  (default of -1 sets the proper value for the actual band  automatically) (float; default: -1.0) [default=-1.0].
.. py:attribute:: kmo_sci_red.param.obj_sky_table

    The path to the file with the modified obj/sky associations. (str;  default: '') [default=""].
.. py:attribute:: kmo_sci_red.param.velocity_offset

    Specify velocity offset correction in km/s for lambda scale (float;  default: 0.0) [default=0.0].
.. py:attribute:: kmo_sci_red.param.save_interims

    Save interim object and sky cubes. Can only be used together with  --sky_tweak (bool; default: False) [default=False].
.. py:attribute:: kmo_sci_red.param.cmethod

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

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

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

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

    The number of maximum pixel values to clip with min/max-clipping.  (long; default: 1) [default=1].
.. py:attribute:: kmo_sci_red.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
   kmo_sci_red = cpl.Recipe("kmo_sci_red")

   kmo_sci_red.param.imethod = "CS"
   kmo_sci_red.param.smethod = "CS"
   kmo_sci_red.param.method = "header"
   kmo_sci_red.param.fmethod = "gauss"
   kmo_sci_red.param.name = ""
   kmo_sci_red.param.ifus = ""
   kmo_sci_red.param.pix_scale = 0.2
   kmo_sci_red.param.suppress_extension = False
   kmo_sci_red.param.neighborhoodRange = 1.001
   kmo_sci_red.param.filename = ""
   kmo_sci_red.param.flux = False
   kmo_sci_red.param.background = False
   kmo_sci_red.param.fast_mode = False
   kmo_sci_red.param.extrapolate = False
   kmo_sci_red.param.xcal_interpolation = True
   kmo_sci_red.param.edge_nan = False
   kmo_sci_red.param.no_combine = False
   kmo_sci_red.param.no_subtract = False
   kmo_sci_red.param.sky_tweak = False
   kmo_sci_red.param.tbsub = True
   kmo_sci_red.param.b_samples = 2048
   kmo_sci_red.param.b_start = -1.0
   kmo_sci_red.param.b_end = -1.0
   kmo_sci_red.param.obj_sky_table = ""
   kmo_sci_red.param.velocity_offset = 0.0
   kmo_sci_red.param.save_interims = False
   kmo_sci_red.param.cmethod = "ksigma"
   kmo_sci_red.param.cpos_rej = 3.0
   kmo_sci_red.param.cneg_rej = 3.0
   kmo_sci_red.param.citer = 3
   kmo_sci_red.param.cmax = 1
   kmo_sci_red.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
   kmo_sci_red = cpl.Recipe("kmo_sci_red")
   [...]
   res = kmo_sci_red( ..., param = {"imethod":"CS", "smethod":"CS"})


.. 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 <kmos-spark@mpe.mpg.de>`_. Alternatively, you may 
send a report to the `ESO User Support Department <usd-help@eso.org>`_.

Copyright
---------

This file is part of the KMOS 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, 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA

.. codeauthor:: Alex Agudo Berbel <kmos-spark@mpe.mpg.de>
