flames_utl_unpack.c

00001 /* $Id: flames_utl_unpack.c,v 1.1 2008/04/07 07:53:38 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: 2008/04/07 07:53:38 $
00024  * $Revision: 1.1 $
00025  * $Name: uves-3_9_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 <uves_dfs.h>
00051 #include <flames_dfs.h>
00052 #include <uves_chip.h>
00053 #include <uves_pfits.h>
00054 #include <uves_msg.h>
00055 #include <uves_error.h>
00056 #include <uves_utils_wrappers.h>
00057 
00058 #define recipe_id "flames_utl_unpack"
00059 
00060 /*-----------------------------------------------------------------------------
00061                             Functions prototypes
00062  ----------------------------------------------------------------------------*/
00063 
00064 static int flames_utl_unpack_create(cpl_plugin *) ;
00065 static int flames_utl_unpack_exec(cpl_plugin *) ;
00066 static int flames_utl_unpack_destroy(cpl_plugin *) ;
00067 static int flames_utl_unpack(cpl_parameterlist *, cpl_frameset *) ;
00068 
00069 
00070 static cpl_error_code
00071 flames_unpack_spectra_from_image(cpl_frame* frm,
00072                               cpl_parameterlist   *   parameters,
00073                                cpl_frameset* set);
00074 static cpl_error_code
00075 flames_unpack_image_from_cube(cpl_frame* frm,
00076                               cpl_parameterlist   *   parameters,
00077                                cpl_frameset* set);
00078 
00079 /*-----------------------------------------------------------------------------
00080                             Static variables
00081  ----------------------------------------------------------------------------*/
00082 
00083 static char flames_utl_unpack_description[] =
00084 "This recipe unpack flames-uves packed products.\n"
00085 "Information on relevant parameters can be found with\n"
00086 "esorex --params flames_utl_unpack\n"
00087 "esorex --help flames_utl_unpack\n"
00088 "\n";
00089 
00090 /*-----------------------------------------------------------------------------
00091                                 Functions code
00092  ----------------------------------------------------------------------------*/
00093 /*---------------------------------------------------------------------------*/
00097 /*---------------------------------------------------------------------------*/
00098 
00100 /*---------------------------------------------------------------------------*/
00108 /*---------------------------------------------------------------------------*/
00109 int cpl_plugin_get_info(cpl_pluginlist * list)
00110 {
00111     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00112     cpl_plugin  *   plugin = &recipe->interface ;
00113 
00114     cpl_plugin_init(plugin,
00115                     CPL_PLUGIN_API,
00116                     UVES_BINARY_VERSION,
00117                     CPL_PLUGIN_TYPE_RECIPE,
00118                     "flames_utl_unpack",
00119                     "Unpack flames-uves packed frames",
00120                     flames_utl_unpack_description,
00121                     "Andrea Modigliani",
00122                     "Andrea.Modigliani@eso.org",
00123                     uves_get_license(),
00124                     flames_utl_unpack_create,
00125                     flames_utl_unpack_exec,
00126                     flames_utl_unpack_destroy) ;
00127 
00128     cpl_pluginlist_append(list, plugin) ;
00129     
00130     return 0;
00131 }
00132 
00133 /*---------------------------------------------------------------------------*/
00142 /*---------------------------------------------------------------------------*/
00143 static int flames_utl_unpack_create(cpl_plugin * plugin)
00144 {
00145     cpl_recipe      * recipe ;
00146 
00147     /* Get the recipe out of the plugin */
00148     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00149         recipe = (cpl_recipe *)plugin ;
00150     else return -1 ;
00151     cpl_error_reset();
00152     irplib_reset();
00153 
00154     /* Create the parameters list in the cpl_recipe object */
00155     recipe->parameters = cpl_parameterlist_new() ; 
00156 
00157     /* Fill the parameters list */
00158     /* --stropt */
00159  
00160     /* Return */
00161     return 0;
00162 }
00163 
00164 /*---------------------------------------------------------------------------*/
00170 /*---------------------------------------------------------------------------*/
00171 static int flames_utl_unpack_exec(cpl_plugin * plugin)
00172 {
00173     cpl_recipe  *   recipe ;
00174      int code=0;
00175      cpl_errorstate initial_errorstate = cpl_errorstate_get();
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     cpl_error_reset();
00182     irplib_reset();
00183     code = flames_utl_unpack(recipe->parameters, recipe->frames) ;
00184 
00185 
00186     if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00187         /* Dump the error history since recipe execution start.                
00188            At this point the recipe cannot recover from the error */           
00189         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00190     } 
00191 
00192     return code ;
00193 }
00194 
00195 /*---------------------------------------------------------------------------*/
00201 /*---------------------------------------------------------------------------*/
00202 static int flames_utl_unpack_destroy(cpl_plugin * plugin)
00203 {
00204     cpl_recipe  *   recipe ;
00205     
00206     /* Get the recipe out of the plugin */
00207     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00208         recipe = (cpl_recipe *)plugin ;
00209     else return -1 ;
00210 
00211     cpl_parameterlist_delete(recipe->parameters) ; 
00212     return 0 ;
00213 }
00214 
00215 /*---------------------------------------------------------------------------*/
00222 /*---------------------------------------------------------------------------*/
00223 static int 
00224 flames_utl_unpack( cpl_parameterlist   *   parlist, 
00225         cpl_frameset        *   set)
00226 {   
00227     uves_propertylist    *   plist=NULL ;
00228     int n=0;
00229     int i=0;
00230     int naxis=0;
00231     cpl_frame* frm=NULL;
00232     const char* tag=NULL;
00233     char name[80];
00234     cpl_frameset* out=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     check_nomsg(out=cpl_frameset_new());
00254     for(i=0;i<n;i++) {
00255       check_nomsg(frm=cpl_frameset_get_frame(set,i));
00256       check_nomsg(tag=cpl_frame_get_tag(frm));
00257       check_nomsg(sprintf(name,"%s",cpl_frame_get_filename(frm)));
00258       check_nomsg(plist=uves_propertylist_load(name,0));
00259       check_nomsg(naxis=uves_pfits_get_naxis(plist));
00260       //uves_msg("name=%s",name);
00261       if(naxis==3) {
00262         check_nomsg(flames_unpack_image_from_cube(frm,parlist,out));
00263       } else if (naxis==2) {
00264     check_nomsg(flames_unpack_spectra_from_image(frm,parlist,out));
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   char fname[80];
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   check_nomsg(name=cpl_frame_get_filename(frm));
00302   check_nomsg(tag=cpl_frame_get_tag(frm));
00303   check_nomsg(href=uves_propertylist_load(name,0));
00304   check_nomsg(hout=uves_propertylist_duplicate(href));
00305 
00306   check_nomsg(iml=cpl_imagelist_load(name,CPL_TYPE_FLOAT,0));
00307   check_nomsg(size=cpl_imagelist_get_size(iml));
00308   for(i=0;i<size;i++) {
00309     frame=cpl_frame_new();
00310     check_nomsg(img=cpl_imagelist_get(iml,i));
00311     sprintf(fname,"%s%d%s%s","slic_",i,"_",name);
00312     check_nomsg( uves_save_image(img,fname,hout,true, true) );
00313     /*
00314     check( flames_frameset_insert(
00315                 set,
00316                 CPL_FRAME_GROUP_PRODUCT,
00317                 CPL_FRAME_TYPE_IMAGE,
00318                 CPL_FRAME_LEVEL_INTERMEDIATE,
00319                 fname,
00320                 tag,
00321                 hout,
00322                 parameters,
00323                 recipe_id,
00324                 PACKAGE "/" PACKAGE_VERSION,
00325                 NULL, NULL, true, 0),
00326      "Could not add image %s (%s) to frameset",
00327      fname, tag);
00328     */
00329 
00330   }
00331 
00332 
00333  cleanup:
00334   uves_free_propertylist(&href);
00335   uves_free_propertylist(&hout);
00336   uves_free_imagelist(&iml);
00337 
00338 
00339   return cpl_error_get_code() ;
00340 
00341 }
00342 
00343 
00344 
00345 static cpl_error_code
00346 flames_unpack_spectra_from_image(cpl_frame* frm,
00347                                cpl_parameterlist   *   parameters,
00348                                cpl_frameset* set)
00349 {
00350 
00351 
00352 
00353   const char* name=NULL;
00354   char fname[80];
00355   const char* tag=NULL;
00356   cpl_propertylist* href=NULL;
00357   cpl_propertylist* hout=NULL;
00358   cpl_image* ima=NULL;
00359 
00360   int i=0;
00361   cpl_frame* frame=NULL;
00362   cpl_image* img=NULL;
00363   int sx=0;
00364   int sy=0;
00365 
00366   check_nomsg(name=cpl_frame_get_filename(frm));
00367   check_nomsg(tag=cpl_frame_get_tag(frm));
00368   check_nomsg(href=cpl_propertylist_load(name,0));
00369   check_nomsg(hout=cpl_propertylist_duplicate(href));
00370 
00371   check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00372   check_nomsg(sx=cpl_image_get_size_x(ima));
00373   check_nomsg(sy=cpl_image_get_size_y(ima));
00374 
00375   for(i=0;i<sy;i++) {
00376     frame=cpl_frame_new();
00377     img=cpl_image_new(sx,1,CPL_TYPE_FLOAT);
00378     check_nomsg(cpl_image_copy(img,cpl_image_extract(ima,1,i+1,sx,i+1),1,1));
00379     sprintf(fname,"%s%d%s%s","slic_",i,"_",name);
00380     cpl_image_save(img,fname,CPL_BPP_IEEE_FLOAT,hout,CPL_IO_DEFAULT);
00381     //uves_msg("i=%d tag=%s name=%s",i,tag,name);
00382  
00383 
00384 
00385 
00386     check_nomsg(cpl_frame_set_filename(frame, fname)) ;
00387     check_nomsg(cpl_frame_set_tag(frame, tag)) ;
00388     check_nomsg(cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE)) ;
00389     check_nomsg(cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT)) ;
00390     check(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL),
00391       "Error while initialising the product frame") ;
00392     
00393     /*
00394     check_nomsg(cpl_propertylist_erase_regexp(hout, "^ESO PRO CATG",0));
00395     check(cpl_dfs_setup_product_header(hout, 
00396                                        frame, 
00397                                        set, 
00398                                        parameters,
00399                                        "flames_utl_unpack", 
00400                                        PACKAGE "/" PACKAGE_VERSION, 
00401                                        "PRO-1.15"),
00402       "Problem in the product DFS-compliance") ;
00403     */
00404     /*
00405     check( flames_frameset_insert(
00406                 set,
00407                 CPL_FRAME_GROUP_PRODUCT,
00408                 CPL_FRAME_TYPE_IMAGE,
00409                 CPL_FRAME_LEVEL_INTERMEDIATE,
00410                 fname,
00411                 tag,
00412                 hout,
00413                 parameters,
00414                     "pippo",
00415                 PACKAGE "/" PACKAGE_VERSION,
00416                 NULL, NULL, true, 0),
00417      "Could not add image %s (%s) to frameset",
00418      fname, tag);
00419     */
00420 
00421   uves_free_image(&img);
00422 
00423   }
00424 
00425 
00426  cleanup:
00427   cpl_propertylist_delete(href);
00428   cpl_propertylist_delete(hout);
00429   uves_free_image(&ima);
00430 
00431   return cpl_error_get_code() ;
00432 
00433 
00434 }
00435 
00436 
00437 
00438 

Generated on Fri Apr 18 14:11:40 2008 for UVES Pipeline Reference Manual by  doxygen 1.5.1