The fors_science recipe
===============================================================

.. data:: fors_science

Synopsis
--------

Extraction of scientific spectra

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

This recipe is used to reduce scientific spectra using the extraction
mask and the products created by the recipe fors_calib. The spectra are
bias subtracted, flat fielded (if a normalised flat field is specified)
and remapped eliminating the optical distortions. The wavelength calibration
can be optionally upgraded using a number of sky lines: if no sky lines
catalog of wavelengths is specified, an internal one is used instead.

If the alignment to the sky lines is performed, the input dispersion
coefficients table is upgraded and saved to disk, and a new CCD wavelengths
map is created.

This recipe accepts both FORS1 and FORS2 frames. A grism table (typically
depending on the instrument mode, and in particular on the grism used)
may also be specified: this table contains a default recipe parameter
setting to control the way spectra are extracted for a specific instrument
mode, as it is used for automatic run of the pipeline on Paranal and in
Garching. If this table is specified, it will modify the default recipe
parameter setting, with the exception of those parameters which have been
explicitly modifyed on the command line. If a grism table is not specified,
the input recipe parameters values will always be read from the command
line, or from an esorex configuration file if present, or from their
generic default values (that are rarely meaningful).

In the table below the MXU acronym can be read alternatively as MOS
and LSS, depending on the instrument mode of the input data. The acronym
SCI on products should be read STD in case of standard stars observations
A CURV_COEFF table is not (yet) expected for LSS data.

Either a scientific or a standard star exposure can be specified in input.

Only in case of a standard star exposure input, the atmospheric extinction
table and a table with the physical fluxes of the observed standard star
must be specified in input, and a spectro-photometric table is created in
output. This table can then be input again to this recipe, always with an
atmospheric extinction table, and if a photometric calibration is requested
then flux calibrated spectra (in units of erg/cm/cm/s/Angstrom) are also
written in output.


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

  DO category:               Type:       Explanation:         Required:
  SCIENCE_MXU                Raw         Scientific exposure      Y
  or STANDARD_MXU            Raw         Standard star exposure   Y
  MASTER_BIAS                Calib       Master bias              Y
  GRISM_TABLE                Calib       Grism table              .

  MASTER_SKYLINECAT          Calib       Sky lines catalog        .


  MASTER_NORM_FLAT_MXU       Calib       Normalised flat field    .

  DISP_COEFF_MXU             Calib       Inverse dispersion       Y
  CURV_COEFF_MXU             Calib       Spectral curvature       Y
  SLIT_LOCATION_MXU          Calib       Slits positions table    Y
  FLAT_SED_MXU               Calib       Slits dispersion profile .


  or, in case of LSS-like MOS/MXU data,

  MASTER_NORM_FLAT_LONG_MXU  Calib       Normalised flat field    .

  DISP_COEFF_LONG_MXU        Calib       Inverse dispersion       Y
  SLIT_LOCATION_LONG_MXU     Calib       Slits positions table    Y
  GLOBAL_DISTORTION_TABLE    Calib       Global distortion        .


  In case STANDARD_MXU is specified in input,

  EXTINCT_TABLE              Calib       Atmospheric extinction   Y
  STD_FLUX_TABLE             Calib       Standard star flux       Y
  TELLURIC_CONTAMINATION     Calib       Telluric regions list    .


  The following input files are mandatory if photometric calibrated  spectra are desired:

  EXTINCT_TABLE              Calib       Atmospheric extinction   Y
  SPECPHOT_TABLE             Calib       Response curves          Y

  If requested for standard star data, the SPECPHOT_TABLE can be dropped:
  in this case the correction is applied using the SPECPHOT_TABLE produced
  in the same run.


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

  DO category:               Data type:  Explanation:
  REDUCED_SCI_MXU            FITS image  Extracted scientific spectra
  REDUCED_SKY_SCI_MXU        FITS image  Extracted sky spectra
  REDUCED_ERROR_SCI_MXU      FITS image  Errors on extracted spectra
  UNMAPPED_SCI_MXU           FITS image  Sky subtracted scientific spectra
  MAPPED_SCI_MXU             FITS image  Rectified scientific spectra
  MAPPED_ALL_SCI_MXU         FITS image  Rectified science spectra with sky
  MAPPED_SKY_SCI_MXU         FITS image  Rectified sky spectra
  UNMAPPED_SKY_SCI_MXU       FITS image  Sky on CCD
  OBJECT_TABLE_SCI_MXU       FITS table  Positions of detected objects

  Only if the global sky subtraction is requested:
  GLOBAL_SKY_SPECTRUM_MXU    FITS table  Global sky spectrum

  Only if the sky-alignment of the wavelength solution is requested:
  SKY_SHIFTS_LONG_SCI_MXU    FITS table  Sky lines offsets (LSS-like data)
  or SKY_SHIFTS_SLIT_SCI_MXU FITS table  Sky lines offsets (MOS-like data)
  DISP_COEFF_SCI_MXU         FITS table  Upgraded dispersion coefficients
  WAVELENGTH_MAP_SCI_MXU     FITS image  Upgraded wavelength map

  Only if a STANDARD_MXU is specified in input:
  SPECPHOT_TABLE             FITS table  Efficiency and response curves

  Only if a photometric calibration was requested:
  REDUCED_FLUX_SCI_MXU       FITS image  Flux calibrated scientific spectra
  REDUCED_FLUX_ERROR_SCI_MXU FITS image  Errors on flux calibrated spectra
  MAPPED_FLUX_SCI_MXU        FITS image  Flux calibrated slit spectra


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

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

   Create an object for the recipe fors_science.

