KMOS Pipeline Reference Manual  1.0.8
kmo_stats.c
00001 /* $Id: kmo_stats.c,v 1.13 2013/01/09 13:54: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/01/09 13:54:20 $
00024  * $Revision: 1.13 $
00025  * $Name: kmosp_v1_0_8__20130220 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033  *                              Includes
00034  *----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 
00038 #include <cpl.h>
00039 
00040 #include "kmo_utils.h"
00041 #include "kmo_dfs.h"
00042 #include "kmo_priv_stats.h"
00043 #include "kmo_priv_functions.h"
00044 #include "kmo_error.h"
00045 #include "kmo_debug.h"
00046 #include "kmo_constants.h"
00047 
00048 /*-----------------------------------------------------------------------------
00049  *                          Functions prototypes
00050  *----------------------------------------------------------------------------*/
00051 
00052 static int kmo_stats_create(cpl_plugin *);
00053 static int kmo_stats_exec(cpl_plugin *);
00054 static int kmo_stats_destroy(cpl_plugin *);
00055 static int kmo_stats(cpl_parameterlist *, cpl_frameset *);
00056 
00057 /*-----------------------------------------------------------------------------
00058  *                          Static variables
00059  *----------------------------------------------------------------------------*/
00060 
00061 static char kmo_stats_description[] =
00062 "This recipe performs basic statistics on KMOS-conform data-frames of type F2D,\n"
00063 "F1I, F2I and F3I either with or without noise and RAW. Optionally a 2D mask\n"
00064 "can be provided to define a region on which the statistics should be calculated\n"
00065 "on (mask 0: exclude pixel, mask 1: include pixel). A mask can’t be provided for\n"
00066 "statistics on F1I frames.\n"
00067 "The output is stored in a vector of length 11. The vector represents following\n"
00068 "values:\n"
00069 "   1.  Number of pixels\n"
00070 "   2.  Number of finite pixels\n"
00071 "   3.  Mean\n"
00072 "   4.  Standard Deviation\n"
00073 "   5.  Mean with iterative rejection (i.e. mean & sigma are calculated iterati-\n"
00074 "       vely, each time rejecting pixels more than +/-N sigma from the mean)\n"
00075 "   6.  Standard Deviation with iterative rejection\n"
00076 "   7.  Median\n"
00077 "   8.  Mode (i.e. the peak in a histogram of pixel values)\n"
00078 "   9.  Noise (a robust estimate given by the standard deviation from the nega-\n"
00079 "       tive side of the histogram of pixel values)\n"
00080 "   10. Minimum\n"
00081 "   11. Maximum\n"
00082 "\n"
00083 "The same numerical operations are applied to the noise as with the data itself.\n"
00084 "\n"
00085 "BASIC PARAMETERS:\n"
00086 "-----------------\n"
00087 "--ext\n"
00088 "These parameters specify with extensions to process. The value 0, which is\n"
00089 "default, calulates all extensions.\n"
00090 "\n"
00091 "ADVANCED PARAMETERS\n"
00092 "-------------------\n"
00093 "--cpos_rej\n"
00094 "--cneg_rej\n"
00095 "--citer\n"
00096 "An iterative sigma clipping is applied in order to calculate the mode (using\n"
00097 "kmo_stats). For each position all pixels in the spectrum are examined. If they\n"
00098 "deviate significantly, they will be rejected according to the conditions:\n"
00099 "       val > mean + stdev * cpos_rej\n"
00100 "   and\n"
00101 "       val < mean - stdev * cneg_rej\n"
00102 "In the first iteration median and percentile level are used.\n"
00103 "\n"
00104 "-------------------------------------------------------------------------------\n"
00105 "  Input files:\n"
00106 "\n"
00107 "   DO            DO      KMOS                                                  \n"
00108 "   category      group   Type   Explanation                    Required #Frames\n"
00109 "   --------      -----   -----  -----------                    -------- -------\n"
00110 "   STATS_DATA      -     F3I    The datacubes                      Y       1   \n"
00111 "   STATS_MASK      -     F2I    The mask                           N      0,1  \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                         "agudo@mpe.mpg.de",
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 kmo_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             KMO_TRY_ASSURE((cpl_frameset_count_tags(frameset, STATS_DATA) == 1) ||
00322                            (cpl_frameset_count_tags(frameset, COMMANDLINE) == 1),
00323                            CPL_ERROR_ILLEGAL_INPUT,
00324                            "The cube must be tagged as STATS_DATA!");
00325 
00326             if (cpl_frameset_count_tags(frameset, STATS_DATA) == 1) {
00327                 strcpy(do_mode1, STATS_DATA);
00328                 strcpy(do_mode2, "");
00329             } else {
00330                 strcpy(do_mode1, "0");
00331                 strcpy(do_mode2, "");
00332             }
00333         } else {
00334             if ((cpl_frameset_count_tags(frameset, STATS_DATA) == 1) &&
00335                 (cpl_frameset_count_tags(frameset, STATS_MASK) == 1)){
00336                 strcpy(do_mode1, STATS_DATA);
00337                 strcpy(do_mode2, STATS_MASK);
00338             } else {
00339                 strcpy(do_mode1, "0");
00340                 strcpy(do_mode2, "1");
00341             }
00342             KMO_TRY_EXIT_IF_NULL(
00343                 mask_frame = kmo_dfs_get_frame(frameset, do_mode2));
00344         }
00345         KMO_TRY_EXIT_IF_NULL(
00346             data_frame = kmo_dfs_get_frame(frameset, do_mode1));
00347 
00348         cpl_msg_info("", "--- Parameter setup for kmo_stats ---------");
00349 
00350         // load descriptor of first operand
00351         desc1 = kmo_identify_fits_header(
00352                     cpl_frame_get_filename(data_frame));
00353         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
00354                                       "in KMOS-format!");
00355 
00356         KMO_TRY_ASSURE((desc1.fits_type == f3i_fits) ||
00357                        (desc1.fits_type == f1i_fits) ||
00358                        (desc1.fits_type == f2i_fits) ||
00359                        (desc1.fits_type == f2d_fits) ||
00360                        (desc1.fits_type == b2d_fits) ||
00361                        (desc1.fits_type == raw_fits),
00362                        CPL_ERROR_ILLEGAL_INPUT,
00363                        "First input file hasn't correct data type "
00364                        "(KMOSTYPE must be F1I, F2I, F3I, F2D, B2D or RAW)!");
00365 
00366         ifu = kmo_dfs_get_parameter_int(parlist,
00367                                             "kmos.kmo_stats.ext");
00368          KMO_TRY_EXIT_IF_ERROR(
00369              kmo_dfs_print_parameter_help(parlist, "kmos.kmo_stats.ext"));
00370 
00371          KMO_TRY_ASSURE((desc1.nr_ext >= ifu) ||
00372                         (ifu == 0),
00373                         CPL_ERROR_ILLEGAL_INPUT,
00374                         "ext must be smaller or equal to the number of "
00375                         "extensions!");
00376 
00377         KMO_TRY_EXIT_IF_ERROR(
00378             kmo_combine_pars_load(parlist,
00379                                   "kmos.kmo_stats",
00380                                   &cmethod,
00381                                   &cpos_rej,
00382                                   &cneg_rej,
00383                                   &citer,
00384                                   NULL,
00385                                   NULL,
00386                                   TRUE));
00387 
00388 
00389         cpl_msg_info("", "-------------------------------------------");
00390 
00391         // check the (optional) mask
00392         if (cpl_frameset_get_size(frameset) == 2) {
00393             kmo_init_fits_desc(&desc2);
00394 
00395             // load descriptor of second operand
00396             desc2 = kmo_identify_fits_header(
00397                         cpl_frame_get_filename(mask_frame));
00398             KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem "
00399                                           "to be in KMOS-format!");
00400 
00401             mask_available = TRUE;
00402             if ((desc1.fits_type == f3i_fits) ||
00403                 (desc1.fits_type == f2i_fits))
00404             {
00405                 KMO_TRY_ASSURE(desc2.fits_type == f2i_fits,
00406                             CPL_ERROR_ILLEGAL_INPUT,
00407                             "Mask hasn't correct data type "
00408                             "(KMOSTYPE must be F2I)!");
00409                 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) &&
00410                                (desc1.naxis2 == desc2.naxis2),
00411                                CPL_ERROR_ILLEGAL_INPUT,
00412                                "STAT_DATA and STAT_MASK don't have the same "
00413                                "dimensions!");
00414             } else if ((desc1.fits_type == f2d_fits) ||
00415                        (desc1.fits_type == b2d_fits) ||
00416                        (desc1.fits_type == raw_fits))
00417             {
00418                 KMO_TRY_ASSURE((desc2.fits_type == f2d_fits) ||
00419                                (desc2.fits_type == b2d_fits),
00420                             CPL_ERROR_ILLEGAL_INPUT,
00421                             "Mask file hasn't correct data type "
00422                             "(KMOSTYPE must be F2D or B2D)!");
00423                 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) &&
00424                                (desc1.naxis2 == desc2.naxis2),
00425                                CPL_ERROR_ILLEGAL_INPUT,
00426                                "STAT_DATA and STAT_MASK don't have the same "
00427                                "dimensions!");
00428             } else if (desc1.fits_type == f1i_fits)
00429             {
00430                 KMO_TRY_ASSURE(1==0,
00431                             CPL_ERROR_ILLEGAL_INPUT,
00432                             "Mask can't be provided for F1I frames!");
00433             } else {
00434                 mask_available = FALSE;
00435             }
00436 
00437             KMO_TRY_ASSURE((desc1.nr_ext == desc2.nr_ext) ||
00438                            (desc1.nr_ext/2 == desc2.nr_ext) ||
00439                            ((desc2.nr_ext == 1) && (ifu > 0) &&
00440                                                          (ifu <= desc1.nr_ext)),
00441                         CPL_ERROR_ILLEGAL_INPUT,
00442                         "Mask hasn't same number of extensions as data!");
00443         }
00444 
00445         if (desc1.ex_noise == TRUE) {
00446             nr_devices = desc1.nr_ext / 2;
00447         } else {
00448             nr_devices = desc1.nr_ext;
00449         }
00450 
00451         if (ifu != 0) {
00452             if (((desc1.ex_noise == FALSE) &&
00453                  (desc1.sub_desc[ifu-1].valid_data == FALSE)) ||
00454                 ((desc1.ex_noise == TRUE) &&
00455                  (desc1.sub_desc[2 * (ifu-1) - 1].valid_data == FALSE)))
00456             {
00457                 cpl_msg_info(cpl_func, "No valid IFU or detector has been "
00458                                        "selected!");
00459                 kmo_free_fits_desc(&desc1);
00460                 kmo_free_fits_desc(&desc2);
00461                 return 0;
00462             }
00463         }
00464 
00465         // --- load, update & save primary header ---
00466         KMO_TRY_EXIT_IF_ERROR(
00467             kmo_dfs_save_main_header(frameset, STATS, "", data_frame,
00468                                      NULL, parlist, cpl_func));
00469         //
00470         // load & process data
00471         //
00472         for (i = 1; i <= nr_devices; i++) {
00473             if (desc1.ex_noise == FALSE) {
00474                 devnr = desc1.sub_desc[i - 1].device_nr;
00475             } else {
00476                 devnr = desc1.sub_desc[2 * i - 1].device_nr;
00477             }
00478 
00479             if (desc1.ex_badpix == FALSE) {
00480                 index_data = kmo_identify_index_desc(desc1, devnr, FALSE);
00481             } else {
00482                 index_data = kmo_identify_index_desc(desc1, devnr, 2);
00483             }
00484             KMO_TRY_CHECK_ERROR_STATE();
00485 
00486             if (desc1.ex_noise) {
00487                 index_noise = kmo_identify_index_desc(desc1, devnr, TRUE);
00488             }
00489             KMO_TRY_CHECK_ERROR_STATE();
00490 
00491             if (i > 1) {
00492                 kmclipm_omit_warning_one_slice = TRUE;
00493             }
00494             if ((ifu == 0) || (ifu == i)) {
00495                 // check if IFU is valid
00496                 valid_ifu = FALSE;
00497                 if (desc1.sub_desc[index_data-1].valid_data == TRUE) {
00498                     valid_ifu = TRUE;
00499                 }
00500 
00501                 if (valid_ifu) {
00502                     // load data
00503 //                    if ((desc1.fits_type == f3i_fits) ||
00504 //                        (desc1.fits_type == f2i_fits) ||
00505 //                        (desc1.fits_type == f2d_fits) ||
00506 //                        (desc1.fits_type == raw_fits)) {
00507                     // load mask, if available
00508                     if (mask_available == TRUE) {
00509                         tmpi = i;
00510                         if (desc2.nr_ext == 1) {
00511                             tmpi = 1;
00512                         }
00513                         if (desc2.naxis == 1) {
00514                             KMO_TRY_EXIT_IF_NULL(
00515                                 mask_vec = kmo_dfs_load_vector(frameset,
00516                                                                do_mode2,
00517                                                                tmpi,
00518                                                                FALSE));
00519                         } else if (desc2.naxis == 2) {
00520                             if (desc2.fits_type == b2d_fits) {
00521                                 KMO_TRY_EXIT_IF_NULL(
00522                                     mask = kmo_dfs_load_image(frameset,
00523                                                               do_mode2,
00524                                                               tmpi,
00525                                                               2, FALSE, NULL));
00526                             } else {
00527                                 KMO_TRY_EXIT_IF_NULL(
00528                                     mask = kmo_dfs_load_image(frameset,
00529                                                               do_mode2,
00530                                                               tmpi,
00531                                                               FALSE, FALSE, NULL));
00532                             }
00533                         } else {
00534                             KMO_TRY_ASSURE(1 == 0,
00535                                            CPL_ERROR_ILLEGAL_INPUT,
00536                                            "STAT_MASK must either be a "
00537                                            "vector or an image!");
00538                         }
00539                     }
00540 //                    }
00541 
00542                     if (desc1.fits_type == f3i_fits) {
00543                         KMO_TRY_EXIT_IF_NULL(
00544                             data_in = kmo_dfs_load_cube(frameset, do_mode1,
00545                                                         devnr, FALSE));
00546                         KMO_TRY_EXIT_IF_NULL(
00547                             data_out = kmo_calc_stats_cube(data_in,
00548                                                            mask,
00549                                                            cpos_rej,
00550                                                            cneg_rej,
00551                                                            citer));
00552                         cpl_imagelist_delete(data_in); data_in = NULL;
00553                     } else if (desc1.fits_type == f1i_fits) {
00554                         KMO_TRY_EXIT_IF_NULL(
00555                             vec_in = kmo_dfs_load_vector(frameset, do_mode1,
00556                                                          devnr, FALSE));
00557                         KMO_TRY_EXIT_IF_NULL(
00558                             data_out = kmo_calc_stats_vec(vec_in,
00559                                                           mask_vec,
00560                                                           cpos_rej,
00561                                                           cneg_rej,
00562                                                           citer));
00563                         kmclipm_vector_delete(vec_in); vec_in = NULL;
00564                     } else if ((desc1.fits_type == f2i_fits) ||
00565                                (desc1.fits_type == f2d_fits) ||
00566                                (desc1.fits_type == b2d_fits) ||
00567                                (desc1.fits_type == raw_fits))
00568                     {
00569                         int sat_mode = FALSE;
00570                         if (desc1.fits_type == raw_fits) {
00571                             sat_mode = TRUE;
00572                         }
00573 
00574                         if (desc1.fits_type == b2d_fits) {
00575                             KMO_TRY_EXIT_IF_NULL(
00576                                 img_in = kmo_dfs_load_image(frameset, do_mode1,
00577                                                             devnr, 2, sat_mode, NULL));
00578                         } else {
00579                             KMO_TRY_EXIT_IF_NULL(
00580                                 img_in = kmo_dfs_load_image(frameset, do_mode1,
00581                                                             devnr,
00582                                                             FALSE, sat_mode, NULL));
00583                         }
00584                         KMO_TRY_EXIT_IF_NULL(
00585                             data_out = kmo_calc_stats_img(img_in,
00586                                                           mask,
00587                                                           cpos_rej,
00588                                                           cneg_rej,
00589                                                           citer));
00590                         cpl_image_delete(img_in); img_in = NULL;
00591                     } else {
00592                         KMO_TRY_ASSURE(1==0,
00593                                        CPL_ERROR_ILLEGAL_INPUT,
00594                                        "Unsupported fits_type!");
00595                     }
00596 
00597                     stats_size = kmclipm_vector_get_size(data_out);
00598 
00599                     if ((desc1.fits_type == f3i_fits) ||
00600                         (desc1.fits_type == f2i_fits) ||
00601                         (desc1.fits_type == f2d_fits) ||
00602                         (desc1.fits_type == b2d_fits) ||
00603                         (desc1.fits_type == raw_fits))
00604                     {
00605                         if (mask_available == TRUE) {
00606                             cpl_image_delete(mask); mask = NULL;
00607                         }
00608                     }
00609 
00610                     // save data
00611                     if (desc1.fits_type == b2d_fits) {
00612                         KMO_TRY_EXIT_IF_NULL(
00613                             sub_header = kmo_dfs_load_sub_header(frameset, do_mode1, devnr,
00614                                                                  2));
00615                     } else {
00616                         KMO_TRY_EXIT_IF_NULL(
00617                             sub_header = kmo_dfs_load_sub_header(frameset, do_mode1, devnr,
00618                                                                  FALSE));
00619                     }
00620 
00621                     if ((desc1.fits_type == raw_fits) ||
00622                         (desc1.fits_type == b2d_fits))
00623                     {
00624                         KMO_TRY_EXIT_IF_NULL(
00625                             tmp_str = cpl_sprintf("%s%d%s", "DET.",
00626                                                   devnr,
00627                                                   ".DATA"));
00628                         KMO_TRY_EXIT_IF_ERROR(
00629                             kmclipm_update_property_string(sub_header,
00630                                                     EXTNAME,
00631                                                     tmp_str,
00632                                                     "FITS extension name"));
00633                         cpl_free(tmp_str); tmp_str = NULL;
00634                     }
00635 
00636                     KMO_TRY_EXIT_IF_ERROR(
00637                         kmo_dfs_save_vector(data_out, STATS, "", sub_header, 0./0.));
00638 
00639                     cpl_propertylist_delete(sub_header); sub_header = NULL;
00640                     kmclipm_vector_delete(data_out); data_out = NULL;
00641 
00642                     //
00643                     // do exactly the same thing for noise, if existing
00644                     //
00645                     if (desc1.ex_noise) {
00646                         if (desc1.sub_desc[index_noise-1].valid_data) {
00647                             if (desc1.fits_type == f3i_fits) {
00648                                 KMO_TRY_EXIT_IF_NULL(
00649                                     data_in = kmo_dfs_load_cube(frameset, do_mode1,
00650                                                                 devnr, TRUE));
00651 
00652                                 KMO_TRY_EXIT_IF_NULL(
00653                                     data_out = kmo_calc_stats_cube(data_in,
00654                                                                    mask,
00655                                                                    cpos_rej,
00656                                                                    cneg_rej,
00657                                                                    citer));
00658                                 cpl_imagelist_delete(data_in); data_in = NULL;
00659                             } else if (desc1.fits_type == f1i_fits) {
00660                                 KMO_TRY_EXIT_IF_NULL(
00661                                     vec_in = kmo_dfs_load_vector(frameset, do_mode1,
00662                                                                  devnr, TRUE));
00663                                 KMO_TRY_EXIT_IF_NULL(
00664                                     data_out = kmo_calc_stats_vec(vec_in,
00665                                                                   mask_vec,
00666                                                                   cpos_rej,
00667                                                                   cneg_rej,
00668                                                                   citer));
00669                                 kmclipm_vector_delete(vec_in); vec_in = NULL;
00670                             } else if ((desc1.fits_type == f2i_fits) ||
00671                                        (desc1.fits_type == f2d_fits) ||
00672                                        (desc1.fits_type == raw_fits))
00673                             {
00674                                 KMO_TRY_EXIT_IF_NULL(
00675                                     img_in = kmo_dfs_load_image(frameset, do_mode1,
00676                                                                 devnr, TRUE, FALSE, NULL));
00677 
00678                                 KMO_TRY_EXIT_IF_NULL(
00679                                     data_out = kmo_calc_stats_img(img_in,
00680                                                                   mask,
00681                                                                   cpos_rej,
00682                                                                   cneg_rej,
00683                                                                   citer));
00684                                 cpl_image_delete(img_in); img_in = NULL;
00685                             }
00686 
00687                             if ((desc1.fits_type == f3i_fits) ||
00688                                 (desc1.fits_type == f2i_fits) ||
00689                                 (desc1.fits_type == f2d_fits) ||
00690                                 (desc1.fits_type == raw_fits))
00691                             {
00692                                 if (mask_available == TRUE) {
00693                                     cpl_image_delete(mask); mask = NULL;
00694                                     kmclipm_vector_delete(mask_vec); mask_vec = NULL;
00695                                 }
00696                             }
00697 
00698                             // save noise
00699                             KMO_TRY_EXIT_IF_NULL(
00700                                 sub_header = kmo_dfs_load_sub_header(frameset,
00701                                                                      do_mode1,
00702                                                                      devnr, TRUE));
00703 
00704                             if ((desc1.fits_type == raw_fits) ||
00705                                 (desc1.fits_type == b2d_fits))
00706                             {
00707                                 KMO_TRY_EXIT_IF_NULL(
00708                                     tmp_str = cpl_sprintf("%s%d%s", "DET.",
00709                                                           devnr,
00710                                                           ".NOISE"));
00711                                 KMO_TRY_EXIT_IF_ERROR(
00712                                     kmclipm_update_property_string(sub_header,
00713                                                             EXTNAME,
00714                                                             tmp_str,
00715                                                             "FITS extension name"));
00716                                 cpl_free(tmp_str); tmp_str = NULL;
00717                             }
00718 
00719                             KMO_TRY_EXIT_IF_ERROR(
00720                                 kmo_dfs_save_vector(data_out, STATS, "",
00721                                                     sub_header, 0./0.));
00722 
00723                             cpl_propertylist_delete(sub_header);sub_header = NULL;
00724                             kmclipm_vector_delete(data_out); data_out = NULL;
00725                         }
00726                     }
00727                 } else {
00728                     // invalid IFU, just save sub_headers
00729                     KMO_TRY_EXIT_IF_NULL(
00730                         sub_header = kmo_dfs_load_sub_header(frameset,
00731                                                              do_mode1,
00732                                                              devnr, FALSE));
00733 
00734                     if ((desc1.fits_type == raw_fits) ||
00735                         (desc1.fits_type == b2d_fits))
00736                     {
00737                         KMO_TRY_EXIT_IF_NULL(
00738                             tmp_str = cpl_sprintf("%s%d%s", "DET.",
00739                                                   devnr,
00740                                                   ".DATA"));
00741                         KMO_TRY_EXIT_IF_ERROR(
00742                             kmclipm_update_property_string(sub_header,
00743                                                     EXTNAME,
00744                                                     tmp_str,
00745                                                     "FITS extension name"));
00746                         cpl_free(tmp_str); tmp_str = NULL;
00747                     }
00748 
00749                      KMO_TRY_EXIT_IF_ERROR(
00750                          kmo_dfs_save_sub_header(STATS, "", sub_header));
00751                      cpl_propertylist_delete(sub_header); sub_header = NULL;
00752 
00753                     if (desc1.ex_noise) {
00754                         cpl_propertylist_delete(sub_header); sub_header = NULL;
00755 
00756                         KMO_TRY_EXIT_IF_NULL(
00757                             sub_header = kmo_dfs_load_sub_header(frameset,
00758                                                                  do_mode1,
00759                                                                  i, TRUE));
00760 
00761                         if ((desc1.fits_type == raw_fits) ||
00762                             (desc1.fits_type == b2d_fits))
00763                         {
00764                             KMO_TRY_EXIT_IF_NULL(
00765                                 tmp_str = cpl_sprintf("%s%d%s", "DET.",
00766                                                       devnr,
00767                                                       ".NOISE"));
00768                             KMO_TRY_EXIT_IF_ERROR(
00769                                 kmclipm_update_property_string(sub_header,
00770                                                         EXTNAME,
00771                                                         tmp_str,
00772                                                         "FITS extension name"));
00773                             cpl_free(tmp_str); tmp_str = NULL;
00774                         }
00775 
00776                         KMO_TRY_EXIT_IF_ERROR(
00777                             kmo_dfs_save_sub_header(STATS, "", sub_header));
00778                         cpl_propertylist_delete(sub_header); sub_header = NULL;
00779                     }
00780                 }
00781             }
00782         }
00783 
00784         // print stats info to console
00785         kmo_free_fits_desc(&desc1);
00786         kmo_init_fits_desc(&desc1);
00787 
00788         KMO_TRY_EXIT_IF_NULL(
00789             data_frame = kmo_dfs_get_frame(frameset, STATS));
00790 
00791         desc1 = kmo_identify_fits_header(
00792                     cpl_frame_get_filename(data_frame));
00793 
00794         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
00795                                       "in KMOS-format!");
00796 
00797         cpl_msg_info("", "-------------------------------------------");
00798         cpl_msg_info("", "--- kmo_stats info                      ---");
00799         cpl_msg_info("", "-------------------------------------------");
00800 
00801         KMO_TRY_EXIT_IF_NULL(
00802             strarr = (char**)cpl_malloc((stats_size+1) * sizeof(char*)));
00803 
00804         for (i = 0; i < stats_size+1; i++) {
00805             KMO_TRY_EXIT_IF_NULL(
00806                 strarr[i] = (char*)cpl_malloc(1024 * sizeof(char)));
00807             switch (i) {
00808             case 0: strcpy(strarr[i], "                  |"); break;
00809             case 1: strcpy(strarr[i], "1.  #pixels:      | "); break;
00810             case 2: strcpy(strarr[i], "2.  #finite pix.: | "); break;
00811             case 3: strcpy(strarr[i], "3.  mean:         | "); break;
00812             case 4: strcpy(strarr[i], "4.  stdev:        | "); break;
00813             case 5: strcpy(strarr[i], "5.  mean w. rej.: | "); break;
00814             case 6: strcpy(strarr[i], "6.  stdev w. rej.:| "); break;
00815             case 7: strcpy(strarr[i], "7.  median:       | "); break;
00816             case 8: strcpy(strarr[i], "8.  mode:         | "); break;
00817             case 9: strcpy(strarr[i], "9.  noise est.:   | "); break;
00818             case 10: strcpy(strarr[i], "10. min. value:   | "); break;
00819             case 11: strcpy(strarr[i], "11. max. value:   | "); break;
00820             default: cpl_msg_error(cpl_func, "To much values in output "
00821                                              "statistic vector!"); break;
00822             }
00823         }
00824 
00825         if (desc1.ex_noise == TRUE) {
00826             nr_devices = desc1.nr_ext / 2;
00827         } else {
00828             nr_devices = desc1.nr_ext;
00829         }
00830 
00831         for (j = 1; j <= nr_devices; j++) {
00832             if (desc1.ex_noise == FALSE) {
00833                 devnr = desc1.sub_desc[j - 1].device_nr;
00834             } else {
00835                 devnr = desc1.sub_desc[2 * j - 1].device_nr;
00836             }
00837 
00838             if (desc1.ex_badpix == FALSE) {
00839                 index_data = kmo_identify_index_desc(desc1, devnr, FALSE);
00840             } else {
00841                 index_data = kmo_identify_index_desc(desc1, devnr, 2);
00842             }
00843             KMO_TRY_CHECK_ERROR_STATE();
00844 
00845             if (desc1.ex_noise) {
00846                 index_noise = kmo_identify_index_desc(desc1, devnr, TRUE);
00847             }
00848             KMO_TRY_CHECK_ERROR_STATE();
00849 
00850             // check if IFU is valid
00851             valid_ifu = FALSE;
00852             if (desc1.sub_desc[index_data-1].valid_data == TRUE) {
00853                 valid_ifu = TRUE;
00854             }
00855 
00856             if (valid_ifu) {
00857                 if (ifu != 0) {
00858                     extnr = ifu;
00859                 } else {
00860                     extnr = devnr;
00861                 }
00862 
00863                 KMO_TRY_EXIT_IF_NULL(
00864                     sub_header = kmo_dfs_load_sub_header(frameset, STATS, extnr,
00865                                                          FALSE));
00866 
00867                 strcat(strarr[0],
00868                        cpl_propertylist_get_string(sub_header, EXTNAME));
00869                 strcat(strarr[0], "|");
00870                 cpl_propertylist_delete(sub_header); sub_header = NULL;
00871 
00872                 KMO_TRY_EXIT_IF_NULL(
00873                     data_out = kmo_dfs_load_vector(frameset, STATS, extnr, FALSE));
00874 
00875                 for (i = 1; i < stats_size+1; i++) {
00876                     double val = 0.;
00877                     int rejected = 0;
00878                     val = kmclipm_vector_get(data_out, i-1, &rejected);
00879                     if (!rejected) {
00880                         KMO_TRY_EXIT_IF_NULL(
00881                             tmp_str = cpl_sprintf("%8.7g |", val));
00882                     } else {
00883                         KMO_TRY_EXIT_IF_NULL(
00884                             tmp_str = cpl_sprintf("    -     |"));
00885                     }
00886 
00887                     strcat(strarr[i], tmp_str);
00888                     cpl_free(tmp_str); tmp_str = NULL;
00889                 }
00890                 kmclipm_vector_delete(data_out); data_out = NULL;
00891 
00892                 if (desc1.ex_noise == TRUE) {
00893                     KMO_TRY_EXIT_IF_NULL(
00894                         sub_header = kmo_dfs_load_sub_header(frameset, STATS,
00895                                                              extnr, TRUE));
00896                     strcat(strarr[0],
00897                            cpl_propertylist_get_string(sub_header, EXTNAME));
00898                     strcat(strarr[0], "|");
00899                     cpl_propertylist_delete(sub_header); sub_header = NULL;
00900 
00901                     KMO_TRY_EXIT_IF_NULL(
00902                         data_out = kmo_dfs_load_vector(frameset, STATS, extnr,
00903                                                        TRUE));
00904 
00905                     for (i = 1; i < stats_size+1; i++) {
00906                         double val = 0.;
00907                         int rejected = 0;
00908                         val = kmclipm_vector_get(data_out, i-1, &rejected);
00909                         if (!rejected) {
00910                             KMO_TRY_EXIT_IF_NULL(
00911                                 tmp_str = cpl_sprintf("%9.7g | ", val));
00912                         } else {
00913                             KMO_TRY_EXIT_IF_NULL(
00914                                 tmp_str = cpl_sprintf("    -     |"));
00915                         }
00916 
00917                         strcat(strarr[i], tmp_str);
00918                         cpl_free(tmp_str); tmp_str = NULL;
00919                     }
00920                     kmclipm_vector_delete(data_out); data_out = NULL;
00921                 }
00922             }
00923         }
00924 
00925         for (i = 0; i < stats_size+1; i++) {
00926             cpl_msg_info("", "%s", strarr[i]);
00927             cpl_free(strarr[i]); strarr[i] = NULL;
00928         }
00929         cpl_free(strarr); strarr = NULL;
00930         cpl_msg_info("", "-------------------------------------------");
00931     }
00932     KMO_CATCH
00933     {
00934         KMO_CATCH_MSG();
00935 
00936         ret_val = -1;
00937     }
00938 
00939     kmo_free_fits_desc(&desc1);
00940     if (mask_available == TRUE) {
00941         kmo_free_fits_desc(&desc2);
00942     }
00943     cpl_propertylist_delete(sub_header); sub_header = NULL;
00944     cpl_imagelist_delete(data_in); data_in = NULL;
00945     kmclipm_vector_delete(data_out); data_out = NULL;
00946     cpl_image_delete(mask); mask = NULL;
00947     kmclipm_vector_delete(mask_vec); mask_vec = NULL;
00948 
00949     return ret_val;
00950 }
00951