sinfo_utl_cube_arith.c

00001 /* $Id: sinfo_utl_cube_arith.c,v 1.14 2012/03/03 10:17:31 amodigli Exp $
00002  *
00003  * This file is part of the IIINSTRUMENT 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: 2012/03/03 10:17:31 $
00024  * $Revision: 1.14 $
00025  * $Name: HEAD $
00026  */
00027 
00028 
00029 #ifdef HAVE_CONFIG_H
00030 #  include <config.h>
00031 #endif
00032 /*----------------------------------------------------------------------------
00033                                 Includes
00034  ----------------------------------------------------------------------------*/
00035 
00036 #include "sinfo_utl_cube_arith.h"
00037 #include <sinfo_spectrum_ops.h>
00038 #include <sinfo_new_cube_ops.h>
00039 #include "sinfo_key_names.h"
00040 #include "sinfo_pro_types.h"
00041 #include <sinfo_error.h>
00042 #include <sinfo_utilities.h>
00043 #include <sinfo_utils_wrappers.h>
00044 
00045 
00046 
00047 /*-----------------------------------------------------------------------------
00048                             Functions prototypes
00049  ----------------------------------------------------------------------------*/
00050 /*-----------------------------------------------------------------------------
00051                             Static variables
00052  ----------------------------------------------------------------------------*/
00053 
00061 /*-----------------------------------------------------------------------------
00062                                 Functions code
00063  ----------------------------------------------------------------------------*/
00064 
00065 /*---------------------------------------------------------------------------*/
00072 /*---------------------------------------------------------------------------*/
00073 int sinfo_utl_cube_arith(
00074    cpl_parameterlist   *   parlist, 
00075    cpl_frameset        *   framelist)
00076 {
00077    cpl_parameter       *   param =NULL;
00078    const char          *   operation =NULL;
00079    const char          *   name_c =NULL;
00080    const char          *   name_s =NULL;
00081    const char          *   name_o =NULL;
00082    const char          *   name_d ="start";
00083 
00084    /* double                  temp=0 ; */
00085    double                  value=0 ;
00086    double                  def_value=0 ;
00087 
00088    cpl_parameter *         p=NULL;
00089    cpl_frame           *   frm_cube=NULL ;
00090    cpl_frame           *   frm_spct=NULL ;
00091 
00092    cpl_propertylist    *   plist=NULL ;
00093    cpl_image           *   image=NULL ;
00094    cpl_frame           *   product_frame=NULL;
00095    cpl_imagelist       *   cub_ims=NULL;
00096    cpl_imagelist * cube_i=NULL;
00097    cpl_imagelist * cube_o=NULL;
00098    cpl_imagelist * cube_d=NULL;
00099    cpl_image * std_star=NULL;
00100    Vector * bb=NULL;
00101    Vector*   spectrum=NULL;
00102    cpl_vector* vec=NULL;
00103    /* Identify the RAW and CALIB frames in the input frameset */
00104    check(sinfo_dfs_set_groups(framelist),
00105          "Cannot identify RAW and CALIB frames") ;
00106 
00107  
00108    /* HOW TO RETRIEVE INPUT PARAMETERS */
00109    /* --stropt */
00110    check_nomsg(param = cpl_parameterlist_find(parlist, 
00111                                               "sinfoni.sinfo_utl_cube_arith.op"));
00112    check_nomsg(operation = cpl_parameter_get_string(param));
00113    name_o = "out_cube.fits";
00114 
00115 
00116    /* --doubleopt */
00117    check_nomsg(param = cpl_parameterlist_find(parlist,
00118                                               "sinfoni.sinfo_utl_cube_arith.value"));
00119    check_nomsg(value = cpl_parameter_get_double(param));
00120 
00121    /* HOW TO ACCESS INPUT DATA */
00122    check(frm_cube = cpl_frameset_find(framelist, SI_UTL_CUBE_ARITH_CUBE),
00123          "SOF does not have a file tagged as %s",SI_UTL_CUBE_ARITH_CUBE);
00124     
00125    check_nomsg(name_c = cpl_frame_get_filename(frm_cube));
00126    check_nomsg(p = cpl_parameterlist_find(parlist, 
00127                                           "sinfoni.sinfo_utl_cube_arith.value"));
00128    check_nomsg(def_value = cpl_parameter_get_default_double(p));
00129    if (value == def_value) {
00130       if ((frm_spct = cpl_frameset_find(framelist, 
00131                                         SI_UTL_CUBE_ARITH_SPECTRUM))==NULL) {
00132          sinfo_msg( "SOF does not have a file tagged as %s",
00133                     SI_UTL_CUBE_ARITH_SPECTRUM);
00134          cpl_error_reset();
00135       } else {
00136          sinfo_msg( "SOF contains a file tagged as %s",
00137                     SI_UTL_CUBE_ARITH_SPECTRUM);
00138          name_s = cpl_frame_get_filename(frm_spct);
00139          name_d = "spectrum";
00140       }
00141    }
00142 
00143    if (value == def_value) {
00144       if ((frm_spct = cpl_frameset_find(framelist, PRO_SPECTRUM))!=NULL) {
00145          sinfo_msg( "SOF contains a file tagged as %s",PRO_SPECTRUM);
00146          name_s = cpl_frame_get_filename(frm_spct);
00147          name_d = "spectrum";
00148       } else if ((frm_spct = cpl_frameset_find(framelist, PRO_IMA))!=NULL) {
00149          sinfo_msg( "SOF contains a file tagged as %s",PRO_IMA);
00150          name_s = cpl_frame_get_filename(frm_spct);
00151          name_d = "image";
00152       } else {
00153          sinfo_msg( "SOF does not contains a file tagged as %s nor as %s",
00154             PRO_SPECTRUM,PRO_IMA);
00155          sinfo_msg( "Assume operation by a constant");
00156          cpl_error_reset();
00157       }
00158    }
00159    check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cube),0),
00160          "Cannot read the FITS header") ;
00161      
00162 
00163    /* Now performing the data reduction */
00164    /* Let's generate one image for the example */
00165    check_nomsg(cube_i= cpl_imagelist_load((char*)name_c,CPL_TYPE_FLOAT,0));
00166    if (value == def_value) {
00167       sinfo_msg("value equal to default");
00168       if( strcmp(name_d,"spectrum") == 0 ) {
00169          sinfo_msg("Operation by spectrum");
00170          sinfo_msg("loading spectrum image file %s",name_s);
00171          check_nomsg(std_star=cpl_image_load((char*)name_s,CPL_TYPE_FLOAT,0,0));
00172 
00173          //check_nomsg(std_star=sinfo_vector_to_image(vec,CPL_TYPE_FLOAT));
00174          check_nomsg(spectrum  = sinfo_new_image_to_vector(std_star));
00175          if (strcmp(operation,"/") == 0) {
00176             cube_o = sinfo_new_div_cube_by_spectrum (cube_i, spectrum);
00177          } else if (strcmp(operation,"-") == 0) {
00178             cube_o = sinfo_new_sub_spectrum_from_cube(cube_i, spectrum);
00179          } else if (strcmp(operation,"+") == 0) {
00180             cube_o = sinfo_new_add_spectrum_to_cube(cube_i, spectrum);
00181          } else if (strcmp(operation,"*") == 0) {
00182             cube_o = sinfo_new_mul_spectrum_to_cube(cube_i, spectrum);
00183             /*
00184               } else if (strcmp(operation,"calib") == 0) {
00185               bb = sinfo_new_blackbody_spectrum ((char*)name_s, temp); 
00186               cube_d   = sinfo_new_div_cube_by_Spectrum (cube_i, spectrum);
00187               cube_o = sinfo_new_mul_spectrum_to_cube(cube_d, bb); 
00188             */
00189       
00190          } else {
00191             sinfo_msg_error("operation %s not supported",operation);
00192             sinfo_msg("If an input spectrum is given and no value is set");
00193             sinfo_msg("operations supported are: `/`,`-`,`+`,'*'");
00194             goto cleanup;
00195          }
00196          if (strcmp(operation,"calib") == 0) {
00197             goto cleanup;
00198          }
00199       } else if ( strcmp(name_d,"image") == 0) {
00200          sinfo_msg("Operation by image");
00201          check_nomsg(std_star=cpl_image_load ((char*)name_s,
00202                                               CPL_TYPE_FLOAT,0,0));
00203          if(strcmp(operation,"+") == 0) {
00204             cknull(cube_o = sinfo_new_add_image_to_cube(cube_i,std_star),
00205                    "operation %s failed",operation);
00206          } else if (strcmp(operation,"-") == 0) {
00207             cknull(cube_o = sinfo_new_sub_image_from_cube(cube_i,std_star),
00208                    "operation %s failed",operation);
00209          } else if (strcmp(operation,"*") == 0) {
00210             cknull(cube_o = sinfo_new_mul_image_to_cube(cube_i,std_star),
00211                    "operation %s failed",operation);
00212          } else if (strcmp(operation,"/") == 0) {
00213             cknull(cube_o = sinfo_new_div_cube_by_image(cube_i,std_star),
00214                    "operation %s failed",operation);
00215          } else {
00216             sinfo_msg_error("operation %s not supported",operation);
00217             sinfo_msg("If an input image is set");
00218             sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
00219             goto cleanup;
00220          }
00221       } else {
00222          sinfo_msg("Operation %s by constant",operation);
00223 
00224          check_nomsg(cube_o = cpl_imagelist_duplicate(cube_i));
00225 
00226          if(strcmp(operation,"+") == 0) {
00227             check(cpl_imagelist_add_scalar(cube_o,value),
00228                   "operation %s failed",operation);
00229          } else if (strcmp(operation,"-") == 0) {
00230             check(cpl_imagelist_subtract_scalar(cube_o,value),
00231                   "operation %s failed",operation);
00232          } else if (strcmp(operation,"*") == 0) {
00233             check(cpl_imagelist_multiply_scalar(cube_o,value),
00234                   "operation %s failed",operation);
00235          } else if (strcmp(operation,"/") == 0) {
00236             check(cpl_imagelist_divide_scalar(cube_o,value),
00237                   "operation %s failed",operation);
00238          } else {
00239             sinfo_msg_error("operation %s not supported",operation);
00240             sinfo_msg("If an input value is set");
00241             sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
00242             goto cleanup;
00243          }
00244 
00245       } /* env case op by constant && value == default */
00246 
00247    } else {
00248 
00249       sinfo_msg("Operation %s by constant",operation);
00250 
00251       check_nomsg(cube_o = cpl_imagelist_duplicate(cube_i));
00252 
00253       if(strcmp(operation,"+") == 0) {
00254          check(cpl_imagelist_add_scalar(cube_o,value),
00255                "operation %s failed",operation);
00256       } else if (strcmp(operation,"-") == 0) {
00257          check(cpl_imagelist_subtract_scalar(cube_o,value),
00258                "operation %s failed",operation);
00259       } else if (strcmp(operation,"*") == 0) {
00260          check(cpl_imagelist_multiply_scalar(cube_o,value),
00261                "operation %s failed",operation);
00262       } else if (strcmp(operation,"/") == 0) {
00263          check(cpl_imagelist_divide_scalar(cube_o,value),
00264                "operation %s failed",operation);
00265       } else {
00266          sinfo_msg_error("operation %s not supported",operation);
00267          sinfo_msg("If an input value is set");
00268          sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
00269          goto cleanup;
00270       }
00271 
00272    }
00273    /* HOW TO SAVE A PRODUCT ON DISK  */
00274    /* Set the file name */
00275    /* Create product frame */
00276    check_nomsg(product_frame = cpl_frame_new());
00277    check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00278    check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE_ARITH_PROCUBE)) ;
00279    check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00280    check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00281    check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00282            "Error while initialising the product frame") ;
00283  /* Add DataFlow keywords */
00284    check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00285 
00286 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)  
00287    check(cpl_dfs_setup_product_header(plist, product_frame, framelist, 
00288                                       parlist,
00289                                       "si_sinfo_utl_cube_arith", 
00290                                       "SINFONI", KEY_VALUE_HPRO_DID,NULL),
00291          "Problem in the product DFS-compliance") ;
00292 #else
00293    check(cpl_dfs_setup_product_header(plist, product_frame, framelist, 
00294                                       parlist,
00295                                       "si_sinfo_utl_cube_arith", 
00296                                       "SINFONI", KEY_VALUE_HPRO_DID),
00297          "Problem in the product DFS-compliance") ;
00298 #endif
00299 
00300   /* Save the file */
00301    check(cpl_imagelist_save(cube_o, name_o, CPL_BPP_IEEE_FLOAT, plist,
00302                             CPL_IO_DEFAULT),
00303          "Could not save product");
00304  
00305 
00306    /* Log the saved file in the input frameset */
00307    check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00308 
00309 
00310 
00311   cleanup:
00312    sinfoni_free_vector(&vec);
00313    sinfo_free_svector(&spectrum);
00314    sinfo_free_svector(&bb);
00315 
00316    sinfo_free_imagelist(&cube_d);
00317    sinfo_free_imagelist(&cube_i);
00318    sinfo_free_imagelist(&cube_o);
00319    //If I free the next image I get core dump!
00320    //sinfo_free_image(&std_star);
00321    sinfo_free_image(&image);
00322    sinfo_free_imagelist(&cub_ims);
00323    sinfo_free_propertylist(&plist);
00324     
00325     /* Return */
00326    if (cpl_error_get_code()) 
00327       return -1 ;
00328    else 
00329       return 0 ;
00330 }

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