KMOS Pipeline Reference Manual  1.3.11
kmos_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 "kmos_priv_sky_tweak.h"
00035 
00036 /*-----------------------------------------------------------------------------
00037  *                          Functions prototypes
00038  *----------------------------------------------------------------------------*/
00039 
00040 static int kmos_sky_tweak_create(cpl_plugin *);
00041 static int kmos_sky_tweak_exec(cpl_plugin *);
00042 static int kmos_sky_tweak_destroy(cpl_plugin *);
00043 static int kmos_sky_tweak(cpl_parameterlist *, cpl_frameset *);
00044 
00045 /*-----------------------------------------------------------------------------
00046  *                          Static variables
00047  *----------------------------------------------------------------------------*/
00048 
00049 static char kmos_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 /*-----------------------------------------------------------------------------
00081  *                              Functions code
00082  *----------------------------------------------------------------------------*/
00083 
00084 /*----------------------------------------------------------------------------*/
00093 /*----------------------------------------------------------------------------*/
00094 int cpl_plugin_get_info(cpl_pluginlist *list)
00095 {
00096     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00097     cpl_plugin *plugin = &recipe->interface;
00098 
00099     cpl_plugin_init(plugin,
00100             CPL_PLUGIN_API,
00101             KMOS_BINARY_VERSION,
00102             CPL_PLUGIN_TYPE_RECIPE,
00103             "kmos_sky_tweak",
00104             "Removal of OH sky lines",
00105             kmos_sky_tweak_description,
00106             "Erich Wiezorrek, Yves Jung",
00107             "usd-help@eso.org",
00108             kmos_get_license(),
00109             kmos_sky_tweak_create,
00110             kmos_sky_tweak_exec,
00111             kmos_sky_tweak_destroy);
00112     cpl_pluginlist_append(list, plugin);
00113     return 0;
00114 }
00115 
00116 /*----------------------------------------------------------------------------*/
00124 /*----------------------------------------------------------------------------*/
00125 static int kmos_sky_tweak_create(cpl_plugin *plugin)
00126 {
00127     cpl_recipe *recipe;
00128     cpl_parameter *p;
00129 
00130     /* Check that the plugin is part of a valid recipe */
00131     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00132         recipe = (cpl_recipe *)plugin;
00133     else
00134         return -1;
00135 
00136     /* Create the parameters list in the cpl_recipe object */
00137     recipe->parameters = cpl_parameterlist_new();
00138 
00139     /* Fill the parameters list */
00140 
00141     /* --ifu */
00142     p = cpl_parameter_new_value("kmos.kmos_sky_tweak.ifu",
00143             CPL_TYPE_INT, "Only reduce the specified IFU",
00144             "kmos.kmos_sky_tweak", 0);
00145     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ifu");
00146     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00147     cpl_parameterlist_append(recipe->parameters, p);
00148 
00149 
00150     /* --tbsub */
00151     p = cpl_parameter_new_value("kmos.kmos_sky_tweak.tbsub", CPL_TYPE_BOOL,
00152             "Subtract thermal background from input cube."
00153             "(TRUE (apply) or FALSE (don't apply)",
00154             "kmos.kmos_sky_tweak", TRUE);
00155     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "tbsub");
00156     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00157     cpl_parameterlist_append(recipe->parameters, p);
00158     return 0;
00159 }
00160 
00161 /*----------------------------------------------------------------------------*/
00167 /*----------------------------------------------------------------------------*/
00168 static int kmos_sky_tweak_exec(cpl_plugin *plugin)
00169 {
00170     cpl_recipe  *recipe;
00171 
00172     /* Get the recipe out of the plugin */
00173     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00174         recipe = (cpl_recipe *)plugin;
00175     else return -1 ;
00176 
00177     return kmos_sky_tweak(recipe->parameters, recipe->frames);
00178 }
00179 
00180 /*----------------------------------------------------------------------------*/
00186 /*----------------------------------------------------------------------------*/
00187 static int kmos_sky_tweak_destroy(cpl_plugin *plugin)
00188 {
00189     cpl_recipe *recipe;
00190 
00191     /* Get the recipe out of the plugin */
00192     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00193         recipe = (cpl_recipe *)plugin;
00194     else return -1 ;
00195 
00196     cpl_parameterlist_delete(recipe->parameters);
00197     return 0 ;
00198 }
00199 
00200 /*----------------------------------------------------------------------------*/
00213 /*----------------------------------------------------------------------------*/
00214 static int kmos_sky_tweak(
00215         cpl_parameterlist   *   parlist, 
00216         cpl_frameset        *   frameset)
00217 {
00218     const cpl_parameter *   par ;
00219     int                     ox, nr_object_frames, nr_obj_devices,
00220                             nr_sky_devices, ifu_nr, sky_index,
00221                             obj_index, tbsub, reduce_ifu;
00222     const char          *   obj_fn ;
00223     const char          *   sky_fn ;
00224     cpl_frame           **  object_frames ;
00225     cpl_frame           *   object_frame ;
00226     cpl_frame           *   sky_frame ;
00227     cpl_imagelist       *   obj_data ;
00228     cpl_imagelist       *   sky_data ;
00229     cpl_imagelist       *   tweaked_data ;
00230     cpl_propertylist    *   main_header ;
00231     cpl_propertylist    *   sub_header ;
00232     main_fits_desc          obj_fits_desc,
00233                             sky_fits_desc;
00234 
00235     /* Check entries */
00236     if (parlist == NULL || frameset == NULL) {
00237         cpl_msg_error(__func__, "Null Inputs") ;
00238         cpl_error_set(__func__, CPL_ERROR_NULL_INPUT) ;
00239         return -1 ;
00240     }
00241     if (cpl_frameset_count_tags(frameset, CUBE_OBJECT) == 0 && 
00242             cpl_frameset_count_tags(frameset, CUBE_SKY) == 0) {
00243         cpl_msg_error(__func__, "Missing Inputs") ;
00244         cpl_error_set(__func__, CPL_ERROR_FILE_NOT_FOUND) ;
00245         return -1 ;
00246     }
00247     if (cpl_frameset_count_tags(frameset, CUBE_SKY) != 1) {
00248         cpl_msg_error(__func__, "one CUBE_SKY is expected") ;
00249         cpl_error_set(__func__, CPL_ERROR_FILE_NOT_FOUND) ;
00250         return -1 ;
00251     }
00252 
00253     /* Initialise */
00254     nr_obj_devices = nr_sky_devices = sky_index = obj_index = 0 ;
00255 
00256     /* Identify the RAW and CALIB frames in the input frameset */
00257     if (kmo_dfs_set_groups(frameset, "kmos_sky_tweak") != 1) {
00258         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00259         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00260         return -1 ;
00261     }
00262 
00263     /* Get Parameters */
00264     par = cpl_parameterlist_find_const(parlist, "kmos.kmos_sky_tweak.tbsub");
00265     tbsub = cpl_parameter_get_bool(par);
00266     par = cpl_parameterlist_find_const(parlist, "kmos.kmos_sky_tweak.ifu");
00267     reduce_ifu = cpl_parameter_get_int(par);
00268 
00269     nr_object_frames = cpl_frameset_count_tags(frameset, CUBE_OBJECT);
00270     object_frames = cpl_malloc(nr_object_frames * sizeof(cpl_frame*));
00271 
00272     for (ox = 0; ox < nr_object_frames; ox++) {
00273         if (ox == 0) {
00274             object_frames[ox] = cpl_frameset_find(frameset, CUBE_OBJECT);
00275         } else {
00276             object_frames[ox] = cpl_frameset_find(frameset, NULL);
00277         }
00278     }
00279 
00280     sky_frame = cpl_frameset_find(frameset, CUBE_SKY);
00281     sky_fits_desc = kmo_identify_fits_header(
00282             cpl_frame_get_filename(sky_frame));
00283     if (sky_fits_desc.ex_noise == TRUE) nr_sky_devices = sky_fits_desc.nr_ext/2;
00284     else                                nr_sky_devices = sky_fits_desc.nr_ext;
00285 
00286     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         if (obj_fits_desc.ex_noise == TRUE) 
00293             nr_obj_devices = obj_fits_desc.nr_ext / 2;
00294         else 
00295             nr_obj_devices = obj_fits_desc.nr_ext;
00296         if (nr_sky_devices != nr_obj_devices && nr_sky_devices != 1) {
00297             cpl_msg_error(__func__,  "SKY frame ext nr must be 1 or match OBJ");
00298             return -1 ;
00299         }
00300 
00301         obj_fn = cpl_frame_get_filename(object_frame);
00302         main_header = kmclipm_propertylist_load(obj_fn, 0);
00303 
00304         kmo_dfs_save_main_header(frameset, SKY_TWEAK, "", object_frame, 
00305                 main_header, parlist, cpl_func);
00306 
00307         for (ifu_nr = 1; ifu_nr <= nr_obj_devices; ifu_nr++) {
00308             /* Compute only one ifu */
00309             if (reduce_ifu != 0 && reduce_ifu != ifu_nr) continue ;
00310 
00311             cpl_msg_info(__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             /* Only reduce valid IFUs */
00327             if (obj_fits_desc.sub_desc[obj_index-1].valid_data &&
00328                     sky_fits_desc.sub_desc[sky_index-1].valid_data) {
00329                 /* Load sky and object */
00330                 sky_data = kmclipm_imagelist_load(sky_fn, CPL_TYPE_FLOAT, 
00331                         sky_index);
00332                 obj_data=kmclipm_imagelist_load(obj_fn, CPL_TYPE_FLOAT,
00333                         obj_index);
00334                 tweaked_data = kmos_priv_sky_tweak(obj_data, sky_data, 
00335                         sub_header, .3, tbsub, ifu_nr);
00336                 cpl_imagelist_delete(obj_data);
00337                 cpl_imagelist_delete(sky_data);
00338             } else {
00339                 /* Empty IFU */
00340                 cpl_msg_warning(__func__, "Empty IFU - skip") ;
00341                 tweaked_data = NULL ;
00342             } 
00343 
00344             kmo_dfs_save_cube(tweaked_data, SKY_TWEAK, "", sub_header, 0./0.);
00345 
00346             cpl_propertylist_delete(sub_header); 
00347             if (tweaked_data != NULL) cpl_imagelist_delete(tweaked_data);
00348         }
00349 
00350         kmo_free_fits_desc(&obj_fits_desc);
00351         cpl_propertylist_delete(main_header);
00352     }
00353     kmo_free_fits_desc(&sky_fits_desc);
00354     cpl_free(object_frames);
00355     return 0;
00356 }
00357