sinfo_utl_ima_arith.c

00001 /* $Id: sinfo_utl_ima_arith.c,v 1.14 2009/01/30 14:56:12 amodigli Exp $
00002  *
00003  * This file is part of the SINFONI 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/01/30 14:56:12 $
00024  * $Revision: 1.14 $
00025  * $Name: HEAD $
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 <sinfo_tpl_utils.h>
00044 #include <sinfo_pfits.h>
00045 #include <sinfo_tpl_dfs.h>
00046 #include <sinfo_key_names.h>
00047 #include <sinfo_pro_types.h>
00048 #include <sinfo_functions.h>
00049 #include <sinfo_msg.h>
00050 #include <sinfo_error.h>
00051 #include <sinfo_utils_wrappers.h>
00052 
00053 /*-----------------------------------------------------------------------------
00054                             Functions prototypes
00055  ----------------------------------------------------------------------------*/
00056 
00057 static int sinfo_utl_ima_arith_create(cpl_plugin *) ;
00058 static int sinfo_utl_ima_arith_exec(cpl_plugin *) ;
00059 static int sinfo_utl_ima_arith_destroy(cpl_plugin *) ;
00060 static int sinfo_utl_ima_arith(cpl_parameterlist *, cpl_frameset *) ;
00061 
00062 /*-----------------------------------------------------------------------------
00063                             Static variables
00064  ----------------------------------------------------------------------------*/
00065 
00066 static char sinfo_utl_ima_arith_description[] =
00067 "This recipe performs image computation.\n"
00068 "The input files are 2 images\n"
00069 "their associated tags should be IMA.\n"
00070 "The output is an image resulting from the IMA op IMA where op indicates\n"
00071 "the operation to be performed specified by the parameter \n"
00072 "sinfoni.sinfo_utl_ima_arith.op having alias 'op'\n"
00073 "Information on relevant parameters can be found with\n"
00074 "esorex --params sinfo_utl_ima_arith\n"
00075 "esorex --help sinfo_utl_ima_arith\n"
00076 "\n";
00077 
00078 /*-----------------------------------------------------------------------------
00079                                 Functions code
00080  ----------------------------------------------------------------------------*/
00081 /*---------------------------------------------------------------------------*/
00085 /*---------------------------------------------------------------------------*/
00086 
00088 /*---------------------------------------------------------------------------*/
00096 /*---------------------------------------------------------------------------*/
00097 int cpl_plugin_get_info(cpl_pluginlist * list)
00098 {
00099     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00100     cpl_plugin  *   plugin = &recipe->interface ;
00101 
00102     cpl_plugin_init(plugin,
00103                     CPL_PLUGIN_API,
00104                     SINFONI_BINARY_VERSION,
00105                     CPL_PLUGIN_TYPE_RECIPE,
00106                     "sinfo_utl_ima_arith",
00107                     "Computes result of ima1 op ima2",
00108                     sinfo_utl_ima_arith_description,
00109                     "Andrea Modigliani",
00110                     "Andrea.Modigliani@eso.org",
00111                     sinfo_get_license(),
00112                     sinfo_utl_ima_arith_create,
00113                     sinfo_utl_ima_arith_exec,
00114                     sinfo_utl_ima_arith_destroy) ;
00115 
00116     cpl_pluginlist_append(list, plugin) ;
00117     
00118     return 0;
00119 }
00120 
00121 /*---------------------------------------------------------------------------*/
00130 /*---------------------------------------------------------------------------*/
00131 static int sinfo_utl_ima_arith_create(cpl_plugin * plugin)
00132 {
00133     cpl_recipe      * recipe ;
00134     cpl_parameter   * p ;
00135 
00136     /* Get the recipe out of the plugin */
00137     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00138         recipe = (cpl_recipe *)plugin ;
00139     else return -1 ;
00140     cpl_error_reset();
00141     irplib_reset();
00142 
00143     /* Create the parameters list in the cpl_recipe object */
00144     recipe->parameters = cpl_parameterlist_new() ; 
00145 
00146     /* Fill the parameters list */
00147     /* --stropt */
00148     p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_arith.op", 
00149                                 CPL_TYPE_STRING, 
00150                                 "A possible operation", 
00151                                 "sinfoni.sinfo_utl_ima_arith","+");
00152     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
00153     cpl_parameterlist_append(recipe->parameters, p) ;
00154 
00155     /* --doubleopt */
00156     p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_arith.value", 
00157             CPL_TYPE_DOUBLE, "a value", "sinfoni.sinfo_utl_ima_arith", 9999.) ;
00158     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "value") ;
00159     cpl_parameterlist_append(recipe->parameters, p) ;
00160  
00161     /* Return */
00162     return 0;
00163 }
00164 
00165 /*---------------------------------------------------------------------------*/
00171 /*---------------------------------------------------------------------------*/
00172 static int sinfo_utl_ima_arith_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 = sinfo_utl_ima_arith(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 sinfo_utl_ima_arith_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 sinfo_utl_ima_arith( cpl_parameterlist   *   parlist, 
00226         cpl_frameset        *   framelist)
00227 {
00228     cpl_parameter       *   param= NULL ;
00229     const char          *   operation=NULL;
00230     double                     value=1 ;
00231     cpl_frame           *   frm_ima1=NULL ;
00232     cpl_frame           *   frm_ima2=NULL ;
00233     cpl_image           *   ima1=NULL ;
00234     cpl_image           *   ima2=NULL ;
00235     int switch_ima2     = 0;
00236     const char                *   name_o=NULL ;
00237     cpl_propertylist    *   plist=NULL ;
00238     cpl_image           *   image=NULL ;
00239     cpl_frame           *   product_frame=NULL;
00240     cpl_frameset * raw_set=NULL;
00241     int nraw=0;
00242     int n=0;
00243           sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00244         SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00245 
00246     /* HOW TO RETRIEVE INPUT PARAMETERS */
00247     /* --stropt */
00248     check_nomsg(param=cpl_parameterlist_find(parlist, 
00249                                              "sinfoni.sinfo_utl_ima_arith.op"));
00250     check_nomsg(operation=cpl_parameter_get_string(param));
00251 
00252     /* --boolopt */
00253     check_nomsg(param=cpl_parameterlist_find(parlist,
00254                                              "sinfoni.sinfo_utl_ima_arith.value"));
00255     check_nomsg(value = cpl_parameter_get_double(param)) ;
00256   
00257     /* Identify the RAW and CALIB frames in the input frameset */
00258     check(sinfo_dfs_set_groups(framelist),
00259          "Cannot identify RAW and CALIB frames") ;
00260  
00261     /* HOW TO ACCESS INPUT DATA */
00262     n=cpl_frameset_get_size(framelist);
00263     if(n<1) {
00264       sinfo_msg_error("Empty input frame list!");
00265       goto cleanup ;
00266     }
00267 
00268     /* HOW TO ACCESS INPUT DATA */
00269     check_nomsg(raw_set=cpl_frameset_new());
00270 
00271     check(sinfo_contains_frames_kind(framelist,raw_set,PRO_IMA),
00272      "Found no input frames with tag %s",PRO_IMA);
00273     check_nomsg(nraw=cpl_frameset_get_size(raw_set));
00274     if (nraw<1) {
00275       sinfo_msg_error("Found no input frames with tag %s",PRO_IMA);
00276       goto cleanup;
00277     } else {
00278          check_nomsg(frm_ima1=cpl_frameset_get_frame(framelist,0));
00279          check_nomsg(ima1=cpl_image_load(cpl_frame_get_filename(frm_ima1),
00280                                          CPL_TYPE_FLOAT,0,0));
00281      if (nraw>1) {
00282          check_nomsg(frm_ima2=cpl_frameset_get_frame(framelist,1));
00283          check_nomsg(ima2 = cpl_image_load(cpl_frame_get_filename(frm_ima2),
00284                                            CPL_TYPE_FLOAT,0,0));
00285          switch_ima2=1;
00286       } else if (value == 9999.) {
00287         sinfo_msg_error("Found only one input frames with tag %s",PRO_IMA);
00288         goto cleanup; 
00289       } else {
00290         sinfo_msg("Perform image arithmetics on frame %s",PRO_IMA);
00291       }
00292     }
00293 
00294     sinfo_free_frameset(&raw_set);
00295 
00296     /* HOW TO GET THE VALUE OF A FITS KEYWORD */
00297     check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_ima1),0),
00298           "Cannot read the FITS header") ;
00299 
00300     /* Now performing the data reduction */
00301     /* Let's generate one image for the example */
00302     if (value == 9999.) {
00303 
00304       if(ima1 != NULL && ima2 != NULL) {
00305     sinfo_msg("ima1 %s ima2",operation);
00306     if (strcmp(operation,"+") == 0 ) {
00307           check(image = cpl_image_add_create(ima1, ima2),
00308                 "Cannot generate the %s image",operation) ;
00309     } else if (strcmp(operation,"-") == 0 ) {
00310           check(image = cpl_image_subtract_create(ima1, ima2),               
00311         "Cannot generate the %s image",operation) ;
00312     } else if (strcmp(operation,"*") == 0 ) {
00313           check(image = cpl_image_multiply_create(ima1, ima2),
00314         "Cannot generate the %s image",operation) ;
00315     } else if (strcmp(operation,"/") == 0 ) {
00316       check(image = cpl_image_divide_create(ima1, ima2),
00317         "Cannot generate the %s image",operation) ;
00318     } else {
00319           sinfo_msg_error("Operation %s not supported",operation);
00320           goto cleanup;
00321     }
00322       sinfo_free_image(&ima1);
00323       sinfo_free_image(&ima2);
00324 
00325       }
00326     
00327     } else {
00328       sinfo_msg("ima1 %s %f",operation,value);
00329        
00330       if(switch_ima2 == 1) {
00331       sinfo_free_image(&ima2);
00332       }
00333        
00334       if (strcmp(operation,"+") == 0 ) {
00335     check(image = cpl_image_add_scalar_create(ima1, value),
00336           "Cannot apply the %s operator",operation) ;
00337       } else if (strcmp(operation,"-") == 0 ) {
00338     check(image = cpl_image_subtract_scalar_create(ima1, value),
00339           "Cannot apply the %s operator",operation) ;
00340       } else if (strcmp(operation,"*") == 0 ) {
00341     check(image = cpl_image_multiply_scalar_create(ima1, value),
00342           "Cannot apply the %s operator",operation) ;
00343       } else if (strcmp(operation,"/") == 0 ) {
00344     check(image = cpl_image_divide_scalar_create(ima1, value),
00345           "Cannot apply the %s operator",operation) ;
00346       } else {
00347     sinfo_msg_error("Operation %s not supported",operation);
00348     goto cleanup;
00349       }
00350     
00351       sinfo_free_image(&ima1);
00352 
00353     }
00354 
00355 
00356     /* HOW TO SAVE A PRODUCT ON DISK  */
00357     /* Set the file name */
00358     name_o = "ima_res.fits" ;
00359 
00360     /* Create product frame */
00361     check_nomsg(product_frame = cpl_frame_new());
00362     check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00363     check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_IMA_ARITH_PROIMA)) ;
00364     check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00365     check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00366     check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00367       "Error while initialising the product frame") ;
00368     
00369     /* Add DataFlow keywords */
00370     check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00371 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)  
00372     check(cpl_dfs_setup_product_header(plist, 
00373                                        product_frame, 
00374                                        framelist, 
00375                                        parlist,
00376                                        "sinfo_utl_ima_arith", 
00377                                        "SINFONI", 
00378                                        KEY_VALUE_HPRO_DID,NULL),
00379       "Problem in the product DFS-compliance") ;
00380 #else
00381     check(cpl_dfs_setup_product_header(plist, 
00382                                        product_frame, 
00383                                        framelist, 
00384                                        parlist,
00385                                        "sinfo_utl_ima_arith", 
00386                                        "SINFONI", 
00387                                        KEY_VALUE_HPRO_DID),
00388       "Problem in the product DFS-compliance") ;
00389 #endif
00390 
00391     /* Save the file */
00392     check(cpl_image_save(image, 
00393                          name_o, 
00394                          CPL_BPP_IEEE_FLOAT, 
00395                          plist,
00396                          CPL_IO_DEFAULT),
00397       "Could not save product");
00398     sinfo_free_propertylist(&plist) ;
00399     sinfo_free_image(&image);
00400 
00401     /* Log the saved file in the input frameset */
00402     check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00403 
00404 
00405  cleanup:
00406    
00407     sinfo_free_image(&ima1);
00408     sinfo_free_image(&ima2);
00409     sinfo_free_frameset(&raw_set);
00410     sinfo_free_propertylist(&plist) ;
00411     /* This is usually freed by esorex: but what about if errors occurs?
00412     sinfo_free_frame(&product_frame) ;
00413     */
00414     sinfo_free_image(&image) ;
00415 
00416     if (cpl_error_get_code()) {
00417         return -1 ;
00418     } else {
00419         return 0 ;
00420     }
00421 
00422 }

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1