KMOS Pipeline Reference Manual  1.2.3
kmo_sky_tweak.c
00001 /* $Id: kmo_sky_tweak.c,v 1.3 2013/08/02 16:22:44 aagudo 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: aagudo $
00023  * $Date: 2013/08/02 16:22:44 $
00024  * $Revision: 1.3 $
00025  * $Name: HEAD $
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 "NO PARAMETERS\n"
00054 "\n"
00055 "-------------------------------------------------------------------------------\n"
00056 "  Input files:\n"
00057 "\n"
00058 "   DO                    KMOS                                                  \n"
00059 "   category              Type   Explanation                    Required #Frames\n"
00060 "   --------              -----  -----------                    -------- -------\n"
00061 "   CUBE_OBJECT           F3I    object cubes                       Y      >=1  \n"
00062 "   CUBE_SKY              F3I    sky cube                           Y       1   \n"
00063 "\n"
00064 "  Output files:\n"
00065 "\n"
00066 "   DO                    KMOS\n"
00067 "   category              Type   Explanation\n"
00068 "   --------              -----  -----------\n"
00069 "   OBJECT_S              F3I    Corrected object cubes\n"
00070 "-------------------------------------------------------------------------------\n"
00071 "\n";
00072 
00089 int cpl_plugin_get_info(cpl_pluginlist *list)
00090 {
00091     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00092     cpl_plugin *plugin = &recipe->interface;
00093 
00094     cpl_plugin_init(plugin,
00095                         CPL_PLUGIN_API,
00096                         KMOS_BINARY_VERSION,
00097                         CPL_PLUGIN_TYPE_RECIPE,
00098                         "kmo_sky_tweak",
00099                         "Removal of OH sky lines",
00100                         kmo_sky_tweak_description,
00101                         "Erich Wiezorrek",
00102                         "kmos-spark@mpe.mpg.de",
00103                         kmos_get_license(),
00104                         kmo_sky_tweak_create,
00105                         kmo_sky_tweak_exec,
00106                         kmo_sky_tweak_destroy);
00107 
00108     cpl_pluginlist_append(list, plugin);
00109 
00110     return 0;
00111 }
00112 
00120 static int kmo_sky_tweak_create(cpl_plugin *plugin)
00121 {
00122     cpl_recipe *recipe;
00123 //    cpl_parameter *p;
00124 
00125     /* Check that the plugin is part of a valid recipe */
00126     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00127         recipe = (cpl_recipe *)plugin;
00128     else
00129         return -1;
00130 
00131     /* Create the parameters list in the cpl_recipe object */
00132     recipe->parameters = cpl_parameterlist_new();
00133 
00134     /* Fill the parameters list */
00135 
00136     return 0;
00137 
00138 }
00139 
00145 static int kmo_sky_tweak_exec(cpl_plugin *plugin)
00146 {
00147     cpl_recipe  *recipe;
00148 
00149     /* Get the recipe out of the plugin */
00150     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00151         recipe = (cpl_recipe *)plugin;
00152     else return -1 ;
00153 
00154     return kmo_sky_tweak(recipe->parameters, recipe->frames);
00155 }
00156 
00162 static int kmo_sky_tweak_destroy(cpl_plugin *plugin)
00163 {
00164     cpl_recipe *recipe;
00165 
00166     /* Get the recipe out of the plugin */
00167     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00168         recipe = (cpl_recipe *)plugin;
00169     else return -1 ;
00170 
00171     cpl_parameterlist_delete(recipe->parameters);
00172     return 0 ;
00173 }
00174 
00189 static int kmo_sky_tweak(cpl_parameterlist *parlist, cpl_frameset *frameset)
00190 {
00191     int              ret_val                = 0;
00192 
00193     int              ox                     = 0,
00194                      nr_object_frames       = 0,
00195                      nr_obj_devices         = 0,
00196                      nr_sky_devices         = 0,
00197                      ifu_nr                 = 0,
00198                      index                  = 0;
00199     const char       *obj_fn                = NULL,
00200                      *sky_fn                = NULL;
00201 
00202     cpl_frame        **object_frames        = NULL,
00203                      *object_frame          = NULL,
00204                      *sky_frame             = NULL;
00205     cpl_imagelist    *obj_data              = NULL/*,
00206                      *obj_noise             = NULL*/,
00207                      *sky_data              = NULL,
00208                      *tweaked_data          = NULL/*,
00209                      *tweaked_noise         = NULL*/;
00210     cpl_propertylist *main_header           = NULL,
00211                      *sub_header            = NULL;
00212     main_fits_desc   obj_fits_desc,
00213                      sky_fits_desc;
00214 
00215     KMO_TRY
00216     {
00217         //
00218         // check frameset
00219         //
00220         KMO_TRY_ASSURE((parlist != NULL) &&
00221                        (frameset != NULL),
00222                        CPL_ERROR_NULL_INPUT,
00223                        "Not all input data is provided!");
00224 
00225         KMO_TRY_ASSURE(! ((cpl_frameset_count_tags(frameset, CUBE_OBJECT) == 0) &&
00226                           (cpl_frameset_count_tags(frameset, CUBE_SKY) == 0)    ),
00227                           CPL_ERROR_FILE_NOT_FOUND,
00228                           "CUBE_OBJECT or  CUBE_SKY frames missing in "
00229                           "frameset!!");
00230 
00231         KMO_TRY_ASSURE(cpl_frameset_count_tags(frameset, CUBE_SKY) == 1,
00232                        CPL_ERROR_FILE_NOT_FOUND,
00233                        "Exactly one CUBE_SKY frame is expected in frameset!");
00234 
00235         KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset, "kmo_sky_tweak") == 1,
00236                        CPL_ERROR_ILLEGAL_INPUT,
00237                        "Cannot identify RAW and CALIB frames!");
00238 
00239 
00240         nr_object_frames = cpl_frameset_count_tags(frameset, CUBE_OBJECT);
00241         KMO_TRY_CHECK_ERROR_STATE();
00242 
00243         KMO_TRY_EXIT_IF_NULL(
00244                 object_frames = cpl_malloc(nr_object_frames * sizeof(cpl_frame*)));
00245 
00246         for (ox=0; ox<nr_object_frames; ox++) {
00247             if (ox == 0) {
00248                 KMO_TRY_EXIT_IF_NULL(
00249                         object_frames[ox] = cpl_frameset_find(frameset, CUBE_OBJECT));
00250             } else {
00251                 KMO_TRY_EXIT_IF_NULL(
00252                         object_frames[ox] =  cpl_frameset_find(frameset, NULL));
00253             }
00254             obj_fits_desc = kmo_identify_fits_header(
00255                         cpl_frame_get_filename(object_frames[ox]));
00256             KMO_TRY_CHECK_ERROR_STATE_MSG("Provided object fits file doesn't seem to be "
00257                                           "in KMOS-format!");
00258             KMO_TRY_ASSURE(obj_fits_desc.fits_type == f3i_fits,
00259                            CPL_ERROR_ILLEGAL_INPUT,
00260                            "Provided object fits file hasn't correct data type "
00261                            "(KMOSTYPE must be F3I)!");
00262             kmo_free_fits_desc(&obj_fits_desc);
00263 
00264         }
00265 
00266         KMO_TRY_EXIT_IF_NULL(
00267                 sky_frame = cpl_frameset_find(frameset, CUBE_SKY));
00268         sky_fits_desc = kmo_identify_fits_header(
00269                     cpl_frame_get_filename(sky_frame));
00270         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided sky fits file doesn't seem to be "
00271                                       "in KMOS-format!");
00272         KMO_TRY_ASSURE(sky_fits_desc.fits_type == f3i_fits,
00273                        CPL_ERROR_ILLEGAL_INPUT,
00274                        "Provided sky fits file hasn't correct data type "
00275                        "(KMOSTYPE must be F3I)!");
00276         if (sky_fits_desc.ex_noise == TRUE) {
00277             nr_sky_devices = sky_fits_desc.nr_ext / 2;
00278         } else {
00279             nr_sky_devices = sky_fits_desc.nr_ext;
00280         }
00281         KMO_TRY_EXIT_IF_NULL(
00282             sky_fn = cpl_frame_get_filename(sky_frame));
00283 
00284         for (ox=0; ox<nr_object_frames; ox++) {
00285             printf("ox: %d\n",ox);
00286             object_frame = object_frames[ox];
00287             obj_fits_desc = kmo_identify_fits_header(cpl_frame_get_filename(object_frame));
00288             KMO_TRY_CHECK_ERROR_STATE_MSG("Provided object fits file doesn't seem to be "
00289                                           "in KMOS-format!");
00290             if (obj_fits_desc.ex_noise == TRUE) {
00291                 nr_obj_devices = obj_fits_desc.nr_ext / 2;
00292             } else {
00293                 nr_obj_devices = obj_fits_desc.nr_ext;
00294             }
00295             KMO_TRY_ASSURE((nr_sky_devices == nr_obj_devices) || (nr_sky_devices == 1),
00296                     CPL_ERROR_ILLEGAL_INPUT,
00297                     "Number of extensions for the SKY frame must be either 1"
00298                     " or the same as for OBJECT frame");
00299 
00300             KMO_TRY_EXIT_IF_NULL(
00301                 obj_fn = cpl_frame_get_filename(object_frame));
00302 
00303             KMO_TRY_EXIT_IF_NULL(
00304                 main_header = kmclipm_propertylist_load(obj_fn, 0));
00305 
00306             KMO_TRY_EXIT_IF_ERROR(
00307                 kmo_dfs_save_main_header(frameset, SKY_TWEAK, "",
00308                                          object_frame,
00309                                          main_header, parlist, cpl_func));
00310 
00311 
00312             for (ifu_nr = 1; ifu_nr <= nr_obj_devices; ifu_nr++) {
00313                 printf("ifu_nr: %d\n", ifu_nr);
00314                 if (nr_sky_devices == nr_obj_devices) {
00315                     index = kmo_identify_index(sky_fn, ifu_nr, FALSE);
00316                 } else {
00317                     index = kmo_identify_index(sky_fn, 1, FALSE);
00318                 }
00319                 KMO_TRY_CHECK_ERROR_STATE();
00320                 KMO_TRY_EXIT_IF_NULL(
00321                     sky_data = kmclipm_imagelist_load(sky_fn, CPL_TYPE_FLOAT, index));
00322 
00323                 index = kmo_identify_index(obj_fn, ifu_nr, FALSE);
00324                 KMO_TRY_CHECK_ERROR_STATE();
00325                 KMO_TRY_EXIT_IF_NULL(
00326                     sub_header = kmclipm_propertylist_load(obj_fn, index));
00327                 KMO_TRY_EXIT_IF_NULL(
00328                     obj_data = kmclipm_imagelist_load(obj_fn, CPL_TYPE_FLOAT, index));
00329 //                index = kmo_identify_index(obj_fn, ifu_nr, TRUE);
00330 //                KMO_TRY_CHECK_ERROR_STATE();
00331 //                KMO_TRY_EXIT_IF_NULL(
00332 //                    obj_noise = kmclipm_imagelist_load(obj_fn, CPL_TYPE_FLOAT, index));
00333 
00334                 KMO_TRY_EXIT_IF_NULL(
00335                         tweaked_data = kmo_priv_sky_tweak (obj_data, sky_data, sub_header, .3));
00336 
00337                 KMO_TRY_EXIT_IF_ERROR(
00338                     kmo_dfs_save_cube(tweaked_data, SKY_TWEAK, "", sub_header, 0./0.));
00339             }
00340 
00341             kmo_free_fits_desc(&obj_fits_desc);
00342 
00343         }
00344         kmo_free_fits_desc(&sky_fits_desc);
00345     }
00346     KMO_CATCH
00347     {
00348         KMO_CATCH_MSG();
00349         ret_val = -1;
00350     }
00351 
00352 
00353     return ret_val;
00354 }
00355