KMOS Pipeline Reference Manual  1.2.7
kmo_fits_stack.c
00001 /* $Id: kmo_fits_stack.c,v 1.8 2013-05-21 12:13:58 aagudo Exp $
00002  *
00003  * This file is part of the KMOS Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: aagudo $
00023  * $Date: 2013-05-21 12:13:58 $
00024  * $Revision: 1.8 $
00025  * $Name: not supported by cvs2svn $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033  *                              Includes
00034  *----------------------------------------------------------------------------*/
00035 
00036 #include <cpl.h>
00037 
00038 #include "kmo_utils.h"
00039 #include "kmo_dfs.h"
00040 #include "kmo_priv_fits_stack.h"
00041 #include "kmo_error.h"
00042 
00043 /*-----------------------------------------------------------------------------
00044  *                          Functions prototypes
00045  *----------------------------------------------------------------------------*/
00046 
00047 static int kmo_fits_stack_create(cpl_plugin *);
00048 static int kmo_fits_stack_exec(cpl_plugin *);
00049 static int kmo_fits_stack_destroy(cpl_plugin *);
00050 static int kmo_fits_stack(cpl_parameterlist *, cpl_frameset *);
00051 
00052 /*-----------------------------------------------------------------------------
00053  *                          Static variables
00054  *----------------------------------------------------------------------------*/
00055 
00056 static char kmo_fits_stack_description[] =
00057 "     #############################################\n"
00058 "     ### INTENDED FOR PIPELINE DEVELOPERS ONLY ###\n"
00059 "     #############################################\n"
00060 "\n"
00061 "FITS files to be processed by the KMOS pipeline have to meet certain condi-\n"
00062 "tions. This recipe is intended to provide to the user a simple way to test the\n"
00063 "pipeline with own data, which wasn't produced by KMOS itself.\n"
00064 "\n"
00065 "The input set of frame is checked for integrity (do all the frames have the\n"
00066 "same size, do they correspond to the desired output type, is there the correct\n"
00067 "number of files). Then an empty main header is written with desired keywords.\n"
00068 "A keyword consists of the name, data type and value.\n"
00069 "Additional keywords can be added either to the empty primary header or to all\n"
00070 "sub headers. \n"
00071 "\n"
00072 "BASIC PARAMETERS:\n"
00073 "-----------------\n"
00074 "--type\n"
00075 "Depending on the type of the FITS file to create different combinations of\n"
00076 "frames have to be provided:\n"
00077 "   * RAW\n"
00078 "     exactly 3 files tagged as STACK_DATA\n"
00079 "   * F2D\n"
00080 "     exactly 3 files tagged as STACK_DATA or\n"
00081 "     exactly 6 files tagged alternating as STACK_DATA and STACK_NOISE\n"
00082 "     (beginning with DATA)\n"
00083 "   * B2D\n"
00084 "     exactly 3 files tagged as STACK_BADPIX\n"
00085 "   * F1I, F2I, F3I\n"
00086 "     as many DATA frames as wanted (at least one) or\n"
00087 "     as many DATA and NOISE frames as wanted (at least one of each, the number\n"
00088 "     of STACK_DATA frames has to match the one of STACK_NOISE frames)\n"
00089 "   * F1S\n"
00090 "     exactly 1 file tagged as STACK_DATA\n"
00091 "   * F1L\n"
00092 "     exactly 1 file tagged as STACK_DATA (either plain text or binary fits\n"
00093 "     table)\n"
00094 "   * F2L\n"
00095 "     exactly 1 file tagged as STACK_DATA (either plain text or binary fits)\n"
00096 "     table)\n"
00097 "\n"
00098 "--mainkey\n"
00099 "--subkey\n"
00100 "Additional keywords can be added either to the empty primary header or to all\n"
00101 "sub headers. Provided keywords must have following form:\n"
00102 "   \"keyword;type;value;keyword;type;value\" (no spaces inbetween!)\n"
00103 "Allowed values for type are: string, int, float, double, bool\n"
00104 "\n"
00105 "--valid\n"
00106 "With the –valid parameter one can specifiy which values should be handled as\n"
00107 "invalid by the pipeline. The keyword ESO OCS ARMi NOTUSED will be set accor-\n"
00108 "dingly. When it is set to \"none\" nothing will be added.\n"
00109 "\n"
00110 "-------------------------------------------------------------------------------\n"
00111 "  Input files:\n"
00112 "\n"
00113 "   DO                    KMOS                                                  \n"
00114 "   category              Type   Explanation                    Required #Frames\n"
00115 "   --------              -----  -----------                    -------- -------\n"
00116 "   STACK_DATA            <none> >= 1 plain FITS files             Y       1-n  \n"
00117 "   STACK_NOISE                  >= 1 plain FITS files             N       0,1-n\n"
00118 "   or                                                                          \n"
00119 "   STACK_BADPIXEL               3 plain FITS files                Y        3   \n"
00120 "\n"
00121 "  Output files:\n"
00122 "\n"
00123 "   DO                     KMOS\n"
00124 "   category               Type    Explanation\n"
00125 "   --------               -----   -----------\n"
00126 "   FITS_STACK             RAW or  Stacked KMOS FITS file             \n"
00127 "                          F1D or                                     \n"
00128 "                          F2D or                                     \n"
00129 "                          B2D or                                     \n"
00130 "                          F1I or                                     \n"
00131 "                          F2I or                                     \n"
00132 "                          F3I or                                     \n"
00133 "                          F1S or                                     \n"
00134 "                          F1L or                                     \n"
00135 "                          F2L                                        \n"
00136 "-------------------------------------------------------------------------------\n"
00137 "\n";
00138 
00139 /*-----------------------------------------------------------------------------
00140  *                              Functions code
00141  *----------------------------------------------------------------------------*/
00142 
00159 int cpl_plugin_get_info(cpl_pluginlist *list)
00160 {
00161     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00162     cpl_plugin *plugin = &recipe->interface;
00163 
00164     cpl_plugin_init(plugin,
00165                         CPL_PLUGIN_API,
00166                         KMOS_BINARY_VERSION,
00167                         CPL_PLUGIN_TYPE_RECIPE,
00168                         "kmo_fits_stack",
00169                         "Creates KMOS conform fits-files",
00170                         kmo_fits_stack_description,
00171                         "Alex Agudo Berbel",
00172                         "kmos-spark@mpe.mpg.de",
00173                         kmos_get_license(),
00174                         kmo_fits_stack_create,
00175                         kmo_fits_stack_exec,
00176                         kmo_fits_stack_destroy);
00177 
00178     cpl_pluginlist_append(list, plugin);
00179 
00180     return 0;
00181 }
00182 
00190 static int kmo_fits_stack_create(cpl_plugin *plugin)
00191 {
00192     cpl_recipe *recipe;
00193     cpl_parameter *p;
00194 
00195     /* Check that the plugin is part of a valid recipe */
00196     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00197         recipe = (cpl_recipe *)plugin;
00198     else
00199         return -1;
00200 
00201     /* Create the parameters list in the cpl_recipe object */
00202     recipe->parameters = cpl_parameterlist_new();
00203 
00204     /* Fill the parameters list */
00205     /* --type */
00206     p = cpl_parameter_new_value("kmos.kmo_fits_stack.type",
00207                                 CPL_TYPE_STRING,
00208                                 "The KMOS data format type (either \"RAW\", "
00209                                 "\"F1D\", \"F2D\", \"F1I\", \"F2I\", \"F3I\", "
00210                                 "\"F1S\", \"F1L\", \"F2L\")",
00211                                 "kmos.kmo_fits_stack",
00212                                 "");
00213     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "type");
00214     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00215     cpl_parameterlist_append(recipe->parameters, p);
00216 
00217     /* --format */
00218     p = cpl_parameter_new_value("kmos.kmo_fits_stack.format",
00219                                 CPL_TYPE_STRING,
00220                                 "The format of the columns for \"F1L\" and "
00221                                 "\"F2L\" frames: e.g. \"%f;%f;%s\"",
00222                                 "kmos.kmo_fits_stack",
00223                                 "");
00224     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "format");
00225     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00226     cpl_parameterlist_append(recipe->parameters, p);
00227 
00228     /* --title */
00229     p = cpl_parameter_new_value("kmos.kmo_fits_stack.title",
00230                                 CPL_TYPE_STRING,
00231                                 "The titles of the columns for \"F1L\" and "
00232                                 "\"F2L\" frames: e.g. \"wavelength;strength\"",
00233                                 "kmos.kmo_fits_stack",
00234                                 "");
00235     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "title");
00236     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00237     cpl_parameterlist_append(recipe->parameters, p);
00238 
00239     /* --filename */
00240     p = cpl_parameter_new_value("kmos.kmo_fits_stack.filename",
00241                                 CPL_TYPE_STRING,
00242                                 "Optional: The output filename (.fits will be "
00243                                                             "added as postfix)",
00244                                 "kmos.kmo_fits_stack",
00245                                 FITS_STACK);
00246     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "filename");
00247     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00248     cpl_parameterlist_append(recipe->parameters, p);
00249 
00250     /* --mainkey */
00251     p = cpl_parameter_new_value("kmos.kmo_fits_stack.mainkey",
00252                              CPL_TYPE_STRING,
00253                              "Optional: Additional keywords for primary header",
00254                              "kmos.kmo_fits_stack",
00255                              "");
00256     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mainkey");
00257     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00258     cpl_parameterlist_append(recipe->parameters, p);
00259 
00260     /* --subkey */
00261     p = cpl_parameter_new_value("kmos.kmo_fits_stack.subkey",
00262                                 CPL_TYPE_STRING,
00263                                 "Optional: Additional keywords for sub headers"
00264                                 "(This is ignored when category=TRUE)",
00265                                 "kmos.kmo_fits_stack",
00266                                 "");
00267     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "subkey");
00268     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00269     cpl_parameterlist_append(recipe->parameters, p);
00270 
00271     /* --valid */
00272     p = cpl_parameter_new_value("kmos.kmo_fits_stack.valid",
00273                                 CPL_TYPE_STRING,
00274                                 "Optional: Specify which IFUs are active. "
00275                                 "Either empty string or string with 24 elements"
00276                                 " (ones or zeros) e.g: [1;0;1;0;0;...;1]",
00277                                 "kmos.kmo_fits_stack",
00278                                 "");
00279     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "valid");
00280     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00281     cpl_parameterlist_append(recipe->parameters, p);
00282 
00283     /* --category */
00284     p = cpl_parameter_new_value("kmos.kmo_fits_stack.category",
00285                                 CPL_TYPE_BOOL,
00286                                 "Optional: IF a PRO.CATG should be added.",
00287                                 "kmos.kmo_fits_stack",
00288                                 0);
00289     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "category");
00290     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00291     cpl_parameterlist_append(recipe->parameters, p);
00292 
00293     /* --input */
00294     p = cpl_parameter_new_value("kmos.kmo_fits_stack.input",
00295                                 CPL_TYPE_STRING,
00296                                 "Optional: input file",
00297                                 "kmos.kmo_fits_stack",
00298                                 "");
00299     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "input");
00300     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00301     cpl_parameterlist_append(recipe->parameters, p);
00302 
00303     return 0;
00304 }
00305 
00311 static int kmo_fits_stack_exec(cpl_plugin *plugin)
00312 {
00313     cpl_recipe  *recipe;
00314 
00315     /* Get the recipe out of the plugin */
00316     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00317         recipe = (cpl_recipe *)plugin;
00318     else return -1;
00319 
00320     return kmo_fits_stack(recipe->parameters, recipe->frames);
00321 }
00322 
00328 static int kmo_fits_stack_destroy(cpl_plugin *plugin)
00329 {
00330     cpl_recipe *recipe;
00331 
00332     /* Get the recipe out of the plugin */
00333     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00334         recipe = (cpl_recipe *)plugin;
00335     else return -1 ;
00336 
00337     cpl_parameterlist_delete(recipe->parameters);
00338     return 0 ;
00339 }
00340 
00355 static int kmo_fits_stack(cpl_parameterlist *parlist, cpl_frameset *frameset)
00356 {
00357     int ret_val = 0;
00358 
00359     KMO_TRY
00360     {
00361         ret_val = kmo_priv_fits_stack(parlist, frameset);
00362         KMO_TRY_CHECK_ERROR_STATE();
00363     }
00364     KMO_CATCH
00365     {
00366         KMO_CATCH_MSG();
00367 
00368         ret_val = -1;
00369     }
00370 
00371     return ret_val;
00372 }
00373