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