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