hawki_step_stitch.c

00001 /* $Id: hawki_step_stitch.c,v 1.6 2010/09/28 14:11:29 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: 2010/09/28 14:11:29 $
00024  * $Revision: 1.6 $
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 
00046 /*-----------------------------------------------------------------------------
00047                             Functions prototypes
00048  -----------------------------------------------------------------------------*/
00049 
00050 static int hawki_step_stitch_create(cpl_plugin *) ;
00051 static int hawki_step_stitch_exec(cpl_plugin *) ;
00052 static int hawki_step_stitch_destroy(cpl_plugin *) ;
00053 static int hawki_step_stitch(cpl_parameterlist *, cpl_frameset *) ;
00054 static int hawki_step_stitch_save
00055 (cpl_image           *   in,
00056  cpl_frame           *   combined,
00057  cpl_parameterlist   *   parlist,
00058  cpl_frameset        *   set);
00059 
00060 /*-----------------------------------------------------------------------------
00061                             Static variables
00062  -----------------------------------------------------------------------------*/
00063 
00064 static char hawki_step_stitch_description[] =
00065 "hawki_step_stitch -- Stitching utility\n"
00066 "This recipe accepts 1 parameter:\n"
00067 "First parameter:   the HAWKI image to stitch "
00068 "                   (PRO CATG = "HAWKI_CALPRO_COMBINED")\n"
00069 "\n"
00070 "This recipe produces 1 file:\n"
00071 "First product:     the stitch image.\n"
00072 "                   (PRO CATG = "HAWKI_CALPRO_STITCHED")\n" ;
00073 
00074 /*-----------------------------------------------------------------------------
00075                                 Functions code
00076  -----------------------------------------------------------------------------*/
00077 
00078 /*----------------------------------------------------------------------------*/
00087 /*----------------------------------------------------------------------------*/
00088 int cpl_plugin_get_info(cpl_pluginlist * list)
00089 {
00090     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00091     cpl_plugin  *   plugin = &recipe->interface ;
00092 
00093     cpl_plugin_init(plugin,
00094                     CPL_PLUGIN_API,
00095                     HAWKI_BINARY_VERSION,
00096                     CPL_PLUGIN_TYPE_RECIPE,
00097                     "hawki_step_stitch",
00098                     "Stitching utility",
00099                     hawki_step_stitch_description,
00100                     "Cesar Enrique Garcia",
00101                     PACKAGE_BUGREPORT,  
00102                     hawki_get_license(),
00103                     hawki_step_stitch_create,
00104                     hawki_step_stitch_exec,
00105                     hawki_step_stitch_destroy) ;
00106 
00107     cpl_pluginlist_append(list, plugin) ;
00108     
00109     return 0;
00110 }
00111 
00112 /*----------------------------------------------------------------------------*/
00120 /*----------------------------------------------------------------------------*/
00121 static int hawki_step_stitch_create(cpl_plugin * plugin)
00122 {
00123     cpl_recipe      *   recipe ;
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 return -1 ;
00129 
00130     /* Create the parameters list in the cpl_recipe object */
00131     recipe->parameters = cpl_parameterlist_new() ; 
00132 
00133     /* Return */
00134     return 0;
00135 }
00136 
00137 /*----------------------------------------------------------------------------*/
00143 /*----------------------------------------------------------------------------*/
00144 static int hawki_step_stitch_exec(cpl_plugin * plugin)
00145 {
00146     cpl_recipe  *   recipe ;
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     /* Issue a banner */
00154     hawki_print_banner();
00155 
00156     return hawki_step_stitch(recipe->parameters, recipe->frames) ;
00157 }
00158 
00159 /*----------------------------------------------------------------------------*/
00165 /*----------------------------------------------------------------------------*/
00166 static int hawki_step_stitch_destroy(cpl_plugin * plugin)
00167 {
00168     cpl_recipe  *   recipe ;
00169     
00170     /* Get the recipe out of the plugin */
00171     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00172         recipe = (cpl_recipe *)plugin ;
00173     else return -1 ;
00174 
00175     cpl_parameterlist_delete(recipe->parameters) ; 
00176     return 0 ;
00177 }
00178 
00179 /*----------------------------------------------------------------------------*/
00186 /*----------------------------------------------------------------------------*/
00187 static int hawki_step_stitch(
00188         cpl_parameterlist   *   parlist,
00189         cpl_frameset        *   frameset)
00190 {
00191     const char          *   comb_filename ;
00192     cpl_frameset        *   combframes;
00193     cpl_frame           *   combframe;
00194     cpl_propertylist    *   plist ;
00195     cpl_image           *   stitched ;
00196     cpl_image           *   in[HAWKI_NB_DETECTORS] ;
00197     double                  posx[HAWKI_NB_DETECTORS] ;
00198     double                  posy[HAWKI_NB_DETECTORS] ;
00199     int                     i, j ;
00200     cpl_errorstate          error_prevstate;
00201 
00202 
00203     /* Retrieve input parameters */
00204  
00205     /* Identify the RAW and CALIB frames in the input frameset */
00206     if (hawki_dfs_set_groups(frameset)) {
00207         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00208         return -1 ;
00209     }
00210     
00211     /* Identifying the combined frame */
00212     cpl_msg_info(__func__, "Identifying the combined frame");
00213     combframes = hawki_extract_frameset
00214         (frameset, HAWKI_CALPRO_COMBINED);
00215     if (combframes == NULL)
00216     {
00217         cpl_msg_error(__func__, "No combined images found (%s)",
00218                 HAWKI_CALPRO_COMBINED);
00219         cpl_frameset_delete(combframes);
00220         return -1 ;
00221     }
00222 
00223     /* Check that we have 1 files in input */
00224     if (cpl_frameset_get_size(combframes) != 1) {
00225         cpl_msg_error(__func__, "Expects one single combined images") ;
00226         cpl_frameset_delete(combframes);
00227         return -1 ;
00228     }
00229 
00230     /* Load the HAWKI images */
00231     cpl_msg_info(__func__,"Loading combined frame");
00232     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
00233         if ((in[i] = hawki_load_image(combframes, 0, i+1, 
00234                         CPL_TYPE_FLOAT)) == NULL) {
00235             cpl_msg_error(__func__, "Cannot load chip nb %d", i+1) ;
00236             for (j=0 ; j<i ; i++) cpl_image_delete(in[j]) ;
00237             cpl_frameset_delete(combframes);
00238             return -1 ;
00239         }
00240     }
00241 
00242     /* Get the first input frame */
00243     combframe     = cpl_frameset_get_first(combframes);
00244     comb_filename = cpl_frame_get_filename(combframe);
00245 
00246     /* Get the POSX / POSY informations */
00247     error_prevstate = cpl_errorstate_get();
00248     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
00249         plist = cpl_propertylist_load_regexp(comb_filename, i+1, "QC", 0) ;
00250         posx[i] = hawki_pfits_get_comb_posx(plist); 
00251         posy[i] = hawki_pfits_get_comb_posy(plist);
00252         cpl_propertylist_delete(plist) ;
00253         if(!cpl_errorstate_is_equal(error_prevstate))
00254         {
00255             cpl_msg_error(__func__, "Cannot get POS infos for chip %d", i+1) ;
00256             return -1 ;
00257         }
00258     }
00259 
00260     /* Compute the stitched image */
00261     cpl_msg_info(__func__, "Computing the stiched image") ;
00262     if ((stitched = hawki_images_stitch(in, posx, posy)) == NULL) {
00263         cpl_msg_error(__func__, "Cannot stitch the images") ;
00264         for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) cpl_image_delete(in[i]) ;
00265         return -1 ;
00266     }
00267     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) cpl_image_delete(in[i]) ;
00268 
00269     /* Save the corrected image */
00270     if (hawki_step_stitch_save(stitched, combframe, parlist, frameset) == -1) 
00271         cpl_msg_warning(__func__,"Some data could not be saved. "
00272                                  "Check permisions or disk space");
00273 
00274     /* Free and Return */
00275     cpl_frameset_delete(combframes);
00276     cpl_image_delete(stitched);
00277 
00278     /* Return */
00279     if (cpl_error_get_code())
00280     {
00281         cpl_msg_error(__func__,
00282                       "HAWK-I pipeline could not recover from previous errors");
00283         return -1 ;
00284     }
00285     else return 0 ;
00286 }
00287 
00288 /*----------------------------------------------------------------------------*/
00296 /*----------------------------------------------------------------------------*/
00297 static int hawki_step_stitch_save
00298 (cpl_image           *   in,
00299  cpl_frame           *   combined,
00300  cpl_parameterlist   *   parlist,
00301  cpl_frameset        *   set)
00302 {
00303     cpl_propertylist    *   plist;
00304     cpl_propertylist    *   wcslist;
00305     const char          *   recipe_name = "hawki_step_stitch" ;
00306     int                     ext_chip_1;
00307     cpl_errorstate          error_prevstate = cpl_errorstate_get();
00308 
00309     cpl_msg_indent_more();
00310 
00311     /* Create a propertylist for PRO.x */
00312     plist = cpl_propertylist_new();
00313     cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
00314                                    HAWKI_PROTYPE_STITCHED) ;
00315     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
00316                                    HAWKI_CALPRO_STITCHED) ;
00317 
00318     /* Handle WCS keys */
00319     ext_chip_1 = 1;
00320     wcslist = cpl_propertylist_load_regexp(
00321             cpl_frame_get_filename(combined), ext_chip_1, HAWKI_HEADER_WCS, 0);
00322     cpl_propertylist_append(plist, wcslist);
00323 
00324     /* Save the image */
00325     if(cpl_dfs_save_image(set,
00326                           NULL,
00327                           parlist,
00328                           set,
00329                           NULL,
00330                           in,
00331                           CPL_BPP_IEEE_FLOAT,
00332                           recipe_name,
00333                           plist,
00334                           NULL,
00335                           PACKAGE "/" PACKAGE_VERSION,
00336                           "hawki_step_stitch.fits") != CPL_ERROR_NONE)
00337         cpl_msg_error(__func__,"Could not save stitched image");
00338 
00339     cpl_propertylist_delete(plist) ;
00340     cpl_propertylist_delete(wcslist) ;
00341     cpl_msg_indent_less();
00342     if(!cpl_errorstate_is_equal(error_prevstate))
00343     {
00344         cpl_errorstate_set(CPL_ERROR_NONE);
00345         return -1;
00346     }
00347     return  0;
00348 }
00349 

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