HAWKI Pipeline Reference Manual 1.8.12
hawki_step_subtract_bkg.c
00001 /* $Id: hawki_step_subtract_bkg.c,v 1.18 2012/11/30 14:50:51 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: 2012/11/30 14:50:51 $
00024  * $Revision: 1.18 $
00025  * $Name: hawki-1_8_12 $
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 #ifdef __cplusplus
00057 extern "C"
00058 #endif
00059 int cpl_plugin_get_info(cpl_pluginlist * list);
00060 
00061 static int hawki_step_subtract_bkg_create(cpl_plugin *) ;
00062 static int hawki_step_subtract_bkg_exec(cpl_plugin *) ;
00063 static int hawki_step_subtract_bkg_destroy(cpl_plugin *) ;
00064 static int hawki_step_subtract_bkg(cpl_parameterlist *, cpl_frameset *) ;
00065 
00066 static int hawki_step_subtract_bkg_apply_one_to_one_save
00067 (cpl_frameset        *  objframes,
00068  cpl_frameset        *  bkgframes,
00069  cpl_parameterlist   *  parlist,
00070  cpl_frameset        *  recipe_framelist);
00071 
00072 static int hawki_step_subtract_bkg_apply_one_to_all_save
00073 (cpl_frameset        *  objframes,
00074  cpl_frameset        *  bkgframes,
00075  cpl_parameterlist   *  recipe_parlist,
00076  cpl_frameset        *  recipe_framelist);
00077 
00078 static int hawki_step_subtract_bkg_save
00079 (cpl_imagelist     *  obj_images,
00080  int                  iserie,
00081  cpl_frameset      *  used_frameset,
00082  cpl_parameterlist *  recipe_parlist,
00083  cpl_frameset      *  recipe_framelist);
00084 
00085 /*-----------------------------------------------------------------------------
00086                             Static variables
00087  -----------------------------------------------------------------------------*/
00088 
00089 static char hawki_step_subtract_bkg_description[] =
00090 "hawki_step_subtract_bkg -- hawki background subtraction utility.\n"
00091 "This recipe will subtract the given background to the science images.\n"
00092 "The background can be obtained from the sky or object images\n"
00093 "using the hawki_util_compute_bkg utility.\n"
00094 "There are two modes of operation:\n"
00095 "One single background image that it is subtracted to all object images.\n"
00096 "As many background images as objects. A one to one relationship is applied.\n"
00097 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00098 "obj_basic_cal-file.fits "HAWKI_CALPRO_BASICCALIBRATED" or\n"
00099 "background-file.fits "HAWKI_CALPRO_BKGIMAGE" \n";
00100 
00101 /*-----------------------------------------------------------------------------
00102                                 Functions code
00103  -----------------------------------------------------------------------------*/
00104 
00105 /*----------------------------------------------------------------------------*/
00113 /*----------------------------------------------------------------------------*/
00114 int cpl_plugin_get_info(cpl_pluginlist * list)
00115 {
00116     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00117     cpl_plugin  *   plugin = &recipe->interface ;
00118 
00119     cpl_plugin_init(plugin,
00120                     CPL_PLUGIN_API,
00121                     HAWKI_BINARY_VERSION,
00122                     CPL_PLUGIN_TYPE_RECIPE,
00123                     "hawki_step_subtract_bkg",
00124                     "Background subtraction utility",
00125                     hawki_step_subtract_bkg_description,
00126                     "Cesar Enrique Garcia Dabo",
00127                     PACKAGE_BUGREPORT,  
00128                     hawki_get_license(),
00129                     hawki_step_subtract_bkg_create,
00130                     hawki_step_subtract_bkg_exec,
00131                     hawki_step_subtract_bkg_destroy) ;
00132 
00133     cpl_pluginlist_append(list, plugin) ;
00134 
00135     return 0;
00136 }
00137 
00138 /*----------------------------------------------------------------------------*/
00147 /*----------------------------------------------------------------------------*/
00148 static int hawki_step_subtract_bkg_create(cpl_plugin * plugin)
00149 {
00150     cpl_recipe      * recipe ;
00151     //cpl_parameter   * p ;
00152 
00153     /* Get the recipe out of the plugin */
00154     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00155         recipe = (cpl_recipe *)plugin ;
00156     else return -1 ;
00157 
00158     /* Create the parameters list in the cpl_recipe object */
00159     recipe->parameters = cpl_parameterlist_new() ;
00160     if (recipe->parameters == NULL)
00161         return 1;
00162 
00163     /* Fill the parameters list */
00164     /* None.. */
00165 
00166     /* Return */
00167     return 0;
00168 }
00169 
00170 /*----------------------------------------------------------------------------*/
00176 /*----------------------------------------------------------------------------*/
00177 static int hawki_step_subtract_bkg_exec(cpl_plugin * plugin)
00178 {
00179     cpl_recipe  *   recipe ;
00180 
00181     /* Get the recipe out of the plugin */
00182     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00183         recipe = (cpl_recipe *)plugin ;
00184     else return -1 ;
00185 
00186     /* Issue a banner */
00187     hawki_print_banner();
00188 
00189     return hawki_step_subtract_bkg(recipe->parameters, recipe->frames) ;
00190 }
00191 
00192 /*----------------------------------------------------------------------------*/
00198 /*----------------------------------------------------------------------------*/
00199 static int hawki_step_subtract_bkg_destroy(cpl_plugin * plugin)
00200 {
00201     cpl_recipe  *   recipe ;
00202 
00203     /* Get the recipe out of the plugin */
00204     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00205         recipe = (cpl_recipe *)plugin ;
00206     else return -1 ;
00207 
00208     cpl_parameterlist_delete(recipe->parameters) ;
00209     return 0 ;
00210 }
00211 
00212 /*----------------------------------------------------------------------------*/
00219 /*----------------------------------------------------------------------------*/
00220 static int hawki_step_subtract_bkg(
00221         cpl_parameterlist   *   parlist,
00222         cpl_frameset        *   framelist)
00223 {
00224     int                 nobj;
00225     int                 nbkg;
00226     cpl_frameset    *   objframes;
00227     cpl_frameset    *   bkgframes;
00228 
00229 
00230     /* Identify the RAW and CALIB frames in the input frameset */
00231     if (hawki_dfs_set_groups(framelist))
00232     {
00233         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00234         return -1 ;
00235     }
00236 
00237     /* Identifying objects and bkg data frames */
00238     cpl_msg_info(__func__, "Identifying objects and background data");
00239     objframes = hawki_extract_frameset
00240         (framelist, HAWKI_CALPRO_BASICCALIBRATED);
00241     if (objframes == NULL)
00242     {
00243         cpl_msg_error(__func__, "No object frames provided (%s)",
00244                 HAWKI_CALPRO_BASICCALIBRATED);
00245         return -1 ;
00246     }
00247     /* Retrieve bkg frames */
00248     bkgframes = hawki_extract_frameset
00249         (framelist, HAWKI_CALPRO_BKGIMAGE);
00250     if (bkgframes == NULL)
00251     {
00252         cpl_msg_error(__func__, "No background frames provided (%s)",
00253                 HAWKI_CALPRO_BKGIMAGE);
00254         cpl_frameset_delete(objframes);
00255         return -1 ;
00256     }
00257 
00258     /* Subtract the background */
00259     nobj = cpl_frameset_get_size(objframes);
00260     nbkg = cpl_frameset_get_size(bkgframes);
00261     if(nobj == nbkg)
00262         hawki_step_subtract_bkg_apply_one_to_one_save
00263             (objframes, bkgframes, parlist, framelist);
00264     else if(nbkg == 1)
00265         hawki_step_subtract_bkg_apply_one_to_all_save
00266             (objframes, bkgframes, parlist, framelist);
00267     else
00268     {
00269         cpl_msg_error(__func__,"Incompatible number of science and background"
00270                                " images.");
00271         cpl_msg_error(__func__,"Supply only 1 bkg frame or as many as objects");
00272         cpl_frameset_delete(objframes);
00273         cpl_frameset_delete(bkgframes);
00274         return -1;
00275     }
00276 
00277     /* Free resources */
00278     cpl_frameset_delete(objframes);
00279     cpl_frameset_delete(bkgframes);
00280 
00281     /* Return */
00282     if (cpl_error_get_code())
00283     {
00284         cpl_msg_error(__func__,
00285                       "HAWK-I pipeline could not recover from previous errors");
00286         return -1 ;
00287     }
00288     else return 0 ;
00289 }
00290 
00291 /*----------------------------------------------------------------------------*/
00300 /*----------------------------------------------------------------------------*/
00301 static int hawki_step_subtract_bkg_apply_one_to_one_save
00302 (cpl_frameset        *  objframes,
00303  cpl_frameset        *  bkgframes,
00304  cpl_parameterlist   *  recipe_parlist,
00305  cpl_frameset        *  recipe_framelist)
00306 {
00307     int          iobj;
00308     int          nobjs;
00309 
00310     /* Subtract the background to each object frame */
00311     cpl_msg_info(__func__,"Using a one to one relation btw objects and bkgs");
00312     nobjs = cpl_frameset_get_size(objframes);
00313     for(iobj = 0; iobj < nobjs; ++iobj)
00314     {
00315         cpl_frame     * obj_frame = NULL;
00316         cpl_frame     * bkg_frame = NULL;
00317         cpl_imagelist * obj_images = NULL;
00318         cpl_imagelist * bkg_images = NULL;
00319         cpl_frameset  * used_frameset;
00320 
00321         /* Allocate resources */
00322         used_frameset = cpl_frameset_new();
00323 
00324         /* Read the object frame */
00325         cpl_msg_indent_more();
00326         cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00327         obj_frame = cpl_frameset_get_frame(objframes, iobj);
00328         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00329         if(obj_frame != NULL)
00330             obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00331         if(obj_images == NULL)
00332         {
00333             cpl_msg_indent_less();
00334             cpl_msg_error(__func__, "Error reading obj image") ;
00335             cpl_frameset_delete(used_frameset);
00336             return -1;
00337         }
00338 
00339         /* Read the bkg */
00340         bkg_frame = cpl_frameset_get_frame(bkgframes, iobj);
00341         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00342         if(bkg_frame != NULL)
00343             bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00344         if(bkg_images == NULL)
00345         {
00346             cpl_msg_error(__func__, "Error reading background image") ;
00347             cpl_msg_indent_less();
00348             cpl_imagelist_delete(obj_images);
00349             cpl_frameset_delete(used_frameset);
00350             return -1;
00351         }
00352 
00353         /* Make the correction */
00354         hawki_bkg_imglist_calib(obj_images, bkg_images);
00355 
00356         /* Save the subtracted frame */
00357         if(hawki_step_subtract_bkg_save(obj_images,
00358                                         iobj,
00359                                         used_frameset,
00360                                         recipe_parlist,
00361                                         recipe_framelist) != 0)
00362             cpl_msg_warning(__func__,"Some data could not be saved. "
00363                             "Check permisions or disk space");
00364 
00365         /* Free in loop */
00366         cpl_msg_indent_less();
00367         cpl_imagelist_delete(obj_images);
00368         cpl_imagelist_delete(bkg_images);
00369         cpl_frameset_delete(used_frameset);
00370     }
00371 
00372     /* Exit */
00373     return 0;
00374 }
00375 
00376 /*----------------------------------------------------------------------------*/
00385 /*----------------------------------------------------------------------------*/
00386 static int hawki_step_subtract_bkg_apply_one_to_all_save
00387 (cpl_frameset        *  objframes,
00388  cpl_frameset        *  bkgframes,
00389  cpl_parameterlist   *  recipe_parlist,
00390  cpl_frameset        *  recipe_framelist)
00391 {
00392     int             iobj;
00393     int             nobjs;
00394     cpl_frame     * bkg_frame;
00395     cpl_imagelist * bkg_images = NULL;
00396 
00397     /* Read the bkg */
00398     cpl_msg_info(__func__,"Using the same bkg for all the objects");
00399     bkg_frame = cpl_frameset_get_first(bkgframes);
00400     if(bkg_frame != NULL)
00401         bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00402     if(bkg_images == NULL)
00403     {
00404         cpl_msg_error(__func__, "Error reading background image");
00405         return -1;
00406     }
00407 
00408     /* Subtract the background to each object frame */
00409     nobjs = cpl_frameset_get_size(objframes);
00410     for(iobj = 0; iobj < nobjs; ++iobj)
00411     {
00412         cpl_frame     * obj_frame;
00413         cpl_imagelist * obj_images = NULL;
00414         cpl_frameset  * used_frameset;
00415 
00416         /* Allocate resources */
00417         used_frameset = cpl_frameset_new();
00418 
00419         /* Read the object frame */
00420         cpl_msg_indent_more();
00421         cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00422         obj_frame = cpl_frameset_get_frame(objframes, iobj);
00423         if(obj_frame != NULL)
00424             obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00425         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00426         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00427         if(obj_images == NULL)
00428         {
00429             cpl_msg_indent_less();
00430             cpl_msg_error(__func__, "Error reading obj image") ;
00431             cpl_frameset_delete(used_frameset);
00432             return -1;
00433         }
00434 
00435         /* Make the correction */
00436         hawki_bkg_imglist_calib(obj_images, bkg_images);
00437 
00438         /* Save the subtracted frame */
00439         hawki_step_subtract_bkg_save(obj_images,
00440                                      iobj,
00441                                      used_frameset,
00442                                      recipe_parlist,
00443                                      recipe_framelist);
00444 
00445         /* Free in loop */
00446         cpl_msg_indent_less();
00447         cpl_imagelist_delete(obj_images);
00448         cpl_frameset_delete(used_frameset);
00449     }
00450 
00451     /* Free and return */
00452     cpl_imagelist_delete(bkg_images);
00453     return 0;
00454 }
00455 
00456 /*----------------------------------------------------------------------------*/
00466 /*----------------------------------------------------------------------------*/
00467 static int hawki_step_subtract_bkg_save
00468 (cpl_imagelist     *  obj_images,
00469  int                  iserie,
00470  cpl_frameset      *  used_frameset,
00471  cpl_parameterlist *  recipe_parlist,
00472  cpl_frameset      *  recipe_framelist)
00473 {
00474     const cpl_frame     *   raw_reference;
00475     cpl_propertylist    **  extproplists;
00476     char                    filename[256] ;
00477     cpl_propertylist    *   inputlist ;
00478     int                     ext_nb ;
00479     const char          *   recipe_name = "hawki_step_subtract_bkg";
00480     int                     idet;
00481     cpl_errorstate          error_prevstate = cpl_errorstate_get();
00482 
00483     /* Get the reference frame (the raw frame) */
00484     raw_reference = irplib_frameset_get_first_from_group
00485         (used_frameset, CPL_FRAME_GROUP_RAW);
00486 
00487     /* Create the prop lists */
00488     cpl_msg_indent_more();
00489     cpl_msg_info(__func__, "Creating the keywords list") ;
00490     extproplists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00491     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00492     {
00493         /* Get the extension number */
00494         ext_nb=hawki_get_ext_from_detector
00495             (cpl_frame_get_filename(raw_reference), idet+1);
00496 
00497         /* Allocate this property list */
00498         extproplists[idet] = cpl_propertylist_new();
00499 
00500         /* Propagate some keywords from input raw frame extensions */
00501         inputlist = cpl_propertylist_load_regexp(
00502                 cpl_frame_get_filename(raw_reference), ext_nb,
00503                 HAWKI_HEADER_EXT_FORWARD, 0);
00504         cpl_propertylist_append(extproplists[idet], inputlist);
00505         cpl_propertylist_delete(inputlist);
00506         inputlist = cpl_propertylist_load_regexp(
00507                 cpl_frame_get_filename(raw_reference), ext_nb,
00508                 HAWKI_HEADER_WCS, 0);
00509         cpl_propertylist_append(extproplists[idet], inputlist);
00510         cpl_propertylist_delete(inputlist);
00511     }
00512 
00513     /* Write the image */
00514     snprintf(filename, 256, "hawki_step_subtract_bkg_%04d.fits", iserie+1);
00515     hawki_imagelist_save(recipe_framelist,
00516                          recipe_parlist,
00517                          used_frameset,
00518                          obj_images,
00519                          recipe_name,
00520                          HAWKI_CALPRO_BKG_SUBTRACTED,
00521                          HAWKI_PROTYPE_BKG_SUBTRACTED,
00522                          NULL,
00523                          (const cpl_propertylist**)extproplists,
00524                          filename);
00525 
00526     /* Free and return */
00527     cpl_msg_indent_less();
00528     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00529     {
00530         cpl_propertylist_delete(extproplists[idet]) ;
00531     }
00532     cpl_free(extproplists) ;
00533     if(!cpl_errorstate_is_equal(error_prevstate))
00534     {
00535         cpl_errorstate_set(CPL_ERROR_NONE);
00536         return -1;
00537     }
00538     return  0;
00539 }