::

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

Parameters
----------

.. py:attribute:: fors_science.param.skyalign

    Polynomial order for sky lines alignment, or -1 to avoid alignment  (int; default: -1) [default=-1].
.. py:attribute:: fors_science.param.flatfield

    Apply flat field (bool; default: True) [default=True].
.. py:attribute:: fors_science.param.skyglobal

    Subtract global sky spectrum from CCD (bool; default: False) [default=False].
.. py:attribute:: fors_science.param.skymedian

    Sky subtraction from extracted slit spectra (bool; default: False) [default=False].
.. py:attribute:: fors_science.param.skylocal

    Sky subtraction from CCD slit spectra (bool; default: True) [default=True].
.. py:attribute:: fors_science.param.cosmics

    Eliminate cosmic rays hits, only if either global or local (not for  LSS) sky subtraction is also requested. (bool; default: False) [default=False].
.. py:attribute:: fors_science.param.slit_margin

    Number of pixels to exclude at each slit in object detection and  extraction (int; default: 3) [default=3].
.. py:attribute:: fors_science.param.ext_radius

    Maximum extraction radius for detected objects (unbinned pixel) (int;  default: 12) [default=12].
.. py:attribute:: fors_science.param.cont_radius

    Minimum distance at which two objects of equal luminosity do not  contaminate each other (pixel) (int; default: 0) [default=0].
.. py:attribute:: fors_science.param.ext_mode

    Object extraction method: 0 = aperture, 1 = Horne optimal extraction  (int; default: 1) [default=1].
.. py:attribute:: fors_science.param.resp_fit_nknots

    Number of knots in spline fitting of the instrument response. (-1: No  fitting. -2: Read from grism table) (int; default: -2) [default=-2].
.. py:attribute:: fors_science.param.resp_fit_degree

    Degree of polynomial in fitting of the instrument response. (-1: No  fitting. -2: Read from grism table) (int; default: -2) [default=-2].
.. py:attribute:: fors_science.param.resp_ignore_mode

    Types of lines/regions to ignore in response. Valid ones are  'stellar_absorption', 'telluric' and 'command_line' (from parameter  resp_ignore_lines) (str; default:  'stellar_absorption,telluric,command_line') [default="stellar_absorption,telluric,command_line"].
.. py:attribute:: fors_science.param.resp_ignore_points

    Extra lines/regions to ignore in response. Use a comma separated list  of values. A range can be specified like 4500.0-4600.0 (str; default:  '') [default=""].
.. py:attribute:: fors_science.param.resp_use_flat_sed

    Use the flat SED to normalise the observed spectra. Value are true,  false, grism_table. (str; default: 'grism_table') [default="grism_table"].
.. py:attribute:: fors_science.param.nonlinear_level

    Level above which the detector is not linear (float; default: 60000.0) [default=60000.0].
.. py:attribute:: fors_science.param.generate_idp

    Set to TRUE to request IDP generation (bool; default: False) [default=False].


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

::

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

   fors_science.param.skyalign = -1
   fors_science.param.flatfield = True
   fors_science.param.skyglobal = False
   fors_science.param.skymedian = False
   fors_science.param.skylocal = True
   fors_science.param.cosmics = False
   fors_science.param.slit_margin = 3
   fors_science.param.ext_radius = 12
   fors_science.param.cont_radius = 0
   fors_science.param.ext_mode = 1
   fors_science.param.resp_fit_nknots = -2
   fors_science.param.resp_fit_degree = -2
   fors_science.param.resp_ignore_mode = "stellar_absorption,telluric,command_line"
   fors_science.param.resp_ignore_points = ""
   fors_science.param.resp_use_flat_sed = "grism_table"
   fors_science.param.nonlinear_level = 60000.0
   fors_science.param.generate_idp = False


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

::

   import cpl
   fors_science = cpl.Recipe("fors_science")
   [...]
   res = fors_science( ..., param = {"skyalign":-1, "flatfield":True})


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

Copyright
---------

This file is currently part of the FORS Instrument Pipeline
Copyright (C) 2002-2011 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  02110-1301  USA


.. codeauthor:: Carlo Izzo <usd-help@eso.org>
