uves_utl_ima_arith.c

00001 /* $Id: uves_utl_ima_arith.c,v 1.1 2009/02/18 12:19: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: 2009/02/18 12:19:38 $
00024  * $Revision: 1.1 $
00025  * $Name: uves-4_9_1 $
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_pfits.h>
00045 #include <uves_dfs.h>
00046 //#include <uves_key_names.h>
00047 //#include <uves_pro_types.h>
00048 //#include <uves_functions.h>
00049 #include <uves_msg.h>
00050 #include <uves_error.h>
00051 #include <uves_utils_wrappers.h>
00052 #define KEY_VALUE_HPRO_DID                 "PRO-1.15"
00053 #define PRO_IMA "IMA"
00054 /*-----------------------------------------------------------------------------
00055                             Functions prototypes
00056  ----------------------------------------------------------------------------*/
00057 
00058 static int uves_utl_ima_arith_create(cpl_plugin *) ;
00059 static int uves_utl_ima_arith_exec(cpl_plugin *) ;
00060 static int uves_utl_ima_arith_destroy(cpl_plugin *) ;
00061 static int uves_utl_ima_arith(cpl_parameterlist *, cpl_frameset *) ;
00062 
00063 /*-----------------------------------------------------------------------------
00064                             Static variables
00065  ----------------------------------------------------------------------------*/
00066 
00067 static char uves_utl_ima_arith_description[] =
00068 "This recipe performs image computation.\n"
00069 "The input files are 2 images\n"
00070 "their associated tags should be IMA.\n"
00071 "The output is an image resulting from the IMA op IMA where op indicates\n"
00072 "the operation to be performed specified by the parameter \n"
00073 "uves.uves_utl_ima_arith.op having alias 'op'\n"
00074 "Information on relevant parameters can be found with\n"
00075 "esorex --params uves_utl_ima_arith\n"
00076 "esorex --help uves_utl_ima_arith\n"
00077 "\n";
00078 
00079 /*-----------------------------------------------------------------------------
00080                                 Functions code
00081  ----------------------------------------------------------------------------*/
00082 /*---------------------------------------------------------------------------*/
00086 /*---------------------------------------------------------------------------*/
00087 
00089 /*---------------------------------------------------------------------------*/
00097 /*---------------------------------------------------------------------------*/
00098 int cpl_plugin_get_info(cpl_pluginlist * list)
00099 {
00100     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00101     cpl_plugin  *   plugin = &recipe->interface ;
00102 
00103     cpl_plugin_init(plugin,
00104                     CPL_PLUGIN_API,
00105                     UVES_BINARY_VERSION,
00106                     CPL_PLUGIN_TYPE_RECIPE,
00107                     "uves_utl_ima_arith",
00108                     "Computes result of ima1 op ima2",
00109                     uves_utl_ima_arith_description,
00110                     "Andrea Modigliani",
00111                     "Andrea.Modigliani@eso.org",
00112                     uves_get_license(),
00113                     uves_utl_ima_arith_create,
00114                     uves_utl_ima_arith_exec,
00115                     uves_utl_ima_arith_destroy) ;
00116 
00117     cpl_pluginlist_append(list, plugin) ;
00118     
00119     return 0;
00120 }
00121 
00122 /*---------------------------------------------------------------------------*/
00131 /*---------------------------------------------------------------------------*/
00132 static int uves_utl_ima_arith_create(cpl_plugin * plugin)
00133 {
00134     cpl_recipe      * recipe ;
00135     cpl_parameter   * p ;
00136 
00137     /* Get the recipe out of the plugin */
00138     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00139         recipe = (cpl_recipe *)plugin ;
00140     else return -1 ;
00141     cpl_error_reset();
00142     irplib_reset();
00143 
00144     /* Create the parameters list in the cpl_recipe object */
00145     recipe->parameters = cpl_parameterlist_new() ; 
00146 
00147     /* Fill the parameters list */
00148     /* --stropt */
00149     p = cpl_parameter_new_value("uves.uves_utl_ima_arith.op", 
00150                                 CPL_TYPE_STRING, 
00151                                 "A possible operation", 
00152                                 "uves.uves_utl_ima_arith","+");
00153     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
00154     cpl_parameterlist_append(recipe->parameters, p) ;
00155 
00156     /* --doubleopt */
00157     p = cpl_parameter_new_value("uves.uves_utl_ima_arith.value", 
00158             CPL_TYPE_DOUBLE, "a value", "uves.uves_utl_ima_arith", 9999.) ;
00159     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "value") ;
00160     cpl_parameterlist_append(recipe->parameters, p) ;
00161  
00162     /* Return */
00163     return 0;
00164 }
00165 
00166 /*---------------------------------------------------------------------------*/
00172 /*---------------------------------------------------------------------------*/
00173 static int uves_utl_ima_arith_exec(cpl_plugin * plugin)
00174 {
00175     cpl_recipe  *   recipe ;
00176      int code=0;
00177      cpl_errorstate initial_errorstate = cpl_errorstate_get();
00178 
00179     /* Get the recipe out of the plugin */
00180     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00181         recipe = (cpl_recipe *)plugin ;
00182     else return -1 ;
00183     cpl_error_reset();
00184     irplib_reset();
00185     code = uves_utl_ima_arith(recipe->parameters, recipe->frames) ;
00186 
00187 
00188     if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00189         /* Dump the error history since recipe execution start.                
00190            At this point the recipe cannot recover from the error */           
00191         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00192     } 
00193 
00194     return code ;
00195 }
00196 
00197 /*---------------------------------------------------------------------------*/
00203 /*---------------------------------------------------------------------------*/
00204 static int uves_utl_ima_arith_destroy(cpl_plugin * plugin)
00205 {
00206     cpl_recipe  *   recipe ;
00207     
00208     /* Get the recipe out of the plugin */
00209     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00210         recipe = (cpl_recipe *)plugin ;
00211     else return -1 ;
00212 
00213     cpl_parameterlist_delete(recipe->parameters) ; 
00214     return 0 ;
00215 }
00216 
00217 /*---------------------------------------------------------------------------*/
00224 /*---------------------------------------------------------------------------*/
00225 static int 
00226 uves_utl_ima_arith( cpl_parameterlist   *   parlist, 
00227         cpl_frameset        *   framelist)
00228 {
00229     cpl_parameter       *   param= NULL ;
00230     const char          *   operation=NULL;
00231     double                     value=1 ;
00232     cpl_frame           *   frm_ima1=NULL ;
00233     cpl_frame           *   frm_ima2=NULL ;
00234     cpl_image           *   ima1=NULL ;
00235     cpl_image           *   ima2=NULL ;
00236     int switch_ima2     = 0;
00237     const char                *   name_o=NULL ;
00238     cpl_propertylist    *   plist=NULL ;
00239     cpl_image           *   image=NULL ;
00240     cpl_frame           *   product_frame=NULL;
00241     cpl_frameset * raw_set=NULL;
00242     int nraw=0;
00243     int n=0;
00244           uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
00245         UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
00246 
00247     /* HOW TO RETRIEVE INPUT PARAMETERS */
00248     /* --stropt */
00249     check_nomsg(param=cpl_parameterlist_find(parlist, 
00250                                              "uves.uves_utl_ima_arith.op"));
00251     check_nomsg(operation=cpl_parameter_get_string(param));
00252 
00253     /* --boolopt */
00254     check_nomsg(param=cpl_parameterlist_find(parlist,
00255                                              "uves.uves_utl_ima_arith.value"));
00256     check_nomsg(value = cpl_parameter_get_double(param)) ;
00257   
00258     /* Identify the RAW and CALIB frames in the input frameset */
00259     check(uves_dfs_set_groups(framelist),
00260          "Cannot identify RAW and CALIB frames") ;
00261  
00262     /* HOW TO ACCESS INPUT DATA */
00263     n=cpl_frameset_get_size(framelist);
00264     if(n<1) {
00265       uves_msg_error("Empty input frame list!");
00266       goto cleanup ;
00267     }
00268 
00269     /* HOW TO ACCESS INPUT DATA */
00270     check_nomsg(raw_set=cpl_frameset_new());
00271 
00272     check(uves_contains_frames_kind(framelist,raw_set,PRO_IMA),
00273      "Found no input frames with tag %s",PRO_IMA);
00274     check_nomsg(nraw=cpl_frameset_get_size(raw_set));
00275     if (nraw<1) {
00276       uves_msg_error("Found no input frames with tag %s",PRO_IMA);
00277       goto cleanup;
00278     } else {
00279          check_nomsg(frm_ima1=cpl_frameset_get_frame(framelist,0));
00280          check_nomsg(ima1=cpl_image_load(cpl_frame_get_filename(frm_ima1),
00281                                          CPL_TYPE_FLOAT,0,0));
00282      if (nraw>1) {
00283          check_nomsg(frm_ima2=cpl_frameset_get_frame(framelist,1));
00284          check_nomsg(ima2 = cpl_image_load(cpl_frame_get_filename(frm_ima2),
00285                                            CPL_TYPE_FLOAT,0,0));
00286          switch_ima2=1;
00287       } else if (value == 9999.) {
00288         uves_msg_error("Found only one input frames with tag %s",PRO_IMA);
00289         goto cleanup; 
00290       } else {
00291         uves_msg("Perform image arithmetics on frame %s",PRO_IMA);
00292       }
00293     }
00294 
00295     uves_free_frameset(&raw_set);
00296 
00297     /* HOW TO GET THE VALUE OF A FITS KEYWORD */
00298     check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_ima1),0),
00299           "Cannot read the FITS header") ;
00300 
00301     /* Now performing the data reduction */
00302     /* Let's generate one image for the example */
00303     if (value == 9999.) {
00304 
00305       if(ima1 != NULL && ima2 != NULL) {
00306     uves_msg("ima1 %s ima2",operation);
00307     if (strcmp(operation,"+") == 0 ) {
00308           check(image = cpl_image_add_create(ima1, ima2),
00309                 "Cannot generate the %s image",operation) ;
00310     } else if (strcmp(operation,"-") == 0 ) {
00311           check(image = cpl_image_subtract_create(ima1, ima2),               
00312         "Cannot generate the %s image",operation) ;
00313     } else if (strcmp(operation,"*") == 0 ) {
00314           check(image = cpl_image_multiply_create(ima1, ima2),
00315         "Cannot generate the %s image",operation) ;
00316     } else if (strcmp(operation,"/") == 0 ) {
00317       check(image = cpl_image_divide_create(ima1, ima2),
00318         "Cannot generate the %s image",operation) ;
00319     } else {
00320           uves_msg_error("Operation %s not supported",operation);
00321           goto cleanup;
00322     }
00323       uves_free_image(&ima1);
00324       uves_free_image(&ima2);
00325 
00326       }
00327     
00328     } else {
00329       uves_msg("ima1 %s %f",operation,value);
00330        
00331       if(switch_ima2 == 1) {
00332       uves_free_image(&ima2);
00333       }
00334        
00335       if (strcmp(operation,"+") == 0 ) {
00336     check(image = cpl_image_add_scalar_create(ima1, value),
00337           "Cannot apply the %s operator",operation) ;
00338       } else if (strcmp(operation,"-") == 0 ) {
00339     check(image = cpl_image_subtract_scalar_create(ima1, value),
00340           "Cannot apply the %s operator",operation) ;
00341       } else if (strcmp(operation,"*") == 0 ) {
00342     check(image = cpl_image_multiply_scalar_create(ima1, value),
00343           "Cannot apply the %s operator",operation) ;
00344       } else if (strcmp(operation,"/") == 0 ) {
00345     check(image = cpl_image_divide_scalar_create(ima1, value),
00346           "Cannot apply the %s operator",operation) ;
00347       } else {
00348     uves_msg_error("Operation %s not supported",operation);
00349     goto cleanup;
00350       }
00351     
00352       uves_free_image(&ima1);
00353 
00354     }
00355 
00356 
00357     /* HOW TO SAVE A PRODUCT ON DISK  */
00358     /* Set the file name */
00359     name_o = "ima_res.fits" ;
00360 
00361     /* Create product frame */
00362     check_nomsg(product_frame = cpl_frame_new());
00363     check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00364     check_nomsg(cpl_frame_set_tag(product_frame, PRO_IMA)) ;
00365     check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00366     check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00367     check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00368       "Error while initialising the product frame") ;
00369     
00370     /* Add DataFlow keywords */
00371     check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00372 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)  
00373     check(cpl_dfs_setup_product_header(plist, 
00374                                        product_frame, 
00375                                        framelist, 
00376                                        parlist,
00377                                        "uves_utl_ima_arith", 
00378                                        "UVES", 
00379                                        KEY_VALUE_HPRO_DID,NULL),
00380       "Problem in the product DFS-compliance") ;
00381 #else
00382     check(cpl_dfs_setup_product_header(plist, 
00383                                        product_frame, 
00384                                        framelist, 
00385                                        parlist,
00386                                        "uves_utl_ima_arith", 
00387                                        "UVES", 
00388                                        KEY_VALUE_HPRO_DID),
00389       "Problem in the product DFS-compliance") ;
00390 #endif
00391 
00392     /* Save the file */
00393     check(cpl_image_save(image, 
00394                          name_o, 
00395                          CPL_BPP_IEEE_FLOAT, 
00396                          plist,
00397                          CPL_IO_DEFAULT),
00398       "Could not save product");
00399 
00400     if (plist != NULL) {
00401         cpl_propertylist_delete(plist) ; 
00402         plist=NULL;
00403     }
00404     
00405     uves_free_image(&image);
00406 
00407     /* Log the saved file in the input frameset */
00408     check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00409 
00410 
00411  cleanup:
00412    
00413     uves_free_image(&ima1);
00414     uves_free_image(&ima2);
00415     uves_free_frameset(&raw_set);
00416     if (plist != NULL) {
00417         cpl_propertylist_delete(plist) ; 
00418         plist=NULL;
00419     }
00420     /* This is usually freed by esorex: but what about if errors occurs?
00421     uves_free_frame(&product_frame) ;
00422     */
00423     uves_free_image(&image) ;
00424 
00425     if (cpl_error_get_code()) {
00426         return -1 ;
00427     } else {
00428         return 0 ;
00429     }
00430 
00431 }

Generated on 8 Mar 2011 for UVES Pipeline Reference Manual by  doxygen 1.6.1