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