KMOS Pipeline Reference Manual  1.3.0b1
kmo_sky_tweak.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 #include <cpl.h>
00025 #include <cpl_wcs.h>
00026 
00027 #include "kmo_dfs.h"
00028 #include "kmo_error.h"
00029 #include "kmo_constants.h"
00030 #include "kmo_priv_sky_tweak.h"
00031 
00032 static int kmo_sky_tweak_create(cpl_plugin *);
00033 static int kmo_sky_tweak_exec(cpl_plugin *);
00034 static int kmo_sky_tweak_destroy(cpl_plugin *);
00035 static int kmo_sky_tweak(cpl_parameterlist *, cpl_frameset *);
00036 
00037 static char kmo_sky_tweak_description[] =
00038 " This recipes is an advanced tool to remove OH sky lines.\n"
00039 "\n"
00040 "BASIC PARAMETERS:\n"
00041 "-----------------\n"
00042 "--tbsub\n"
00043 "If set to TRUE subtract the thermal background from the input cube.\n"
00044 "Default value is TRUE.\n"
00045 "\n"
00046 "-------------------------------------------------------------------------------\n"
00047 "  Input files:\n"
00048 "\n"
00049 "   DO                    KMOS                                                  \n"
00050 "   category              Type   Explanation                    Required #Frames\n"
00051 "   --------              -----  -----------                    -------- -------\n"
00052 "   CUBE_OBJECT           F3I    object cubes                       Y      >=1  \n"
00053 "   CUBE_SKY              F3I    sky cube                           Y       1   \n"
00054 "\n"
00055 "  Output files:\n"
00056 "\n"
00057 "   DO                    KMOS\n"
00058 "   category              Type   Explanation\n"
00059 "   --------              -----  -----------\n"
00060 "   OBJECT_S              F3I    Corrected object cubes\n"
00061 "-------------------------------------------------------------------------------\n"
00062 "\n";
00063 
00080 int cpl_plugin_get_info(cpl_pluginlist *list)
00081 {
00082     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00083     cpl_plugin *plugin = &recipe->interface;
00084 
00085     cpl_plugin_init(plugin,
00086                         CPL_PLUGIN_API,
00087                         KMOS_BINARY_VERSION,
00088                         CPL_PLUGIN_TYPE_RECIPE,
00089                         "kmo_sky_tweak",
00090                         "Removal of OH sky lines",
00091                         kmo_sky_tweak_description,
00092                         "Erich Wiezorrek",
00093                         "usd-help@eso.org",
00094                         kmos_get_license(),
00095                         kmo_sky_tweak_create,
00096                         kmo_sky_tweak_exec,
00097                         kmo_sky_tweak_destroy);
00098 
00099     cpl_pluginlist_append(list, plugin);
00100 
00101     return 0;
00102 }
00103 
00111 static int kmo_sky_tweak_create(cpl_plugin *plugin)
00112 {
00113     cpl_recipe *recipe;
00114     cpl_parameter *p;
00115 
00116     /* Check that the plugin is part of a valid recipe */
00117     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00118         recipe = (cpl_recipe *)plugin;
00119     else
00120         return -1;
00121 
00122     /* Create the parameters list in the cpl_recipe object */
00123     recipe->parameters = cpl_parameterlist_new();
00124 
00125     /* Fill the parameters list */
00126 
00127     /* --tbsub */
00128     p = cpl_parameter_new_value("kmos.kmo_sky_tweak.tbsub",
00129                                 CPL_TYPE_BOOL,
00130                                 "Subtract thermal background from input cube."
00131                                 "(TRUE (apply) or "
00132                                 "FALSE (don't apply)",
00133                                 "kmos.kmo_sky_tweak",
00134                                 TRUE);
00135     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "tbsub");
00136     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00137     cpl_parameterlist_append(recipe->parameters, p);
00138 
00139     return 0;
00140 
00141 }
00142 
00148 static int kmo_sky_tweak_exec(cpl_plugin *plugin)
00149 {
00150     cpl_recipe  *recipe;
00151 
00152     /* Get the recipe out of the plugin */
00153     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00154         recipe = (cpl_recipe *)plugin;
00155     else return -1 ;
00156 
00157     return kmo_sky_tweak(recipe->parameters, recipe->frames);
00158 }
00159 
00165 static int kmo_sky_tweak_destroy(cpl_plugin *plugin)
00166 {
00167     cpl_recipe *recipe;
00168 
00169     /* Get the recipe out of the plugin */
00170     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00171         recipe = (cpl_recipe *)plugin;
00172     else return -1 ;
00173 
00174     cpl_parameterlist_delete(recipe->parameters);
00175     return 0 ;
00176 }
00177 
00192 static int kmo_sky_tweak(cpl_parameterlist *parlist, cpl_frameset *frameset)
00193 {
00194     int              ret_val                = 0;
00195 
00196     int              ox                     = 0,
00197                      nr_object_frames       = 0,
00198                      nr_obj_devices         = 0,
00199                      nr_sky_devices         = 0,
00200                      ifu_nr                 = 0,
00201                      index                  = 0,
00202                      tbsub                  = TRUE;
00203     const char       *obj_fn                = NULL,
00204                      *sky_fn                = NULL;
00205 
00206     cpl_frame        **object_frames        = NULL,
00207                      *object_frame          = NULL,
00208                      *sky_frame             = NULL;
00209     cpl_imagelist    *obj_data              = NULL/*,
00210                      *obj_noise             = NULL*/,
00211                      *sky_data              = NULL,
00212                      *tweaked_data          = NULL/*,
00213                      *tweaked_noise         = NULL*/;
00214     cpl_propertylist *main_header           = NULL,
00215                      *sub_header            = NULL;
00216     main_fits_desc   obj_fits_desc,
00217                      sky_fits_desc;
00218 
00219     KMO_TRY
00220     {
00221         //
00222         // check frameset
00223         //
00224         KMO_TRY_ASSURE((parlist != NULL) &&
00225                        (frameset != NULL),
00226                        CPL_ERROR_NULL_INPUT,
00227                        "Not all input data is provided!");
00228 
00229         KMO_TRY_ASSURE(! ((cpl_frameset_count_tags(frameset, CUBE_OBJECT) == 0) &&
00230                           (cpl_frameset_count_tags(frameset, CUBE_SKY) == 0)    ),
00231                           CPL_ERROR_FILE_NOT_FOUND,
00232                           "CUBE_OBJECT or  CUBE_SKY frames missing in "
00233                           "frameset!!");
00234 
00235         KMO_TRY_ASSURE(cpl_frameset_count_tags(frameset, CUBE_SKY) == 1,
00236                        CPL_ERROR_FILE_NOT_FOUND,
00237                        "Exactly one CUBE_SKY frame is expected in frameset!");
00238 
00239         KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset, "kmo_sky_tweak") == 1,
00240                        CPL_ERROR_ILLEGAL_INPUT,
00241                        "Cannot identify RAW and CALIB frames!");
00242 
00243         tbsub = kmo_dfs_get_parameter_bool(parlist, "kmos.kmo_sky_tweak.tbsub");
00244         KMO_TRY_CHECK_ERROR_STATE();
00245 
00246         nr_object_frames = cpl_frameset_count_tags(frameset, CUBE_OBJECT);
00247         KMO_TRY_CHECK_ERROR_STATE();
00248 
00249         KMO_TRY_EXIT_IF_NULL(
00250             object_frames = cpl_malloc(nr_object_frames * sizeof(cpl_frame*)));
00251 
00252         for (ox = 0; ox < nr_object_frames; ox++) {
00253             if (ox == 0) {
00254                 KMO_TRY_EXIT_IF_NULL(
00255                     object_frames[ox] = cpl_frameset_find(frameset, CUBE_OBJECT));
00256             } else {
00257                 KMO_TRY_EXIT_IF_NULL(
00258                     object_frames[ox] =  cpl_frameset_find(frameset, NULL));
00259             }
00260             obj_fits_desc = kmo_identify_fits_header(cpl_frame_get_filename(object_frames[ox]));
00261             KMO_TRY_CHECK_ERROR_STATE_MSG("Provided object fits file doesn't seem to be "
00262                                           "in KMOS-format!");
00263             KMO_TRY_ASSURE(obj_fits_desc.fits_type == f3i_fits,
00264                            CPL_ERROR_ILLEGAL_INPUT,
00265                            "Provided object fits file hasn't correct data type "
00266                            "(KMOSTYPE must be F3I)!");
00267             kmo_free_fits_desc(&obj_fits_desc);
00268         }
00269 
00270         KMO_TRY_EXIT_IF_NULL(
00271             sky_frame = cpl_frameset_find(frameset, CUBE_SKY));
00272 
00273         sky_fits_desc = kmo_identify_fits_header(cpl_frame_get_filename(sky_frame));
00274         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided sky fits file doesn't seem to be "
00275                                       "in KMOS-format!");
00276 
00277         KMO_TRY_ASSURE(sky_fits_desc.fits_type == f3i_fits,
00278                        CPL_ERROR_ILLEGAL_INPUT,
00279                        "Provided sky fits file hasn't correct data type "
00280                        "(KMOSTYPE must be F3I)!");
00281 
00282         if (sky_fits_desc.ex_noise == TRUE) {
00283             nr_sky_devices = sky_fits_desc.nr_ext / 2;
00284         } else {
00285             nr_sky_devices = sky_fits_desc.nr_ext;
00286         }
00287 
00288         KMO_TRY_EXIT_IF_NULL(
00289             sky_fn = cpl_frame_get_filename(sky_frame));
00290 
00291         for (ox = 0; ox < nr_object_frames; ox++) {
00292             printf("ox: %d\n",ox);
00293             object_frame = object_frames[ox];
00294             obj_fits_desc = kmo_identify_fits_header(cpl_frame_get_filename(object_frame));
00295             KMO_TRY_CHECK_ERROR_STATE_MSG("Provided object fits file doesn't seem to be "
00296                                           "in KMOS-format!");
00297             if (obj_fits_desc.ex_noise == TRUE) {
00298                 nr_obj_devices = obj_fits_desc.nr_ext / 2;
00299             } else {
00300                 nr_obj_devices = obj_fits_desc.nr_ext;
00301             }
00302             KMO_TRY_ASSURE((nr_sky_devices == nr_obj_devices) || (nr_sky_devices == 1),
00303                            CPL_ERROR_ILLEGAL_INPUT,
00304                            "Number of extensions for the SKY frame must be either 1 "
00305                            "or the same as for OBJECT frame");
00306 
00307             KMO_TRY_EXIT_IF_NULL(
00308                 obj_fn = cpl_frame_get_filename(object_frame));
00309 
00310             KMO_TRY_EXIT_IF_NULL(
00311                 main_header = kmclipm_propertylist_load(obj_fn, 0));
00312 
00313             KMO_TRY_EXIT_IF_ERROR(
00314                 kmo_dfs_save_main_header(frameset, SKY_TWEAK, "", object_frame,
00315                                          main_header, parlist, cpl_func));
00316 
00317             for (ifu_nr = 1; ifu_nr <= nr_obj_devices; ifu_nr++) {
00318                 cpl_msg_info(cpl_func, "Processing IFU#: %d", ifu_nr);
00319 
00320                 if (nr_sky_devices == nr_obj_devices) {
00321                     index = kmo_identify_index(sky_fn, ifu_nr, FALSE);
00322                 } else {
00323                     index = kmo_identify_index(sky_fn, 1, FALSE);
00324                 }
00325 
00326                 KMO_TRY_CHECK_ERROR_STATE();
00327                 KMO_TRY_EXIT_IF_NULL(
00328                     sky_data = kmclipm_imagelist_load(sky_fn, CPL_TYPE_FLOAT, index));
00329 
00330                 index = kmo_identify_index(obj_fn, ifu_nr, FALSE);
00331                 KMO_TRY_CHECK_ERROR_STATE();
00332                 KMO_TRY_EXIT_IF_NULL(
00333                     sub_header = kmclipm_propertylist_load(obj_fn, index));
00334                 KMO_TRY_EXIT_IF_NULL(
00335                     obj_data = kmclipm_imagelist_load(obj_fn, CPL_TYPE_FLOAT, index));
00336 //                index = kmo_identify_index(obj_fn, ifu_nr, TRUE);
00337 //                KMO_TRY_CHECK_ERROR_STATE();
00338 //                KMO_TRY_EXIT_IF_NULL(
00339 //                    obj_noise = kmclipm_imagelist_load(obj_fn, CPL_TYPE_FLOAT, index));
00340 
00341                 KMO_TRY_EXIT_IF_NULL(
00342                     tweaked_data = kmo_priv_sky_tweak(obj_data, sky_data, sub_header, .3, tbsub));
00343 
00344                 KMO_TRY_EXIT_IF_ERROR(
00345                     kmo_dfs_save_cube(tweaked_data, SKY_TWEAK, "", sub_header, 0./0.));
00346 
00347                 cpl_propertylist_delete(sub_header); sub_header = NULL;
00348                 cpl_imagelist_delete(obj_data); obj_data = NULL;
00349                 cpl_imagelist_delete(sky_data); sky_data = NULL;
00350                 cpl_imagelist_delete(tweaked_data); tweaked_data = NULL;
00351             }
00352 
00353             kmo_free_fits_desc(&obj_fits_desc);
00354             cpl_propertylist_delete(main_header); main_header = NULL;
00355         }
00356         kmo_free_fits_desc(&sky_fits_desc);
00357         cpl_free(object_frames); object_frames = NULL;
00358     }
00359     KMO_CATCH
00360     {
00361         KMO_CATCH_MSG();
00362         ret_val = -1;
00363     }
00364 
00365 
00366     return ret_val;
00367 }
00368