flames_utl_unpack.c

00001 /* $Id: flames_utl_unpack.c,v 1.7 2010/09/24 09:30:44 amodigli Exp $
00002  *
00003  * This file is part of the UVES 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: amodigli $
00023  * $Date: 2010/09/24 09:30:44 $
00024  * $Revision: 1.7 $
00025  * $Name: uves-5_0_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  ----------------------------------------------------------------------------*/
00035 #include <string.h>
00036 
00037 /* cpl */
00038 #include <cpl.h>
00039 
00040 /* irplib */
00041 #include <irplib_utils.h>
00042 /*
00043 #include <uves_tpl_utils.h>
00044 #include <uves_key_names.h>
00045 #include <uves_pro_types.h>
00046 #include <uves_functions.h>
00047 */
00048 
00049 #include <flames_utils_science.h>
00050 #include <flames_utils.h>
00051 #include <uves_dfs.h>
00052 #include <flames_dfs.h>
00053 #include <uves_chip.h>
00054 #include <uves_pfits.h>
00055 #include <uves_msg.h>
00056 #include <uves_error.h>
00057 #include <uves_utils_wrappers.h>
00058 
00059 #define recipe_id "flames_utl_unpack"
00060 
00061 /*-----------------------------------------------------------------------------
00062                             Functions prototypes
00063  ----------------------------------------------------------------------------*/
00064 
00065 static int flames_utl_unpack_create(cpl_plugin *) ;
00066 static int flames_utl_unpack_exec(cpl_plugin *) ;
00067 static int flames_utl_unpack_destroy(cpl_plugin *) ;
00068 static int flames_utl_unpack(cpl_parameterlist *, cpl_frameset *) ;
00069 
00070 
00071 static cpl_error_code
00072 flames_unpack_spectra_from_image(cpl_frame* frm,
00073                               cpl_parameterlist   *   parameters,
00074                                cpl_frameset* set);
00075 static cpl_error_code
00076 flames_unpack_image_from_cube(cpl_frame* frm,
00077                               cpl_parameterlist   *   parameters,
00078                                cpl_frameset* set);
00079 
00080 /*-----------------------------------------------------------------------------
00081                             Static variables
00082  ----------------------------------------------------------------------------*/
00083 
00084 static char flames_utl_unpack_description[] =
00085 "This recipe unpack flames-uves packed products.\n"
00086 "Information on relevant parameters can be found with\n"
00087 "esorex --params flames_utl_unpack\n"
00088 "esorex --help flames_utl_unpack\n"
00089 "\n";
00090 
00091 /*-----------------------------------------------------------------------------
00092                                 Functions code
00093  ----------------------------------------------------------------------------*/
00094 /*---------------------------------------------------------------------------*/
00098 /*---------------------------------------------------------------------------*/
00099 
00101 /*---------------------------------------------------------------------------*/
00109 /*---------------------------------------------------------------------------*/
00110 int cpl_plugin_get_info(cpl_pluginlist * list)
00111 {
00112     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00113     cpl_plugin  *   plugin = &recipe->interface ;
00114 
00115     cpl_plugin_init(plugin,
00116                     CPL_PLUGIN_API,
00117                     UVES_BINARY_VERSION,
00118                     CPL_PLUGIN_TYPE_RECIPE,
00119                     "flames_utl_unpack",
00120                     "Unpack flames-uves packed frames",
00121                     flames_utl_unpack_description,
00122                     "Andrea Modigliani",
00123                     "Andrea.Modigliani@eso.org",
00124                     uves_get_license(),
00125                     flames_utl_unpack_create,
00126                     flames_utl_unpack_exec,
00127                     flames_utl_unpack_destroy) ;
00128 
00129     cpl_pluginlist_append(list, plugin) ;
00130     
00131     return 0;
00132 }
00133 
00134 /*---------------------------------------------------------------------------*/
00143 /*---------------------------------------------------------------------------*/
00144 static int flames_utl_unpack_create(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     cpl_error_reset();
00153     irplib_reset();
00154 
00155     /* Create the parameters list in the cpl_recipe object */
00156     recipe->parameters = cpl_parameterlist_new() ; 
00157 
00158     /* Fill the parameters list */
00159     /* --stropt */
00160  
00161     /* Return */
00162     return 0;
00163 }
00164 
00165 /*---------------------------------------------------------------------------*/
00171 /*---------------------------------------------------------------------------*/
00172 static int flames_utl_unpack_exec(cpl_plugin * plugin)
00173 {
00174     cpl_recipe  *   recipe ;
00175      int code=0;
00176      cpl_errorstate initial_errorstate = cpl_errorstate_get();
00177 
00178     /* Get the recipe out of the plugin */
00179     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00180         recipe = (cpl_recipe *)plugin ;
00181     else return -1 ;
00182     cpl_error_reset();
00183     irplib_reset();
00184     code = flames_utl_unpack(recipe->parameters, recipe->frames) ;
00185 
00186 
00187     if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00188         /* Dump the error history since recipe execution start.                
00189            At this point the recipe cannot recover from the error */           
00190         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00191     } 
00192 
00193     return code ;
00194 }
00195 
00196 /*---------------------------------------------------------------------------*/
00202 /*---------------------------------------------------------------------------*/
00203 static int flames_utl_unpack_destroy(cpl_plugin * plugin)
00204 {
00205     cpl_recipe  *   recipe ;
00206     
00207     /* Get the recipe out of the plugin */
00208     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00209         recipe = (cpl_recipe *)plugin ;
00210     else return -1 ;
00211 
00212     cpl_parameterlist_delete(recipe->parameters) ; 
00213     return 0 ;
00214 }
00215 
00216 /*---------------------------------------------------------------------------*/
00223 /*---------------------------------------------------------------------------*/
00224 static int 
00225 flames_utl_unpack( cpl_parameterlist   *   parlist, 
00226         cpl_frameset        *   set)
00227 {   
00228     uves_propertylist    *   plist=NULL ;
00229     int n=0;
00230     int i=0;
00231     int naxis=0;
00232     cpl_frame* frm=NULL;
00233     const char* tag=NULL;
00234     const char* name=NULL;
00235     
00236           uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
00237         UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
00238 
00239     /* HOW TO RETRIEVE INPUT PARAMETERS */
00240     /* --stropt */
00241    
00242     /* Identify the RAW and CALIB frames in the input frameset */
00243     //check(uves_dfs_set_groups(set),
00244     //     "Cannot identify RAW and CALIB frames") ;
00245  
00246     /* HOW TO ACCESS INPUT DATA */
00247     n=cpl_frameset_get_size(set);
00248     if(n<1) {
00249       uves_msg_error("Empty input frame list!");
00250       goto cleanup ;
00251     }
00252 
00253     for(i=0;i<n;i++) {
00254       check_nomsg(frm=cpl_frameset_get_frame(set,i));
00255       check_nomsg(tag=cpl_frame_get_tag(frm));
00256       check_nomsg(name=cpl_frame_get_filename(frm));
00257       check_nomsg(plist=uves_propertylist_load(name,0));
00258       check_nomsg(naxis=uves_pfits_get_naxis(plist));
00259       //uves_msg("name=%s",name);
00260       if(naxis==3) {
00261         check_nomsg(flames_unpack_image_from_cube(frm,parlist,set));
00262       } else if (naxis==2) {
00263     check_nomsg(flames_unpack_spectra_from_image(frm,parlist,set));
00264       } else {
00265 
00266       }
00267 
00268       uves_free_propertylist(&plist);
00269     }
00270 
00271 
00272  cleanup:
00273    
00274 
00275     if (cpl_error_get_code()) {
00276         return -1 ;
00277     } else {
00278         return 0 ;
00279     }
00280 
00281 }
00282 
00283 
00284 static cpl_error_code
00285 flames_unpack_image_from_cube(cpl_frame* frm,
00286                               cpl_parameterlist   *   parameters,
00287                               cpl_frameset* set)
00288 {
00289 
00290   const char* name=NULL;
00291   const char* fname=NULL;
00292   const char* tag=NULL;
00293   uves_propertylist* href=NULL;
00294   uves_propertylist* hout=NULL;
00295   cpl_imagelist* iml=NULL;
00296   int size=0;
00297   int i=0;
00298   cpl_frame* frame=NULL;
00299   cpl_image* img=NULL;
00300 
00301   cknull_nomsg(parameters);
00302   check_nomsg(name=cpl_frame_get_filename(frm));
00303   check_nomsg(tag=cpl_frame_get_tag(frm));
00304   check_nomsg(href=uves_propertylist_load(name,0));
00305   check_nomsg(hout=uves_propertylist_duplicate(href));
00306 
00307   check_nomsg(iml=cpl_imagelist_load(name,CPL_TYPE_FLOAT,0));
00308   check_nomsg(size=cpl_imagelist_get_size(iml));
00309   for(i=0;i<size;i++) {
00310     frame=cpl_frame_new();
00311     check_nomsg(img=cpl_imagelist_get(iml,i));
00312     check_nomsg(fname=uves_sprintf("slic_%d_%s",i,flames_get_basename(name)));
00313     check_nomsg( uves_save_image(img,fname,hout,true, true) );
00314 
00315     check_nomsg(cpl_frame_set_filename(frame, fname)) ;
00316     check_nomsg(cpl_frame_set_tag(frame, tag)) ;
00317     check_nomsg(cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE)) ;
00318     check_nomsg(cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT)) ;
00319     check(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL),
00320       "Error while initialising the product frame") ;
00321 
00322     cpl_frameset_insert(set,frame);
00323     /*
00324     check_nomsg(cpl_propertylist_erase_regexp(hout, "^ESO PRO CATG",0));
00325     check(cpl_dfs_setup_product_header(hout, 
00326                                        frame, 
00327                                        set, 
00328                                        parameters,
00329                                        "flames_utl_unpack", 
00330                                        PACKAGE "/" PACKAGE_VERSION, 
00331                                        "PRO-1.15"),
00332       "Problem in the product DFS-compliance") ;
00333 
00334     check( flames_frameset_insert(
00335                 set,
00336                 CPL_FRAME_GROUP_PRODUCT,
00337                 CPL_FRAME_TYPE_IMAGE,
00338                 CPL_FRAME_LEVEL_INTERMEDIATE,
00339                 fname,
00340                 tag,
00341                 hout,
00342                 parameters,
00343                 recipe_id,
00344                 PACKAGE "/" PACKAGE_VERSION,
00345                 NULL, NULL, true, 0),
00346      "Could not add image %s (%s) to frameset",
00347      fname, tag);
00348     */
00349 
00350   }
00351 
00352 
00353  cleanup:
00354   uves_free_propertylist(&href);
00355   uves_free_propertylist(&hout);
00356   uves_free_imagelist(&iml);
00357 
00358 
00359   return cpl_error_get_code() ;
00360 
00361 }
00362 
00363 
00364 
00365 static cpl_error_code
00366 flames_unpack_spectra_from_image(cpl_frame* frm,
00367                                cpl_parameterlist   *   parameters,
00368                                cpl_frameset* set)
00369 {
00370 
00371 
00372 
00373   const char* name=NULL;
00374   const char* fname=NULL;
00375   const char* tag=NULL;
00376   cpl_propertylist* href=NULL;
00377   cpl_propertylist* hout=NULL;
00378   cpl_image* ima=NULL;
00379 
00380   int i=0;
00381   cpl_frame* frame=NULL;
00382   cpl_image* img=NULL;
00383   int sx=0;
00384   int sy=0;
00385 
00386   cknull_nomsg(parameters);
00387   check_nomsg(name=cpl_frame_get_filename(frm));
00388   check_nomsg(tag=cpl_frame_get_tag(frm));
00389   check_nomsg(href=cpl_propertylist_load(name,0));
00390   check_nomsg(hout=cpl_propertylist_duplicate(href));
00391 
00392   check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00393   check_nomsg(sx=cpl_image_get_size_x(ima));
00394   check_nomsg(sy=cpl_image_get_size_y(ima));
00395 
00396   for(i=0;i<sy;i++) {
00397     frame=cpl_frame_new();
00398     //img=cpl_image_new(sx,1,CPL_TYPE_FLOAT);
00399     check_nomsg(img=cpl_image_extract(ima,1,i+1,sx,i+1));
00400      check_nomsg(fname=uves_sprintf("slic_%d_%s",i,flames_get_basename(name)));
00401     cpl_image_save(img,fname,CPL_BPP_IEEE_FLOAT,hout,CPL_IO_DEFAULT);
00402     //uves_msg("i=%d tag=%s name=%s",i,tag,name);
00403  
00404 
00405 
00406 
00407     check_nomsg(cpl_frame_set_filename(frame, fname)) ;
00408     check_nomsg(cpl_frame_set_tag(frame, tag)) ;
00409     check_nomsg(cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE)) ;
00410     check_nomsg(cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT)) ;
00411     check(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL),
00412       "Error while initialising the product frame") ;
00413     cpl_frameset_insert(set,frame);
00414     
00415     /*
00416     check_nomsg(cpl_propertylist_erase_regexp(hout, "^ESO PRO CATG",0));
00417     check(cpl_dfs_setup_product_header(hout, 
00418                                        frame, 
00419                                        set, 
00420                                        parameters,
00421                                        "flames_utl_unpack", 
00422                                        PACKAGE "/" PACKAGE_VERSION, 
00423                                        "PRO-1.15"),
00424       "Problem in the product DFS-compliance") ;
00425    
00426    
00427     check( flames_frameset_insert(
00428                 set,
00429                 CPL_FRAME_GROUP_PRODUCT,
00430                 CPL_FRAME_TYPE_IMAGE,
00431                 CPL_FRAME_LEVEL_INTERMEDIATE,
00432                 fname,
00433                 tag,
00434                 hout,
00435                 parameters,
00436                     "pippo",
00437                 PACKAGE "/" PACKAGE_VERSION,
00438                 NULL, NULL, true, 0),
00439      "Could not add image %s (%s) to frameset",
00440      fname, tag);
00441     
00442     */
00443   uves_free_image(&img);
00444 
00445   }
00446 
00447 
00448  cleanup:
00449   cpl_propertylist_delete(href);
00450   cpl_propertylist_delete(hout);
00451   uves_free_image(&ima);
00452   uves_free_image(&img);
00453 
00454   return cpl_error_get_code() ;
00455 
00456 
00457 }
00458 
00459 
00460 
00461 

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1