hawki_step_subtract_bkg.c

00001 /* $Id: hawki_step_subtract_bkg.c,v 1.16 2010/09/28 14:12:31 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: 2010/09/28 14:12:31 $
00024  * $Revision: 1.16 $
00025  * $Name: hawki-1_8_0 $
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 
00156     /* Fill the parameters list */
00157     /* None.. */
00158 
00159     /* Return */
00160     return 0;
00161 }
00162 
00163 /*----------------------------------------------------------------------------*/
00169 /*----------------------------------------------------------------------------*/
00170 static int hawki_step_subtract_bkg_exec(cpl_plugin * plugin)
00171 {
00172     cpl_recipe  *   recipe ;
00173 
00174     /* Get the recipe out of the plugin */
00175     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00176         recipe = (cpl_recipe *)plugin ;
00177     else return -1 ;
00178 
00179     /* Issue a banner */
00180     hawki_print_banner();
00181 
00182     return hawki_step_subtract_bkg(recipe->parameters, recipe->frames) ;
00183 }
00184 
00185 /*----------------------------------------------------------------------------*/
00191 /*----------------------------------------------------------------------------*/
00192 static int hawki_step_subtract_bkg_destroy(cpl_plugin * plugin)
00193 {
00194     cpl_recipe  *   recipe ;
00195 
00196     /* Get the recipe out of the plugin */
00197     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00198         recipe = (cpl_recipe *)plugin ;
00199     else return -1 ;
00200 
00201     cpl_parameterlist_delete(recipe->parameters) ;
00202     return 0 ;
00203 }
00204 
00205 /*----------------------------------------------------------------------------*/
00212 /*----------------------------------------------------------------------------*/
00213 static int hawki_step_subtract_bkg(
00214         cpl_parameterlist   *   parlist,
00215         cpl_frameset        *   framelist)
00216 {
00217     int                 nobj;
00218     int                 nbkg;
00219     cpl_frameset    *   objframes;
00220     cpl_frameset    *   bkgframes;
00221 
00222 
00223     /* Identify the RAW and CALIB frames in the input frameset */
00224     if (hawki_dfs_set_groups(framelist))
00225     {
00226         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00227         return -1 ;
00228     }
00229 
00230     /* Identifying objects and bkg data frames */
00231     cpl_msg_info(__func__, "Identifying objects and background data");
00232     objframes = hawki_extract_frameset
00233         (framelist, HAWKI_CALPRO_BASICCALIBRATED);
00234     if (objframes == NULL)
00235     {
00236         cpl_msg_error(__func__, "No object frames provided (%s)",
00237                 HAWKI_CALPRO_BASICCALIBRATED);
00238         return -1 ;
00239     }
00240     /* Retrieve bkg frames */
00241     bkgframes = hawki_extract_frameset
00242         (framelist, HAWKI_CALPRO_BKGIMAGE);
00243     if (bkgframes == NULL)
00244     {
00245         cpl_msg_error(__func__, "No background frames provided (%s)",
00246                 HAWKI_CALPRO_BKGIMAGE);
00247         cpl_frameset_delete(objframes);
00248         return -1 ;
00249     }
00250 
00251     /* Subtract the background */
00252     nobj = cpl_frameset_get_size(objframes);
00253     nbkg = cpl_frameset_get_size(bkgframes);
00254     if(nobj == nbkg)
00255         hawki_step_subtract_bkg_apply_one_to_one_save
00256             (objframes, bkgframes, parlist, framelist);
00257     else if(nbkg == 1)
00258         hawki_step_subtract_bkg_apply_one_to_all_save
00259             (objframes, bkgframes, parlist, framelist);
00260     else
00261     {
00262         cpl_msg_error(__func__,"Incompatible number of science and background"
00263                                " images.");
00264         cpl_msg_error(__func__,"Supply only 1 bkg frame or as many as objects");
00265         cpl_frameset_delete(objframes);
00266         cpl_frameset_delete(bkgframes);
00267         return -1;
00268     }
00269 
00270     /* Free resources */
00271     cpl_frameset_delete(objframes);
00272     cpl_frameset_delete(bkgframes);
00273 
00274     /* Return */
00275     if (cpl_error_get_code())
00276     {
00277         cpl_msg_error(__func__,
00278                       "HAWK-I pipeline could not recover from previous errors");
00279         return -1 ;
00280     }
00281     else return 0 ;
00282 }
00283 
00284 /*----------------------------------------------------------------------------*/
00293 /*----------------------------------------------------------------------------*/
00294 static int hawki_step_subtract_bkg_apply_one_to_one_save
00295 (cpl_frameset        *  objframes,
00296  cpl_frameset        *  bkgframes,
00297  cpl_parameterlist   *  recipe_parlist,
00298  cpl_frameset        *  recipe_framelist)
00299 {
00300     int          iobj;
00301     int          nobjs;
00302 
00303     /* Subtract the background to each object frame */
00304     cpl_msg_info(__func__,"Using a one to one relation btw objects and bkgs");
00305     nobjs = cpl_frameset_get_size(objframes);
00306     for(iobj = 0; iobj < nobjs; ++iobj)
00307     {
00308         cpl_frame     * obj_frame = NULL;
00309         cpl_frame     * bkg_frame = NULL;
00310         cpl_imagelist * obj_images = NULL;
00311         cpl_imagelist * bkg_images = NULL;
00312         cpl_frameset  * used_frameset;
00313 
00314         /* Allocate resources */
00315         used_frameset = cpl_frameset_new();
00316 
00317         /* Read the object frame */
00318         cpl_msg_indent_more();
00319         cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00320         obj_frame = cpl_frameset_get_frame(objframes, iobj);
00321         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00322         if(obj_frame != NULL)
00323             obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00324         if(obj_images == NULL)
00325         {
00326             cpl_msg_indent_less();
00327             cpl_msg_error(__func__, "Error reading obj image") ;
00328             cpl_frameset_delete(used_frameset);
00329             return -1;
00330         }
00331 
00332         /* Read the bkg */
00333         bkg_frame = cpl_frameset_get_frame(bkgframes, iobj);
00334         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00335         if(bkg_frame != NULL)
00336             bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00337         if(bkg_images == NULL)
00338         {
00339             cpl_msg_error(__func__, "Error reading background image") ;
00340             cpl_msg_indent_less();
00341             cpl_imagelist_delete(obj_images);
00342             cpl_frameset_delete(used_frameset);
00343             return -1;
00344         }
00345 
00346         /* Make the correction */
00347         hawki_bkg_imglist_calib(obj_images, bkg_images);
00348 
00349         /* Save the subtracted frame */
00350         if(hawki_step_subtract_bkg_save(obj_images,
00351                                         iobj,
00352                                         used_frameset,
00353                                         recipe_parlist,
00354                                         recipe_framelist) != 0)
00355             cpl_msg_warning(__func__,"Some data could not be saved. "
00356                             "Check permisions or disk space");
00357 
00358         /* Free in loop */
00359         cpl_msg_indent_less();
00360         cpl_imagelist_delete(obj_images);
00361         cpl_imagelist_delete(bkg_images);
00362         cpl_frameset_delete(used_frameset);
00363     }
00364 
00365     /* Exit */
00366     return 0;
00367 }
00368 
00369 /*----------------------------------------------------------------------------*/
00378 /*----------------------------------------------------------------------------*/
00379 static int hawki_step_subtract_bkg_apply_one_to_all_save
00380 (cpl_frameset        *  objframes,
00381  cpl_frameset        *  bkgframes,
00382  cpl_parameterlist   *  recipe_parlist,
00383  cpl_frameset        *  recipe_framelist)
00384 {
00385     int             iobj;
00386     int             nobjs;
00387     cpl_frame     * bkg_frame;
00388     cpl_imagelist * bkg_images = NULL;
00389 
00390     /* Read the bkg */
00391     cpl_msg_info(__func__,"Using the same bkg for all the objects");
00392     bkg_frame = cpl_frameset_get_first(bkgframes);
00393     if(bkg_frame != NULL)
00394         bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00395     if(bkg_images == NULL)
00396     {
00397         cpl_msg_error(__func__, "Error reading background image");
00398         return -1;
00399     }
00400 
00401     /* Subtract the background to each object frame */
00402     nobjs = cpl_frameset_get_size(objframes);
00403     for(iobj = 0; iobj < nobjs; ++iobj)
00404     {
00405         cpl_frame     * obj_frame;
00406         cpl_imagelist * obj_images = NULL;
00407         cpl_frameset  * used_frameset;
00408 
00409         /* Allocate resources */
00410         used_frameset = cpl_frameset_new();
00411 
00412         /* Read the object frame */
00413         cpl_msg_indent_more();
00414         cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00415         obj_frame = cpl_frameset_get_frame(objframes, iobj);
00416         if(obj_frame != NULL)
00417             obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00418         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00419         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00420         if(obj_images == NULL)
00421         {
00422             cpl_msg_indent_less();
00423             cpl_msg_error(__func__, "Error reading obj image") ;
00424             cpl_frameset_delete(used_frameset);
00425             return -1;
00426         }
00427 
00428         /* Make the correction */
00429         hawki_bkg_imglist_calib(obj_images, bkg_images);
00430 
00431         /* Save the subtracted frame */
00432         hawki_step_subtract_bkg_save(obj_images,
00433                                      iobj,
00434                                      used_frameset,
00435                                      recipe_parlist,
00436                                      recipe_framelist);
00437 
00438         /* Free in loop */
00439         cpl_msg_indent_less();
00440         cpl_imagelist_delete(obj_images);
00441         cpl_frameset_delete(used_frameset);
00442     }
00443 
00444     /* Free and return */
00445     cpl_imagelist_delete(bkg_images);
00446     return 0;
00447 }
00448 
00449 /*----------------------------------------------------------------------------*/
00459 /*----------------------------------------------------------------------------*/
00460 static int hawki_step_subtract_bkg_save
00461 (cpl_imagelist     *  obj_images,
00462  int                  iserie,
00463  cpl_frameset      *  used_frameset,
00464  cpl_parameterlist *  recipe_parlist,
00465  cpl_frameset      *  recipe_framelist)
00466 {
00467     const cpl_frame     *   raw_reference;
00468     cpl_propertylist    **  extproplists;
00469     char                    filename[256] ;
00470     cpl_propertylist    *   inputlist ;
00471     int                     ext_nb ;
00472     const char          *   recipe_name = "hawki_step_subtract_bkg";
00473     int                     idet;
00474     cpl_errorstate          error_prevstate = cpl_errorstate_get();
00475 
00476     /* Get the reference frame (the raw frame) */
00477     raw_reference = irplib_frameset_get_first_from_group
00478         (used_frameset, CPL_FRAME_GROUP_RAW);
00479 
00480     /* Create the prop lists */
00481     cpl_msg_indent_more();
00482     cpl_msg_info(__func__, "Creating the keywords list") ;
00483     extproplists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00484     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00485     {
00486         /* Get the extension number */
00487         ext_nb=hawki_get_ext_from_detector
00488             (cpl_frame_get_filename(raw_reference), idet+1);
00489 
00490         /* Allocate this property list */
00491         extproplists[idet] = cpl_propertylist_new();
00492 
00493         /* Propagate some keywords from input raw frame extensions */
00494         inputlist = cpl_propertylist_load_regexp(
00495                 cpl_frame_get_filename(raw_reference), ext_nb,
00496                 HAWKI_HEADER_EXT_FORWARD, 0);
00497         cpl_propertylist_append(extproplists[idet], inputlist);
00498         cpl_propertylist_delete(inputlist);
00499         inputlist = cpl_propertylist_load_regexp(
00500                 cpl_frame_get_filename(raw_reference), ext_nb,
00501                 HAWKI_HEADER_WCS, 0);
00502         cpl_propertylist_append(extproplists[idet], inputlist);
00503         cpl_propertylist_delete(inputlist);
00504     }
00505 
00506     /* Write the image */
00507     snprintf(filename, 256, "hawki_step_subtract_bkg_%03d.fits", iserie+1);
00508     hawki_imagelist_save(recipe_framelist,
00509                          recipe_parlist,
00510                          used_frameset,
00511                          obj_images,
00512                          recipe_name,
00513                          HAWKI_CALPRO_BKG_SUBTRACTED,
00514                          HAWKI_PROTYPE_BKG_SUBTRACTED,
00515                          NULL,
00516                          (const cpl_propertylist**)extproplists,
00517                          filename);
00518 
00519     /* Free and return */
00520     cpl_msg_indent_less();
00521     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00522     {
00523         cpl_propertylist_delete(extproplists[idet]) ;
00524     }
00525     cpl_free(extproplists) ;
00526     if(!cpl_errorstate_is_equal(error_prevstate))
00527     {
00528         cpl_errorstate_set(CPL_ERROR_NONE);
00529         return -1;
00530     }
00531     return  0;
00532 }

Generated on Thu Feb 17 17:13:08 2011 for HAWKI Pipeline Reference Manual by  doxygen 1.4.7