KMOS Pipeline Reference Manual  1.3.17
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     /* --discard_subband */
00150     p = cpl_parameter_new_value("kmos.kmos_sky_tweak.discard_subband",
00151             CPL_TYPE_BOOL, "Ignore last sub-band in the sky tweaking",
00152             "kmos.kmos_sky_tweak", FALSE);
00153     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "discard_subband");
00154     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00155     cpl_parameterlist_append(recipe->parameters, p);
00156 
00157     /* --tbsub */
00158     p = cpl_parameter_new_value("kmos.kmos_sky_tweak.tbsub", CPL_TYPE_BOOL,
00159             "Subtract thermal background from input cube."
00160             "(TRUE (apply) or FALSE (don't apply)",
00161             "kmos.kmos_sky_tweak", TRUE);
00162     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "tbsub");
00163     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00164     cpl_parameterlist_append(recipe->parameters, p);
00165     return 0;
00166 }
00167 
00168 /*----------------------------------------------------------------------------*/
00174 /*----------------------------------------------------------------------------*/
00175 static int kmos_sky_tweak_exec(cpl_plugin *plugin)
00176 {
00177     cpl_recipe  *recipe;
00178 
00179     /* Get the recipe out of the plugin */
00180     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00181         recipe = (cpl_recipe *)plugin;
00182     else return -1 ;
00183 
00184     return kmos_sky_tweak(recipe->parameters, recipe->frames);
00185 }
00186 
00187 /*----------------------------------------------------------------------------*/
00193 /*----------------------------------------------------------------------------*/
00194 static int kmos_sky_tweak_destroy(cpl_plugin *plugin)
00195 {
00196     cpl_recipe *recipe;
00197 
00198     /* Get the recipe out of the plugin */
00199     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00200         recipe = (cpl_recipe *)plugin;
00201     else return -1 ;
00202 
00203     cpl_parameterlist_delete(recipe->parameters);
00204     return 0 ;
00205 }
00206 
00207 /*----------------------------------------------------------------------------*/
00220 /*----------------------------------------------------------------------------*/
00221 static int kmos_sky_tweak(
00222         cpl_parameterlist   *   parlist, 
00223         cpl_frameset        *   frameset)
00224 {
00225     const cpl_parameter *   par ;
00226     int                     ox, nr_object_frames, nr_obj_devices,
00227                             nr_sky_devices, ifu_nr, sky_index,
00228                             obj_index, tbsub, reduce_ifu, discard_subband ;
00229     const char          *   obj_fn ;
00230     const char          *   sky_fn ;
00231     cpl_frame           **  object_frames ;
00232     cpl_frame           *   object_frame ;
00233     cpl_frame           *   sky_frame ;
00234     cpl_imagelist       *   obj_data ;
00235     cpl_imagelist       *   sky_data ;
00236     cpl_imagelist       *   tweaked_data ;
00237     cpl_propertylist    *   main_header ;
00238     cpl_propertylist    *   sub_header ;
00239     main_fits_desc          obj_fits_desc,
00240                             sky_fits_desc;
00241 
00242     /* Check entries */
00243     if (parlist == NULL || frameset == NULL) {
00244         cpl_msg_error(__func__, "Null Inputs") ;
00245         cpl_error_set(__func__, CPL_ERROR_NULL_INPUT) ;
00246         return -1 ;
00247     }
00248     if (cpl_frameset_count_tags(frameset, CUBE_OBJECT) == 0 && 
00249             cpl_frameset_count_tags(frameset, CUBE_SKY) == 0) {
00250         cpl_msg_error(__func__, "Missing Inputs") ;
00251         cpl_error_set(__func__, CPL_ERROR_FILE_NOT_FOUND) ;
00252         return -1 ;
00253     }
00254     if (cpl_frameset_count_tags(frameset, CUBE_SKY) != 1) {
00255         cpl_msg_error(__func__, "one CUBE_SKY is expected") ;
00256         cpl_error_set(__func__, CPL_ERROR_FILE_NOT_FOUND) ;
00257         return -1 ;
00258     }
00259 
00260     /* Initialise */
00261     nr_obj_devices = nr_sky_devices = sky_index = obj_index = 0 ;
00262 
00263     /* Identify the RAW and CALIB frames in the input frameset */
00264     if (kmo_dfs_set_groups(frameset, "kmos_sky_tweak") != 1) {
00265         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00266         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00267         return -1 ;
00268     }
00269 
00270     /* Get Parameters */
00271     par = cpl_parameterlist_find_const(parlist, "kmos.kmos_sky_tweak.tbsub");
00272     tbsub = cpl_parameter_get_bool(par);
00273     par = cpl_parameterlist_find_const(parlist, "kmos.kmos_sky_tweak.ifu");
00274     reduce_ifu = cpl_parameter_get_int(par);
00275     par = cpl_parameterlist_find_const(parlist, 
00276             "kmos.kmos_sky_tweak.discard_subband");
00277     discard_subband = cpl_parameter_get_bool(par);
00278 
00279     nr_object_frames = cpl_frameset_count_tags(frameset, CUBE_OBJECT);
00280     object_frames = cpl_malloc(nr_object_frames * sizeof(cpl_frame*));
00281 
00282     for (ox = 0; ox < nr_object_frames; ox++) {
00283         if (ox == 0) {
00284             object_frames[ox] = cpl_frameset_find(frameset, CUBE_OBJECT);
00285         } else {
00286             object_frames[ox] = cpl_frameset_find(frameset, NULL);
00287         }
00288     }
00289 
00290     sky_frame = cpl_frameset_find(frameset, CUBE_SKY);
00291     sky_fits_desc = kmo_identify_fits_header(
00292             cpl_frame_get_filename(sky_frame));
00293     if (sky_fits_desc.ex_noise == TRUE) nr_sky_devices = sky_fits_desc.nr_ext/2;
00294     else                                nr_sky_devices = sky_fits_desc.nr_ext;
00295 
00296     sky_fn = cpl_frame_get_filename(sky_frame);
00297 
00298     for (ox = 0; ox < nr_object_frames; ox++) {
00299         object_frame = object_frames[ox];
00300         obj_fits_desc = kmo_identify_fits_header(
00301                 cpl_frame_get_filename(object_frame));
00302         if (obj_fits_desc.ex_noise == TRUE) 
00303             nr_obj_devices = obj_fits_desc.nr_ext / 2;
00304         else 
00305             nr_obj_devices = obj_fits_desc.nr_ext;
00306         if (nr_sky_devices != nr_obj_devices && nr_sky_devices != 1) {
00307             cpl_msg_error(__func__,  "SKY frame ext nr must be 1 or match OBJ");
00308             return -1 ;
00309         }
00310 
00311         obj_fn = cpl_frame_get_filename(object_frame);
00312         main_header = kmclipm_propertylist_load(obj_fn, 0);
00313 
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             /* Compute only one ifu */
00319             if (reduce_ifu != 0 && reduce_ifu != ifu_nr) continue ;
00320 
00321             cpl_msg_info(__func__, "Processing IFU#: %d", ifu_nr);
00322 
00323             /* Get sky index */
00324             if (nr_sky_devices == nr_obj_devices) {
00325                 sky_index = kmo_identify_index(sky_fn, ifu_nr, FALSE);
00326             } else {
00327                 sky_index = kmo_identify_index(sky_fn, 1, FALSE);
00328             }
00329             
00330             /* Get Object index */
00331             obj_index = kmo_identify_index(obj_fn, ifu_nr, FALSE);
00332 
00333             /* Load Object header */
00334             sub_header = kmclipm_propertylist_load(obj_fn, obj_index);
00335                             
00336             /* Only reduce valid IFUs */
00337             if (obj_fits_desc.sub_desc[obj_index-1].valid_data &&
00338                     sky_fits_desc.sub_desc[sky_index-1].valid_data) {
00339                 /* Load sky and object */
00340                 sky_data = kmclipm_imagelist_load(sky_fn, CPL_TYPE_FLOAT, 
00341                         sky_index);
00342                 obj_data=kmclipm_imagelist_load(obj_fn, CPL_TYPE_FLOAT,
00343                         obj_index);
00344                 tweaked_data = kmos_priv_sky_tweak(obj_data, sky_data, 
00345                         sub_header, .3, tbsub, ifu_nr, discard_subband);
00346                 cpl_imagelist_delete(obj_data);
00347                 cpl_imagelist_delete(sky_data);
00348             } else {
00349                 /* Empty IFU */
00350                 cpl_msg_warning(__func__, "Empty IFU - skip") ;
00351                 tweaked_data = NULL ;
00352             } 
00353 
00354             kmo_dfs_save_cube(tweaked_data, SKY_TWEAK, "", sub_header, 0./0.);
00355 
00356             cpl_propertylist_delete(sub_header); 
00357             if (tweaked_data != NULL) cpl_imagelist_delete(tweaked_data);
00358         }
00359 
00360         kmo_free_fits_desc(&obj_fits_desc);
00361         cpl_propertylist_delete(main_header);
00362     }
00363     kmo_free_fits_desc(&sky_fits_desc);
00364     cpl_free(object_frames);
00365     return 0;
00366 }
00367