KMOS Pipeline Reference Manual  1.1.5
kmo_fits_strip.c
00001 /* $Id: kmo_fits_strip.c,v 1.9 2013/05/21 12:13:58 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/05/21 12:13:58 $
00024  * $Revision: 1.9 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <math.h>
00033 #include <string.h>
00034 
00035 #include <cpl.h>
00036 
00037 #include "kmclipm_constants.h"
00038 #include "kmclipm_functions.h"
00039 
00040 #include "kmo_dfs.h"
00041 #include "kmo_error.h"
00042 #include "kmo_debug.h"
00043 
00044 static int kmo_fits_strip_create(cpl_plugin *);
00045 static int kmo_fits_strip_exec(cpl_plugin *);
00046 static int kmo_fits_strip_destroy(cpl_plugin *);
00047 static int kmo_fits_strip(cpl_parameterlist *, cpl_frameset *);
00048 
00049 static char kmo_fits_strip_description[] =
00050 "With this recipe KMOS fits frames can be stripped in following way:\n"
00051 "\n"
00052 "--noise\n"
00053 "All noise extensions will be removed. Only the data extensions remain.\n"
00054 "\n"
00055 "--angle\n"
00056 "Applies only to calibration products from kmo_flat and kmo_wave_cal.\n"
00057 "All extensions matching provided angle are kept, the others are removed.\n"
00058 "\n"
00059 "--empty\n"
00060 "All empty extensions will be removed.\n"
00061 "\n"
00062 "All parameters can be combined. When no parameter is provided, no output\n"
00063 "will be generated.\n"
00064 "\n"
00065 "-------------------------------------------------------------------------------\n"
00066 "  Input files:\n"
00067 "\n"
00068 "   DO                    KMOS                                                  \n"
00069 "   category              Type   Explanation                    Required #Frames\n"
00070 "   --------              -----  -----------                    -------- -------\n"
00071 "   <none or any>         F2D or frame to strip                     Y       1   \n"
00072 "                         F3I or\n"
00073 "                         F2I or\n"
00074 "                         F1I or\n"
00075 "\n"
00076 "  Output files:\n"
00077 "\n"
00078 "   DO                    KMOS\n"
00079 "   category              Type   Explanation\n"
00080 "   --------              -----  -----------\n"
00081 "   STRIP                 F2D    Stripped frame\n"
00082 "-------------------------------------------------------------------------------\n"
00083 "\n";
00084 
00101 int cpl_plugin_get_info(cpl_pluginlist *list)
00102 {
00103     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00104     cpl_plugin *plugin = &recipe->interface;
00105 
00106     cpl_plugin_init(plugin,
00107                         CPL_PLUGIN_API,
00108                         KMOS_BINARY_VERSION,
00109                         CPL_PLUGIN_TYPE_RECIPE,
00110                         "kmo_fits_strip",
00111                         "Strip noise, rotator and/or empty extensions from a "
00112                         "processed KMOS fits frame",
00113                         kmo_fits_strip_description,
00114                         "Alex Agudo Berbel",
00115                         "kmos-spark@mpe.mpg.de",
00116                         kmos_get_license(),
00117                         kmo_fits_strip_create,
00118                         kmo_fits_strip_exec,
00119                         kmo_fits_strip_destroy);
00120 
00121     cpl_pluginlist_append(list, plugin);
00122 
00123     return 0;
00124 }
00125 
00133 static int kmo_fits_strip_create(cpl_plugin *plugin)
00134 {
00135     cpl_recipe *recipe = NULL;
00136     cpl_parameter *p = NULL;
00137 
00138     /* Check that the plugin is part of a valid recipe */
00139     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00140         recipe = (cpl_recipe *)plugin;
00141     else
00142         return -1;
00143 
00144     /* Create the parameters list in the cpl_recipe object */
00145     recipe->parameters = cpl_parameterlist_new();
00146 
00147     /* Fill the parameters list */
00148     /* --empty */
00149     p = cpl_parameter_new_value("kmos.kmo_fits_strip.empty",
00150                                 CPL_TYPE_BOOL,
00151                                 "TRUE: if empty extensions shall be removed,"
00152                                 " FALSE: otherwise",
00153                                 "kmos.kmo_fits_strip",
00154                                 FALSE);
00155     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "empty");
00156     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00157     cpl_parameterlist_append(recipe->parameters, p);
00158 
00159     /* --noise */
00160     p = cpl_parameter_new_value("kmos.kmo_fits_strip.noise",
00161                                 CPL_TYPE_BOOL,
00162                                 "TRUE: if noise extensions shall be removed,"
00163                                 " FALSE: otherwise",
00164                                 "kmos.kmo_fits_strip",
00165                                 FALSE);
00166     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "noise");
00167     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00168     cpl_parameterlist_append(recipe->parameters, p);
00169 
00170     /* --angle */
00171     p = cpl_parameter_new_value("kmos.kmo_fits_strip.angle",
00172                                 CPL_TYPE_INT,
00173                                 "All extensions not matching provided angle "
00174                                 "are stripped.",
00175                                 "kmos.kmo_fits_strip",
00176                                 -1);
00177     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "angle");
00178     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00179     cpl_parameterlist_append(recipe->parameters, p);
00180 
00181     return 0;
00182 }
00183 
00189 static int kmo_fits_strip_exec(cpl_plugin *plugin)
00190 {
00191     cpl_recipe  *recipe;
00192 
00193     /* Get the recipe out of the plugin */
00194     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00195         recipe = (cpl_recipe *)plugin;
00196     else return -1 ;
00197 
00198     return kmo_fits_strip(recipe->parameters, recipe->frames);
00199 }
00200 
00206 static int kmo_fits_strip_destroy(cpl_plugin *plugin)
00207 {
00208     cpl_recipe *recipe;
00209 
00210     /* Get the recipe out of the plugin */
00211     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00212         recipe = (cpl_recipe *)plugin;
00213     else return -1 ;
00214 
00215     cpl_parameterlist_delete(recipe->parameters);
00216     return 0 ;
00217 }
00218 
00219 static const char * kmos_get_base_name(const char * self)
00220 {
00221     const char * p = self ? strrchr(self, '/') : NULL;
00222 
00223     return p ? p + 1 : self;
00224 }
00225 
00244 static int kmo_fits_strip(cpl_parameterlist *parlist, cpl_frameset *frameset)
00245 {
00246 #define PRO_REC_PARAMi_NAME         "ESO PRO REC1 PARAM%d NAME"
00247 #define PRO_REC_PARAMi_VALUE        "ESO PRO REC1 PARAM%d VALUE"
00248 
00249     int                 ret_val             = 0,
00250                         nr_devices          = 0,
00251                         remove_noise        = FALSE,
00252                         remove_empty        = FALSE,
00253                         remove_angle        = FALSE,
00254                         found_angle         = FALSE,
00255                         isCalFrame          = 0,
00256                         isMasterFlat        = 0,
00257                         actDetNr            = 0,
00258                         cal_device_nr       = 0,
00259                         dummy               = 0,
00260                         npar                = 0,
00261                         index               = 0;
00262     double              angle               = 0.,
00263                         tmp_angle           = 0.,
00264                         ret_angle           = 0.,
00265                         secClosestAng       = 0.,
00266                         ocsRotAngle         = 0.,
00267                         proRotAngle         = 0.;
00268     cpl_propertylist    *header             = NULL,
00269                         *sub_header         = NULL;
00270     cpl_imagelist       *cube               = NULL;
00271     cpl_image           *img                = NULL;
00272     cpl_vector          *vec                = NULL;
00273     main_fits_desc      desc;
00274     cpl_frame           *frame              = NULL;
00275     const char          *kname              = NULL,
00276                         *filename           = NULL;
00277     char                *ggg                = NULL,
00278                         cval[1024];
00279     const cpl_parameter *param              = NULL;
00280     cpl_frame           *product_frame      = NULL;
00281     kmclipm_vector      *kv                 = NULL;
00282 
00283     KMO_TRY
00284     {
00285         kmo_init_fits_desc(&desc);
00286 
00287         /* --- check input --- */
00288         KMO_TRY_ASSURE((parlist != NULL) &&
00289                        (frameset != NULL),
00290                        CPL_ERROR_NULL_INPUT,
00291                        "Not all input data is provided!");
00292 
00293         KMO_TRY_ASSURE(cpl_frameset_get_size(frameset) == 1,
00294                        CPL_ERROR_NULL_INPUT,
00295                        "A fits-file must be provided!");
00296 
00297         KMO_TRY_EXIT_IF_NULL(
00298             frame = kmo_dfs_get_frame(frameset, "0"));
00299 
00300         KMO_TRY_EXIT_IF_NULL(
00301             filename = cpl_frame_get_filename(frame));
00302 
00303         desc = kmo_identify_fits_header(filename);
00304         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
00305                                       "in KMOS-format!");
00306 
00307         KMO_TRY_ASSURE((desc.fits_type == f2d_fits) ||
00308                        (desc.fits_type == f3i_fits) ||
00309                        (desc.fits_type == f2i_fits) ||
00310                        (desc.fits_type == f1i_fits),
00311                        CPL_ERROR_ILLEGAL_INPUT,
00312                        "Input data hasn't correct data type "
00313                        "(KMOSTYPE must be F2D, F3I, F2I or F1I)!");
00314 
00315         KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset, "kmo_fits_strip") == 1,
00316                        CPL_ERROR_ILLEGAL_INPUT,
00317                        "Cannot identify RAW and CALIB frames!");
00318 
00319         cpl_msg_info("", "--- Parameter setup for kmo_fits_strip ----");
00320         remove_empty = kmo_dfs_get_parameter_bool(parlist,
00321                                                   "kmos.kmo_fits_strip.empty");
00322         KMO_TRY_CHECK_ERROR_STATE();
00323         KMO_TRY_ASSURE((remove_empty == TRUE) ||
00324                        (remove_empty == FALSE),
00325                        CPL_ERROR_ILLEGAL_INPUT,
00326                        "empty must be TRUE or FALSE!");
00327         KMO_TRY_EXIT_IF_ERROR(
00328             kmo_dfs_print_parameter_help(parlist, "kmos.kmo_fits_strip.empty"));
00329 
00330         remove_noise = kmo_dfs_get_parameter_bool(parlist,
00331                                                   "kmos.kmo_fits_strip.noise");
00332         KMO_TRY_CHECK_ERROR_STATE();
00333         KMO_TRY_ASSURE((remove_noise == TRUE) ||
00334                        (remove_noise == FALSE),
00335                        CPL_ERROR_ILLEGAL_INPUT,
00336                        "noise must be TRUE or FALSE!");
00337         KMO_TRY_EXIT_IF_ERROR(
00338             kmo_dfs_print_parameter_help(parlist, "kmos.kmo_fits_strip.noise"));
00339 
00340         angle = kmo_dfs_get_parameter_int(parlist,
00341                                           "kmos.kmo_fits_strip.angle");
00342         KMO_TRY_CHECK_ERROR_STATE();
00343         KMO_TRY_EXIT_IF_ERROR(
00344             kmo_dfs_print_parameter_help(parlist, "kmos.kmo_fits_strip.angle"));
00345 
00346         if (angle >= 0) {
00347             remove_angle = TRUE;
00348         }
00349 
00350         KMO_TRY_ASSURE(!remove_angle || ((angle >=0) && (angle < 360)),
00351                        CPL_ERROR_ILLEGAL_INPUT,
00352                        "angle must be between 0 and 360 degrees!");
00353 
00354         cpl_msg_info("", "-------------------------------------------");
00355 
00356         if ((!remove_empty) && (!remove_noise) && (!remove_angle)) {
00357             // do nothing
00358             cpl_msg_info("","No action has been specified (angle-, noise- or empty-parameter),"
00359                             " therefore no output is generated");
00360         } else {
00361             // check if it is a multi-angle-calibration file
00362             isCalFrame = FALSE;
00363             KMO_TRY_EXIT_IF_NULL(
00364                 sub_header = kmo_dfs_load_sub_header(frameset, "0", 1, FALSE));
00365             if (cpl_propertylist_has(sub_header, CAL_ROTANGLE)) {
00366                 isCalFrame = TRUE;
00367             }
00368             cpl_propertylist_delete(sub_header); sub_header = NULL;
00369 
00370             //
00371             // --- save primary extension ---
00372             //
00373             // load data and save it away again
00374             KMO_TRY_EXIT_IF_NULL(
00375                 header = kmo_dfs_load_primary_header(frameset, "0"));
00376 
00377             if (strcmp(MASTER_FLAT, cpl_propertylist_get_string(header, CPL_DFS_PRO_CATG))==0) {
00378                 isMasterFlat = TRUE;
00379             }
00380 
00381             if (remove_angle && isCalFrame) {
00382                 // update OCS.ROT.NAANGLE if it differs
00383                 ocsRotAngle = cpl_propertylist_get_double(header, ROTANGLE);
00384                 ocsRotAngle = kmclipm_strip_angle(&ocsRotAngle);
00385                 proRotAngle = kmclipm_cal_propertylist_find_angle(filename, 1, FALSE, angle,
00386                         &dummy, &secClosestAng);
00387                 KMO_TRY_CHECK_ERROR_STATE();
00388                 if (fabs(ocsRotAngle-proRotAngle) > 0.1) {
00389                     cpl_msg_warning("", "In the product the original ESO OCS ROT NAANGLE keyword "
00390                                         "has been updated with the chosen "
00391                                         "ESO PRO ROT NAANGLE (was: %g, is: %g)!", ocsRotAngle, proRotAngle);
00392                     KMO_TRY_EXIT_IF_ERROR(
00393                         cpl_propertylist_update_double(header, ROTANGLE, proRotAngle));
00394                 }
00395             }
00396 //            // kmo_dfs_save_main_header() doesn't allow to change ESO OCS ROT NAANGLE
00397 //            KMO_TRY_EXIT_IF_ERROR(
00398 //                kmo_dfs_save_main_header(frameset, STRIP, "", frame,
00399 //                                         header, parlist, cpl_func));
00400             {
00401                 // setup DFS manually (no MD5 calculated...)
00402                 KMO_TRY_EXIT_IF_ERROR(
00403                     cpl_propertylist_update_string(header, "PIPEFILE", "strip.fits"));
00404                 KMO_TRY_EXIT_IF_ERROR(
00405                     cpl_propertylist_update_string(header, CPL_DFS_PRO_CATG, STRIP));
00406                 KMO_TRY_EXIT_IF_ERROR(
00407                     cpl_propertylist_update_string(header, "ESO PRO REC1 ID", cpl_func));
00408                 ggg = cpl_sprintf("cpl-%d.%d.%d", cpl_version_get_major(), cpl_version_get_minor(), cpl_version_get_micro());
00409                 KMO_TRY_EXIT_IF_ERROR(
00410                     cpl_propertylist_update_string(header, "ESO PRO REC1 DRS ID", ggg));
00411                 cpl_free(ggg); ggg = NULL;
00412                 KMO_TRY_EXIT_IF_ERROR(
00413                     cpl_propertylist_update_string(header, "ESO PRO REC1 PIPE ID", VERSION));
00414                 KMO_TRY_EXIT_IF_ERROR(
00415                     cpl_propertylist_update_string(header, "ESO PRO REC1 CAL1 NAME", kmos_get_base_name(filename)));
00416                 KMO_TRY_EXIT_IF_ERROR(
00417                     cpl_propertylist_update_string(header, "ESO PRO REC1 CAL1 CATG", COMMANDLINE));
00418                 KMO_TRY_EXIT_IF_ERROR(
00419                     cpl_propertylist_update_string(header, "ESO PRO REC1 CAL1 DATAMD5", cpl_propertylist_get_string(header, "DATAMD5")));
00420                 KMO_TRY_CHECK_ERROR_STATE();
00421 
00422                 while (param != NULL) {
00423                     char *pval, *dval;
00424                     ++npar;
00425 
00426                     kname = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
00427                     const char * comment = cpl_parameter_get_help(param);
00428                     switch (cpl_parameter_get_type(param)) {
00429                     case CPL_TYPE_BOOL:
00430                         pval = cpl_strdup(cpl_parameter_get_bool(param) == 1 ? "true" : "false");
00431                         dval = cpl_sprintf("Default: %s", cpl_parameter_get_default_bool(param) == 1 ? "true" : "false");
00432                         break;
00433                     case CPL_TYPE_INT:
00434                         pval = cpl_sprintf("%d", cpl_parameter_get_int(param));
00435                         dval = cpl_sprintf("Default: %d", cpl_parameter_get_default_int(param));
00436                         break;
00437                     case CPL_TYPE_DOUBLE:
00438                         pval = cpl_sprintf("%g", cpl_parameter_get_double(param));
00439                         dval = cpl_sprintf("Default: %g", cpl_parameter_get_default_double(param));
00440                         break;
00441                     case CPL_TYPE_STRING:
00442                         pval = cpl_strdup(cpl_parameter_get_string(param));
00443                         dval = cpl_sprintf("Default: '%s'", cpl_parameter_get_default_string(param));
00444                         break;
00445                     default:
00446                         /* Theoretically impossible to get here */
00447                         KMO_TRY_ASSURE(1==0,
00448                                        CPL_ERROR_UNSPECIFIED,
00449                                        "what?");
00450                     }
00451                     snprintf(cval, 1024, PRO_REC_PARAMi_NAME, npar);
00452                     cpl_propertylist_update_string(header, cval, kname);
00453                     cpl_propertylist_set_comment(header, cval, comment);
00454 
00455                     snprintf(cval, 1024, PRO_REC_PARAMi_VALUE, npar);
00456                     cpl_propertylist_update_string(header, cval, pval);
00457                     cpl_propertylist_set_comment(header, cval, dval);
00458 
00459                     cpl_free((void*)pval);
00460                     cpl_free((void*)dval);
00461 
00462                     param = cpl_parameterlist_get_next_const(parlist);
00463                 }
00464             }
00465             KMO_TRY_CHECK_ERROR_STATE();
00466 
00467             cpl_msg_info(cpl_func, "Writing FITS %s product(%s): %s", "propertylist", "STRIP", "strip.fits");
00468             KMO_TRY_EXIT_IF_NULL(
00469                 product_frame = cpl_frame_new());
00470             KMO_TRY_EXIT_IF_ERROR(
00471                 cpl_frame_set_filename(product_frame, "strip.fits"));
00472             KMO_TRY_EXIT_IF_ERROR(
00473                 cpl_frame_set_tag(product_frame, "STRIP"));
00474             KMO_TRY_EXIT_IF_ERROR(
00475                 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY));
00476             KMO_TRY_EXIT_IF_ERROR(
00477                 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT));
00478             KMO_TRY_EXIT_IF_ERROR(
00479                 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL));
00480             KMO_TRY_EXIT_IF_ERROR(
00481                 cpl_frameset_insert(frameset, product_frame));
00482 
00483             KMO_TRY_EXIT_IF_ERROR(
00484                 cpl_propertylist_save(header, "strip.fits", CPL_IO_CREATE));
00485             cpl_propertylist_delete(header); header = NULL;
00486 
00487             if (!isCalFrame) {
00488                 if (!desc.ex_noise) {
00489                     nr_devices = desc.nr_ext;
00490                 } else {
00491                     nr_devices = desc.nr_ext / 2;
00492                 }
00493             } else {
00494                 nr_devices = 3;
00495             }
00496 
00497             if (isCalFrame) {
00498                 if (isMasterFlat) {
00499                     remove_noise = !remove_noise;
00500                 }
00501             } else {
00502                 if (desc.ex_noise) {
00503                     remove_noise = !remove_noise;
00504                 }
00505             }
00506 
00507             actDetNr = 1;
00508             for (int i = 0; i < nr_devices; i++)
00509             {
00510                 // either loop noise or not
00511                 for (int n = FALSE; n <= remove_noise; n++) {
00512                     if (isCalFrame) {
00513                         KMO_TRY_EXIT_IF_NULL(
00514                             header = kmclipm_cal_propertylist_load(filename, i+1, n, angle, &ret_angle));
00515                     } else {
00516                         KMO_TRY_EXIT_IF_NULL(
00517                             header = kmo_dfs_load_sub_header(frameset, "0", i + 1, n));
00518                     }
00519 
00520                     if (remove_angle) {
00521                         // examine angle
00522                         if (cpl_propertylist_has(header, CAL_ROTANGLE)) {
00523                             tmp_angle = cpl_propertylist_get_double(header, CAL_ROTANGLE);
00524                             KMO_TRY_CHECK_ERROR_STATE();
00525 
00526                             if (fabs(angle - tmp_angle) < 0.01) {
00527                                 found_angle = TRUE;
00528                                 isCalFrame = TRUE;
00529                             } else {
00530                                 found_angle = FALSE;
00531                             }
00532                         } else {
00533                             // frame doesn't seem to have the CAL_ROTANGLE keyword
00534                             // process all extensions
00535                             found_angle = TRUE;
00536                         }
00537                     } else {
00538                         found_angle = TRUE;
00539                     }
00540 
00541                     index = kmo_identify_index(filename, i+1, n);
00542                     if (found_angle) {
00543                         if ((desc.fits_type == f2d_fits) ||
00544                             (desc.fits_type == f2i_fits))
00545                         {
00546                             if (isCalFrame) {
00547                                 cal_device_nr = cpl_propertylist_get_int(header, CHIPINDEX);
00548                                 KMO_TRY_CHECK_ERROR_STATE();
00549                                 if (cal_device_nr == actDetNr) {
00550                                     KMO_TRY_EXIT_IF_ERROR(
00551                                         kmo_update_sub_keywords(header,
00552                                                                 n,
00553                                                                 FALSE,
00554                                                                 desc.frame_type,
00555                                                                 actDetNr));
00556                                     KMO_TRY_EXIT_IF_NULL(
00557                                         img = kmo_dfs_load_cal_image(frameset, "0",
00558                                                                      i+1, n, angle,
00559                                                                      FALSE, NULL,
00560                                                                      &ret_angle, -1, 0, 0));
00561 
00562                                     if (fabs(angle-ret_angle) > 0.01) {
00563                                         cpl_msg_warning("","Angle provided: %g, angle found: %g", angle, ret_angle);
00564                                     }
00565                                     if (n == remove_noise) {
00566                                         actDetNr++;
00567                                     }
00568                                 }
00569                                 if (img != NULL) {
00570                                     KMO_TRY_EXIT_IF_ERROR(
00571                                         kmo_dfs_save_image(img, STRIP, "", header, 0./0.));
00572                                 }
00573                             } else {
00574                                 img = cpl_image_load(filename,CPL_TYPE_FLOAT, 0, index);
00575                                 if (CPL_ERROR_NONE != cpl_error_get_code()) {
00576                                     cpl_error_reset();
00577                                     if (!remove_empty) {
00578                                         KMO_TRY_EXIT_IF_ERROR(
00579                                             kmo_dfs_save_image(img, STRIP, "", header, 0./0.));
00580                                     }
00581                                 } else {
00582                                     KMO_TRY_EXIT_IF_ERROR(
00583                                         kmo_dfs_save_image(img, STRIP, "", header, 0./0.));
00584                                 }
00585                             }
00586                             cpl_image_delete(img); img = NULL;
00587                         } else if (desc.fits_type == f3i_fits) {
00588                             cube = cpl_imagelist_load(filename,CPL_TYPE_FLOAT, index);
00589                             if (CPL_ERROR_NONE != cpl_error_get_code()) {
00590                                 cpl_error_reset();
00591                                 if (!remove_empty) {
00592                                     KMO_TRY_EXIT_IF_ERROR(
00593                                         kmo_dfs_save_cube(cube, STRIP, "", header, 0./0.));
00594                                 }
00595                             } else {
00596                                 KMO_TRY_EXIT_IF_ERROR(
00597                                     kmo_dfs_save_cube(cube, STRIP, "", header, 0./0.));
00598                             }
00599                             cpl_imagelist_delete(cube); cube = NULL;
00600                         } else if (desc.fits_type == f1i_fits) {
00601                             vec = cpl_vector_load(filename, index);
00602                             if (CPL_ERROR_NONE != cpl_error_get_code()) {
00603                                 cpl_error_reset();
00604                                 if (!remove_empty) {
00605                                     KMO_TRY_EXIT_IF_ERROR(
00606                                         kmo_dfs_save_vector(NULL, STRIP, "", header, 0./0.));
00607                                 }
00608                             } else {
00609                                 KMO_TRY_EXIT_IF_NULL(
00610                                     kv = kmclipm_vector_create(vec));
00611                                 KMO_TRY_EXIT_IF_ERROR(
00612                                     kmo_dfs_save_vector(kv, STRIP, "", header, 0./0.));
00613                             }
00614                             kmclipm_vector_delete(kv); kv = NULL;
00615                         }
00616                     } // if (found_angle)
00617                     cpl_propertylist_delete(header); header = NULL;
00618                 } // for (n)
00619             } // for (i = nr_devices)
00620         } // if ((!remove_empty) && (!remove_noise) && (!remove_angle))
00621     }
00622     KMO_CATCH
00623     {
00624         KMO_CATCH_MSG();
00625 
00626         ret_val = -1;
00627     }
00628 
00629     cpl_propertylist_delete(header); header = NULL;
00630     cpl_imagelist_delete(cube); cube = NULL;
00631     cpl_image_delete(img); img = NULL;
00632     kmclipm_vector_delete(kv); kv = NULL;
00633     kmo_free_fits_desc(&desc);
00634 
00635     return ret_val;
00636 }
00637