utl_cube_arith.c

00001 /* $Id: utl_cube_arith.c,v 1.9 2005/10/14 14:04:50 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: 2005/10/14 14:04:50 $
00024  * $Revision: 1.9 $
00025  * $Name:  $
00026  */
00027 
00028 
00029 /*-----------------------------------------------------------------------------
00030                                 Includes
00031  -----------------------------------------------------------------------------*/
00032 
00033 
00034 #include "utl_cube_arith.h"
00035 #include <eclipse.h>
00036 #include <spectrum_ops.h>
00037 #include "sinfoni_key_names.h"
00038 #include "sinfoni_pro_types.h"
00039 
00040 
00041 /*-----------------------------------------------------------------------------
00042                             Functions prototypes
00043  -----------------------------------------------------------------------------*/
00044 /*-----------------------------------------------------------------------------
00045                             Static variables
00046  -----------------------------------------------------------------------------*/
00047 
00048 
00049 /*-----------------------------------------------------------------------------
00050                                 Functions code
00051  -----------------------------------------------------------------------------*/
00052 
00053 /*----------------------------------------------------------------------------*/
00060 /*----------------------------------------------------------------------------*/
00061 int si_utl_cube_arith(
00062         cpl_parameterlist   *   parlist, 
00063         cpl_frameset        *   framelist)
00064 {
00065     const char          *   fctid = "si_utl_cube_arith" ;
00066     cpl_parameter       *   param =NULL;
00067     const char          *   operation =NULL;
00068     const char          *   name_c =NULL;
00069     const char          *   name_s =NULL;
00070     const char          *   name_o =NULL;
00071     const char          *   name_d ="start";
00072 
00073     /* double                  temp=0 ; */
00074     double                  value=0 ;
00075     double                  def_value=0 ;
00076 
00077     cpl_parameter *         p=NULL;
00078     cpl_frame           *   frm_cube=NULL ;
00079     cpl_frame           *   frm_spct=NULL ;
00080 
00081     cpl_propertylist    *   plist=NULL ;
00082     cpl_image           *   image=NULL ;
00083     cpl_frame           *   product_frame=NULL;
00084     cpl_imagelist       *   cub_ims=NULL;
00085     OneCube * cube_i=NULL;
00086     OneCube * cube_o=NULL;
00087     OneCube * cube_d=NULL;
00088     OneImage * std_star=NULL;
00089     Vector * bb=NULL;
00090     Vector*   spectrum=NULL;
00091     int i=0;
00092     OneImage * eimg=NULL;
00093     cpl_image * cimg=NULL;
00094     cpl_image * wimg=NULL;
00095 
00096     /* Identify the RAW and CALIB frames in the input frameset */
00097     if (sinfoni_dfs_set_groups(framelist)) {
00098         cpl_msg_error(fctid, "Cannot identify RAW and CALIB frames") ;
00099         return -1 ;
00100     }
00101  
00102     /* HOW TO RETRIEVE INPUT PARAMETERS */
00103     /* --stropt */
00104     param = cpl_parameterlist_find(parlist, "sinfoni.si_utl_cube_arith.op");
00105     operation = cpl_parameter_get_string(param);
00106 
00107 
00108     name_o = "out_cube.fits";
00109 
00110 
00111       /* --doubleopt */
00112     /*
00113     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube_arith.temperature");
00114     temp = cpl_parameter_get_double(param) ;
00115     */
00116 
00117     /* --doubleopt */
00118     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube_arith.value");
00119     value = cpl_parameter_get_double(param) ;
00120 
00121     /* HOW TO ACCESS INPUT DATA */
00122     if ((frm_cube = cpl_frameset_find(framelist, SI_UTL_CUBE_ARITH_CUBE))==NULL) {
00123         cpl_msg_error(fctid, "SOF does not have a file tagged as %s",SI_UTL_CUBE_ARITH_CUBE);
00124         return -1 ;
00125     }
00126     name_c = cpl_frame_get_filename(frm_cube);
00127     p = cpl_parameterlist_find(parlist, "sinfoni.si_utl_cube_arith.value");
00128     def_value = cpl_parameter_get_default_double(p);
00129     if (value == def_value) {
00130        if ((frm_spct = cpl_frameset_find(framelist, SI_UTL_CUBE_ARITH_SPECTRUM))==NULL) {
00131           cpl_msg_info(fctid, "SOF does not have a file tagged as %s",SI_UTL_CUBE_ARITH_SPECTRUM);
00132           cpl_error_reset();
00133        } else {
00134           cpl_msg_info(fctid, "SOF contains a file tagged as %s",SI_UTL_CUBE_ARITH_SPECTRUM);
00135        name_s = cpl_frame_get_filename(frm_spct);
00136        name_d = "spectrum";
00137        }
00138     }
00139 
00140     if (value == def_value) {
00141        if ((frm_spct = cpl_frameset_find(framelist, PRO_IMA))==NULL) {
00142           cpl_msg_info(fctid, "SOF does not have a file tagged as %s",PRO_IMA);
00143           cpl_error_reset();
00144        } else {
00145           cpl_msg_info(fctid, "SOF contains a file tagged as %s",PRO_IMA);
00146        name_s = cpl_frame_get_filename(frm_spct);
00147        name_d = "image";
00148        }
00149     }
00150 
00151     if (frm_spct == NULL) {
00152       cpl_msg_info(fctid,"Assuming operation with a constant");
00153     }
00154 
00155     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cube), 
00156                     0)) == NULL) {
00157         cpl_msg_error(fctid, "Cannot read the FITS header") ;
00158         return -1 ;
00159     }
00160     /* Now performing the data reduction */
00161     /* Let's generate one image for the example */
00162     cube_i    = load_cube((char*)name_c);
00163     if (value == def_value) {
00164       cpl_msg_info(fctid,"value equal to default");
00165       if( strcmp(name_d,"spectrum") == 0 ) {
00166       cpl_msg_info(fctid,"Operation by spectrum");
00167       std_star  = load_image ((char*)name_s);
00168       if(std_star==NULL) {
00169 
00170          cpl_msg_error(fctid,"Could not load input STD STAR spectrum");
00171          destroy_cube(cube_i);
00172          cpl_propertylist_delete(plist);
00173          return -1;
00174       }                                   
00175       spectrum  = imageToVector(std_star);
00176 
00177       if (strcmp(operation,"/") == 0) {
00178              cube_o = divCubeBySpectrum (cube_i, spectrum);
00179       } else if (strcmp(operation,"-") == 0) {
00180              cube_o = subSpectrumFromCube(cube_i, spectrum);
00181       } else if (strcmp(operation,"+") == 0) {
00182              cube_o = addSpectrumToCube(cube_i, spectrum);
00183       } else if (strcmp(operation,"*") == 0) {
00184              cube_o = mulSpectrumToCube(cube_i, spectrum);
00185       /*
00186           } else if (strcmp(operation,"calib") == 0) {
00187              bb = blackbodySpectrum ((char*)name_s, temp); 
00188              cube_d   = divCubeBySpectrum (cube_i, spectrum);
00189              cube_o = mulSpectrumToCube(cube_d, bb); 
00190       */
00191       
00192       } else {
00193          cpl_msg_error(fctid,"operation %s not supported",operation);
00194              cpl_msg_info(fctid,"If an input spectrum is given and no value is set");
00195              cpl_msg_info(fctid,"operations supported are: `/`,`-`,`+`,'*'");
00196              destroyVector(spectrum);
00197              destroy_cube(cube_i);
00198              cpl_propertylist_delete(plist);
00199              return -1;
00200       }
00201       if (strcmp(operation,"calib") == 0) {
00202              destroy_cube(cube_d);
00203              destroyVector(bb);
00204       }
00205      } else if ( strcmp(name_d,"image") == 0) {
00206           cpl_msg_info(fctid,"Operation by image");
00207       std_star  = load_image ((char*)name_s);
00208       if(strcmp(operation,"+") == 0) {
00209           if (NULL == (cube_o = addImageToCube(cube_i,std_star))){
00210              cpl_msg_error(fctid,"operation %s failed",operation);
00211                  destroy_cube(cube_i);
00212                  destroy_image(std_star);
00213                  cpl_imagelist_delete(cub_ims);
00214                  cpl_propertylist_delete(plist);
00215                  return -1;
00216           }
00217       } else if (strcmp(operation,"-") == 0) {
00218           if(NULL == (cube_o = subImageFromCube(cube_i,std_star))) {
00219              cpl_msg_error(fctid,"operation %s failed",operation);
00220                  destroy_cube(cube_i);
00221                  destroy_image(std_star);
00222                  cpl_imagelist_delete(cub_ims);
00223                  cpl_propertylist_delete(plist);
00224                  return -1;
00225           }
00226       } else if (strcmp(operation,"*") == 0) {
00227           if(NULL == (cube_o = mulImageToCube(cube_i,std_star))) {
00228              cpl_msg_error(fctid,"operation %s failed",operation);
00229                  destroy_cube(cube_i);
00230                  destroy_image(std_star);
00231                  cpl_imagelist_delete(cub_ims);
00232                  cpl_propertylist_delete(plist);
00233                  return -1;
00234           }
00235       } else if (strcmp(operation,"/") == 0) {
00236           if(NULL == (cube_o = divCubeByImage(cube_i,std_star))) {
00237              cpl_msg_error(fctid,"operation %s failed",operation);
00238                  destroy_cube(cube_i);
00239                  destroy_image(std_star);
00240                  cpl_imagelist_delete(cub_ims);
00241                  cpl_propertylist_delete(plist);
00242                  return -1;
00243           }
00244       } else {
00245           cpl_msg_error(fctid,"operation %s not supported",operation);
00246           cpl_msg_info(fctid,"If an input image is set");
00247           cpl_msg_info(fctid,"operations supported are: `-`, `+`, `*`, `/`");
00248               destroy_cube(cube_i);
00249               destroy_image(std_star);
00250               cpl_imagelist_delete(cub_ims);
00251               cpl_propertylist_delete(plist);
00252               return -1;
00253       }
00254       } else {
00255     cpl_msg_info(fctid,"Operation %s by constant",operation);
00256 
00257     cube_o = copy_cube(cube_i);
00258 
00259     if(strcmp(operation,"+") == 0) {
00260       if(-1 == cst_op_cube(cube_o,value,'+') ) {
00261         cpl_msg_error(fctid,"operation %s failed",operation);
00262             destroy_cube(cube_i);
00263             destroy_cube(cube_o);
00264             cpl_imagelist_delete(cub_ims);
00265             cpl_propertylist_delete(plist);
00266             return -1;
00267       }
00268     } else if (strcmp(operation,"-") == 0) {
00269       if(-1 == cst_op_cube(cube_o,value,'-') ) {
00270         cpl_msg_error(fctid,"operation %s failed",operation);
00271             destroy_cube(cube_i);
00272             destroy_cube(cube_o);
00273             cpl_imagelist_delete(cub_ims);
00274             cpl_propertylist_delete(plist);
00275             return -1;
00276       }
00277     } else if (strcmp(operation,"*") == 0) {
00278       if(-1 == cst_op_cube(cube_o,value,'*') ) {
00279         cpl_msg_error(fctid,"operation %s failed",operation);
00280             destroy_cube(cube_i);
00281             destroy_cube(cube_o);
00282             cpl_imagelist_delete(cub_ims);
00283             cpl_propertylist_delete(plist);
00284             return -1;
00285       }
00286     } else if (strcmp(operation,"/") == 0) {
00287       if(-1 == cst_op_cube(cube_o,value,'/') ) {
00288         cpl_msg_error(fctid,"operation %s failed",operation);
00289             destroy_cube(cube_i);
00290             destroy_cube(cube_o);
00291             cpl_imagelist_delete(cub_ims);
00292             cpl_propertylist_delete(plist);
00293             return -1;
00294       }
00295     } else {
00296       cpl_msg_error(fctid,"operation %s not supported",operation);
00297       cpl_msg_info(fctid,"If an input value is set");
00298       cpl_msg_info(fctid,"operations supported are: `-`, `+`, `*`, `/`");
00299       destroy_cube(cube_i);
00300       destroy_cube(cube_o);
00301       cpl_imagelist_delete(cub_ims);
00302       cpl_propertylist_delete(plist);
00303           return -1;
00304     }
00305 
00306       } /* env case op by constant && value == default */
00307 
00308       } else {
00309 
00310     cpl_msg_info(fctid,"Operation %s by constant",operation);
00311 
00312     cube_o = copy_cube(cube_i);
00313 
00314     if(strcmp(operation,"+") == 0) {
00315       if(-1 == cst_op_cube(cube_o,value,'+') ) {
00316         cpl_msg_error(fctid,"operation %s failed",operation);
00317             destroy_cube(cube_i);
00318             destroy_cube(cube_o);
00319             cpl_imagelist_delete(cub_ims);
00320             cpl_propertylist_delete(plist);
00321             return -1;
00322       }
00323     } else if (strcmp(operation,"-") == 0) {
00324       if(-1 == cst_op_cube(cube_o,value,'-') ) {
00325         cpl_msg_error(fctid,"operation %s failed",operation);
00326             destroy_cube(cube_i);
00327             destroy_cube(cube_o);
00328             cpl_imagelist_delete(cub_ims);
00329             cpl_propertylist_delete(plist);
00330             return -1;
00331       }
00332     } else if (strcmp(operation,"*") == 0) {
00333       if(-1 == cst_op_cube(cube_o,value,'*') ) {
00334         cpl_msg_error(fctid,"operation %s failed",operation);
00335             destroy_cube(cube_i);
00336             destroy_cube(cube_o);
00337             cpl_imagelist_delete(cub_ims);
00338             cpl_propertylist_delete(plist);
00339             return -1;
00340       }
00341     } else if (strcmp(operation,"/") == 0) {
00342       if(-1 == cst_op_cube(cube_o,value,'/') ) {
00343         cpl_msg_error(fctid,"operation %s failed",operation);
00344             destroy_cube(cube_i);
00345             destroy_cube(cube_o);
00346             cpl_imagelist_delete(cub_ims);
00347             cpl_propertylist_delete(plist);
00348             return -1;
00349       }
00350     } else {
00351       cpl_msg_error(fctid,"operation %s not supported",operation);
00352       cpl_msg_info(fctid,"If an input value is set");
00353       cpl_msg_info(fctid,"operations supported are: `-`, `+`, `*`, `/`");
00354       destroy_cube(cube_i);
00355       destroy_cube(cube_o);
00356       cpl_imagelist_delete(cub_ims);
00357       cpl_propertylist_delete(plist);
00358       return -1;
00359     }
00360 
00361       }
00362   
00363     /* HOW TO SAVE A PRODUCT ON DISK  */
00364     /* Set the file name */
00365     /* Create product frame */
00366      product_frame = cpl_frame_new();
00367      cpl_frame_set_filename(product_frame, name_o) ;
00368      cpl_frame_set_tag(product_frame, SI_UTL_CUBE_ARITH_PROCUBE) ;
00369      cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00370      cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00371      cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00372      if (cpl_error_get_code()) {
00373         cpl_msg_error(fctid, "Error while initialising the product frame") ;
00374         cpl_propertylist_delete(plist) ;
00375         cpl_frame_delete(product_frame) ;
00376         cpl_image_delete(image) ;
00377         return -1 ;
00378     }
00379 
00380     /* Add DataFlow keywords */
00381     cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG");
00382 
00383     if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00384             "si_utl_cube_arith", "SINFONI", KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00385         cpl_msg_error(fctid, "Problem in the product DFS-compliance") ;
00386         cpl_propertylist_delete(plist) ;
00387         cpl_frame_delete(product_frame) ;
00388         cpl_image_delete(image) ;
00389         return -1 ;
00390     }
00391 
00392     /* Save the file */
00393     cub_ims=cpl_imagelist_new(cube_o->lx,cube_o->ly,cube_o->np,CPL_TYPE_FLOAT);
00394      for(i=0;i<cube_o->np;i++) {
00395  
00396       eimg=cube_o->plane[i];
00397        wimg=cpl_image_wrap_float(eimg->lx,eimg->ly,eimg->data);
00398        cimg=cpl_image_duplicate(wimg);
00399        cpl_image_unwrap(wimg);
00400        cpl_imagelist_set(cub_ims,cimg,i);
00401      }
00402 
00403     if (cpl_imagelist_save(cub_ims, name_o, CPL_BPP_DEFAULT, plist,
00404                        CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00405         cpl_msg_error(fctid, "Could not save product");
00406         cpl_propertylist_delete(plist) ;
00407         cpl_frame_delete(product_frame) ;
00408         cpl_imagelist_delete(cub_ims) ;
00409         return -1 ;
00410     }
00411 
00412      destroy_cube(cube_i);
00413     destroy_cube(cube_o);
00414     if(spectrum != NULL) {
00415        destroyVector(spectrum);
00416     }
00417     cpl_propertylist_delete(plist) ;
00418 
00419     /* Log the saved file in the input frameset */
00420     cpl_frameset_insert(framelist, product_frame) ;
00421     cpl_imagelist_delete(cub_ims);
00422     
00423     /* Return */
00424     if (cpl_error_get_code()) 
00425         return -1 ;
00426     else 
00427         return 0 ;
00428 }

Generated on Wed Oct 26 13:08:56 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001