sinfo_utl_ima_arith.c

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

Generated on Wed Jan 17 08:33:44 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4