KMOS Pipeline Reference Manual  1.3.11
kmo_stats.c
00001 /* 
00002  * This file is part of the KMOS Pipeline
00003  * Copyright (C) 2002,2003 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 
00024 /*-----------------------------------------------------------------------------
00025  *                              Includes
00026  *----------------------------------------------------------------------------*/
00027 
00028 #include <string.h>
00029 
00030 #include <cpl.h>
00031 
00032 #include "kmo_utils.h"
00033 #include "kmo_dfs.h"
00034 #include "kmo_priv_stats.h"
00035 #include "kmo_priv_functions.h"
00036 #include "kmo_error.h"
00037 #include "kmo_debug.h"
00038 #include "kmo_constants.h"
00039 
00040 /*-----------------------------------------------------------------------------
00041  *                          Functions prototypes
00042  *----------------------------------------------------------------------------*/
00043 
00044 static int kmo_stats_create(cpl_plugin *);
00045 static int kmo_stats_exec(cpl_plugin *);
00046 static int kmo_stats_destroy(cpl_plugin *);
00047 static int kmo_stats(cpl_parameterlist *, cpl_frameset *);
00048 
00049 /*-----------------------------------------------------------------------------
00050  *                          Static variables
00051  *----------------------------------------------------------------------------*/
00052 
00053 static char kmo_stats_description[] =
00054 "This recipe performs basic statistics on KMOS-conform data-frames of type F2D,\n"
00055 "F1I, F2I and F3I either with or without noise and RAW. Optionally a 2D mask\n"
00056 "can be provided to define a region on which the statistics should be calculated\n"
00057 "on (mask 0: exclude pixel, mask 1: include pixel). A mask can’t be provided for\n"
00058 "statistics on F1I frames.\n"
00059 "The output is stored in a vector of length 11. The vector represents following\n"
00060 "values:\n"
00061 "   1.  Number of pixels\n"
00062 "   2.  Number of finite pixels\n"
00063 "   3.  Mean\n"
00064 "   4.  Standard Deviation\n"
00065 "   5.  Mean with iterative rejection (i.e. mean & sigma are calculated iterati-\n"
00066 "       vely, each time rejecting pixels more than +/-N sigma from the mean)\n"
00067 "   6.  Standard Deviation with iterative rejection\n"
00068 "   7.  Median\n"
00069 "   8.  Mode (i.e. the peak in a histogram of pixel values)\n"
00070 "   9.  Noise (a robust estimate given by the standard deviation from the nega-\n"
00071 "       tive side of the histogram of pixel values)\n"
00072 "   10. Minimum\n"
00073 "   11. Maximum\n"
00074 "\n"
00075 "The same numerical operations are applied to the noise as with the data itself.\n"
00076 "\n"
00077 "BASIC PARAMETERS:\n"
00078 "-----------------\n"
00079 "--ext\n"
00080 "These parameters specify with extensions to process. The value 0, which is\n"
00081 "default, calulates all extensions.\n"
00082 "\n"
00083 "ADVANCED PARAMETERS\n"
00084 "-------------------\n"
00085 "--cpos_rej\n"
00086 "--cneg_rej\n"
00087 "--citer\n"
00088 "An iterative sigma clipping is applied in order to calculate the mode (using\n"
00089 "kmo_stats). For each position all pixels in the spectrum are examined. If they\n"
00090 "deviate significantly, they will be rejected according to the conditions:\n"
00091 "       val > mean + stdev * cpos_rej\n"
00092 "   and\n"
00093 "       val < mean - stdev * cneg_rej\n"
00094 "In the first iteration median and percentile level are used.\n"
00095 "\n"
00096 "-------------------------------------------------------------------------------\n"
00097 "  Input files:\n"
00098 "\n"
00099 "   DO            DO      KMOS                                                  \n"
00100 "   category      group   Type   Explanation                    Required #Frames\n"
00101 "   --------      -----   -----  -----------                    -------- -------\n"
00102 "   <none or any>   -     F3I or The datacubes                      Y       1   \n"
00103 "                         F2I or                                                \n"
00104 "                         F1I or                                                \n"
00105 "                         F2D or                                                \n"
00106 "                         B2D or                                                \n"
00107 "                         RAW                                                   \n"
00108 "   <none or any>   -     F2I or The mask                           N      0,1  \n"
00109 "                         F2D or                                                \n"
00110 "                         B2D or                                                \n"
00111 "                         RAW                                                   \n"
00112 "\n"
00113 "  Output files:\n"
00114 "\n"
00115 "   DO                    KMOS\n"
00116 "   category              Type   Explanation\n"
00117 "   --------              -----  -----------\n"
00118 "   STATS                 F1I    Calculated statistics parameters \n"
00119 "-------------------------------------------------------------------------------\n"
00120 "\n";
00121 
00122 /*-----------------------------------------------------------------------------
00123  *                              Functions code
00124  *----------------------------------------------------------------------------*/
00125 
00142 int cpl_plugin_get_info(cpl_pluginlist *list)
00143 {
00144     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00145     cpl_plugin *plugin = &recipe->interface;
00146 
00147     cpl_plugin_init(plugin,
00148                         CPL_PLUGIN_API,
00149                         KMOS_BINARY_VERSION,
00150                         CPL_PLUGIN_TYPE_RECIPE,
00151                         "kmo_stats",
00152                         "Perform basic statistics on a KMOS-conform fits-file",
00153                         kmo_stats_description,
00154                         "Alex Agudo Berbel",
00155                         "usd-help@eso.org",
00156                         kmos_get_license(),
00157                         kmo_stats_create,
00158                         kmo_stats_exec,
00159                         kmo_stats_destroy);
00160 
00161     cpl_pluginlist_append(list, plugin);
00162 
00163     return 0;
00164 }
00165 
00173 static int kmo_stats_create(cpl_plugin *plugin)
00174 {
00175     cpl_recipe *recipe;
00176     cpl_parameter *p;
00177 
00178     /* Check that the plugin is part of a valid recipe */
00179     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00180         recipe = (cpl_recipe *)plugin;
00181     else
00182         return -1;
00183 
00184     /* Create the parameters list in the cpl_recipe object */
00185     recipe->parameters = cpl_parameterlist_new();
00186 
00187     /* --ext */
00188     p = cpl_parameter_new_value("kmos.kmo_stats.ext",
00189                                 CPL_TYPE_INT,
00190                                 "The extension the stats should be calculated "
00191                                 "of. Zero is default and calculates the stats "
00192                                 "of all extensions",
00193                                 "kmos.kmo_stats",
00194                                 0);
00195     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ext");
00196     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00197     cpl_parameterlist_append(recipe->parameters, p);
00198 
00199     /* Fill the parameters list */
00200     return kmos_combine_pars_create(recipe->parameters,
00201                                    "kmos.kmo_stats",
00202                                    DEF_REJ_METHOD,
00203                                    TRUE);
00204 }
00205 
00211 static int kmo_stats_exec(cpl_plugin *plugin)
00212 {
00213     cpl_recipe  *recipe;
00214 
00215     /* Get the recipe out of the plugin */
00216     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00217         recipe = (cpl_recipe *)plugin;
00218     else return -1 ;
00219 
00220     return kmo_stats(recipe->parameters, recipe->frames);
00221 }
00222 
00228 static int kmo_stats_destroy(cpl_plugin *plugin)
00229 {
00230     cpl_recipe *recipe;
00231 
00232     /* Get the recipe out of the plugin */
00233     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00234         recipe = (cpl_recipe *)plugin;
00235     else return -1 ;
00236 
00237     cpl_parameterlist_delete(recipe->parameters);
00238     return 0 ;
00239 }
00240 
00255 static int kmo_stats(cpl_parameterlist *parlist, cpl_frameset *frameset)
00256 {
00257     double           cpos_rej            = 0.0,
00258                      cneg_rej            = 0.0;
00259 
00260     cpl_imagelist    *data_in            = NULL;
00261 
00262     cpl_image        *mask               = NULL,
00263                      *img_in             = NULL;
00264 
00265     kmclipm_vector   *vec_in             = NULL,
00266                      *mask_vec           = NULL,
00267                      *data_out           = NULL;
00268 
00269     int              ret_val             = 0,
00270                      nr_devices          = 0,
00271                      i                   = 0,
00272                      j                   = 0,
00273                      tmpi                = 0,
00274                      valid_ifu           = FALSE,
00275                      citer               = 0,
00276                      mask_available      = FALSE,
00277                      ifu                 = 0,
00278                      stats_size          = 0,
00279                      extnr               = 0,
00280                      devnr               = 0,
00281                      index_data          = 0,
00282                      index_noise         = 0;
00283 
00284     cpl_propertylist *sub_header         = NULL;
00285 
00286     main_fits_desc   desc1,
00287                      desc2;
00288 
00289     cpl_frame        *data_frame         = NULL,
00290                      *mask_frame         = NULL;
00291 
00292     char             do_mode1[256],
00293                      do_mode2[256];
00294 
00295     const char       *cmethod            = "ksigma";
00296 
00297     char             *tmp_str            = NULL,
00298                      **strarr            = NULL;
00299 
00300     KMO_TRY
00301     {
00302         kmo_init_fits_desc(&desc1);
00303 
00304         // --- check inputs ---
00305         KMO_TRY_ASSURE((parlist != NULL) &&
00306                        (frameset != NULL),
00307                        CPL_ERROR_NULL_INPUT,
00308                        "Not all input data is provided!");
00309 
00310         KMO_TRY_ASSURE((cpl_frameset_get_size(frameset) == 1) ||
00311                        ((cpl_frameset_get_size(frameset) == 2)),
00312                        CPL_ERROR_NULL_INPUT,
00313                        "Either a cube (F3I) or a cube and a mask (F2I) "
00314                        "must be provided!");
00315 
00316         KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset, "kmo_stats") == 1,
00317                        CPL_ERROR_ILLEGAL_INPUT,
00318                        "Cannot identify RAW and CALIB frames!");
00319 
00320         if (cpl_frameset_get_size(frameset) == 1) {
00321             strcpy(do_mode1, "0");
00322             strcpy(do_mode2, "");
00323         } else {
00324             strcpy(do_mode1, "0");
00325             strcpy(do_mode2, "1");
00326             KMO_TRY_EXIT_IF_NULL(
00327                 mask_frame = kmo_dfs_get_frame(frameset, do_mode2));
00328         }
00329         KMO_TRY_EXIT_IF_NULL(
00330             data_frame = kmo_dfs_get_frame(frameset, do_mode1));
00331 
00332         cpl_msg_info("", "--- Parameter setup for kmo_stats ---------");
00333 
00334         // load descriptor of first operand
00335         desc1 = kmo_identify_fits_header(
00336                     cpl_frame_get_filename(data_frame));
00337         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
00338                                       "in KMOS-format!");
00339 
00340         KMO_TRY_ASSURE((desc1.fits_type == f3i_fits) ||
00341                        (desc1.fits_type == f1i_fits) ||
00342                        (desc1.fits_type == f2i_fits) ||
00343                        (desc1.fits_type == f2d_fits) ||
00344                        (desc1.fits_type == b2d_fits) ||
00345                        (desc1.fits_type == raw_fits),
00346                        CPL_ERROR_ILLEGAL_INPUT,
00347                        "First input file hasn't correct data type "
00348                        "(KMOSTYPE must be F1I, F2I, F3I, F2D, B2D or RAW)!");
00349 
00350         ifu = kmo_dfs_get_parameter_int(parlist,
00351                                             "kmos.kmo_stats.ext");
00352          KMO_TRY_EXIT_IF_ERROR(
00353              kmo_dfs_print_parameter_help(parlist, "kmos.kmo_stats.ext"));
00354 
00355          KMO_TRY_ASSURE((desc1.nr_ext >= ifu) ||
00356                         (ifu == 0),
00357                         CPL_ERROR_ILLEGAL_INPUT,
00358                         "ext must be smaller or equal to the number of "
00359                         "extensions!");
00360 
00361         KMO_TRY_EXIT_IF_ERROR(
00362             kmos_combine_pars_load(parlist,
00363                                   "kmos.kmo_stats",
00364                                   &cmethod,
00365                                   &cpos_rej,
00366                                   &cneg_rej,
00367                                   &citer,
00368                                   NULL,
00369                                   NULL,
00370                                   TRUE));
00371 
00372 
00373         cpl_msg_info("", "-------------------------------------------");
00374 
00375         // check the (optional) mask
00376         if (cpl_frameset_get_size(frameset) == 2) {
00377             kmo_init_fits_desc(&desc2);
00378 
00379             // load descriptor of second operand
00380             desc2 = kmo_identify_fits_header(
00381                         cpl_frame_get_filename(mask_frame));
00382             KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem "
00383                                           "to be in KMOS-format!");
00384 
00385             mask_available = TRUE;
00386             if ((desc1.fits_type == f3i_fits) ||
00387                 (desc1.fits_type == f2i_fits))
00388             {
00389                 KMO_TRY_ASSURE(desc2.fits_type == f2i_fits,
00390                             CPL_ERROR_ILLEGAL_INPUT,
00391                             "Mask hasn't correct data type "
00392                             "(KMOSTYPE must be F2I)!");
00393                 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) &&
00394                                (desc1.naxis2 == desc2.naxis2),
00395                                CPL_ERROR_ILLEGAL_INPUT,
00396                                "STAT_DATA and STAT_MASK don't have the same "
00397                                "dimensions!");
00398             } else if ((desc1.fits_type == f2d_fits) ||
00399                        (desc1.fits_type == b2d_fits) ||
00400                        (desc1.fits_type == raw_fits))
00401             {
00402                 KMO_TRY_ASSURE((desc2.fits_type == f2d_fits) ||
00403                                (desc2.fits_type == b2d_fits),
00404                             CPL_ERROR_ILLEGAL_INPUT,
00405                             "Mask file hasn't correct data type "
00406                             "(KMOSTYPE must be F2D or B2D)!");
00407                 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) &&
00408                                (desc1.naxis2 == desc2.naxis2),
00409                                CPL_ERROR_ILLEGAL_INPUT,
00410                                "STAT_DATA and STAT_MASK don't have the same "
00411                                "dimensions!");
00412             } else if (desc1.fits_type == f1i_fits)
00413             {
00414                 KMO_TRY_ASSURE(1==0,
00415                             CPL_ERROR_ILLEGAL_INPUT,
00416                             "Mask can't be provided for F1I frames!");
00417             } else {
00418                 mask_available = FALSE;
00419             }
00420 
00421             KMO_TRY_ASSURE((desc1.nr_ext == desc2.nr_ext) ||
00422                            (desc1.nr_ext/2 == desc2.nr_ext) ||
00423                            ((desc2.nr_ext == 1) && (ifu > 0) &&
00424                                                          (ifu <= desc1.nr_ext)),
00425                         CPL_ERROR_ILLEGAL_INPUT,
00426                         "Mask hasn't same number of extensions as data!");
00427         }
00428 
00429         if (desc1.ex_noise == TRUE) {
00430             nr_devices = desc1.nr_ext / 2;
00431         } else {
00432             nr_devices = desc1.nr_ext;
00433         }
00434 
00435         if (ifu != 0) {
00436             if (((desc1.ex_noise == FALSE) &&
00437                  (desc1.sub_desc[ifu-1].valid_data == FALSE)) ||
00438                 ((desc1.ex_noise == TRUE) &&
00439                  (desc1.sub_desc[2 * (ifu-1) - 1].valid_data == FALSE)))
00440             {
00441                 cpl_msg_info(cpl_func, "No valid IFU or detector has been "
00442                                        "selected!");
00443                 kmo_free_fits_desc(&desc1);
00444                 kmo_free_fits_desc(&desc2);
00445                 return 0;
00446             }
00447         }
00448 
00449         // --- load, update & save primary header ---
00450         KMO_TRY_EXIT_IF_ERROR(
00451             kmo_dfs_save_main_header(frameset, STATS, "", data_frame,
00452                                      NULL, parlist, cpl_func));
00453         //
00454         // load & process data
00455         //
00456         for (i = 1; i <= nr_devices; i++) {
00457             if (desc1.ex_noise == FALSE) {
00458                 devnr = desc1.sub_desc[i - 1].device_nr;
00459             } else {
00460                 devnr = desc1.sub_desc[2 * i - 1].device_nr;
00461             }
00462 
00463             if (desc1.ex_badpix == FALSE) {
00464                 index_data = kmo_identify_index_desc(desc1, devnr, FALSE);
00465             } else {
00466                 index_data = kmo_identify_index_desc(desc1, devnr, 2);
00467             }
00468             KMO_TRY_CHECK_ERROR_STATE();
00469 
00470             if (desc1.ex_noise) {
00471                 index_noise = kmo_identify_index_desc(desc1, devnr, TRUE);
00472             }
00473             KMO_TRY_CHECK_ERROR_STATE();
00474 
00475             if (i > 1) {
00476                 kmclipm_omit_warning_one_slice = TRUE;
00477             }
00478             if ((ifu == 0) || (ifu == i)) {
00479                 // check if IFU is valid
00480                 valid_ifu = FALSE;
00481                 if (desc1.sub_desc[index_data-1].valid_data == TRUE) {
00482                     valid_ifu = TRUE;
00483                 }
00484 
00485                 if (valid_ifu) {
00486                     // load data
00487 //                    if ((desc1.fits_type == f3i_fits) ||
00488 //                        (desc1.fits_type == f2i_fits) ||
00489 //                        (desc1.fits_type == f2d_fits) ||
00490 //                        (desc1.fits_type == raw_fits)) {
00491                     // load mask, if available
00492                     if (mask_available == TRUE) {
00493                         tmpi = i;
00494                         if (desc2.nr_ext == 1) {
00495                             tmpi = 1;
00496                         }
00497                         if (desc2.naxis == 1) {
00498                             KMO_TRY_EXIT_IF_NULL(
00499                                 mask_vec = kmo_dfs_load_vector(frameset,
00500                                                                do_mode2,
00501                                                                tmpi,
00502                                                                FALSE));
00503                         } else if (desc2.naxis == 2) {
00504                             if (desc2.fits_type == b2d_fits) {
00505                                 KMO_TRY_EXIT_IF_NULL(
00506                                     mask = kmo_dfs_load_image(frameset,
00507                                                               do_mode2,
00508                                                               tmpi,
00509                                                               2, FALSE, NULL));
00510                             } else {
00511                                 KMO_TRY_EXIT_IF_NULL(
00512                                     mask = kmo_dfs_load_image(frameset,
00513                                                               do_mode2,
00514                                                               tmpi,
00515                                                               FALSE, FALSE, NULL));
00516                             }
00517                         } else {
00518                             KMO_TRY_ASSURE(1 == 0,
00519                                            CPL_ERROR_ILLEGAL_INPUT,
00520                                            "STAT_MASK must either be a "
00521                                            "vector or an image!");
00522                         }
00523                     }
00524 //                    }
00525 
00526                     if (desc1.fits_type == f3i_fits) {
00527                         KMO_TRY_EXIT_IF_NULL(
00528                             data_in = kmo_dfs_load_cube(frameset, do_mode1,
00529                                                         devnr, FALSE));
00530                         KMO_TRY_EXIT_IF_NULL(
00531                             data_out = kmo_calc_stats_cube(data_in,
00532                                                            mask,
00533                                                            cpos_rej,
00534                                                            cneg_rej,
00535                                                            citer));
00536                         cpl_imagelist_delete(data_in); data_in = NULL;
00537                     } else if (desc1.fits_type == f1i_fits) {
00538                         KMO_TRY_EXIT_IF_NULL(
00539                             vec_in = kmo_dfs_load_vector(frameset, do_mode1,
00540                                                          devnr, FALSE));
00541                         KMO_TRY_EXIT_IF_NULL(
00542                             data_out = kmo_calc_stats_vec(vec_in,
00543                                                           mask_vec,
00544                                                           cpos_rej,
00545                                                           cneg_rej,
00546                                                           citer));
00547                         kmclipm_vector_delete(vec_in); vec_in = NULL;
00548                     } else if ((desc1.fits_type == f2i_fits) ||
00549                                (desc1.fits_type == f2d_fits) ||
00550                                (desc1.fits_type == b2d_fits) ||
00551                                (desc1.fits_type == raw_fits))
00552                     {
00553                         int sat_mode = FALSE;
00554                         if (desc1.fits_type == raw_fits) {
00555                             sat_mode = TRUE;
00556                         }
00557 
00558                         if (desc1.fits_type == b2d_fits) {
00559                             KMO_TRY_EXIT_IF_NULL(
00560                                 img_in = kmo_dfs_load_image(frameset, do_mode1,
00561                                                             devnr, 2, sat_mode, NULL));
00562                         } else {
00563                             KMO_TRY_EXIT_IF_NULL(
00564                                 img_in = kmo_dfs_load_image(frameset, do_mode1,
00565                                                             devnr,
00566                                                             FALSE, sat_mode, NULL));
00567                         }
00568                         KMO_TRY_EXIT_IF_NULL(
00569                             data_out = kmo_calc_stats_img(img_in,
00570                                                           mask,
00571                                                           cpos_rej,
00572                                                           cneg_rej,
00573                                                           citer));
00574                         cpl_image_delete(img_in); img_in = NULL;
00575                     } else {
00576                         KMO_TRY_ASSURE(1==0,
00577                                        CPL_ERROR_ILLEGAL_INPUT,
00578                                        "Unsupported fits_type!");
00579                     }
00580 
00581                     stats_size = kmclipm_vector_get_size(data_out);
00582 
00583                     if ((desc1.fits_type == f3i_fits) ||
00584                         (desc1.fits_type == f2i_fits) ||
00585                         (desc1.fits_type == f2d_fits) ||
00586                         (desc1.fits_type == b2d_fits) ||
00587                         (desc1.fits_type == raw_fits))
00588                     {
00589                         if (mask_available == TRUE) {
00590                             cpl_image_delete(mask); mask = NULL;
00591                         }
00592                     }
00593 
00594                     // save data
00595                     if (desc1.fits_type == b2d_fits) {
00596                         KMO_TRY_EXIT_IF_NULL(
00597                             sub_header = kmo_dfs_load_sub_header(frameset, do_mode1, devnr,
00598                                                                  2));
00599                     } else {
00600                         KMO_TRY_EXIT_IF_NULL(
00601                             sub_header = kmo_dfs_load_sub_header(frameset, do_mode1, devnr,
00602                                                                  FALSE));
00603                     }
00604 
00605                     if ((desc1.fits_type == raw_fits) ||
00606                         (desc1.fits_type == b2d_fits))
00607                     {
00608                         KMO_TRY_EXIT_IF_NULL(
00609                             tmp_str = cpl_sprintf("%s%d%s", "DET.",
00610                                                   devnr,
00611                                                   ".DATA"));
00612                         KMO_TRY_EXIT_IF_ERROR(
00613                             kmclipm_update_property_string(sub_header,
00614                                                     EXTNAME,
00615                                                     tmp_str,
00616                                                     "FITS extension name"));
00617                         cpl_free(tmp_str); tmp_str = NULL;
00618                     }
00619 
00620                     KMO_TRY_EXIT_IF_ERROR(
00621                         kmo_dfs_save_vector(data_out, STATS, "", sub_header, 0./0.));
00622 
00623                     cpl_propertylist_delete(sub_header); sub_header = NULL;
00624                     kmclipm_vector_delete(data_out); data_out = NULL;
00625 
00626                     //
00627                     // do exactly the same thing for noise, if existing
00628                     //
00629                     if (desc1.ex_noise) {
00630                         if (desc1.sub_desc[index_noise-1].valid_data) {
00631                             if (desc1.fits_type == f3i_fits) {
00632                                 KMO_TRY_EXIT_IF_NULL(
00633                                     data_in = kmo_dfs_load_cube(frameset, do_mode1,
00634                                                                 devnr, TRUE));
00635 
00636                                 KMO_TRY_EXIT_IF_NULL(
00637                                     data_out = kmo_calc_stats_cube(data_in,
00638                                                                    mask,
00639                                                                    cpos_rej,
00640                                                                    cneg_rej,
00641                                                                    citer));
00642                                 cpl_imagelist_delete(data_in); data_in = NULL;
00643                             } else if (desc1.fits_type == f1i_fits) {
00644                                 KMO_TRY_EXIT_IF_NULL(
00645                                     vec_in = kmo_dfs_load_vector(frameset, do_mode1,
00646                                                                  devnr, TRUE));
00647                                 KMO_TRY_EXIT_IF_NULL(
00648                                     data_out = kmo_calc_stats_vec(vec_in,
00649                                                                   mask_vec,
00650                                                                   cpos_rej,
00651                                                                   cneg_rej,
00652                                                                   citer));
00653                                 kmclipm_vector_delete(vec_in); vec_in = NULL;
00654                             } else if ((desc1.fits_type == f2i_fits) ||
00655                                        (desc1.fits_type == f2d_fits) ||
00656                                        (desc1.fits_type == raw_fits))
00657                             {
00658                                 KMO_TRY_EXIT_IF_NULL(
00659                                     img_in = kmo_dfs_load_image(frameset, do_mode1,
00660                                                                 devnr, TRUE, FALSE, NULL));
00661 
00662                                 KMO_TRY_EXIT_IF_NULL(
00663                                     data_out = kmo_calc_stats_img(img_in,
00664                                                                   mask,
00665                                                                   cpos_rej,
00666                                                                   cneg_rej,
00667                                                                   citer));
00668                                 cpl_image_delete(img_in); img_in = NULL;
00669                             }
00670 
00671                             if ((desc1.fits_type == f3i_fits) ||
00672                                 (desc1.fits_type == f2i_fits) ||
00673                                 (desc1.fits_type == f2d_fits) ||
00674                                 (desc1.fits_type == raw_fits))
00675                             {
00676                                 if (mask_available == TRUE) {
00677                                     cpl_image_delete(mask); mask = NULL;
00678                                     kmclipm_vector_delete(mask_vec); mask_vec = NULL;
00679                                 }
00680                             }
00681 
00682                             // save noise
00683                             KMO_TRY_EXIT_IF_NULL(
00684                                 sub_header = kmo_dfs_load_sub_header(frameset,
00685                                                                      do_mode1,
00686                                                                      devnr, TRUE));
00687 
00688                             if ((desc1.fits_type == raw_fits) ||
00689                                 (desc1.fits_type == b2d_fits))
00690                             {
00691                                 KMO_TRY_EXIT_IF_NULL(
00692                                     tmp_str = cpl_sprintf("%s%d%s", "DET.",
00693                                                           devnr,
00694                                                           ".NOISE"));
00695                                 KMO_TRY_EXIT_IF_ERROR(
00696                                     kmclipm_update_property_string(sub_header,
00697                                                             EXTNAME,
00698                                                             tmp_str,
00699                                                             "FITS extension name"));
00700                                 cpl_free(tmp_str); tmp_str = NULL;
00701                             }
00702 
00703                             KMO_TRY_EXIT_IF_ERROR(
00704                                 kmo_dfs_save_vector(data_out, STATS, "",
00705                                                     sub_header, 0./0.));
00706 
00707                             cpl_propertylist_delete(sub_header);sub_header = NULL;
00708                             kmclipm_vector_delete(data_out); data_out = NULL;
00709                         }
00710                     }
00711                 } else {
00712                     // invalid IFU, just save sub_headers
00713                     KMO_TRY_EXIT_IF_NULL(
00714                         sub_header = kmo_dfs_load_sub_header(frameset,
00715                                                              do_mode1,
00716                                                              devnr, FALSE));
00717 
00718                     if ((desc1.fits_type == raw_fits) ||
00719                         (desc1.fits_type == b2d_fits))
00720                     {
00721                         KMO_TRY_EXIT_IF_NULL(
00722                             tmp_str = cpl_sprintf("%s%d%s", "DET.",
00723                                                   devnr,
00724                                                   ".DATA"));
00725                         KMO_TRY_EXIT_IF_ERROR(
00726                             kmclipm_update_property_string(sub_header,
00727                                                     EXTNAME,
00728                                                     tmp_str,
00729                                                     "FITS extension name"));
00730                         cpl_free(tmp_str); tmp_str = NULL;
00731                     }
00732 
00733                      KMO_TRY_EXIT_IF_ERROR(
00734                          kmo_dfs_save_sub_header(STATS, "", sub_header));
00735                      cpl_propertylist_delete(sub_header); sub_header = NULL;
00736 
00737                     if (desc1.ex_noise) {
00738                         cpl_propertylist_delete(sub_header); sub_header = NULL;
00739 
00740                         KMO_TRY_EXIT_IF_NULL(
00741                             sub_header = kmo_dfs_load_sub_header(frameset,
00742                                                                  do_mode1,
00743                                                                  i, TRUE));
00744 
00745                         if ((desc1.fits_type == raw_fits) ||
00746                             (desc1.fits_type == b2d_fits))
00747                         {
00748                             KMO_TRY_EXIT_IF_NULL(
00749                                 tmp_str = cpl_sprintf("%s%d%s", "DET.",
00750                                                       devnr,
00751                                                       ".NOISE"));
00752                             KMO_TRY_EXIT_IF_ERROR(
00753                                 kmclipm_update_property_string(sub_header,
00754                                                         EXTNAME,
00755                                                         tmp_str,
00756                                                         "FITS extension name"));
00757                             cpl_free(tmp_str); tmp_str = NULL;
00758                         }
00759 
00760                         KMO_TRY_EXIT_IF_ERROR(
00761                             kmo_dfs_save_sub_header(STATS, "", sub_header));
00762                         cpl_propertylist_delete(sub_header); sub_header = NULL;
00763                     }
00764                 }
00765             }
00766         }
00767 
00768         // print stats info to console
00769         kmo_free_fits_desc(&desc1);
00770         kmo_init_fits_desc(&desc1);
00771 
00772         KMO_TRY_EXIT_IF_NULL(
00773             data_frame = kmo_dfs_get_frame(frameset, STATS));
00774 
00775         desc1 = kmo_identify_fits_header(
00776                     cpl_frame_get_filename(data_frame));
00777 
00778         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
00779                                       "in KMOS-format!");
00780 
00781         cpl_msg_info("", "-------------------------------------------");
00782         cpl_msg_info("", "--- kmo_stats info                      ---");
00783         cpl_msg_info("", "-------------------------------------------");
00784 
00785         KMO_TRY_EXIT_IF_NULL(
00786             strarr = (char**)cpl_malloc((stats_size+1) * sizeof(char*)));
00787 
00788         for (i = 0; i < stats_size+1; i++) {
00789             KMO_TRY_EXIT_IF_NULL(
00790                 strarr[i] = (char*)cpl_malloc(1024 * sizeof(char)));
00791             switch (i) {
00792             case 0: strcpy(strarr[i], "                  |"); break;
00793             case 1: strcpy(strarr[i], "1.  #pixels:      | "); break;
00794             case 2: strcpy(strarr[i], "2.  #finite pix.: | "); break;
00795             case 3: strcpy(strarr[i], "3.  mean:         | "); break;
00796             case 4: strcpy(strarr[i], "4.  stdev:        | "); break;
00797             case 5: strcpy(strarr[i], "5.  mean w. rej.: | "); break;
00798             case 6: strcpy(strarr[i], "6.  stdev w. rej.:| "); break;
00799             case 7: strcpy(strarr[i], "7.  median:       | "); break;
00800             case 8: strcpy(strarr[i], "8.  mode:         | "); break;
00801             case 9: strcpy(strarr[i], "9.  noise est.:   | "); break;
00802             case 10: strcpy(strarr[i], "10. min. value:   | "); break;
00803             case 11: strcpy(strarr[i], "11. max. value:   | "); break;
00804             default: cpl_msg_error(cpl_func, "To much values in output "
00805                                              "statistic vector!"); break;
00806             }
00807         }
00808 
00809         if (desc1.ex_noise == TRUE) {
00810             nr_devices = desc1.nr_ext / 2;
00811         } else {
00812             nr_devices = desc1.nr_ext;
00813         }
00814 
00815         for (j = 1; j <= nr_devices; j++) {
00816             if (desc1.ex_noise == FALSE) {
00817                 devnr = desc1.sub_desc[j - 1].device_nr;
00818             } else {
00819                 devnr = desc1.sub_desc[2 * j - 1].device_nr;
00820             }
00821 
00822             if (desc1.ex_badpix == FALSE) {
00823                 index_data = kmo_identify_index_desc(desc1, devnr, FALSE);
00824             } else {
00825                 index_data = kmo_identify_index_desc(desc1, devnr, 2);
00826             }
00827             KMO_TRY_CHECK_ERROR_STATE();
00828 
00829             if (desc1.ex_noise) {
00830                 index_noise = kmo_identify_index_desc(desc1, devnr, TRUE);
00831             }
00832             KMO_TRY_CHECK_ERROR_STATE();
00833 
00834             // check if IFU is valid
00835             valid_ifu = FALSE;
00836             if (desc1.sub_desc[index_data-1].valid_data == TRUE) {
00837                 valid_ifu = TRUE;
00838             }
00839 
00840             if (valid_ifu) {
00841                 if (ifu != 0) {
00842                     extnr = ifu;
00843                 } else {
00844                     extnr = devnr;
00845                 }
00846 
00847                 KMO_TRY_EXIT_IF_NULL(
00848                     sub_header = kmo_dfs_load_sub_header(frameset, STATS, extnr,
00849                                                          FALSE));
00850 
00851                 strcat(strarr[0],
00852                        cpl_propertylist_get_string(sub_header, EXTNAME));
00853                 strcat(strarr[0], "|");
00854                 cpl_propertylist_delete(sub_header); sub_header = NULL;
00855 
00856                 KMO_TRY_EXIT_IF_NULL(
00857                     data_out = kmo_dfs_load_vector(frameset, STATS, extnr, FALSE));
00858 
00859                 for (i = 1; i < stats_size+1; i++) {
00860                     double val = 0.;
00861                     int rejected = 0;
00862                     val = kmclipm_vector_get(data_out, i-1, &rejected);
00863                     if (!rejected) {
00864                         KMO_TRY_EXIT_IF_NULL(
00865                             tmp_str = cpl_sprintf("%8.7g |", val));
00866                     } else {
00867                         KMO_TRY_EXIT_IF_NULL(
00868                             tmp_str = cpl_sprintf("    -     |"));
00869                     }
00870 
00871                     strcat(strarr[i], tmp_str);
00872                     cpl_free(tmp_str); tmp_str = NULL;
00873                 }
00874                 kmclipm_vector_delete(data_out); data_out = NULL;
00875 
00876                 if (desc1.ex_noise == TRUE) {
00877                     KMO_TRY_EXIT_IF_NULL(
00878                         sub_header = kmo_dfs_load_sub_header(frameset, STATS,
00879                                                              extnr, TRUE));
00880                     strcat(strarr[0],
00881                            cpl_propertylist_get_string(sub_header, EXTNAME));
00882                     strcat(strarr[0], "|");
00883                     cpl_propertylist_delete(sub_header); sub_header = NULL;
00884 
00885                     KMO_TRY_EXIT_IF_NULL(
00886                         data_out = kmo_dfs_load_vector(frameset, STATS, extnr,
00887                                                        TRUE));
00888 
00889                     for (i = 1; i < stats_size+1; i++) {
00890                         double val = 0.;
00891                         int rejected = 0;
00892                         val = kmclipm_vector_get(data_out, i-1, &rejected);
00893                         if (!rejected) {
00894                             KMO_TRY_EXIT_IF_NULL(
00895                                 tmp_str = cpl_sprintf("%9.7g | ", val));
00896                         } else {
00897                             KMO_TRY_EXIT_IF_NULL(
00898                                 tmp_str = cpl_sprintf("    -     |"));
00899                         }
00900 
00901                         strcat(strarr[i], tmp_str);
00902                         cpl_free(tmp_str); tmp_str = NULL;
00903                     }
00904                     kmclipm_vector_delete(data_out); data_out = NULL;
00905                 }
00906             }
00907         }
00908 
00909         for (i = 0; i < stats_size+1; i++) {
00910             cpl_msg_info("", "%s", strarr[i]);
00911             cpl_free(strarr[i]); strarr[i] = NULL;
00912         }
00913         cpl_free(strarr); strarr = NULL;
00914         cpl_msg_info("", "-------------------------------------------");
00915     }
00916     KMO_CATCH
00917     {
00918         KMO_CATCH_MSG();
00919 
00920         ret_val = -1;
00921     }
00922 
00923     kmo_free_fits_desc(&desc1);
00924     if (mask_available == TRUE) {
00925         kmo_free_fits_desc(&desc2);
00926     }
00927     cpl_propertylist_delete(sub_header); sub_header = NULL;
00928     cpl_imagelist_delete(data_in); data_in = NULL;
00929     kmclipm_vector_delete(data_out); data_out = NULL;
00930     cpl_image_delete(mask); mask = NULL;
00931     kmclipm_vector_delete(mask_vec); mask_vec = NULL;
00932 
00933     return ret_val;
00934 }
00935