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