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