HAWKI Pipeline Reference Manual 1.8.6
hawki_step_stitch.c
00001 /* $Id: hawki_step_stitch.c,v 1.7 2011/10/24 10:41:54 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/10/24 10:41:54 $
00024  * $Revision: 1.7 $
00025  * $Name: hawki-1_8_6 $
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     if (recipe->parameters == NULL)
00133         return 1;
00134 
00135     /* Return */
00136     return 0;
00137 }
00138 
00139 /*----------------------------------------------------------------------------*/
00145 /*----------------------------------------------------------------------------*/
00146 static int hawki_step_stitch_exec(cpl_plugin * plugin)
00147 {
00148     cpl_recipe  *   recipe ;
00149     
00150     /* Get the recipe out of the plugin */
00151     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00152         recipe = (cpl_recipe *)plugin ;
00153     else return -1 ;
00154 
00155     /* Issue a banner */
00156     hawki_print_banner();
00157 
00158     return hawki_step_stitch(recipe->parameters, recipe->frames) ;
00159 }
00160 
00161 /*----------------------------------------------------------------------------*/
00167 /*----------------------------------------------------------------------------*/
00168 static int hawki_step_stitch_destroy(cpl_plugin * plugin)
00169 {
00170     cpl_recipe  *   recipe ;
00171     
00172     /* Get the recipe out of the plugin */
00173     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00174         recipe = (cpl_recipe *)plugin ;
00175     else return -1 ;
00176 
00177     cpl_parameterlist_delete(recipe->parameters) ; 
00178     return 0 ;
00179 }
00180 
00181 /*----------------------------------------------------------------------------*/
00188 /*----------------------------------------------------------------------------*/
00189 static int hawki_step_stitch(
00190         cpl_parameterlist   *   parlist,
00191         cpl_frameset        *   frameset)
00192 {
00193     const char          *   comb_filename ;
00194     cpl_frameset        *   combframes;
00195     cpl_frame           *   combframe;
00196     cpl_propertylist    *   plist ;
00197     cpl_image           *   stitched ;
00198     cpl_image           *   in[HAWKI_NB_DETECTORS] ;
00199     double                  posx[HAWKI_NB_DETECTORS] ;
00200     double                  posy[HAWKI_NB_DETECTORS] ;
00201     int                     i, j ;
00202     cpl_errorstate          error_prevstate;
00203 
00204 
00205     /* Retrieve input parameters */
00206  
00207     /* Identify the RAW and CALIB frames in the input frameset */
00208     if (hawki_dfs_set_groups(frameset)) {
00209         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00210         return -1 ;
00211     }
00212     
00213     /* Identifying the combined frame */
00214     cpl_msg_info(__func__, "Identifying the combined frame");
00215     combframes = hawki_extract_frameset
00216         (frameset, HAWKI_CALPRO_COMBINED);
00217     if (combframes == NULL)
00218     {
00219         cpl_msg_error(__func__, "No combined images found (%s)",
00220                 HAWKI_CALPRO_COMBINED);
00221         cpl_frameset_delete(combframes);
00222         return -1 ;
00223     }
00224 
00225     /* Check that we have 1 files in input */
00226     if (cpl_frameset_get_size(combframes) != 1) {
00227         cpl_msg_error(__func__, "Expects one single combined images") ;
00228         cpl_frameset_delete(combframes);
00229         return -1 ;
00230     }
00231 
00232     /* Load the HAWKI images */
00233     cpl_msg_info(__func__,"Loading combined frame");
00234     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
00235         if ((in[i] = hawki_load_image(combframes, 0, i+1, 
00236                         CPL_TYPE_FLOAT)) == NULL) {
00237             cpl_msg_error(__func__, "Cannot load chip nb %d", i+1) ;
00238             for (j=0 ; j<i ; i++) cpl_image_delete(in[j]) ;
00239             cpl_frameset_delete(combframes);
00240             return -1 ;
00241         }
00242     }
00243 
00244     /* Get the first input frame */
00245     combframe     = cpl_frameset_get_first(combframes);
00246     comb_filename = cpl_frame_get_filename(combframe);
00247 
00248     /* Get the POSX / POSY informations */
00249     error_prevstate = cpl_errorstate_get();
00250     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
00251         plist = cpl_propertylist_load_regexp(comb_filename, i+1, "QC", 0) ;
00252         posx[i] = hawki_pfits_get_comb_posx(plist); 
00253         posy[i] = hawki_pfits_get_comb_posy(plist);
00254         cpl_propertylist_delete(plist) ;
00255         if(!cpl_errorstate_is_equal(error_prevstate))
00256         {
00257             cpl_msg_error(__func__, "Cannot get POS infos for chip %d", i+1) ;
00258             return -1 ;
00259         }
00260     }
00261 
00262     /* Compute the stitched image */
00263     cpl_msg_info(__func__, "Computing the stiched image") ;
00264     if ((stitched = hawki_images_stitch(in, posx, posy)) == NULL) {
00265         cpl_msg_error(__func__, "Cannot stitch the images") ;
00266         for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) cpl_image_delete(in[i]) ;
00267         return -1 ;
00268     }
00269     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) cpl_image_delete(in[i]) ;
00270 
00271     /* Save the corrected image */
00272     if (hawki_step_stitch_save(stitched, combframe, parlist, frameset) == -1) 
00273         cpl_msg_warning(__func__,"Some data could not be saved. "
00274                                  "Check permisions or disk space");
00275 
00276     /* Free and Return */
00277     cpl_frameset_delete(combframes);
00278     cpl_image_delete(stitched);
00279 
00280     /* Return */
00281     if (cpl_error_get_code())
00282     {
00283         cpl_msg_error(__func__,
00284                       "HAWK-I pipeline could not recover from previous errors");
00285         return -1 ;
00286     }
00287     else return 0 ;
00288 }
00289 
00290 /*----------------------------------------------------------------------------*/
00298 /*----------------------------------------------------------------------------*/
00299 static int hawki_step_stitch_save
00300 (cpl_image           *   in,
00301  cpl_frame           *   combined,
00302  cpl_parameterlist   *   parlist,
00303  cpl_frameset        *   set)
00304 {
00305     cpl_propertylist    *   plist;
00306     cpl_propertylist    *   wcslist;
00307     const char          *   recipe_name = "hawki_step_stitch" ;
00308     int                     ext_chip_1;
00309     cpl_errorstate          error_prevstate = cpl_errorstate_get();
00310 
00311     cpl_msg_indent_more();
00312 
00313     /* Create a propertylist for PRO.x */
00314     plist = cpl_propertylist_new();
00315     cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
00316                                    HAWKI_PROTYPE_STITCHED) ;
00317     cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
00318                                    HAWKI_CALPRO_STITCHED) ;
00319 
00320     /* Handle WCS keys */
00321     ext_chip_1 = 1;
00322     wcslist = cpl_propertylist_load_regexp(
00323             cpl_frame_get_filename(combined), ext_chip_1, HAWKI_HEADER_WCS, 0);
00324     cpl_propertylist_append(plist, wcslist);
00325 
00326     /* Save the image */
00327     if(cpl_dfs_save_image(set,
00328                           NULL,
00329                           parlist,
00330                           set,
00331                           NULL,
00332                           in,
00333                           CPL_BPP_IEEE_FLOAT,
00334                           recipe_name,
00335                           plist,
00336                           NULL,
00337                           PACKAGE "/" PACKAGE_VERSION,
00338                           "hawki_step_stitch.fits") != CPL_ERROR_NONE)
00339         cpl_msg_error(__func__,"Could not save stitched image");
00340 
00341     cpl_propertylist_delete(plist) ;
00342     cpl_propertylist_delete(wcslist) ;
00343     cpl_msg_indent_less();
00344     if(!cpl_errorstate_is_equal(error_prevstate))
00345     {
00346         cpl_errorstate_set(CPL_ERROR_NONE);
00347         return -1;
00348     }
00349     return  0;
00350 }
00351