KMOS Pipeline Reference Manual  1.2.6
kmo_extract_spec.c
00001 /* $Id: kmo_extract_spec.c,v 1.15 2013-06-07 15:41:20 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-07 15:41:20 $
00024  * $Revision: 1.15 $
00025  * $Name: not supported by cvs2svn $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033  *                              Includes
00034  *----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <math.h>
00038 
00039 #include <cpl.h>
00040 
00041 #include "kmo_utils.h"
00042 #include "kmo_dfs.h"
00043 #include "kmo_error.h"
00044 #include "kmo_priv_extract_spec.h"
00045 #include "kmo_priv_functions.h"
00046 #include "kmo_cpl_extensions.h"
00047 #include "kmo_constants.h"
00048 #include "kmo_priv_fit_profile.h"
00049 #include "kmo_debug.h"
00050 
00051 
00052 /*-----------------------------------------------------------------------------
00053  *                          Functions prototypes
00054  *----------------------------------------------------------------------------*/
00055 
00056 static int kmo_extract_spec_create(cpl_plugin *);
00057 static int kmo_extract_spec_exec(cpl_plugin *);
00058 static int kmo_extract_spec_destroy(cpl_plugin *);
00059 static int kmo_extract_spec(cpl_parameterlist *, cpl_frameset *);
00060 
00061 /*-----------------------------------------------------------------------------
00062  *                          Static variables
00063  *----------------------------------------------------------------------------*/
00064 
00065 static char kmo_extract_spec_description[] =
00066 "This recipe extracts a spectrum from a datacube. The datacube must be in\n"
00067 "F3I KMOS FITS format (either with or without noise). The output will be a\n"
00068 "similarly formatted F1I KMOS FITS file.\n"
00069 "\n"
00070 "BASIC PARAMETERS:\n"
00071 "-----------------\n"
00072 "--mask_method\n"
00073 "There are several ways to define the region to consider for spectrum calculation:\n"
00074 "  * 'integrated'' (default)\n"
00075 "  A circular mask with defined centre and radius is created (--centre and\n"
00076 "  --radius have to be defined). This mask is applied to all extensions.\n"
00077 "\n"
00078 "  * 'mask'\n"
00079 "  An arbitrary mask can be provided (for example the mask created by kmo_sky_mask\n"
00080 "  can be used). The mask must be in F2I KMOS FITS format, mustn't contain noise\n"
00081 "  and must have as many extensions as the input cube. The mask can be binary as\n"
00082 "  well as it can contain float values, so a weighted mask is also possible.\n"
00083 "  (0: pixels is ignored, 1: pixel is included) The mask must be of the same size\n"
00084 "  that the input datacube.\n"
00085 "\n"
00086 "  * 'optimal'\n"
00087 "  The mask is created automatically by fitting a normalised profile (using\n"
00088 "  kmo_fit_profile) to the image of the datacube (using kmo_make_image the data-\n"
00089 "  cube is summed up in spectral direction according to the specified --cmethod).\n"
00090 "  This profile is then used as mask input. When --save_mask is set to true the\n"
00091 "  mask is saved on disk. The remaining parameters not described here apply to\n"
00092 "  the fitting of the profile.\n"
00093 "\n"
00094 "If the spectra of several objects in a IFU should be extracted, --mask_method=\n"
00095 "'mask' is recommended. With several calls to kmo_extract_spec using different\n"
00096 "masks all spectra can be extracted.\n"
00097 "\n"
00098 "ADVANCED PARAMETERS\n"
00099 "-------------------\n"
00100 "--centre\n"
00101 "--radius\n"
00102 "see --mask_method = 'integrated'\n"
00103 "\n"
00104 "--save_mask\n"
00105 "see --mask_method = 'optimal'\n"
00106 "\n"
00107 "--cmethod\n"
00108 "Applies only if –mask_method = 'integral'\n"
00109 "Following methods of frame combination are available:\n"
00110 "   * 'ksigma' (Default)\n"
00111 "   An iterative sigma clipping. For each position all pixels in the spectrum\n"
00112 "   are examined. If they deviate significantly, they will be rejected according\n"
00113 "   to the conditions:\n"
00114 "       val > mean + stdev * cpos_rej\n"
00115 "   and\n"
00116 "       val < mean - stdev * cneg_rej\n"
00117 "   where --cpos_rej, --cneg_rej and --citer are the corresponding configuration\n"
00118 "   parameters. In the first iteration median and percentile level are used.\n"
00119 "\n"
00120 "   * 'median'\n"
00121 "   At each pixel position the median is calculated.\n"
00122 "\n"
00123 "   * 'average'\n"
00124 "   At each pixel position the average is calculated.\n"
00125 "\n"
00126 "   * 'sum'\n"
00127 "   At each pixel position the sum is calculated.\n"
00128 "\n"
00129 "   * 'min_max'\n"
00130 "   The specified number of minimum and maximum pixel values will be rejected.\n"
00131 "   --cmax and --cmin apply to this method.\n"
00132 "\n"
00133 "--cpos_rej\n"
00134 "--cneg_rej\n"
00135 "--citer\n"
00136 "see --cmethod='ksigma'\n"
00137 "\n"
00138 "--cmax\n"
00139 "--cmin\n"
00140 "see --cmethod='min_max'\n"
00141 
00142 "\n"
00143 "-------------------------------------------------------------------------------\n"
00144 "  Input files:\n"
00145 "\n"
00146 "   DO                    KMOS                                                  \n"
00147 "   category              Type   Explanation                    Required #Frames\n"
00148 "   --------              -----  -----------                    -------- -------\n"
00149 "   <none or any>         F3I    The datacubes                     Y        1   \n"
00150 "   <none or any>         F2I    The mask                          N       0,1  \n"
00151 "\n"
00152 "  Output files:\n"
00153 "\n"
00154 "   DO                    KMOS\n"
00155 "   category              Type   Explanation\n"
00156 "   --------              -----  -----------\n"
00157 "   EXTRACT_SPEC          F1I    Extracted spectrum                             \n"
00158 "   EXTRACT_SPEC_MASK     F2I    (optional, if --save_mask=true and             \n"
00159 "                                --mask_method='optimal': The calculated mask)  \n"
00160 "-------------------------------------------------------------------------------\n"
00161 "\n";
00162 
00163 /*-----------------------------------------------------------------------------
00164  *                              Functions code
00165  *----------------------------------------------------------------------------*/
00166 
00183 int cpl_plugin_get_info(cpl_pluginlist *list)
00184 {
00185     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00186     cpl_plugin *plugin = &recipe->interface;
00187 
00188     cpl_plugin_init(plugin,
00189                         CPL_PLUGIN_API,
00190                         KMOS_BINARY_VERSION,
00191                         CPL_PLUGIN_TYPE_RECIPE,
00192                         "kmo_extract_spec",
00193                         "Extract a spectrum from a cube.",
00194                         kmo_extract_spec_description,
00195                         "Alex Agudo Berbel",
00196                         "kmos-spark@mpe.mpg.de",
00197                         kmos_get_license(),
00198                         kmo_extract_spec_create,
00199                         kmo_extract_spec_exec,
00200                         kmo_extract_spec_destroy);
00201 
00202     cpl_pluginlist_append(list, plugin);
00203 
00204     return 0;
00205 }
00206 
00214 static int kmo_extract_spec_create(cpl_plugin *plugin)
00215 {
00216     cpl_recipe *recipe;
00217     cpl_parameter *p;
00218 
00219     /* Check that the plugin is part of a valid recipe */
00220     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00221         recipe = (cpl_recipe *)plugin;
00222     else
00223         return -1;
00224 
00225     /* Create the parameters list in the cpl_recipe object */
00226     recipe->parameters = cpl_parameterlist_new();
00227 
00228     /* Fill the parameters list */
00229     /* --mask_method */
00230     p = cpl_parameter_new_value("kmos.kmo_extract_spec.mask_method",
00231                                 CPL_TYPE_STRING,
00232                                 "Either apply 'mask', 'integrated' or "
00233                                     "'optimal' masking method.",
00234                                 "kmos.kmo_extract_spec",
00235                                 "integrated");
00236     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mask_method");
00237     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00238     cpl_parameterlist_append(recipe->parameters, p);
00239 
00240     /* --centre */
00241     p = cpl_parameter_new_value("kmos.kmo_extract_spec.centre",
00242                                 CPL_TYPE_STRING,
00243                                 "The centre of the circular mask (pixel).",
00244                                 "kmos.kmo_extract_spec",
00245                                 "7.5,7.5");
00246     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "centre");
00247     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00248     cpl_parameterlist_append(recipe->parameters, p);
00249 
00250     /* --radius */
00251     p = cpl_parameter_new_value("kmos.kmo_extract_spec.radius",
00252                                 CPL_TYPE_DOUBLE,
00253                                 "The radius of the circular mask (pixel).",
00254                                 "kmos.kmo_extract_spec",
00255                                 3.0);
00256     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "radius");
00257     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00258     cpl_parameterlist_append(recipe->parameters, p);
00259 
00260     /* --save_mask */
00261     p = cpl_parameter_new_value("kmos.kmo_extract_spec.save_mask",
00262                                 CPL_TYPE_BOOL,
00263                                 "True if the calculated mask should be saved.",
00264                                 "kmos.kmo_extract_spec",
00265                                 FALSE);
00266     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "save_mask");
00267     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00268     cpl_parameterlist_append(recipe->parameters, p);
00269 
00270     return kmo_combine_pars_create(recipe->parameters,
00271                                    "kmos.kmo_extract_spec",
00272                                    DEF_REJ_METHOD,
00273                                    FALSE);
00274 }
00275 
00281 static int kmo_extract_spec_exec(cpl_plugin *plugin)
00282 {
00283     cpl_recipe  *recipe;
00284 
00285     /* Get the recipe out of the plugin */
00286     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00287         recipe = (cpl_recipe *)plugin;
00288     else return -1;
00289 
00290     return kmo_extract_spec(recipe->parameters, recipe->frames);
00291 }
00292 
00298 static int kmo_extract_spec_destroy(cpl_plugin *plugin)
00299 {
00300     cpl_recipe *recipe;
00301 
00302     /* Get the recipe out of the plugin */
00303     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00304         recipe = (cpl_recipe *)plugin;
00305     else return -1 ;
00306 
00307     cpl_parameterlist_delete(recipe->parameters);
00308     return 0 ;
00309 }
00310 
00325 static int kmo_extract_spec(cpl_parameterlist *parlist, cpl_frameset *frameset)
00326 {
00327     cpl_imagelist    *data_in            = NULL,
00328                      *noise_in           = NULL;
00329 
00330     cpl_image        *mask               = NULL,
00331                      *made_data_img      = NULL;
00332 
00333     cpl_vector       *spec_data_out      = NULL,
00334                      *spec_noise_out     = NULL,
00335                      *centre             = NULL,
00336                      *fit_par            = NULL;
00337 
00338     int              ret_val             = 0,
00339                      nr_devices          = 0,
00340                      i                   = 0,
00341                      valid_ifu           = FALSE,
00342                      citer               = 0,
00343                      cmin                = 0,
00344                      cmax                = 0,
00345                      x                   = 0,
00346                      y                   = 0,
00347                      save_mask           = FALSE,
00348                      devnr1              = 0,
00349                      devnr2              = 0,
00350                      index_data          = 0,
00351                      index_noise          = 0;
00352 
00353     cpl_propertylist *sub_header_data    = NULL,
00354                      *sub_header_noise   = NULL,
00355                      *sub_header_mask    = NULL,
00356                      *fit_pl             = NULL;
00357 
00358     const char       *mask_method        = NULL,
00359                      *cmethod            = NULL,
00360                      *centre_txt         = NULL;
00361 
00362     double           cpos_rej            = 0.0,
00363                      cneg_rej            = 0.0,
00364                      radius              = 0.0,
00365                      r                   = 0.0,
00366                      x_lo                = 0.0,
00367                      y_lo                = 0.0,
00368                      x_hi                = 0.0,
00369                      y_hi                = 0.0,
00370                      cen_x               = 0.0,
00371                      cen_y               = 0.0;
00372 
00373     float            *pmask              = NULL;
00374 
00375     main_fits_desc   desc1,
00376                      desc2;
00377 
00378     cpl_frame        *op1_frame          = NULL,
00379                      *op2_frame          = NULL;
00380 
00381     char             do_mode1[256],
00382                      do_mode2[256];
00383 
00384     KMO_TRY
00385     {
00386         kmo_init_fits_desc(&desc1);
00387         kmo_init_fits_desc(&desc2);
00388 
00389         /* --- check input --- */
00390         KMO_TRY_ASSURE((parlist != NULL) &&
00391                        (frameset != NULL),
00392                        CPL_ERROR_NULL_INPUT,
00393                        "Not all input data is provided!");
00394 
00395         KMO_TRY_ASSURE((cpl_frameset_get_size(frameset) == 1) ||
00396                        ((cpl_frameset_get_size(frameset) == 2)),
00397                        CPL_ERROR_NULL_INPUT,
00398                        "Either one data cube or a datacube and a mask"
00399                        "must be provided!");
00400 
00401         if (cpl_frameset_get_size(frameset) == 1) {
00402             strcpy(do_mode1, "0");
00403             strcpy(do_mode2, "");
00404         } else {
00405             strcpy(do_mode1, "0");
00406             strcpy(do_mode2, "1");
00407             KMO_TRY_EXIT_IF_NULL(
00408                 op2_frame = kmo_dfs_get_frame(frameset, do_mode2));
00409         }
00410         KMO_TRY_EXIT_IF_NULL(
00411             op1_frame = kmo_dfs_get_frame(frameset, do_mode1));
00412 
00413         desc1 = kmo_identify_fits_header(
00414                     cpl_frame_get_filename(op1_frame));
00415         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
00416                                       "in KMOS-format!");
00417 
00418         KMO_TRY_ASSURE(desc1.fits_type == f3i_fits,
00419                CPL_ERROR_ILLEGAL_INPUT,
00420                "The input data hasn't the correct data type "
00421                "(KMOSTYPE must be F3I)!");
00422 
00423         KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset, "kmo_extract_spec") == 1,
00424                        CPL_ERROR_ILLEGAL_INPUT,
00425                        "Cannot identify RAW and CALIB frames!");
00426 
00427         cpl_msg_info("", "--- Parameter setup for kmo_extract_spec ---");
00428 
00429         KMO_TRY_EXIT_IF_NULL(
00430             mask_method = kmo_dfs_get_parameter_string(parlist,
00431                                           "kmos.kmo_extract_spec.mask_method"));
00432 
00433         KMO_TRY_EXIT_IF_ERROR(
00434             kmo_dfs_print_parameter_help(parlist, "kmos.kmo_extract_spec.mask_method"));
00435 
00436         if (strcmp(mask_method, "integrated") == 0) {
00437             KMO_TRY_EXIT_IF_NULL(
00438                 centre_txt = kmo_dfs_get_parameter_string(parlist,
00439                                             "kmos.kmo_extract_spec.centre"));
00440             KMO_TRY_EXIT_IF_ERROR(
00441                 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_extract_spec.centre"));
00442 
00443             centre = kmo_identify_ranges(centre_txt);
00444             KMO_TRY_CHECK_ERROR_STATE();
00445 
00446             KMO_TRY_ASSURE(cpl_vector_get_size(centre) == 2,
00447                            CPL_ERROR_ILLEGAL_INPUT,
00448                          "centre must have exactly 2 values like \"2.0:3.1\"!");
00449 
00450             cen_x = cpl_vector_get(centre, 0);
00451             cen_y = cpl_vector_get(centre, 1);
00452 
00453             KMO_TRY_ASSURE((cen_x >= 0.0) &&
00454                            (cen_y >= 0.0),
00455                            CPL_ERROR_ILLEGAL_INPUT,
00456                            "centre must be greater than 0.0!");
00457 
00458             radius = kmo_dfs_get_parameter_double(parlist,
00459                                             "kmos.kmo_extract_spec.radius");
00460             KMO_TRY_CHECK_ERROR_STATE();
00461             KMO_TRY_EXIT_IF_ERROR(
00462                 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_extract_spec.radius"));
00463 
00464             KMO_TRY_ASSURE(radius >= 0.0,
00465                            CPL_ERROR_ILLEGAL_INPUT,
00466                            "radius must be greater than 0.0!");
00467 
00468         } else if (strcmp(mask_method, "mask") == 0) {
00469             /* load descriptor of second operand */
00470             desc2 = kmo_identify_fits_header(
00471                         cpl_frame_get_filename(op2_frame));
00472             KMO_TRY_CHECK_ERROR_STATE_MSG("Mask doesn't seem to be "
00473                                           "in KMOS-format!");
00474 
00475             KMO_TRY_ASSURE(desc2.fits_type == f2i_fits,
00476                            CPL_ERROR_ILLEGAL_INPUT,
00477                            "Mask hasn't correct data type "
00478                            "(KMOSTYPE must be F2I)!");
00479 
00480             KMO_TRY_ASSURE(desc2.ex_noise == FALSE,
00481                            CPL_ERROR_ILLEGAL_INPUT,
00482                            "Mask must not contain noise extensions!");
00483 
00484             KMO_TRY_ASSURE(
00485                 ((desc1.ex_noise == TRUE) &&
00486                  (desc1.nr_ext / 2 == desc2.nr_ext)) ||
00487                 ((desc1.ex_noise == FALSE) &&
00488                  (desc1.nr_ext == desc2.nr_ext)),
00489                         CPL_ERROR_ILLEGAL_INPUT,
00490                         "Cube and mask haven't same number of extensions!");
00491 
00492             KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) &&
00493                            (desc1.naxis2 == desc2.naxis2),
00494                            CPL_ERROR_ILLEGAL_INPUT,
00495                            "Cube and mask haven't same dimensions in x and y!");
00496 
00497         } else if (strcmp(mask_method, "optimal") == 0) {
00498             KMO_TRY_EXIT_IF_ERROR(
00499                 kmo_combine_pars_load(parlist,
00500                                       "kmos.kmo_extract_spec",
00501                                       &cmethod,
00502                                       &cpos_rej,
00503                                       &cneg_rej,
00504                                       &citer,
00505                                       &cmin,
00506                                       &cmax,
00507                                       FALSE));
00508 
00509             save_mask = kmo_dfs_get_parameter_bool(parlist,
00510                                             "kmos.kmo_extract_spec.save_mask");
00511             KMO_TRY_CHECK_ERROR_STATE();
00512             KMO_TRY_EXIT_IF_ERROR(
00513                 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_extract_spec.save_mask"));
00514 
00515         } else {
00516             KMO_TRY_ERROR_SET_MSG(CPL_ERROR_ILLEGAL_INPUT,
00517                                          "Wrong mask method!");
00518         }
00519 
00520         cpl_msg_info(cpl_func, "-------------------------------------------");
00521 
00522         /* --- load, update & save primary header --- */
00523         KMO_TRY_EXIT_IF_ERROR(
00524             kmo_dfs_save_main_header(frameset, EXTRACT_SPEC, "", op1_frame,
00525                                      NULL, parlist, cpl_func));
00526 
00527         if (save_mask) {
00528             KMO_TRY_EXIT_IF_ERROR(
00529                 kmo_dfs_save_main_header(frameset, EXTRACT_SPEC_MASK, "", op1_frame,
00530                                          NULL, parlist, cpl_func));
00531         }
00532 
00533         /* --- load data --- */
00534         if (desc1.ex_noise == TRUE) {
00535             nr_devices = desc1.nr_ext / 2;
00536         } else {
00537             nr_devices = desc1.nr_ext;
00538         }
00539 
00540         // create mask for integrated-method just once here
00541         // and not in the for-loop
00542         if (strcmp(mask_method, "integrated") == 0) {
00543             KMO_TRY_EXIT_IF_NULL(
00544                 mask = cpl_image_new(desc1.naxis1, desc1.naxis2,
00545                                      CPL_TYPE_FLOAT));
00546 
00547             KMO_TRY_EXIT_IF_ERROR(
00548                 kmo_image_fill(mask,0.0));
00549 
00550             KMO_TRY_EXIT_IF_NULL(
00551                 pmask = cpl_image_get_data_float(mask));
00552 
00553             /* draw circle */
00554             x_lo = floor(cen_x - radius);
00555             if (x_lo < 0) {
00556                 x_lo = 0;
00557             }
00558 
00559             y_lo = floor(cen_y - radius);
00560             if (y_lo < 0) {
00561                 y_lo = 0;
00562             }
00563 
00564             x_hi = ceil(cen_x + radius);
00565             if (x_hi > desc1.naxis1) {
00566                 x_hi = desc1.naxis1;
00567             }
00568 
00569             y_hi = ceil(cen_y + radius);
00570             if (y_hi > desc1.naxis2) {
00571                 y_hi = desc1.naxis2;
00572             }
00573 
00574             for (x = x_lo; x < x_hi; x++) {
00575                 for (y = y_lo; y < y_hi; y++) {
00576                     r = sqrt(pow(x - cen_x,2) + pow(y - cen_y,2));
00577                     if (r <= radius) {
00578                         pmask[x + y * desc1.naxis1] = 1.0;
00579                     }
00580                 }
00581             }            
00582         }
00583 
00584         for (i = 1; i <= nr_devices; i++) {
00585             if (desc1.ex_noise == FALSE) {
00586                 devnr1 = desc1.sub_desc[i - 1].device_nr;
00587             } else {
00588                 devnr1 = desc1.sub_desc[2 * i - 1].device_nr;
00589             }
00590             // mask doesn't contain any noise extensions
00591             if (strcmp(mask_method, "mask") == 0) {
00592                 devnr2 = desc2.sub_desc[i - 1].device_nr;
00593             }
00594 
00595             if (desc1.ex_badpix == FALSE) {
00596                 index_data = kmo_identify_index_desc(desc1, devnr1, FALSE);
00597             } else {
00598                 index_data = kmo_identify_index_desc(desc1, devnr1, 2);
00599             }
00600             KMO_TRY_CHECK_ERROR_STATE();
00601 
00602             if (desc1.ex_noise) {
00603                 index_noise = kmo_identify_index_desc(desc1, devnr1, TRUE);
00604             }
00605             KMO_TRY_CHECK_ERROR_STATE();
00606 
00607             KMO_TRY_EXIT_IF_NULL(
00608                 sub_header_data = kmo_dfs_load_sub_header(frameset, do_mode1,
00609                                                           devnr1, FALSE));
00610 
00611             // check if IFU is valid
00612             valid_ifu = FALSE;
00613             if (desc1.sub_desc[index_data-1].valid_data == TRUE) {
00614                 if ((strcmp(mask_method, "mask") != 0) ||
00615                     ((strcmp(mask_method, "mask") == 0) &&
00616                     (desc2.sub_desc[i - 1].valid_data == TRUE))
00617                    )
00618                 {
00619                     valid_ifu = TRUE;
00620                 }
00621             }
00622 
00623             if (desc1.ex_noise) {
00624                 KMO_TRY_EXIT_IF_NULL(
00625                     sub_header_noise = kmo_dfs_load_sub_header(frameset,
00626                                                                do_mode1,
00627                                                                devnr1, TRUE));
00628             }
00629 
00630             if (valid_ifu) {
00631                 // load data
00632                 KMO_TRY_EXIT_IF_NULL(
00633                     data_in = kmo_dfs_load_cube(frameset, do_mode1, devnr1, FALSE));
00634 
00635                 // load noise, if existing
00636                 if (desc1.ex_noise && desc1.sub_desc[index_noise-1].valid_data) {
00637                     KMO_TRY_EXIT_IF_NULL(
00638                         noise_in = kmo_dfs_load_cube(frameset, do_mode1, devnr1, TRUE));
00639                 }
00640 
00641                 // create or load mask (for integrated-method already
00642                 // done outside the for-loop)
00643                 if (strcmp(mask_method, "mask") == 0) {
00644                     KMO_TRY_EXIT_IF_NULL(
00645                         mask = kmo_dfs_load_image(frameset, do_mode2,
00646                                                   devnr2, FALSE, FALSE, NULL));
00647                 } else if (strcmp(mask_method, "optimal") == 0) {
00648                     KMO_TRY_EXIT_IF_ERROR(
00649                         kmclipm_make_image(data_in, NULL,
00650                                        &made_data_img, NULL,
00651                                        NULL,
00652                                        cmethod, cpos_rej, cneg_rej, citer,
00653                                        cmax, cmin));
00654 
00655                     KMO_TRY_EXIT_IF_NULL(
00656                         fit_par = kmo_fit_profile_2D(made_data_img,
00657                                                      NULL,
00658                                                      "gauss",
00659                                                      &mask,
00660                                                      &fit_pl));
00661 
00662                     // update subheader with fit parameters
00663                     KMO_TRY_EXIT_IF_ERROR(
00664                         cpl_propertylist_append(sub_header_data, fit_pl));
00665 
00666                     cpl_propertylist_delete(fit_pl); fit_pl = NULL;
00667 
00668                     // normalise mask
00669                     // (subtract first background and then normalise)
00670                     cpl_image_subtract_scalar(mask, cpl_vector_get(fit_par, 0));
00671                     cpl_image_divide_scalar(mask, cpl_vector_get(fit_par, 1));
00672                     cpl_vector_delete(fit_par); fit_par = NULL;
00673                     cpl_image_delete(made_data_img); made_data_img = NULL;
00674                 }
00675 
00676                 // process & save data
00677                 KMO_TRY_EXIT_IF_ERROR(
00678                     kmo_priv_extract_spec(data_in,
00679                                           noise_in,
00680                                           mask,
00681                                           &spec_data_out,
00682                                           &spec_noise_out));
00683 
00684                 KMO_TRY_EXIT_IF_NULL(
00685                     sub_header_mask = cpl_propertylist_duplicate(
00686                                                               sub_header_data));
00687 
00688                 // change WCS here (CRPIX3 goes to CRPIX1 etc...)
00689                 KMO_TRY_EXIT_IF_NULL(
00690                     sub_header_data = kmo_priv_update_header(sub_header_data));
00691 
00692                 kmclipm_vector *ddd = kmclipm_vector_create(spec_data_out);
00693                 KMO_TRY_CHECK_ERROR_STATE();
00694 
00695                 KMO_TRY_EXIT_IF_ERROR(
00696                     kmo_dfs_save_vector(ddd, EXTRACT_SPEC, "",
00697                                         sub_header_data, 0./0.));
00698                 kmclipm_vector_delete(ddd); ddd = NULL; spec_data_out = NULL;
00699 
00700                 if (save_mask) {
00701                     // delete WCS for 3rd dimension since mask is 2D
00702                     KMO_TRY_EXIT_IF_ERROR(
00703                         cpl_propertylist_erase(sub_header_data, CRPIX3));
00704                     KMO_TRY_EXIT_IF_ERROR(
00705                         cpl_propertylist_erase(sub_header_data, CRVAL3));
00706                     KMO_TRY_EXIT_IF_ERROR(
00707                         cpl_propertylist_erase(sub_header_data, CDELT3));
00708                     KMO_TRY_EXIT_IF_ERROR(
00709                         cpl_propertylist_erase(sub_header_data, CTYPE3));
00710                     KMO_TRY_EXIT_IF_ERROR(
00711                         cpl_propertylist_erase(sub_header_data, CD1_3));
00712                     KMO_TRY_EXIT_IF_ERROR(
00713                         cpl_propertylist_erase(sub_header_data, CD2_3));
00714                     KMO_TRY_EXIT_IF_ERROR(
00715                         cpl_propertylist_erase(sub_header_data, CD3_3));
00716                     KMO_TRY_EXIT_IF_ERROR(
00717                         cpl_propertylist_erase(sub_header_data, CD3_1));
00718                     KMO_TRY_EXIT_IF_ERROR(
00719                         cpl_propertylist_erase(sub_header_data, CD3_2));
00720 
00721                     KMO_TRY_EXIT_IF_ERROR(
00722                         kmo_dfs_save_image(mask, EXTRACT_SPEC_MASK, "",
00723                                            sub_header_mask, 0.));
00724                 }
00725                 cpl_propertylist_delete(sub_header_mask);
00726                 sub_header_mask = NULL;
00727 
00728                 // process & save noise, if existing
00729                 if (desc1.ex_noise) {
00730                     kmclipm_vector *nnn = NULL;
00731                     if (spec_noise_out != NULL) {
00732                         nnn = kmclipm_vector_create(spec_noise_out);
00733                     }
00734                     KMO_TRY_EXIT_IF_NULL(
00735                         sub_header_noise = kmo_priv_update_header(
00736                                                              sub_header_noise));
00737 
00738                     KMO_TRY_EXIT_IF_ERROR(
00739                         kmo_dfs_save_vector(nnn, EXTRACT_SPEC, "",
00740                                             sub_header_noise, 0./0.));
00741                     kmclipm_vector_delete(nnn); nnn = NULL; spec_noise_out = NULL;
00742                 }
00743 
00744                 // free memory
00745                 cpl_imagelist_delete(data_in); data_in = NULL;
00746                 cpl_imagelist_delete(noise_in); noise_in = NULL;
00747                 KMO_TRY_CHECK_ERROR_STATE();
00748 
00749                 if (strcmp(mask_method, "integrated") != 0) {
00750                     // for integrated-method the mask will be deleted
00751                     // at the very end!
00752                     cpl_image_delete(mask); mask = NULL;
00753                 }
00754             } else {
00755                 // invalid IFU, just save sub_headers
00756                 KMO_TRY_EXIT_IF_ERROR(
00757                     kmo_dfs_save_sub_header(EXTRACT_SPEC, "", sub_header_data));
00758 
00759                 if (desc1.ex_noise) {
00760                     KMO_TRY_EXIT_IF_ERROR(
00761                         kmo_dfs_save_sub_header(EXTRACT_SPEC, "", sub_header_noise));
00762                 }
00763             }
00764 
00765             // free memory
00766             cpl_propertylist_delete(sub_header_data); sub_header_data = NULL;
00767             cpl_propertylist_delete(sub_header_noise); sub_header_noise = NULL;
00768         }
00769 
00770         if (strcmp(mask_method, "integrated") == 0) {
00771             cpl_image_delete(mask); mask = NULL;
00772             cpl_vector_delete(centre); centre = NULL;
00773         }
00774     }
00775     KMO_CATCH
00776     {
00777         KMO_CATCH_MSG();
00778 
00779         ret_val = -1;
00780     }
00781 
00782     kmo_free_fits_desc(&desc1);
00783     kmo_free_fits_desc(&desc2);
00784 
00785     cpl_propertylist_delete(sub_header_data); sub_header_data = NULL;
00786     cpl_propertylist_delete(sub_header_noise); sub_header_noise = NULL;
00787     cpl_propertylist_delete(sub_header_mask); sub_header_mask = NULL;
00788     cpl_imagelist_delete(data_in); data_in = NULL;
00789     cpl_imagelist_delete(noise_in); noise_in = NULL;
00790     cpl_vector_delete(spec_data_out); spec_data_out = NULL;
00791     cpl_vector_delete(spec_noise_out); spec_noise_out = NULL;
00792     cpl_image_delete(mask); mask = NULL;
00793     cpl_vector_delete(centre); centre = NULL;
00794 
00795     return ret_val;
00796 }
00797