sinfoni/sinfo_utl_cube_arith.c

00001 /* $Id: sinfo_utl_cube_arith.c,v 1.4 2006/12/01 12:45:36 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: 2006/12/01 12:45:36 $
00024  * $Revision: 1.4 $
00025  * $Name:  $
00026  */
00027 
00028 
00029 #ifdef HAVE_CONFIG_H
00030 #  include <config.h>
00031 #endif
00032 /*----------------------------------------------------------------------------
00033                                 Includes
00034  ----------------------------------------------------------------------------*/
00035 
00036 /*
00037 #include <eclipse.h>
00038 */
00039 #include "sinfo_utl_cube_arith.h"
00040 #include <sinfo_spectrum_ops.h>
00041 #include <sinfo_new_cube_ops.h>
00042 #include "sinfo_key_names.h"
00043 #include "sinfo_pro_types.h"
00044 #include <sinfo_error.h>
00045 #include <sinfo_utils_wrappers.h>
00046 
00047 
00048 
00049 /*-----------------------------------------------------------------------------
00050                             Functions prototypes
00051  ----------------------------------------------------------------------------*/
00052 /*-----------------------------------------------------------------------------
00053                             Static variables
00054  ----------------------------------------------------------------------------*/
00055 
00063 /*-----------------------------------------------------------------------------
00064                                 Functions code
00065  ----------------------------------------------------------------------------*/
00066 
00067 /*---------------------------------------------------------------------------*/
00074 /*---------------------------------------------------------------------------*/
00075 int sinfo_utl_cube_arith(
00076         cpl_parameterlist   *   parlist, 
00077         cpl_frameset        *   framelist)
00078 {
00079     cpl_parameter       *   param =NULL;
00080     const char          *   operation =NULL;
00081     const char          *   name_c =NULL;
00082     const char          *   name_s =NULL;
00083     const char          *   name_o =NULL;
00084     const char          *   name_d ="start";
00085 
00086     /* double                  temp=0 ; */
00087     double                  value=0 ;
00088     double                  def_value=0 ;
00089 
00090     cpl_parameter *         p=NULL;
00091     cpl_frame           *   frm_cube=NULL ;
00092     cpl_frame           *   frm_spct=NULL ;
00093 
00094     cpl_propertylist    *   plist=NULL ;
00095     cpl_image           *   image=NULL ;
00096     cpl_frame           *   product_frame=NULL;
00097     cpl_imagelist       *   cub_ims=NULL;
00098     cpl_imagelist * cube_i=NULL;
00099     cpl_imagelist * cube_o=NULL;
00100     cpl_imagelist * cube_d=NULL;
00101     cpl_image * std_star=NULL;
00102     Vector * bb=NULL;
00103     Vector*   spectrum=NULL;
00104 
00105     /* Identify the RAW and CALIB frames in the input frameset */
00106     check(sinfo_dfs_set_groups(framelist),
00107       "Cannot identify RAW and CALIB frames") ;
00108 
00109  
00110     /* HOW TO RETRIEVE INPUT PARAMETERS */
00111     /* --stropt */
00112     check_nomsg(param = cpl_parameterlist_find(parlist, 
00113                         "sinfoni.sinfo_utl_cube_arith.op"));
00114     check_nomsg(operation = cpl_parameter_get_string(param));
00115     name_o = "out_cube.fits";
00116 
00117 
00118     /* --doubleopt */
00119     check_nomsg(param = cpl_parameterlist_find(parlist,
00120                         "sinfoni.sinfo_utl_cube_arith.value"));
00121     check_nomsg(value = cpl_parameter_get_double(param));
00122 
00123     /* HOW TO ACCESS INPUT DATA */
00124     check(frm_cube = cpl_frameset_find(framelist, SI_UTL_CUBE_ARITH_CUBE),
00125       "SOF does not have a file tagged as %s",SI_UTL_CUBE_ARITH_CUBE);
00126     
00127     check_nomsg(name_c = cpl_frame_get_filename(frm_cube));
00128     check_nomsg(p = cpl_parameterlist_find(parlist, 
00129                         "sinfoni.sinfo_utl_cube_arith.value"));
00130     check_nomsg(def_value = cpl_parameter_get_default_double(p));
00131     if (value == def_value) {
00132        if ((frm_spct = cpl_frameset_find(framelist, 
00133                                          SI_UTL_CUBE_ARITH_SPECTRUM))==NULL) {
00134           sinfo_msg( "SOF does not have a file tagged as %s",
00135                      SI_UTL_CUBE_ARITH_SPECTRUM);
00136           cpl_error_reset();
00137        } else {
00138           sinfo_msg( "SOF contains a file tagged as %s",
00139                      SI_UTL_CUBE_ARITH_SPECTRUM);
00140        name_s = cpl_frame_get_filename(frm_spct);
00141        name_d = "spectrum";
00142        }
00143     }
00144 
00145     if (value == def_value) {
00146        if ((frm_spct = cpl_frameset_find(framelist, PRO_IMA))==NULL) {
00147           sinfo_msg( "SOF does not have a file tagged as %s",PRO_IMA);
00148           cpl_error_reset();
00149        } else {
00150           sinfo_msg( "SOF contains a file tagged as %s",PRO_IMA);
00151        name_s = cpl_frame_get_filename(frm_spct);
00152        name_d = "image";
00153        }
00154     }
00155 
00156     if (frm_spct == NULL) {
00157       sinfo_msg("Assuming operation with a constant");
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       check(std_star=cpl_image_load ((char*)name_s,CPL_TYPE_FLOAT,0,0),
00171                                      "Could not load input STD STAR spectrum");
00172                                    
00173       spectrum  = sinfo_new_image_to_vector(std_star);
00174 
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 
00284     /* Add DataFlow keywords */
00285     check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00286 
00287 
00288     check(cpl_dfs_setup_product_header(plist, product_frame, framelist, 
00289                                        parlist,
00290                                        "si_sinfo_utl_cube_arith", 
00291                                        "SINFONI", KEY_VALUE_HPRO_DID),
00292         "Problem in the product DFS-compliance") ;
00293 
00294     /* Save the file */
00295 
00296     check(cpl_imagelist_save(cube_o, name_o, CPL_BPP_DEFAULT, plist,
00297                        CPL_IO_DEFAULT),
00298         "Could not save product");
00299  
00300     /* Log the saved file in the input frameset */
00301     check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00302 
00303 
00304 
00305  cleanup:
00306 
00307     sinfo_free_svector(&spectrum);
00308     sinfo_free_svector(&bb);
00309     sinfo_free_imagelist(&cube_d);
00310     sinfo_free_imagelist(&cube_i);
00311     sinfo_free_imagelist(&cube_o);
00312     sinfo_free_image(&std_star);
00313     sinfo_free_image(&image);
00314     sinfo_free_imagelist(&cub_ims);
00315     sinfo_free_propertylist(&plist);
00316     
00317     /* Return */
00318     if (cpl_error_get_code()) 
00319         return -1 ;
00320     else 
00321         return 0 ;
00322 }

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