hawki_step_apply_dist.c

00001 /* $Id: hawki_step_apply_dist.c,v 1.8 2011/01/31 11:02:43 cgarcia Exp $
00002  *
00003  * This file is part of the HAWKI 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: cgarcia $
00023  * $Date: 2011/01/31 11:02:43 $
00024  * $Revision: 1.8 $
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 <math.h>
00037 #include <cpl.h>
00038 
00039 #include "irplib_utils.h"
00040 
00041 #include "hawki_utils.h"
00042 #include "hawki_pfits.h"
00043 #include "hawki_dfs.h"
00044 #include "hawki_load.h"
00045 #include "hawki_save.h"
00046 #include "hawki_distortion.h"
00047 
00048 /*-----------------------------------------------------------------------------
00049                             Functions prototypes
00050  -----------------------------------------------------------------------------*/
00051 
00052 static int hawki_step_apply_dist_create(cpl_plugin *) ;
00053 static int hawki_step_apply_dist_exec(cpl_plugin *) ;
00054 static int hawki_step_apply_dist_destroy(cpl_plugin *) ;
00055 static int hawki_step_apply_dist(cpl_parameterlist *, cpl_frameset *) ;
00056 static int hawki_step_apply_dist_save
00057 (cpl_imagelist      *   images,
00058  int                    iserie,
00059  cpl_parameterlist  *   recipe_parlist,
00060  cpl_frameset       *   used_frameset,
00061  cpl_frameset       *   recipe_frameset);
00062 
00063 int hawki_step_apply_dist_compute_and_save
00064 (cpl_frameset      * objects,
00065  cpl_frameset      * distortion_x,
00066  cpl_frameset      * distortion_y,
00067  cpl_parameterlist * parlist,
00068  cpl_frameset      * recipe_frameset);
00069 
00070 /*-----------------------------------------------------------------------------
00071                             Static variables
00072  -----------------------------------------------------------------------------*/
00073 
00074 static char hawki_step_apply_dist_description[] =
00075 "hawki_step_apply_dist -- Distortion correction utility\n"
00076 "This recipe accepts two types of frames:\n"
00077 "object.fits  Images to correct (PRO.CATG = "HAWKI_CALPRO_BKG_SUBTRACTED")\n"
00078 "distmap_x.fits The image with the distortion in X.\n"
00079 "                   (PRO CATG = "HAWKI_CALPRO_DISTORTION_X")\n"
00080 "distmap_y.fits The image with the distortion in Y.\n"
00081 "                   (PRO CATG = "HAWKI_CALPRO_DISTORTION_Y")\n"
00082 "\n"
00083 "This recipe produces:\n"
00084 "hawki_step_apply_dist.fits:     the corrected image.\n"
00085 "                   (PRO CATG = "HAWKI_CALPRO_DIST_CORRECTED")\n" ;
00086 
00087 /*-----------------------------------------------------------------------------
00088                                 Functions code
00089  -----------------------------------------------------------------------------*/
00090 
00091 /*----------------------------------------------------------------------------*/
00100 /*----------------------------------------------------------------------------*/
00101 int cpl_plugin_get_info(cpl_pluginlist * list)
00102 {
00103     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00104     cpl_plugin  *   plugin = &recipe->interface ;
00105 
00106     cpl_plugin_init(plugin,
00107                     CPL_PLUGIN_API,
00108                     HAWKI_BINARY_VERSION,
00109                     CPL_PLUGIN_TYPE_RECIPE,
00110                     "hawki_step_apply_dist",
00111                     "Distortion correction utility",
00112                     hawki_step_apply_dist_description,
00113                     "Cesar Enrique Garcia Dabo",
00114                     PACKAGE_BUGREPORT,  
00115                     hawki_get_license(),
00116                     hawki_step_apply_dist_create,
00117                     hawki_step_apply_dist_exec,
00118                     hawki_step_apply_dist_destroy) ;
00119 
00120     cpl_pluginlist_append(list, plugin) ;
00121     
00122     return 0;
00123 }
00124 
00125 /*----------------------------------------------------------------------------*/
00133 /*----------------------------------------------------------------------------*/
00134 static int hawki_step_apply_dist_create(cpl_plugin * plugin)
00135 {
00136     cpl_recipe      *   recipe ;
00137         
00138     /* Check that the plugin is part of a valid recipe */
00139     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00140         recipe = (cpl_recipe *)plugin ;
00141     else return -1 ;
00142 
00143     /* Create the parameters list in the cpl_recipe object */
00144     recipe->parameters = cpl_parameterlist_new() ; 
00145 
00146     /* Return */
00147     return 0;
00148 }
00149 
00150 /*----------------------------------------------------------------------------*/
00156 /*----------------------------------------------------------------------------*/
00157 static int hawki_step_apply_dist_exec(cpl_plugin * plugin)
00158 {
00159     cpl_recipe  *   recipe ;
00160     
00161     /* Get the recipe out of the plugin */
00162     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00163         recipe = (cpl_recipe *)plugin ;
00164     else return -1 ;
00165 
00166     /* Issue a banner */
00167     hawki_print_banner();
00168 
00169     return hawki_step_apply_dist(recipe->parameters, recipe->frames) ;
00170 }
00171 
00172 /*----------------------------------------------------------------------------*/
00178 /*----------------------------------------------------------------------------*/
00179 static int hawki_step_apply_dist_destroy(cpl_plugin * plugin)
00180 {
00181     cpl_recipe  *   recipe ;
00182     
00183     /* Get the recipe out of the plugin */
00184     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00185         recipe = (cpl_recipe *)plugin ;
00186     else return -1 ;
00187 
00188     cpl_parameterlist_delete(recipe->parameters) ; 
00189     return 0 ;
00190 }
00191 
00192 /*----------------------------------------------------------------------------*/
00199 /*----------------------------------------------------------------------------*/
00200 static int hawki_step_apply_dist(
00201         cpl_parameterlist   *   parlist,
00202         cpl_frameset        *   frameset)
00203 {
00204     cpl_frameset *   objframes;
00205     cpl_frameset *   distortion_x;
00206     cpl_frameset *   distortion_y;
00207     
00208     /* Retrieve input parameters */
00209  
00210     /* Identify the RAW and CALIB frames in the input frameset */
00211     if (hawki_dfs_set_groups(frameset)) {
00212         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00213         return -1 ;
00214     }
00215 
00216     /* Identifying objects frames */
00217     cpl_msg_info(__func__, "Identifying objects");
00218     objframes = hawki_extract_frameset
00219         (frameset, HAWKI_CALPRO_BKG_SUBTRACTED);
00220     if (objframes == NULL)
00221     {
00222         cpl_msg_error(__func__, "No object frames provided (%s)",
00223                       HAWKI_CALPRO_BKG_SUBTRACTED);
00224         return -1 ;
00225     }
00226     
00227     /* Identifying distortion frames */
00228     cpl_msg_info(__func__, "Identifying distortion maps");
00229     distortion_x = hawki_extract_frameset
00230         (frameset, HAWKI_CALPRO_DISTORTION_X);
00231     distortion_y = hawki_extract_frameset
00232         (frameset, HAWKI_CALPRO_DISTORTION_Y);
00233     if(cpl_frameset_get_size(distortion_x) != 1 && 
00234        cpl_frameset_get_size(distortion_y) != 1 )
00235     {
00236         cpl_msg_error(__func__, "One X-distortion frame and one Y-distortion "
00237                       "must be provided (%s, %s)",
00238                       HAWKI_CALPRO_DISTORTION_X, HAWKI_CALPRO_DISTORTION_Y);
00239         cpl_frameset_delete(objframes);
00240         cpl_frameset_delete(distortion_x);
00241         cpl_frameset_delete(distortion_y);
00242         return -1 ;
00243     }
00244     
00245     /* Apply the correction and save */
00246     if(hawki_step_apply_dist_compute_and_save
00247         (objframes, distortion_x, distortion_y, parlist, frameset) == -1)
00248     {
00249         cpl_msg_error(__func__,"Could not correct the frames"); 
00250         cpl_frameset_delete(objframes);
00251         cpl_frameset_delete(distortion_x);
00252         cpl_frameset_delete(distortion_y);
00253         return -1;
00254     }
00255     
00256     /* Free and return */
00257     cpl_frameset_delete(objframes);
00258     cpl_frameset_delete(distortion_x);
00259     cpl_frameset_delete(distortion_y);
00260     
00261     if (cpl_error_get_code()) return -1 ;
00262     else return 0;
00263 }
00264 
00265 /*----------------------------------------------------------------------------*/
00274 /*----------------------------------------------------------------------------*/
00275 int hawki_step_apply_dist_compute_and_save
00276 (cpl_frameset      * objects,
00277  cpl_frameset      * distortion_x,
00278  cpl_frameset      * distortion_y,
00279  cpl_parameterlist * parlist,
00280  cpl_frameset      * recipe_frameset)
00281 {
00282     const cpl_frame   *  distframe_x;
00283     const cpl_frame   *  distframe_y;
00284     cpl_image        **  dist_x;
00285     cpl_image        **  dist_y;
00286     cpl_image         *  first_image;
00287     int                  nx;
00288     int                  ny;
00289     int                  iframe;
00290     int                  nframes;
00291     int                  idet;
00292     int                  jdet;
00293     cpl_errorstate       error_prevstate = cpl_errorstate_get();
00294 
00295     /* Get the distortion filename and distortion maps */
00296     cpl_msg_info(__func__,"Creating the distortion maps");
00297     distframe_x = cpl_frameset_get_first_const(distortion_x);
00298     distframe_y = cpl_frameset_get_first_const(distortion_y);
00299     first_image = hawki_load_image(objects, 0, 1, CPL_TYPE_FLOAT);
00300     nx = cpl_image_get_size_x(first_image);
00301     ny = cpl_image_get_size_y(first_image);
00302     cpl_image_delete(first_image);
00303     dist_x = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_image *));
00304     dist_y = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_image *));
00305     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00306     {
00307         hawki_distortion * distortion;
00308         dist_x[idet] = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00309         dist_y[idet] = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE) ;
00310         
00311         /* Load the distortion */
00312         if ((distortion = hawki_distortion_load
00313                 (distframe_x, distframe_y , idet+1)) == NULL) 
00314         {
00315             cpl_msg_error(__func__, "Cannot load the distortion for chip %d: %s", 
00316                     idet+1, cpl_error_get_message());
00317             for (jdet=0 ; jdet<=idet; jdet++)
00318             {
00319                 cpl_image_delete(dist_x[jdet]);
00320                 cpl_image_delete(dist_y[jdet]);
00321             }
00322             cpl_free(dist_x);
00323             cpl_free(dist_y);
00324             return -1 ;
00325         }
00326         if (hawki_distortion_create_maps_detector
00327                 (distortion, dist_x[idet], dist_y[idet]))
00328         {
00329             cpl_msg_error(__func__, "Cannot create the distortion maps") ;
00330             for (jdet=0 ; jdet<=idet; jdet++)
00331             {
00332                 cpl_image_delete(dist_x[jdet]);
00333                 cpl_image_delete(dist_y[jdet]);
00334             }
00335             cpl_free(dist_x);
00336             cpl_free(dist_y);
00337             hawki_distortion_delete(distortion);
00338             return -1;
00339         }
00340         hawki_distortion_delete(distortion);
00341     }
00342     
00343     /* Loop on frames */
00344     nframes = cpl_frameset_get_size(objects);
00345     cpl_msg_info(__func__,"Number of frames to process: %d", nframes);
00346     cpl_msg_indent_more();
00347     for(iframe = 0 ; iframe < nframes; ++iframe)
00348     {
00349         cpl_imagelist * object_images;
00350         cpl_frame     * this_frame;
00351         cpl_frameset  * used_frameset;
00352     
00353         /* Msg */
00354         cpl_msg_info(__func__,"Correcting distortion in frame %d",iframe+1);
00355         
00356         /* Load the HAWKI images */
00357         this_frame = cpl_frameset_get_frame(objects, iframe);
00358         if((object_images = hawki_load_frame(this_frame, CPL_TYPE_FLOAT)) == NULL)
00359         {
00360             cpl_msg_error(__func__,"Could not load input object images");
00361             cpl_msg_indent_less();
00362             for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00363             {
00364                 cpl_image_delete(dist_x[idet]);
00365                 cpl_image_delete(dist_y[idet]);
00366             }
00367             cpl_free(dist_x);
00368             cpl_free(dist_y);
00369             return -1;
00370         }
00371 
00372         /* Apply the correction on the current image */
00373         if (hawki_distortion_apply_maps(object_images, dist_x, dist_y) == -1) 
00374         {
00375             cpl_msg_error(__func__, "Cannot correct distortion") ;
00376             cpl_msg_indent_less();
00377             cpl_imagelist_delete(object_images);
00378             for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00379             {
00380                 cpl_image_delete(dist_x[idet]);
00381                 cpl_image_delete(dist_y[idet]);
00382             }
00383             cpl_free(dist_x);
00384             cpl_free(dist_y);
00385             return -1 ;
00386         }
00387 
00388         /* Set the used frameset */
00389         used_frameset = cpl_frameset_new(); 
00390         cpl_frameset_insert(used_frameset, cpl_frame_duplicate(this_frame));
00391         cpl_frameset_insert
00392             (used_frameset,cpl_frame_duplicate
00393                  (cpl_frameset_get_first_const((distortion_x))));
00394         cpl_frameset_insert
00395             (used_frameset,cpl_frame_duplicate
00396                  (cpl_frameset_get_first_const((distortion_y))));
00397             
00398         /* Save the corrected image */
00399         if (hawki_step_apply_dist_save
00400                 (object_images, iframe, 
00401                  parlist, used_frameset, recipe_frameset) == -1)
00402         {
00403             cpl_errorstate_set(CPL_ERROR_NONE);
00404         }
00405         
00406         /* Free resources */
00407         cpl_frameset_delete(used_frameset);
00408         cpl_imagelist_delete(object_images);
00409         
00410     }
00411     cpl_msg_indent_less();
00412     
00413     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00414     {
00415         cpl_image_delete(dist_x[idet]);
00416         cpl_image_delete(dist_y[idet]);
00417     }
00418     cpl_free(dist_x);
00419     cpl_free(dist_y);
00420     if(!cpl_errorstate_is_equal(error_prevstate))
00421     {
00422         cpl_msg_warning(__func__,"Probably some data could not be saved. "
00423                         "Check permisions or disk space");
00424     }                           
00425     return 0;
00426 }
00427 
00428 /*----------------------------------------------------------------------------*/
00437 /*----------------------------------------------------------------------------*/
00438 static int hawki_step_apply_dist_save
00439 (cpl_imagelist      *   images,
00440  int                    iserie,
00441  cpl_parameterlist  *   recipe_parlist,
00442  cpl_frameset       *   used_frameset,
00443  cpl_frameset       *   recipe_frameset)
00444 {
00445     const cpl_frame     *   raw_reference;
00446     cpl_propertylist    **  extproplists;
00447     char                    filename[256] ;
00448     cpl_propertylist    *   inputlist ;
00449     int                     ext_nb ;
00450     const char          *   recipe_name = "hawki_step_apply_dist";
00451     int                     idet;
00452 
00453     /* Get the reference frame (the raw frame) */
00454     raw_reference = irplib_frameset_get_first_from_group
00455         (used_frameset, CPL_FRAME_GROUP_RAW);
00456     
00457     /* Create the prop lists */
00458     cpl_msg_indent_more();
00459     extproplists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00460     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00461     {
00462         /* Get the extension number */
00463         ext_nb=hawki_get_ext_from_detector
00464             (cpl_frame_get_filename(raw_reference), idet+1);
00465 
00466         /* Allocate this property list */
00467         extproplists[idet] = cpl_propertylist_new();
00468 
00469         /* Propagate some keywords from input raw frame extensions */
00470         inputlist = cpl_propertylist_load_regexp(
00471                 cpl_frame_get_filename(raw_reference), ext_nb,
00472                 HAWKI_HEADER_EXT_FORWARD, 0);
00473         cpl_propertylist_append(extproplists[idet], inputlist);
00474         cpl_propertylist_delete(inputlist);
00475         inputlist = cpl_propertylist_load_regexp(
00476                 cpl_frame_get_filename(raw_reference), ext_nb,
00477                 HAWKI_HEADER_WCS, 0);
00478         cpl_propertylist_append(extproplists[idet], inputlist);
00479         cpl_propertylist_delete(inputlist);
00480     }
00481     
00482     /* Write the image */
00483     snprintf(filename, 256, "hawki_step_apply_dist_%03d.fits", iserie+1);
00484     if(hawki_imagelist_save
00485         (recipe_frameset,
00486          recipe_parlist,
00487          used_frameset,
00488          images,
00489          recipe_name,
00490          HAWKI_CALPRO_DIST_CORRECTED,
00491          HAWKI_PROTYPE_DIST_CORRECTED,
00492          NULL,
00493          (const cpl_propertylist**)extproplists,
00494          filename) != 0)
00495     {
00496         for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00497             cpl_propertylist_delete(extproplists[idet]) ;
00498         cpl_free(extproplists) ;
00499         cpl_msg_indent_less();
00500         return -1;
00501     }
00502     
00503     /* Free and return */
00504     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
00505         cpl_propertylist_delete(extproplists[idet]) ;
00506     }
00507     cpl_free(extproplists) ;
00508     cpl_msg_indent_less();
00509     return  0;
00510 }
00511 

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