KMOS Pipeline Reference Manual  1.2.8
kmo_dev_setup.c
00001 /* $Id: kmo_dev_setup.c,v 1.10 2013-06-18 07:56:47 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-06-18 07:56:47 $
00024  * $Revision: 1.10 $
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 
00037 #include <math.h>
00038 #include <string.h>
00039 
00040 #include <cpl.h>
00041 
00042 #include "kmclipm_math.h"
00043 
00044 #include "kmo_priv_reconstruct.h"
00045 #include "kmo_priv_functions.h"
00046 #include "kmo_priv_fits_stack.h"
00047 #include "kmo_cpl_extensions.h"
00048 #include "kmo_dfs.h"
00049 #include "kmo_error.h"
00050 #include "kmo_constants.h"
00051 #include "kmo_debug.h"
00052 
00053 /*-----------------------------------------------------------------------------
00054  *                          Functions prototypes
00055  *----------------------------------------------------------------------------*/
00056 
00057 static int kmo_dev_setup_create(cpl_plugin *);
00058 static int kmo_dev_setup_exec(cpl_plugin *);
00059 static int kmo_dev_setup_destroy(cpl_plugin *);
00060 static int kmo_dev_setup(cpl_parameterlist *, cpl_frameset *);
00061 
00062 /*-----------------------------------------------------------------------------
00063  *                          Static variables
00064  *----------------------------------------------------------------------------*/
00065 
00066 static char kmo_dev_setup_description[] =
00067 "     #############################################\n"
00068 "     ### INTENDED FOR PIPELINE DEVELOPERS ONLY ###\n"
00069 "     #############################################\n"
00070 "\n"
00071 "This recipe is intended to create KMOS conform files in a semi-automatic manner.\n"
00072 "It is sufficient to provide a single FITS file and a few parameters to create\n"
00073 "KMOS conform FITS files suited for different recipes. Internally it calls repea-\n"
00074 "tedly the recipe kmo_fits_stack. There are also parameters that allow to prepare\n"
00075 "the frames, i.e. early test out of the lab, in a way they can be processed.\n"
00076 "\n"
00077 "One extension from the input frame is taken, some noise is added automatically\n"
00078 "in order to create similar frames for the other extensions.\n"
00079 "\n"
00080 "BASIC PARAMETERS:\n"
00081 "-----------------\n"
00082 "--type\n"
00083 "Defines for which recipe the files should be created ('DARK', \n"
00084 "'FLAT_ON', 'FLAT_OFF', 'ARC_ON', 'ARC_OFF', 'STD', 'SKY').\n"
00085 "\n"
00086 "--extension\n"
00087 "Defines which extension is used to craete frames\n"
00088 "\n"
00089 "--xshift\n"
00090 "--yshift\n"
00091 "Shift frames in x and y\n"
00092 "\n"
00093 "--rotangle\n"
00094 "Sets the ESO OCS ROT NAANGLE keyword in the primary header.\n"
00095 "\n"
00096 "--topcrop\n"
00097 "--bottomcrop\n"
00098 "--leftcrop\n"
00099 "--rightcrop\n"
00100 "These are cropping the image (filled with 0).\n"
00101 "\n"
00102 "--mainkey\n"
00103 "--subkey\n"
00104 "Add individual keywords to primary- or sub-header\n"
00105 "\n"
00106 "--valid\n"
00107 "Defines if IFUs are active or inactive\n"
00108 "\n"
00109 "--objects\n"
00110 "Defines if IFUs contain object or sky.\n"
00111 "\n"
00112 "--date\n"
00113 "Sets the DATE-OBS keword in the primary header.\n"
00114 "\n"
00115 "--filter\n"
00116 "Sets the filter type for all extensions.\n"
00117 "\n"
00118 "--grating\n"
00119 "Sets the grating type for all extensions.\n"
00120 "\n"
00121 "-------------------------------------------------------------------------------\n"
00122 "  Input files:\n"
00123 "\n"
00124 "   DO                     KMOS                                                 \n"
00125 "   category               Type  Explanation                    Required #Frames\n"
00126 "   --------               ----- -----------                    -------- -------\n"
00127 "    DARK        or        RAW   Frame to create Master dark or    Y        1   \n"
00128 "    FLAT_ON     or              Flat-on or                                     \n"
00129 "    FLAT_OFF    or              Flat-off or                                    \n"
00130 "    ARC_ON      or              Arc-on or                                      \n"
00131 "    ARC_OFF     or              Arc-off or                                     \n"
00132 "    SKY         or              Sky or                                         \n"
00133 "    STD         or              Std star or                                    \n"
00134 "    GENERIC                     any other generic KMOS frame                   \n"
00135 "\n"
00136 "  Output files:\n"
00137 "\n"
00138 "   DO                    KMOS\n"
00139 "   category              Type   Explanation\n"
00140 "   --------              -----  -----------\n"
00141 "   <see comment>         RAW    Named depending on --type parameter\n"
00142 "-------------------------------------------------------------------------------\n"
00143 "\n";
00144 
00145 /*-----------------------------------------------------------------------------
00146  *                              Functions code
00147  *----------------------------------------------------------------------------*/
00148 
00165 int cpl_plugin_get_info(cpl_pluginlist *list)
00166 {
00167     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00168     cpl_plugin *plugin = &recipe->interface;
00169 
00170     cpl_plugin_init(plugin,
00171                         CPL_PLUGIN_API,
00172                         KMOS_BINARY_VERSION,
00173                         CPL_PLUGIN_TYPE_RECIPE,
00174                         "kmo_dev_setup",
00175                         "Create aligned KMOS files out of test frames",
00176                         kmo_dev_setup_description,
00177                         "Alex Agudo Berbel",
00178                         "kmos-spark@mpe.mpg.de",
00179                         kmos_get_license(),
00180                         kmo_dev_setup_create,
00181                         kmo_dev_setup_exec,
00182                         kmo_dev_setup_destroy);
00183 
00184     cpl_pluginlist_append(list, plugin);
00185 
00186     return 0;
00187 }
00188 
00196 static int kmo_dev_setup_create(cpl_plugin *plugin)
00197 {
00198     cpl_recipe *recipe;
00199     cpl_parameter *p;
00200 
00201     /* Check that the plugin is part of a valid recipe */
00202     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00203         recipe = (cpl_recipe *)plugin;
00204     else
00205         return -1;
00206 
00207     /* Create the parameters list in the cpl_recipe object */
00208     recipe->parameters = cpl_parameterlist_new();
00209 
00210      /* Fill the parameters list */
00211     /* --type */
00212     p = cpl_parameter_new_value("kmos.kmo_dev_setup.type",
00213                                 CPL_TYPE_STRING,
00214                                 "FITS type to create (DARK, FLAT_ON, "
00215                                 "FLAT_OFF, ARC_ON, ARC_OFF, SKY, GENERIC, STD)",
00216                                 "kmos.kmo_dev_setup",
00217                                 "");
00218     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "type");
00219     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00220     cpl_parameterlist_append(recipe->parameters, p);
00221 
00222     /* --extension */
00223     p = cpl_parameter_new_value("kmos.kmo_dev_setup.extension",
00224                                 CPL_TYPE_INT,
00225                                 "FITS extension to process (0: primary, 1, 2,...)",
00226                                 "kmos.kmo_dev_setup",
00227                                 0);
00228     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "extension");
00229     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00230     cpl_parameterlist_append(recipe->parameters, p);
00231 
00232     /* --xshift */
00233     p = cpl_parameter_new_value("kmos.kmo_dev_setup.xshift",
00234                                 CPL_TYPE_INT,
00235                                 "integer shift in x (to the right -> pos)",
00236                                 "kmos.kmo_dev_setup",
00237                                 0);
00238     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xshift");
00239     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00240     cpl_parameterlist_append(recipe->parameters, p);
00241 
00242     /* --yshift */
00243     p = cpl_parameter_new_value("kmos.kmo_dev_setup.yshift",
00244                                 CPL_TYPE_INT,
00245                                 "integer shift in y (to the top -> pos)",
00246                                 "kmos.kmo_dev_setup",
00247                                 0);
00248     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "yshift");
00249     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00250     cpl_parameterlist_append(recipe->parameters, p);
00251 
00252 //    /* --rotation */
00253 //    p = cpl_parameter_new_value("kmos.kmo_dev_setup.rotation",
00254 //                                CPL_TYPE_DOUBLE,
00255 //                                "rotation in degrees (CCW)",
00256 //                                "kmos.kmo_dev_setup",
00257 //                                0.0);
00258 //    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "rotation");
00259 //    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00260 //    cpl_parameterlist_append(recipe->parameters, p);
00261 
00262     /* --rotangle */
00263     p = cpl_parameter_new_value("kmos.kmo_dev_setup.rotangle",
00264                                 CPL_TYPE_DOUBLE,
00265                                 "Rotator offset angle in degrees (CCW)",
00266                                 "kmos.kmo_dev_setup",
00267                                 -1.0);
00268     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "rotangle");
00269     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00270     cpl_parameterlist_append(recipe->parameters, p);
00271 
00272     /* --topcrop */
00273     p = cpl_parameter_new_value("kmos.kmo_dev_setup.topcrop",
00274                                 CPL_TYPE_INT,
00275                                 "number of rows to crop at top",
00276                                 "kmos.kmo_dev_setup",
00277                                 0);
00278     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "topcrop");
00279     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00280     cpl_parameterlist_append(recipe->parameters, p);
00281 
00282     /* --bottomcrop */
00283     p = cpl_parameter_new_value("kmos.kmo_dev_setup.bottomcrop",
00284                                 CPL_TYPE_INT,
00285                                 "number of rows to crop at bottom",
00286                                 "kmos.kmo_dev_setup",
00287                                 0);
00288     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bottomcrop");
00289     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00290     cpl_parameterlist_append(recipe->parameters, p);
00291 
00292     /* --leftcrop */
00293     p = cpl_parameter_new_value("kmos.kmo_dev_setup.leftcrop",
00294                                 CPL_TYPE_INT,
00295                                 "number of columns to crop at left",
00296                                 "kmos.kmo_dev_setup",
00297                                 0);
00298     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "leftcrop");
00299     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00300     cpl_parameterlist_append(recipe->parameters, p);
00301 
00302     /* --rightcrop */
00303     p = cpl_parameter_new_value("kmos.kmo_dev_setup.rightcrop",
00304                                 CPL_TYPE_INT,
00305                                 "number of columns to crop at right",
00306                                 "kmos.kmo_dev_setup",
00307                                 0);
00308     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "rightcrop");
00309     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00310     cpl_parameterlist_append(recipe->parameters, p);
00311 
00312     /* --mainkey */
00313     p = cpl_parameter_new_value("kmos.kmo_dev_setup.mainkey",
00314                              CPL_TYPE_STRING,
00315                              "Optional: Additional keywords for primary header",
00316                              "kmos.kmo_dev_setup",
00317                              "");
00318     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mainkey");
00319     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00320     cpl_parameterlist_append(recipe->parameters, p);
00321 
00322     /* --subkey */
00323     p = cpl_parameter_new_value("kmos.kmo_dev_setup.subkey",
00324                                 CPL_TYPE_STRING,
00325                                 "Optional: Additional keywords for sub headers",
00326                                 "kmos.kmo_dev_setup",
00327                                 "");
00328     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "subkey");
00329     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00330     cpl_parameterlist_append(recipe->parameters, p);
00331 
00332     /* --valid */
00333     p = cpl_parameter_new_value("kmos.kmo_dev_setup.valid",
00334                                 CPL_TYPE_STRING,
00335                                 "Optional: Specify which IFUs are active. "
00336                                 "Either empty string or string with 8 elements"
00337                                 " (ones or zeros) e.g: [1;0;1;0;0;...;1]",
00338                                 "kmos.kmo_dev_setup",
00339                                 "");
00340     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "valid");
00341     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00342     cpl_parameterlist_append(recipe->parameters, p);
00343 
00344     /* --objects */
00345     p = cpl_parameter_new_value("kmos.kmo_dev_setup.objects",
00346                                 CPL_TYPE_STRING,
00347                                 "Optional (STD only): Specify which IFUs contain"
00348                                 " objects. Either empty string or string with 8"
00349                                 " elements (ones or zeros) "
00350                                 "e.g: [1;0;1;0;0;...;1]",
00351                                 "kmos.kmo_dev_setup",
00352                                 "");
00353     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "objects");
00354     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00355     cpl_parameterlist_append(recipe->parameters, p);
00356 
00357     /* --date */
00358     p = cpl_parameter_new_value("kmos.kmo_dev_setup.date",
00359                                 CPL_TYPE_STRING,
00360                                 "Optional (STD only): Specify the date to save "
00361                                 "into DATE-OBS "
00362                                 "e.g: [2010-01-31T11:53:15.9789]",
00363                                 "kmos.kmo_dev_setup",
00364                                 "");
00365     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "date");
00366     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00367     cpl_parameterlist_append(recipe->parameters, p);
00368 
00369     /* --filter */
00370     p = cpl_parameter_new_value("kmos.kmo_dev_setup.filter",
00371                                 CPL_TYPE_STRING,
00372                                 "filter type (K, H, HK, etc.)",
00373                                 "kmos.kmo_dev_setup",
00374                                 "");
00375     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "filter");
00376     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00377     cpl_parameterlist_append(recipe->parameters, p);
00378 
00379     /* --grating */
00380     p = cpl_parameter_new_value("kmos.kmo_dev_setup.grating",
00381                                 CPL_TYPE_STRING,
00382                                 "grating type (K, H, HK, etc.)",
00383                                 "kmos.kmo_dev_setup",
00384                                 "");
00385     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "grating");
00386     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00387     cpl_parameterlist_append(recipe->parameters, p);
00388 
00389     return 0;
00390 }
00391 
00397 static int kmo_dev_setup_exec(cpl_plugin *plugin)
00398 {
00399     cpl_recipe  *recipe;
00400 
00401     /* Get the recipe out of the plugin */
00402     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00403         recipe = (cpl_recipe *)plugin;
00404     else return -1;
00405 
00406     return kmo_dev_setup(recipe->parameters, recipe->frames);
00407 }
00408 
00414 static int kmo_dev_setup_destroy(cpl_plugin *plugin)
00415 {
00416     cpl_recipe *recipe;
00417 
00418     /* Get the recipe out of the plugin */
00419     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00420         recipe = (cpl_recipe *)plugin;
00421     else return -1 ;
00422 
00423     cpl_parameterlist_delete(recipe->parameters);
00424     return 0 ;
00425 }
00426 
00427 cpl_frameset* dev_frameset(const char *p1, const char *p2, const char *p3) {
00428     cpl_frameset *frset = NULL;
00429     cpl_frame *fr = NULL;
00430 
00431     KMO_TRY
00432     {
00433 
00434         KMO_TRY_EXIT_IF_NULL(
00435             frset = cpl_frameset_new());
00436 
00437         KMO_TRY_EXIT_IF_NULL(
00438             fr = cpl_frame_new());
00439         KMO_TRY_EXIT_IF_ERROR(
00440             cpl_frame_set_filename(fr, p1));
00441         KMO_TRY_EXIT_IF_ERROR(
00442             cpl_frame_set_tag(fr, FS_DATA));
00443         KMO_TRY_EXIT_IF_ERROR(
00444             cpl_frame_set_type(fr, CPL_FRAME_TYPE_NONE));
00445         KMO_TRY_EXIT_IF_ERROR(
00446             cpl_frame_set_group(fr, CPL_FRAME_GROUP_NONE));
00447         KMO_TRY_EXIT_IF_ERROR(
00448             cpl_frame_set_level(fr, CPL_FRAME_LEVEL_NONE));
00449         KMO_TRY_EXIT_IF_ERROR(
00450             cpl_frameset_insert(frset, fr));
00451 
00452         KMO_TRY_EXIT_IF_NULL(
00453             fr = cpl_frame_new());
00454         KMO_TRY_EXIT_IF_ERROR(
00455             cpl_frame_set_filename(fr, p2));
00456         KMO_TRY_EXIT_IF_ERROR(
00457             cpl_frame_set_tag(fr, FS_DATA));
00458         KMO_TRY_EXIT_IF_ERROR(
00459             cpl_frame_set_type(fr, CPL_FRAME_TYPE_NONE));
00460         KMO_TRY_EXIT_IF_ERROR(
00461             cpl_frame_set_group(fr, CPL_FRAME_GROUP_NONE));
00462         KMO_TRY_EXIT_IF_ERROR(
00463             cpl_frame_set_level(fr, CPL_FRAME_LEVEL_NONE));
00464         KMO_TRY_EXIT_IF_ERROR(
00465             cpl_frameset_insert(frset, fr));
00466 
00467         KMO_TRY_EXIT_IF_NULL(
00468             fr = cpl_frame_new());
00469         KMO_TRY_EXIT_IF_ERROR(
00470             cpl_frame_set_filename(fr, p3));
00471         KMO_TRY_EXIT_IF_ERROR(
00472             cpl_frame_set_tag(fr, FS_DATA));
00473         KMO_TRY_EXIT_IF_ERROR(
00474             cpl_frame_set_type(fr, CPL_FRAME_TYPE_NONE));
00475         KMO_TRY_EXIT_IF_ERROR(
00476             cpl_frame_set_group(fr, CPL_FRAME_GROUP_NONE));
00477         KMO_TRY_EXIT_IF_ERROR(
00478             cpl_frame_set_level(fr, CPL_FRAME_LEVEL_NONE));
00479         KMO_TRY_EXIT_IF_ERROR(
00480             cpl_frameset_insert(frset, fr));
00481     }
00482     KMO_CATCH
00483     {
00484         cpl_frameset_delete(frset); frset = NULL;
00485     }
00486     return frset;
00487 }
00488 
00489 cpl_frameset* dev_frameset_master(const char *p1) {
00490     cpl_frameset *frset = NULL;
00491 
00492     cpl_frame *fr = NULL;
00493     int i = 0;
00494 
00495     KMO_TRY
00496     {
00497 
00498         KMO_TRY_EXIT_IF_NULL(
00499             frset = cpl_frameset_new());
00500         for (i = 0; i < 3; i++) {
00501             KMO_TRY_EXIT_IF_NULL(
00502                 fr = cpl_frame_new());
00503             KMO_TRY_EXIT_IF_ERROR(
00504                 cpl_frame_set_filename(fr, p1));
00505             KMO_TRY_EXIT_IF_ERROR(
00506                 cpl_frame_set_tag(fr, FS_DATA));
00507             KMO_TRY_EXIT_IF_ERROR(
00508                 cpl_frame_set_type(fr, CPL_FRAME_TYPE_NONE));
00509             KMO_TRY_EXIT_IF_ERROR(
00510                 cpl_frame_set_group(fr, CPL_FRAME_GROUP_NONE));
00511             KMO_TRY_EXIT_IF_ERROR(
00512                 cpl_frame_set_level(fr, CPL_FRAME_LEVEL_NONE));
00513             KMO_TRY_EXIT_IF_ERROR(
00514                 cpl_frameset_insert(frset, fr));
00515 
00516             KMO_TRY_EXIT_IF_NULL(
00517                 fr = cpl_frame_new());
00518             KMO_TRY_EXIT_IF_ERROR(
00519                 cpl_frame_set_filename(fr, p1));
00520             KMO_TRY_EXIT_IF_ERROR(
00521                 cpl_frame_set_tag(fr, FS_NOISE));
00522             KMO_TRY_EXIT_IF_ERROR(
00523                 cpl_frame_set_type(fr, CPL_FRAME_TYPE_NONE));
00524             KMO_TRY_EXIT_IF_ERROR(
00525                 cpl_frame_set_group(fr, CPL_FRAME_GROUP_NONE));
00526             KMO_TRY_EXIT_IF_ERROR(
00527                 cpl_frame_set_level(fr, CPL_FRAME_LEVEL_NONE));
00528             KMO_TRY_EXIT_IF_ERROR(
00529                 cpl_frameset_insert(frset, fr));
00530         }
00531     }
00532     KMO_CATCH
00533     {
00534         cpl_frameset_delete(frset); frset = NULL;
00535     }
00536     return frset;
00537 }
00538 
00553 static int kmo_dev_setup(cpl_parameterlist *parlist, cpl_frameset *frameset)
00554 {
00555     int         ret_val     = 0,
00556                 ext         = 0,
00557                 xshift      = 0,
00558                 yshift      = 0,
00559                 topcrop     = 0,
00560                 bottomcrop  = 0,
00561                 leftcrop    = 0,
00562                 rightcrop   = 0,
00563                 x           = 0,
00564                 y           = 0,
00565                 nx          = 0,
00566                 ny          = 0,
00567                 i           = 0;
00568 //    double      rotation    = 0.0;
00569     double      rotangle    = 0.0;
00570 
00571     float       *pimg1      = NULL,
00572                 *ptmp_img   = NULL;
00573 
00574     const char  *type       = NULL,
00575                 *mainkey    = NULL,
00576                 *subkey     = NULL,
00577                 *valid      = NULL,
00578                 *objects_txt= NULL,
00579                 *date       = NULL,
00580                 *filter     = NULL,
00581                 *grating    = NULL;
00582 
00583     char        pmainkey[2048],
00584                 psubkey[2048],
00585                 pvalid[2048],
00586                 pfilename[2048],
00587                 tmp_str[256];
00588 
00589     const char *obsid        = "-2147",
00590                *exptime_min  = "2.47624",
00591                *exptime_long  = "600";
00592 
00593 
00594     cpl_frame   *frame      = NULL;
00595 
00596     cpl_image   *img1       = NULL,
00597                 *img_copy   = NULL,
00598                 *noise      = NULL,
00599                 *noise_copy = NULL,
00600                 *img_noise1 = NULL,
00601                 *img_noise2 = NULL,
00602                 *img_noise3 = NULL,
00603                 *tmp_img    = NULL;
00604 
00605     cpl_vector  *objects    = NULL;
00606 
00607     cpl_frameset *frset     = NULL;
00608 
00609 //    cpl_frame   *fr         = NULL;
00610 
00611     cpl_parameterlist *plist    = NULL;
00612 
00613     cpl_parameter *p        = NULL;
00614 
00615     KMO_TRY
00616     {
00617         strcpy(pmainkey, "");
00618         strcpy(psubkey, "");
00619         strcpy(pvalid, "");
00620         strcpy(pfilename, "");
00621         strcpy(tmp_str, "");
00622 
00623         /* --- check input --- */
00624         KMO_TRY_ASSURE((parlist != NULL) &&
00625                        (frameset != NULL),
00626                        CPL_ERROR_NULL_INPUT,
00627                        "Not all input data is provided!");
00628 
00629         /* --- get parameters --- */
00630         cpl_msg_info("", "--- Parameter setup for kmo_dev_setup -----");
00631 
00632         KMO_TRY_EXIT_IF_NULL(
00633             type = kmo_dfs_get_parameter_string(parlist,
00634                                             "kmos.kmo_dev_setup.type"));
00635         KMO_TRY_EXIT_IF_ERROR(
00636             kmo_dfs_print_parameter_help(parlist,
00637                                         "kmos.kmo_dev_setup.type"));
00638 
00639         ext = kmo_dfs_get_parameter_int(parlist,
00640                                         "kmos.kmo_dev_setup.extension");
00641         KMO_TRY_CHECK_ERROR_STATE();
00642         KMO_TRY_ASSURE(ext >= 0,
00643                        CPL_ERROR_ILLEGAL_INPUT,
00644                        "ext >= 0!");
00645         KMO_TRY_EXIT_IF_ERROR(
00646             kmo_dfs_print_parameter_help(parlist,
00647                                         "kmos.kmo_dev_setup.extension"));
00648 
00649         xshift = kmo_dfs_get_parameter_int(parlist,
00650                                            "kmos.kmo_dev_setup.xshift");
00651         KMO_TRY_CHECK_ERROR_STATE();
00652         KMO_TRY_EXIT_IF_ERROR(
00653             kmo_dfs_print_parameter_help(parlist,
00654                                         "kmos.kmo_dev_setup.xshift"));
00655 
00656         yshift = kmo_dfs_get_parameter_int(parlist,
00657                                            "kmos.kmo_dev_setup.yshift");
00658         KMO_TRY_CHECK_ERROR_STATE();
00659         KMO_TRY_EXIT_IF_ERROR(
00660             kmo_dfs_print_parameter_help(parlist,
00661                                         "kmos.kmo_dev_setup.yshift"));
00662 
00663 //        rotation = kmo_dfs_get_parameter_double(parlist,
00664 //                                           "kmos.kmo_dev_setup.rotation");
00665 //        KMO_TRY_CHECK_ERROR_STATE();
00666 //        KMO_TRY_EXIT_IF_ERROR(
00667 //            kmo_dfs_print_parameter_help(parlist,
00668 //                                        "kmos.kmo_dev_setup.rotation"));
00669 
00670         topcrop = kmo_dfs_get_parameter_int(parlist,
00671                                            "kmos.kmo_dev_setup.topcrop");
00672         KMO_TRY_CHECK_ERROR_STATE();
00673         KMO_TRY_EXIT_IF_ERROR(
00674             kmo_dfs_print_parameter_help(parlist,
00675                                         "kmos.kmo_dev_setup.topcrop"));
00676 
00677         bottomcrop = kmo_dfs_get_parameter_int(parlist,
00678                                            "kmos.kmo_dev_setup.bottomcrop");
00679         KMO_TRY_CHECK_ERROR_STATE();
00680         KMO_TRY_EXIT_IF_ERROR(
00681             kmo_dfs_print_parameter_help(parlist,
00682                                         "kmos.kmo_dev_setup.bottomcrop"));
00683 
00684         leftcrop = kmo_dfs_get_parameter_int(parlist,
00685                                            "kmos.kmo_dev_setup.leftcrop");
00686         KMO_TRY_CHECK_ERROR_STATE();
00687         KMO_TRY_EXIT_IF_ERROR(
00688             kmo_dfs_print_parameter_help(parlist,
00689                                         "kmos.kmo_dev_setup.leftcrop"));
00690 
00691         rightcrop = kmo_dfs_get_parameter_int(parlist,
00692                                            "kmos.kmo_dev_setup.rightcrop");
00693         KMO_TRY_CHECK_ERROR_STATE();
00694         KMO_TRY_EXIT_IF_ERROR(
00695             kmo_dfs_print_parameter_help(parlist,
00696                                         "kmos.kmo_dev_setup.rightcrop"));
00697 
00698         KMO_TRY_EXIT_IF_NULL(
00699             mainkey = kmo_dfs_get_parameter_string(parlist,
00700                                             "kmos.kmo_dev_setup.mainkey"));
00701         KMO_TRY_EXIT_IF_ERROR(
00702             kmo_dfs_print_parameter_help(parlist,
00703                                         "kmos.kmo_dev_setup.mainkey"));
00704 
00705         KMO_TRY_EXIT_IF_NULL(
00706             subkey = kmo_dfs_get_parameter_string(parlist,
00707                                             "kmos.kmo_dev_setup.subkey"));
00708         KMO_TRY_EXIT_IF_ERROR(
00709             kmo_dfs_print_parameter_help(parlist,
00710                                         "kmos.kmo_dev_setup.subkey"));
00711 
00712         KMO_TRY_EXIT_IF_NULL(
00713             valid = kmo_dfs_get_parameter_string(parlist,
00714                                             "kmos.kmo_dev_setup.valid"));
00715         KMO_TRY_EXIT_IF_ERROR(
00716             kmo_dfs_print_parameter_help(parlist,
00717                                         "kmos.kmo_dev_setup.valid"));
00718 
00719         KMO_TRY_EXIT_IF_NULL(
00720             objects_txt = kmo_dfs_get_parameter_string(parlist,
00721                                             "kmos.kmo_dev_setup.objects"));
00722         KMO_TRY_EXIT_IF_ERROR(
00723             kmo_dfs_print_parameter_help(parlist,
00724                                         "kmos.kmo_dev_setup.objects"));
00725 
00726         KMO_TRY_EXIT_IF_NULL(
00727             date = kmo_dfs_get_parameter_string(parlist,
00728                                             "kmos.kmo_dev_setup.date"));
00729         KMO_TRY_EXIT_IF_ERROR(
00730             kmo_dfs_print_parameter_help(parlist,
00731                                         "kmos.kmo_dev_setup.date"));
00732 
00733         KMO_TRY_EXIT_IF_NULL(
00734             filter = kmo_dfs_get_parameter_string(parlist,
00735                                             "kmos.kmo_dev_setup.filter"));
00736         KMO_TRY_EXIT_IF_ERROR(
00737             kmo_dfs_print_parameter_help(parlist,
00738                                         "kmos.kmo_dev_setup.filter"));
00739 
00740         KMO_TRY_EXIT_IF_NULL(
00741             grating = kmo_dfs_get_parameter_string(parlist,
00742                                             "kmos.kmo_dev_setup.grating"));
00743         KMO_TRY_EXIT_IF_ERROR(
00744             kmo_dfs_print_parameter_help(parlist,
00745                                         "kmos.kmo_dev_setup.grating"));
00746 
00747         cpl_msg_info("", "-------------------------------------------");
00748 
00749         KMO_TRY_ASSURE((strcmp(type, "DARK") == 0) ||
00750                        (strcmp(type, "FLAT_ON") == 0) ||
00751                        (strcmp(type, "FLAT_OFF") == 0) ||
00752                        (strcmp(type, "ARC_ON") == 0) ||
00753                        (strcmp(type, "ARC_OFF") == 0) ||
00754                        (strcmp(type, "SKY") == 0) ||
00755                        (strcmp(type, "STD") == 0) ||
00756                        (strcmp(type, "MASTER_FLAT") == 0) ||
00757                        (strcmp(type, "GENERIC") == 0),
00758                        CPL_ERROR_ILLEGAL_INPUT,
00759                        "Wrong type!");
00760 
00761         // get frame
00762         KMO_TRY_EXIT_IF_NULL(
00763             frame = kmo_dfs_get_frame(frameset, "0"));
00764 
00765         // load data
00766         KMO_TRY_EXIT_IF_NULL(
00767             img1 = kmclipm_image_load(cpl_frame_get_filename(frame),
00768                                 CPL_TYPE_FLOAT,
00769                                 0,
00770                                 ext));
00771 
00772         //
00773         // SHIFT
00774         //
00775         if ((xshift != 0) || (yshift != 0)) {
00776             cpl_msg_info(cpl_func, "shift: x=%d, y=%d", xshift, yshift);
00777             KMO_TRY_EXIT_IF_ERROR(
00778                 cpl_image_shift(img1, xshift, yshift));
00779         } else {
00780             cpl_msg_info(cpl_func, "no shift applied.");
00781         }
00782 //cpl_image_save(img1, "dev_shift.fits", CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_CREATE);
00783 
00784 //        //
00785 //        // ROTATION
00786 //        //
00787 //        if (fabs(rotation) >= 0.0001) {
00788 //            cpl_msg_info(cpl_func, "rotation: r=%g CCW", rotation);
00789 //            KMO_TRY_EXIT_IF_NULL(
00790 //                    img2 = kmo_dev_rotate(img1, rotation,
00791 //                                          1, "BCS", NONE_NANS));
00792 //        } else {
00793 //            cpl_msg_info(cpl_func, "no rotation applied.");
00794 //            img2 = img1;
00795 //        }
00796 //cpl_image_save(img2, "dev_rotation.fits", CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_CREATE);
00797 
00798         nx = cpl_image_get_size_x(img1);
00799         ny = cpl_image_get_size_y(img1);
00800 
00801         //
00802         // CROP
00803         //
00804         if ((topcrop != 0) || (bottomcrop != 0) ||
00805             (leftcrop != 0) || (rightcrop != 0)) {
00806             cpl_msg_info(cpl_func, "crop: top=%d, bottom=%d, left=%d, right=%d",
00807                          topcrop, bottomcrop, leftcrop, rightcrop);
00808 
00809             KMO_TRY_EXIT_IF_NULL(
00810                 pimg1 = cpl_image_get_data_float(img1));
00811 
00812             // bottomcrop
00813             for (y = 0; y < bottomcrop; y++) {
00814                 for (x = 0; x < nx; x++) {
00815                     pimg1[x+y*nx] = 0.0;
00816                 }
00817             }
00818 
00819             // topcrop
00820             for (y = nx-topcrop; y < nx; y++) {
00821                 for (x = 0; x < nx; x++) {
00822                     pimg1[x+y*nx] = 0.0;
00823                 }
00824             }
00825 
00826             // leftcrop
00827             for (y = 0; y < ny; y++) {
00828                 for (x = 0; x < leftcrop; x++) {
00829                     pimg1[x+y*nx] = 0.0;
00830                 }
00831             }
00832 
00833             // rightcrop
00834             for (y = 0; y < ny; y++) {
00835                 for (x = nx-rightcrop; x < nx; x++) {
00836                     pimg1[x+y*nx] = 0.0;
00837                 }
00838             }
00839         } else {
00840             cpl_msg_info(cpl_func, "no crop applied.");
00841         }
00842 //cpl_image_save(img1, "dev_crop.fits", CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_CREATE);
00843 
00844         //
00845         // NOISIFY
00846         //
00847         KMO_TRY_EXIT_IF_NULL(
00848             noise = cpl_image_new(nx, ny, CPL_TYPE_FLOAT));
00849         KMO_TRY_EXIT_IF_ERROR(
00850             cpl_image_fill_noise_uniform(noise, -1.0, 1.0));
00851 
00852         // calculate level
00853         float level = 0.0;
00854         if ((strcmp(type, "DARK") == 0) ||
00855             (strcmp(type, "SKY") == 0))
00856         {
00857             KMO_TRY_EXIT_IF_NULL(
00858                 tmp_img = cpl_image_duplicate(img1));
00859             KMO_TRY_EXIT_IF_NULL(
00860                 ptmp_img = cpl_image_get_data(tmp_img));
00861             for (i = 0; i < nx*ny; i++) {
00862                 if (kmclipm_is_nan_or_inf(ptmp_img[i])) {
00863                     ptmp_img[i] = 0;
00864                 }
00865             }
00866             KMO_TRY_CHECK_ERROR_STATE();
00867             level = cpl_image_get_stdev(tmp_img);
00868             KMO_TRY_CHECK_ERROR_STATE();
00869             cpl_image_delete(tmp_img); tmp_img = NULL;
00870 
00871             KMO_TRY_EXIT_IF_ERROR(
00872                 cpl_image_multiply_scalar(noise, level));
00873 
00874             // img_noise1:
00875             img_noise1 = img1;
00876 
00877             // img_noise2: just add noise
00878             KMO_TRY_EXIT_IF_NULL(
00879                 img_noise2 = cpl_image_add_create(img1, noise));
00880 
00881             // img_noise3: add noise flipped horizontally
00882             cpl_image_flip(noise, 0);
00883             KMO_TRY_EXIT_IF_NULL(
00884                 img_noise3 = cpl_image_add_create(img1, noise));
00885         } else {
00886             KMO_TRY_EXIT_IF_NULL(
00887                 img_copy = cpl_image_duplicate(img1));
00888 
00889             KMO_TRY_EXIT_IF_NULL(
00890                 noise_copy = cpl_image_duplicate(noise));
00891 
00892             // img_noise1:
00893             img_noise1 = img1;
00894 
00895             // img_noise2: add noise level 1
00896             KMO_TRY_EXIT_IF_ERROR(
00897                 cpl_image_multiply_scalar(noise, 10));
00898             KMO_TRY_EXIT_IF_NULL(
00899                 img_noise2 = cpl_image_add_create(img1, noise));
00900 
00901             // img_noise3: add noise level 2
00902             KMO_TRY_EXIT_IF_ERROR(
00903                 cpl_image_multiply_scalar(noise_copy, 15));
00904             KMO_TRY_EXIT_IF_NULL(
00905                 img_noise3 = cpl_image_add_create(img_copy, noise_copy));
00906 
00907             cpl_image_delete(img_copy); img_copy = NULL;
00908             cpl_image_delete(noise_copy); noise_copy = NULL;
00909         }
00910         cpl_image_delete(noise); noise = NULL;
00911 
00912         // these have to be stored!
00913         kmclipm_image_save(img_noise1, "tmp_delete1.fits", CPL_BPP_IEEE_FLOAT,
00914                            NULL, CPL_IO_CREATE, 0);
00915         kmclipm_image_save(img_noise2, "tmp_delete2.fits", CPL_BPP_IEEE_FLOAT,
00916                            NULL, CPL_IO_CREATE, 0);
00917         kmclipm_image_save(img_noise3, "tmp_delete3.fits", CPL_BPP_IEEE_FLOAT,
00918                            NULL, CPL_IO_CREATE, 0);
00919         cpl_image_delete(img_noise2); img_noise2 = NULL;
00920         cpl_image_delete(img_noise3); img_noise3 = NULL;
00921 
00922         //
00923         // STACK
00924         //
00925 
00926         // setup parameterlist
00927 
00928 // all types
00929         /* --mainkey */
00930 
00931         if (strcmp(mainkey, "") != 0) {
00932             strcpy(pmainkey, mainkey);
00933             strcat(pmainkey, ";");
00934             strcat(pmainkey,"ESO OBS ID;int;");strcat(pmainkey, obsid);
00935         } else {
00936             strcpy(pmainkey,"ESO OBS ID;int;");strcat(pmainkey, obsid);
00937         }
00938 //        strcat(pmainkey, ";");
00939         if (rotangle != -1) {
00940             char *nr = NULL;
00941             KMO_TRY_EXIT_IF_NULL(
00942                 nr = cpl_sprintf("%g", rotangle));
00943             strcat(pmainkey,";ESO OCS ROT NAANGLE;double;");strcat(pmainkey, nr);
00944             cpl_free(nr); nr = NULL;
00945         }
00946 
00947         if (strcmp(filter, "") != 0) {
00948             strcat(pmainkey, ";ESO INS FILT1 ID;string;");strcat(pmainkey, filter);
00949             strcat(pmainkey,";ESO INS FILT2 ID;string;");strcat(pmainkey, filter);
00950             strcat(pmainkey,";ESO INS FILT3 ID;string;");strcat(pmainkey, filter);
00951         }
00952 
00953         if (strcmp(grating, "") != 0) {
00954             strcat(pmainkey,";ESO INS GRAT1 ID;string;");strcat(pmainkey, grating);
00955             strcat(pmainkey,";ESO INS GRAT2 ID;string;");strcat(pmainkey, grating);
00956             strcat(pmainkey,";ESO INS GRAT3 ID;string;");strcat(pmainkey, grating);
00957         }
00958         KMO_TRY_CHECK_ERROR_STATE();
00959 
00960         /* --subkey */
00961         if (strcmp(subkey, "") != 0) {
00962             strcpy(psubkey, subkey);
00963         }
00964         KMO_TRY_CHECK_ERROR_STATE();
00965 
00966 //        if (strcmp(type, "DARK") == 0) {
00967             /* --mainkey */
00968             strcat(pmainkey, ";ESO DET SEQ1 MINDIT;double;");strcat(pmainkey, exptime_min);
00969             strcat(pmainkey, ";ESO DET NDIT;int;1");
00970             strcat(pmainkey, ";EXPTIME;double;");strcat(pmainkey, exptime_long);
00971             strcat(pmainkey, ";ESO DET READ CURNAME;string;Double");
00972 
00973             /* --subkey */
00974             if (strcmp(subkey, "") != 0) strcat(psubkey, ";");
00975             strcat(psubkey, "EXPTIME;double;");strcat(psubkey, exptime_long);
00976 //        }
00977 
00978         if (strcmp(type, "FLAT_ON") == 0) {
00979             /* --mainkey */
00980             strcat(pmainkey,";ESO INS LAMP3 ST;bool;1");
00981 //            strcat(pmainkey, "EXPTIME;double;");strcat(pmainkey, exptime_long);
00982 
00983             /* --subkey */
00984 //            if (strcmp(subkey, "") != 0)  strcat(psubkey, ";");
00985 //            strcat(psubkey, "EXPTIME;double;");strcat(psubkey, exptime_long);
00986         }
00987         KMO_TRY_CHECK_ERROR_STATE();
00988 
00989         if (strcmp(type, "ARC_ON") == 0)
00990         {
00991             strcat(pmainkey,";ESO INS LAMP1 ST;bool;1");
00992 //            strcat(pmainkey, "EXPTIME;double;");strcat(pmainkey, exptime_long);
00993 
00994             /* --subkey */
00995             if (strcmp(subkey, "") != 0)  strcat(psubkey, ";");
00996 //            strcat(psubkey, "EXPTIME;double;");strcat(psubkey, exptime_long);
00997         }
00998         KMO_TRY_CHECK_ERROR_STATE();
00999 
01000         if ((strcmp(type, "FLAT_OFF") == 0) ||
01001             (strcmp(type, "ARC_OFF") == 0)) {
01002             /* --mainkey */
01003 //            strcat(pmainkey, "EXPTIME;double;");strcat(pmainkey, exptime_long);
01004 
01005             /* --subkey */
01006             if (strcmp(subkey, "") != 0)  strcat(psubkey, ";");
01007 //            strcat(psubkey, "EXPTIME;double;");strcat(psubkey, exptime_long);
01008         }
01009         KMO_TRY_CHECK_ERROR_STATE();
01010 
01011         if ((strcmp(type, "SKY") == 0) ||
01012             (strcmp(type, "MASTER_FLAT") == 0) ||
01013             (strcmp(type, "STD") == 0) ||
01014             (strcmp(type, "GENERIC") == 0))
01015         {
01016             /* --mainkey */
01017             if (strcmp(type, "STD") == 0) {
01018                 // add DATE-OBS keyword
01019 //                strcat(pmainkey,";");
01020                 strcat(pmainkey,";DATE-OBS;string;");strcat(pmainkey, date);
01021 //                strcat(pmainkey, ";EXPTIME;double;");strcat(pmainkey, exptime_long);
01022 
01023                 // add ESO OCS ARMi TYPE keywords
01024                 objects = kmo_identify_values(objects_txt);
01025                 KMO_TRY_ASSURE(cpl_vector_get_size(objects) == KMOS_IFUS_PER_DETECTOR,
01026                                CPL_ERROR_ILLEGAL_INPUT,
01027                                "valid parameter must have 8 elements!");
01028 
01029                 for (i = 0; i < cpl_vector_get_size(objects)*3; i++) {
01030                     strcat(pmainkey,";");
01031                     strcat(pmainkey,"ESO OCS ARM");
01032                     sprintf(tmp_str, "%d ", i+1);
01033                     strcat(pmainkey,tmp_str);
01034                     strcat(pmainkey,"TYPE;string;");
01035 
01036                     if (fabs(cpl_vector_get(objects, i%KMOS_IFUS_PER_DETECTOR)-1) > 0.01 ) {
01037                         // sky
01038                         strcat(pmainkey,"S");
01039                     } else {
01040                         //object
01041                         strcat(pmainkey,"O");
01042                     }
01043                 }
01044                 cpl_vector_delete(objects); objects = NULL;
01045             }
01046 //printf("----------\n");
01047 //printf("%s\n", pmainkey);
01048 //printf("----------\n");
01049 //            /* --subkey */
01050 //            if (strcmp(subkey, "") != 0) {
01051 //                strcat(psubkey, ";");
01052 //            }
01053 //            strcat(psubkey, ";EXPTIME;double;");
01054 //            strcat(psubkey, exptime_long);
01055         }
01056         KMO_TRY_CHECK_ERROR_STATE();
01057 
01058 // SETUP PARAMETERLIST
01059         plist = cpl_parameterlist_new();
01060 
01061         /* --valid */
01062         strcpy(pvalid, valid);
01063         strcat(pvalid, ";");
01064         strcat(pvalid, valid);
01065         strcat(pvalid, ";");
01066         strcat(pvalid, valid);
01067 
01068         p = cpl_parameter_new_value("kmos.kmo_fits_stack.valid",
01069                                     CPL_TYPE_STRING,
01070                                     "Optional: Specify which IFUs are active. "
01071                                     "Either empty string or string with 24 elements"
01072                                     " (ones or zeros) e.g: [1;0;1;0;0;...;1]",
01073                                     "kmos.kmo_fits_stack",
01074                                     pvalid);
01075         cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "valid");
01076         cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01077         cpl_parameterlist_append(plist, p);
01078 
01079         if (strcmp(type, "MASTER_FLAT") != 0) {
01080             // all types except MASTER_FLAT
01081 
01082             /* --type (RAW, F1D, F2D, F1I, F2I, F3I) */
01083             p = cpl_parameter_new_value("kmos.kmo_fits_stack.type",
01084                                         CPL_TYPE_STRING,
01085                                         "The KMOS data format type (either \"RAW\", "
01086                                         "\"F1D\", \"F2D\", \"F1I\", \"F2I\", \"F3I\", "
01087                                         "\"F1S\", \"F1L\", \"F2L\")",
01088                                         "kmos.kmo_fits_stack",
01089                                         RAW);
01090             cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "type");
01091             cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01092             cpl_parameterlist_append(plist, p);
01093             KMO_TRY_CHECK_ERROR_STATE();
01094 
01095             /* --mainkey */
01096             p = cpl_parameter_new_value("kmos.kmo_fits_stack.mainkey",
01097                                      CPL_TYPE_STRING,
01098                                      "Optional: Additional keywords for primary header",
01099                                      "kmos.kmo_fits_stack",
01100                                      pmainkey);
01101             cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mainkey");
01102             cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01103             cpl_parameterlist_append(plist, p);
01104 
01105             /* --subkey */
01106             p = cpl_parameter_new_value("kmos.kmo_fits_stack.subkey",
01107                                         CPL_TYPE_STRING,
01108                                         "Optional: Additional keywords for sub headers",
01109                                         "kmos.kmo_fits_stack",
01110                                         psubkey);
01111             cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "subkey");
01112             cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01113             cpl_parameterlist_append(plist, p);
01114             KMO_TRY_CHECK_ERROR_STATE();
01115 
01116             // stack 1st
01117             KMO_TRY_EXIT_IF_NULL(
01118                 frset = dev_frameset("tmp_delete1.fits", "tmp_delete2.fits", "tmp_delete3.fits"));
01119             KMO_TRY_CHECK_ERROR_STATE();
01120 
01121             /* --filename */
01122             strcpy(pfilename, type);
01123             strcat(pfilename, "_123");
01124             p = cpl_parameter_new_value("kmos.kmo_fits_stack.filename",
01125                                         CPL_TYPE_STRING,
01126                                         "Optional: The output filename (.fits will be "
01127                                                                     "added as postfix)",
01128                                         "kmos.kmo_fits_stack",
01129                                         pfilename);
01130             cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "filename");
01131             cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01132             cpl_parameterlist_append(plist, p);
01133 
01134             kmo_priv_fits_stack(plist, frset);
01135             KMO_TRY_CHECK_ERROR_STATE();
01136             cpl_frameset_delete(frset); frset = NULL;
01137 
01138             if ((strcmp(type, "DARK") == 0) ||
01139                 (strcmp(type, "FLAT_ON") == 0) ||
01140                 (strcmp(type, "FLAT_OFF") == 0) ||
01141                 (strcmp(type, "SKY") == 0))
01142             {
01143                 // stack 2nd
01144                 KMO_TRY_EXIT_IF_NULL(
01145                     frset = dev_frameset("tmp_delete2.fits", "tmp_delete3.fits", "tmp_delete1.fits"));
01146                 strcpy(pfilename, type);
01147                 strcat(pfilename, "_231");
01148                 KMO_TRY_EXIT_IF_NULL(
01149                     p = cpl_parameterlist_find(plist, "kmos.kmo_fits_stack.filename"));
01150                 KMO_TRY_EXIT_IF_ERROR(
01151                     cpl_parameter_set_string(p, pfilename));
01152 
01153                 kmo_priv_fits_stack(plist, frset);
01154                 KMO_TRY_CHECK_ERROR_STATE();
01155                 cpl_frameset_delete(frset); frset = NULL;
01156 
01157                 // stack 3rd
01158                 KMO_TRY_EXIT_IF_NULL(
01159                     frset = dev_frameset("tmp_delete3.fits", "tmp_delete1.fits", "tmp_delete2.fits"));
01160                 strcpy(pfilename, type);
01161                 strcat(pfilename, "_312");
01162                 KMO_TRY_EXIT_IF_NULL(
01163                     p = cpl_parameterlist_find(plist, "kmos.kmo_fits_stack.filename"));
01164                 KMO_TRY_EXIT_IF_ERROR(
01165                     cpl_parameter_set_string(p, pfilename));
01166 
01167                 kmo_priv_fits_stack(plist, frset);
01168                 KMO_TRY_CHECK_ERROR_STATE();
01169                 cpl_frameset_delete(frset); frset = NULL;
01170             }
01171         } else{
01172             /* --type (RAW, F1D, F2D, F1I, F2I, F3I) */
01173             p = cpl_parameter_new_value("kmos.kmo_fits_stack.type",
01174                                         CPL_TYPE_STRING,
01175                                         "The KMOS data format type (either \"RAW\", "
01176                                         "\"F1D\", \"F2D\", \"F1I\", \"F2I\", \"F3I\", "
01177                                         "\"F1S\", \"F1L\", \"F2L\")",
01178                                         "kmos.kmo_fits_stack",
01179                                         F2D);
01180             cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "type");
01181             cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01182             cpl_parameterlist_append(plist, p);
01183             KMO_TRY_CHECK_ERROR_STATE();
01184 
01185             /* --mainkey */
01186             p = cpl_parameter_new_value("kmos.kmo_fits_stack.mainkey",
01187                                      CPL_TYPE_STRING,
01188                                      "Optional: Additional keywords for primary header",
01189                                      "kmos.kmo_fits_stack",
01190                                      pmainkey);
01191             cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mainkey");
01192             cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01193             cpl_parameterlist_append(plist, p);
01194 
01195             /* --subkey */
01196             p = cpl_parameter_new_value("kmos.kmo_fits_stack.subkey",
01197                                         CPL_TYPE_STRING,
01198                                         "Optional: Additional keywords for sub headers",
01199                                         "kmos.kmo_fits_stack",
01200                                         psubkey);
01201             cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "subkey");
01202             cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01203             cpl_parameterlist_append(plist, p);
01204             KMO_TRY_CHECK_ERROR_STATE();
01205 
01206             /* --filename */
01207             strcpy(pfilename, "master_flat_ones");
01208             p = cpl_parameter_new_value("kmos.kmo_fits_stack.filename",
01209                                         CPL_TYPE_STRING,
01210                                         "Optional: The output filename (.fits will be "
01211                                                                     "added as postfix)",
01212                                         "kmos.kmo_fits_stack",
01213                                         pfilename);
01214             cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "filename");
01215             cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
01216             cpl_parameterlist_append(plist, p);
01217 
01218             KMO_TRY_EXIT_IF_NULL(
01219                 frset = dev_frameset_master(cpl_frame_get_filename(frame)));
01220             KMO_TRY_CHECK_ERROR_STATE();
01221 
01222             kmo_priv_fits_stack(plist, frset);
01223             KMO_TRY_CHECK_ERROR_STATE();
01224             cpl_frameset_delete(frset); frset = NULL;
01225         }
01226 
01227         KMO_TRY_CHECK_ERROR_STATE();
01228         cpl_msg_info("", "*******************************************");
01229         cpl_msg_info("", "Saved a set of KMOS frames for recipe %s.", type );
01230         cpl_msg_info("", "*******************************************");
01231     }
01232     KMO_CATCH
01233     {
01234         KMO_CATCH_MSG();
01235         ret_val = -1;
01236     }
01237 
01238     cpl_image_delete(img1); img1 = NULL;
01239     cpl_parameterlist_delete(plist); plist = NULL;
01240 
01241     return ret_val;
01242 }
01243