HAWKI Pipeline Reference Manual 1.8.6
hawki_step_subtract_bkg.c
00001 /* $Id: hawki_step_subtract_bkg.c,v 1.17 2011/10/24 10:41:43 cgarcia Exp $
00002  *
00003  * This file is part of the HAWKI Pipeline
00004  * Copyright (C) 2008 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: cgarcia $
00023  * $Date: 2011/10/24 10:41:43 $
00024  * $Revision: 1.17 $
00025  * $Name: hawki-1_8_6 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039 
00040 #include "irplib_utils.h"
00041 #include "hawki_utils.h"
00042 #include "hawki_load.h"
00043 #include "hawki_save.h"
00044 #include "hawki_pfits.h"
00045 #include "hawki_dfs.h"
00046 #include "hawki_calib.h"
00047 
00048 /*-----------------------------------------------------------------------------
00049                                 Structs
00050  -----------------------------------------------------------------------------*/
00051 
00052 /*-----------------------------------------------------------------------------
00053                             Functions prototypes
00054  -----------------------------------------------------------------------------*/
00055 
00056 static int hawki_step_subtract_bkg_create(cpl_plugin *) ;
00057 static int hawki_step_subtract_bkg_exec(cpl_plugin *) ;
00058 static int hawki_step_subtract_bkg_destroy(cpl_plugin *) ;
00059 static int hawki_step_subtract_bkg(cpl_parameterlist *, cpl_frameset *) ;
00060 
00061 static int hawki_step_subtract_bkg_apply_one_to_one_save
00062 (cpl_frameset        *  objframes,
00063  cpl_frameset        *  bkgframes,
00064  cpl_parameterlist   *  parlist,
00065  cpl_frameset        *  recipe_framelist);
00066 
00067 static int hawki_step_subtract_bkg_apply_one_to_all_save
00068 (cpl_frameset        *  objframes,
00069  cpl_frameset        *  bkgframes,
00070  cpl_parameterlist   *  recipe_parlist,
00071  cpl_frameset        *  recipe_framelist);
00072 
00073 static int hawki_step_subtract_bkg_save
00074 (cpl_imagelist     *  obj_images,
00075  int                  iserie,
00076  cpl_frameset      *  used_frameset,
00077  cpl_parameterlist *  recipe_parlist,
00078  cpl_frameset      *  recipe_framelist);
00079 
00080 /*-----------------------------------------------------------------------------
00081                             Static variables
00082  -----------------------------------------------------------------------------*/
00083 
00084 static char hawki_step_subtract_bkg_description[] =
00085 "hawki_step_subtract_bkg -- hawki background subtraction utility.\n"
00086 "This recipe will subtract the given background to the science images.\n"
00087 "The background can be obtained from the sky or object images\n"
00088 "using the hawki_util_compute_bkg utility.\n"
00089 "There are two modes of operation:\n"
00090 "One single background image that it is subtracted to all object images.\n"
00091 "As many background images as objects. A one to one relationship is applied.\n"
00092 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00093 "obj_basic_cal-file.fits "HAWKI_CALPRO_BASICCALIBRATED" or\n"
00094 "background-file.fits "HAWKI_CALPRO_BKGIMAGE" \n";
00095 
00096 /*-----------------------------------------------------------------------------
00097                                 Functions code
00098  -----------------------------------------------------------------------------*/
00099 
00100 /*----------------------------------------------------------------------------*/
00108 /*----------------------------------------------------------------------------*/
00109 int cpl_plugin_get_info(cpl_pluginlist * list)
00110 {
00111     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00112     cpl_plugin  *   plugin = &recipe->interface ;
00113 
00114     cpl_plugin_init(plugin,
00115                     CPL_PLUGIN_API,
00116                     HAWKI_BINARY_VERSION,
00117                     CPL_PLUGIN_TYPE_RECIPE,
00118                     "hawki_step_subtract_bkg",
00119                     "Background subtraction utility",
00120                     hawki_step_subtract_bkg_description,
00121                     "Cesar Enrique Garcia Dabo",
00122                     PACKAGE_BUGREPORT,  
00123                     hawki_get_license(),
00124                     hawki_step_subtract_bkg_create,
00125                     hawki_step_subtract_bkg_exec,
00126                     hawki_step_subtract_bkg_destroy) ;
00127 
00128     cpl_pluginlist_append(list, plugin) ;
00129 
00130     return 0;
00131 }
00132 
00133 /*----------------------------------------------------------------------------*/
00142 /*----------------------------------------------------------------------------*/
00143 static int hawki_step_subtract_bkg_create(cpl_plugin * plugin)
00144 {
00145     cpl_recipe      * recipe ;
00146     //cpl_parameter   * p ;
00147 
00148     /* Get the recipe out of the plugin */
00149     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00150         recipe = (cpl_recipe *)plugin ;
00151     else return -1 ;
00152 
00153     /* Create the parameters list in the cpl_recipe object */
00154     recipe->parameters = cpl_parameterlist_new() ;
00155     if (recipe->parameters == NULL)
00156         return 1;
00157 
00158     /* Fill the parameters list */
00159     /* None.. */
00160 
00161     /* Return */
00162     return 0;
00163 }
00164 
00165 /*----------------------------------------------------------------------------*/
00171 /*----------------------------------------------------------------------------*/
00172 static int hawki_step_subtract_bkg_exec(cpl_plugin * plugin)
00173 {
00174     cpl_recipe  *   recipe ;
00175 
00176     /* Get the recipe out of the plugin */
00177     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00178         recipe = (cpl_recipe *)plugin ;
00179     else return -1 ;
00180 
00181     /* Issue a banner */
00182     hawki_print_banner();
00183 
00184     return hawki_step_subtract_bkg(recipe->parameters, recipe->frames) ;
00185 }
00186 
00187 /*----------------------------------------------------------------------------*/
00193 /*----------------------------------------------------------------------------*/
00194 static int hawki_step_subtract_bkg_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 /*----------------------------------------------------------------------------*/
00214 /*----------------------------------------------------------------------------*/
00215 static int hawki_step_subtract_bkg(
00216         cpl_parameterlist   *   parlist,
00217         cpl_frameset        *   framelist)
00218 {
00219     int                 nobj;
00220     int                 nbkg;
00221     cpl_frameset    *   objframes;
00222     cpl_frameset    *   bkgframes;
00223 
00224 
00225     /* Identify the RAW and CALIB frames in the input frameset */
00226     if (hawki_dfs_set_groups(framelist))
00227     {
00228         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00229         return -1 ;
00230     }
00231 
00232     /* Identifying objects and bkg data frames */
00233     cpl_msg_info(__func__, "Identifying objects and background data");
00234     objframes = hawki_extract_frameset
00235         (framelist, HAWKI_CALPRO_BASICCALIBRATED);
00236     if (objframes == NULL)
00237     {
00238         cpl_msg_error(__func__, "No object frames provided (%s)",
00239                 HAWKI_CALPRO_BASICCALIBRATED);
00240         return -1 ;
00241     }
00242     /* Retrieve bkg frames */
00243     bkgframes = hawki_extract_frameset
00244         (framelist, HAWKI_CALPRO_BKGIMAGE);
00245     if (bkgframes == NULL)
00246     {
00247         cpl_msg_error(__func__, "No background frames provided (%s)",
00248                 HAWKI_CALPRO_BKGIMAGE);
00249         cpl_frameset_delete(objframes);
00250         return -1 ;
00251     }
00252 
00253     /* Subtract the background */
00254     nobj = cpl_frameset_get_size(objframes);
00255     nbkg = cpl_frameset_get_size(bkgframes);
00256     if(nobj == nbkg)
00257         hawki_step_subtract_bkg_apply_one_to_one_save
00258             (objframes, bkgframes, parlist, framelist);
00259     else if(nbkg == 1)
00260         hawki_step_subtract_bkg_apply_one_to_all_save
00261             (objframes, bkgframes, parlist, framelist);
00262     else
00263     {
00264         cpl_msg_error(__func__,"Incompatible number of science and background"
00265                                " images.");
00266         cpl_msg_error(__func__,"Supply only 1 bkg frame or as many as objects");
00267         cpl_frameset_delete(objframes);
00268         cpl_frameset_delete(bkgframes);
00269         return -1;
00270     }
00271 
00272     /* Free resources */
00273     cpl_frameset_delete(objframes);
00274     cpl_frameset_delete(bkgframes);
00275 
00276     /* Return */
00277     if (cpl_error_get_code())
00278     {
00279         cpl_msg_error(__func__,
00280                       "HAWK-I pipeline could not recover from previous errors");
00281         return -1 ;
00282     }
00283     else return 0 ;
00284 }
00285 
00286 /*----------------------------------------------------------------------------*/
00295 /*----------------------------------------------------------------------------*/
00296 static int hawki_step_subtract_bkg_apply_one_to_one_save
00297 (cpl_frameset        *  objframes,
00298  cpl_frameset        *  bkgframes,
00299  cpl_parameterlist   *  recipe_parlist,
00300  cpl_frameset        *  recipe_framelist)
00301 {
00302     int          iobj;
00303     int          nobjs;
00304 
00305     /* Subtract the background to each object frame */
00306     cpl_msg_info(__func__,"Using a one to one relation btw objects and bkgs");
00307     nobjs = cpl_frameset_get_size(objframes);
00308     for(iobj = 0; iobj < nobjs; ++iobj)
00309     {
00310         cpl_frame     * obj_frame = NULL;
00311         cpl_frame     * bkg_frame = NULL;
00312         cpl_imagelist * obj_images = NULL;
00313         cpl_imagelist * bkg_images = NULL;
00314         cpl_frameset  * used_frameset;
00315 
00316         /* Allocate resources */
00317         used_frameset = cpl_frameset_new();
00318 
00319         /* Read the object frame */
00320         cpl_msg_indent_more();
00321         cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00322         obj_frame = cpl_frameset_get_frame(objframes, iobj);
00323         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00324         if(obj_frame != NULL)
00325             obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00326         if(obj_images == NULL)
00327         {
00328             cpl_msg_indent_less();
00329             cpl_msg_error(__func__, "Error reading obj image") ;
00330             cpl_frameset_delete(used_frameset);
00331             return -1;
00332         }
00333 
00334         /* Read the bkg */
00335         bkg_frame = cpl_frameset_get_frame(bkgframes, iobj);
00336         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00337         if(bkg_frame != NULL)
00338             bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00339         if(bkg_images == NULL)
00340         {
00341             cpl_msg_error(__func__, "Error reading background image") ;
00342             cpl_msg_indent_less();
00343             cpl_imagelist_delete(obj_images);
00344             cpl_frameset_delete(used_frameset);
00345             return -1;
00346         }
00347 
00348         /* Make the correction */
00349         hawki_bkg_imglist_calib(obj_images, bkg_images);
00350 
00351         /* Save the subtracted frame */
00352         if(hawki_step_subtract_bkg_save(obj_images,
00353                                         iobj,
00354                                         used_frameset,
00355                                         recipe_parlist,
00356                                         recipe_framelist) != 0)
00357             cpl_msg_warning(__func__,"Some data could not be saved. "
00358                             "Check permisions or disk space");
00359 
00360         /* Free in loop */
00361         cpl_msg_indent_less();
00362         cpl_imagelist_delete(obj_images);
00363         cpl_imagelist_delete(bkg_images);
00364         cpl_frameset_delete(used_frameset);
00365     }
00366 
00367     /* Exit */
00368     return 0;
00369 }
00370 
00371 /*----------------------------------------------------------------------------*/
00380 /*----------------------------------------------------------------------------*/
00381 static int hawki_step_subtract_bkg_apply_one_to_all_save
00382 (cpl_frameset        *  objframes,
00383  cpl_frameset        *  bkgframes,
00384  cpl_parameterlist   *  recipe_parlist,
00385  cpl_frameset        *  recipe_framelist)
00386 {
00387     int             iobj;
00388     int             nobjs;
00389     cpl_frame     * bkg_frame;
00390     cpl_imagelist * bkg_images = NULL;
00391 
00392     /* Read the bkg */
00393     cpl_msg_info(__func__,"Using the same bkg for all the objects");
00394     bkg_frame = cpl_frameset_get_first(bkgframes);
00395     if(bkg_frame != NULL)
00396         bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00397     if(bkg_images == NULL)
00398     {
00399         cpl_msg_error(__func__, "Error reading background image");
00400         return -1;
00401     }
00402 
00403     /* Subtract the background to each object frame */
00404     nobjs = cpl_frameset_get_size(objframes);
00405     for(iobj = 0; iobj < nobjs; ++iobj)
00406     {
00407         cpl_frame     * obj_frame;
00408         cpl_imagelist * obj_images = NULL;
00409         cpl_frameset  * used_frameset;
00410 
00411         /* Allocate resources */
00412         used_frameset = cpl_frameset_new();
00413 
00414         /* Read the object frame */
00415         cpl_msg_indent_more();
00416         cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00417         obj_frame = cpl_frameset_get_frame(objframes, iobj);
00418         if(obj_frame != NULL)
00419             obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00420         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00421         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00422         if(obj_images == NULL)
00423         {
00424             cpl_msg_indent_less();
00425             cpl_msg_error(__func__, "Error reading obj image") ;
00426             cpl_frameset_delete(used_frameset);
00427             return -1;
00428         }
00429 
00430         /* Make the correction */
00431         hawki_bkg_imglist_calib(obj_images, bkg_images);
00432 
00433         /* Save the subtracted frame */
00434         hawki_step_subtract_bkg_save(obj_images,
00435                                      iobj,
00436                                      used_frameset,
00437                                      recipe_parlist,
00438                                      recipe_framelist);
00439 
00440         /* Free in loop */
00441         cpl_msg_indent_less();
00442         cpl_imagelist_delete(obj_images);
00443         cpl_frameset_delete(used_frameset);
00444     }
00445 
00446     /* Free and return */
00447     cpl_imagelist_delete(bkg_images);
00448     return 0;
00449 }
00450 
00451 /*----------------------------------------------------------------------------*/
00461 /*----------------------------------------------------------------------------*/
00462 static int hawki_step_subtract_bkg_save
00463 (cpl_imagelist     *  obj_images,
00464  int                  iserie,
00465  cpl_frameset      *  used_frameset,
00466  cpl_parameterlist *  recipe_parlist,
00467  cpl_frameset      *  recipe_framelist)
00468 {
00469     const cpl_frame     *   raw_reference;
00470     cpl_propertylist    **  extproplists;
00471     char                    filename[256] ;
00472     cpl_propertylist    *   inputlist ;
00473     int                     ext_nb ;
00474     const char          *   recipe_name = "hawki_step_subtract_bkg";
00475     int                     idet;
00476     cpl_errorstate          error_prevstate = cpl_errorstate_get();
00477 
00478     /* Get the reference frame (the raw frame) */
00479     raw_reference = irplib_frameset_get_first_from_group
00480         (used_frameset, CPL_FRAME_GROUP_RAW);
00481 
00482     /* Create the prop lists */
00483     cpl_msg_indent_more();
00484     cpl_msg_info(__func__, "Creating the keywords list") ;
00485     extproplists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00486     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00487     {
00488         /* Get the extension number */
00489         ext_nb=hawki_get_ext_from_detector
00490             (cpl_frame_get_filename(raw_reference), idet+1);
00491 
00492         /* Allocate this property list */
00493         extproplists[idet] = cpl_propertylist_new();
00494 
00495         /* Propagate some keywords from input raw frame extensions */
00496         inputlist = cpl_propertylist_load_regexp(
00497                 cpl_frame_get_filename(raw_reference), ext_nb,
00498                 HAWKI_HEADER_EXT_FORWARD, 0);
00499         cpl_propertylist_append(extproplists[idet], inputlist);
00500         cpl_propertylist_delete(inputlist);
00501         inputlist = cpl_propertylist_load_regexp(
00502                 cpl_frame_get_filename(raw_reference), ext_nb,
00503                 HAWKI_HEADER_WCS, 0);
00504         cpl_propertylist_append(extproplists[idet], inputlist);
00505         cpl_propertylist_delete(inputlist);
00506     }
00507 
00508     /* Write the image */
00509     snprintf(filename, 256, "hawki_step_subtract_bkg_%03d.fits", iserie+1);
00510     hawki_imagelist_save(recipe_framelist,
00511                          recipe_parlist,
00512                          used_frameset,
00513                          obj_images,
00514                          recipe_name,
00515                          HAWKI_CALPRO_BKG_SUBTRACTED,
00516                          HAWKI_PROTYPE_BKG_SUBTRACTED,
00517                          NULL,
00518                          (const cpl_propertylist**)extproplists,
00519                          filename);
00520 
00521     /* Free and return */
00522     cpl_msg_indent_less();
00523     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00524     {
00525         cpl_propertylist_delete(extproplists[idet]) ;
00526     }
00527     cpl_free(extproplists) ;
00528     if(!cpl_errorstate_is_equal(error_prevstate))
00529     {
00530         cpl_errorstate_set(CPL_ERROR_NONE);
00531         return -1;
00532     }
00533     return  0;
00534 }