sinfoni/sinfo_utl_cube2ima.c

00001 /* $Id: sinfo_utl_cube2ima.c,v 1.3 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.3 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 /* 
00037 #include "eclipse.h"
00038 */ 
00039 #include "sinfo_functions.h"
00040 /*#include "fitshead.h" */
00041 #include "sinfo_new_cube_ops.h"
00042 #include "sinfo_utl_cube2ima.h"
00043 #include "sinfo_key_names.h"
00044 #include <sinfo_error.h>
00045 #include <sinfo_utils_wrappers.h>
00046 
00047 /*-----------------------------------------------------------------------------
00048                             Functions prototypes
00049  -----------------------------------------------------------------------------*/
00050 
00051 
00052 
00053 /*-----------------------------------------------------------------------------
00054                             Static variables
00055  -----------------------------------------------------------------------------*/
00056 
00057 /*-----------------------------------------------------------------------------
00058                                 Functions code
00059  -----------------------------------------------------------------------------*/
00060 
00068 /*----------------------------------------------------------------------------*/
00075 /*----------------------------------------------------------------------------*/
00076 int sinfo_utl_cube2ima(
00077         cpl_parameterlist   *   parlist, 
00078         cpl_frameset        *   framelist)
00079 {
00080     cpl_parameter       *   param =NULL ;
00081     const char          *   name_i =NULL;
00082     const char          *   name_o =NULL;
00083 
00084     double                  ws=0 ;
00085     double                  we=0 ;
00086     double                  wc=0 ;
00087     double                  wd=0 ;
00088 
00089 
00090     int naxis3=0;
00091     double lams=0;
00092     double lame=0;
00093 
00094     cpl_frame           *   frm_cub=NULL ;
00095  
00096     cpl_propertylist    *   plist=NULL ;
00097     cpl_frame           *   product_frame=NULL;
00098 
00099     cpl_imagelist * cube=NULL;
00100     cpl_image * img=NULL;
00101 
00102    
00103     /* HOW TO RETRIEVE INPUT PARAMETERS */
00104     /* --stropt */
00105     name_o ="out_ima.fits";
00106  
00107     /* --doubleopt */
00108     check_nomsg(param = cpl_parameterlist_find(parlist,
00109                                             "sinfoni.sinfo_utl_cube2ima.ws"));
00110     check_nomsg(ws = cpl_parameter_get_double(param)) ;
00111 
00112     /* --doubleopt */
00113     check_nomsg(param = cpl_parameterlist_find(parlist,
00114                                             "sinfoni.sinfo_utl_cube2ima.we"));
00115     check_nomsg(we = cpl_parameter_get_double(param)) ;
00116 
00117     /* Identify the RAW and CALIB frames in the input frameset */
00118     check(sinfo_dfs_set_groups(framelist),
00119         "Cannot identify RAW and CALIB frames");
00120  
00121     /* HOW TO ACCESS INPUT DATA */
00122     check(frm_cub = cpl_frameset_find(framelist, SI_UTL_CUBE2IMA_CUBE),
00123         "SOF does not have a file tagged as %s",SI_UTL_CUBE2IMA_CUBE);
00124 
00125     check_nomsg(name_i = cpl_frame_get_filename(frm_cub));
00126 
00127     plist=cpl_propertylist_load(name_i,0);
00128     naxis3=sinfo_pfits_get_naxis3(plist);
00129     wd=sinfo_pfits_get_cdelt3(plist);
00130     wc=sinfo_pfits_get_crval3(plist);
00131     sinfo_free_propertylist(&plist);
00132 
00133     lams=wc-naxis3*wd/2+wd;
00134     lame=lams+(naxis3-2)*wd;
00135 
00136     check_nomsg(param = cpl_parameterlist_find(parlist,
00137                                             "sinfoni.sinfo_utl_cube2ima.ws"));
00138 
00139     if(ws != cpl_parameter_get_default_double(param)) {
00140       if (ws < lams) {
00141         sinfo_msg_warning("ws =%f too low set it to %f",ws,lams);
00142         ws=lams;
00143       }
00144       else if (ws >= lame) {
00145         sinfo_msg_warning("ws =%f too high set it to %f",ws,lams);
00146         ws=lams;
00147       }
00148     } else {
00149       ws=lams;
00150     }
00151 
00152     check_nomsg(param = cpl_parameterlist_find(parlist,
00153                                              "sinfoni.sinfo_utl_cube2ima.we"));
00154     if(we != cpl_parameter_get_default_double(param)) {
00155       if (we > lame) {
00156         sinfo_msg_warning("we =%f too low set it to %f",we,lame);
00157         we=lame;
00158       }
00159       else if (we <= lams) {
00160         sinfo_msg_warning("we =%f too low set it to %f",we,lame);
00161         we=lame;
00162       }
00163     } else {
00164       we=lame;
00165     }
00166 
00167     check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cub),0),
00168           "Cannot read the FITS header") ;
00169 
00170     /* recipe's algorithm */
00171     check_nomsg(cube = cpl_imagelist_load((char*)name_i,CPL_TYPE_FLOAT,0));
00172     if(NULL==(img=sinfo_new_average_cube_to_image_between_waves(cube,wd,wc,
00173                                                                 ws,we))) {
00174      sinfo_msg_error("Cannot average cube to image in wavelength "
00175                      "range [%f,%f] um",ws,we) ;
00176 
00177        sinfo_free_imagelist(&cube);
00178        sinfo_free_propertylist(&plist);    
00179 
00180     }
00181     sinfo_free_imagelist(&cube);
00182 
00183     /* HOW TO SAVE A PRODUCT ON DISK  */
00184     /* Create product frame */
00185     check_nomsg(product_frame = cpl_frame_new());
00186     check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00187     check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE2IMA_PROIMA)) ;
00188     check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00189     check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00190     check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL), 
00191      "Error while initialising the product frame") ;
00192 
00193 
00194     /* Add DataFlow keywords */
00195     check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00196 
00197     check(cpl_dfs_setup_product_header(plist, 
00198                                        product_frame, 
00199                                        framelist, 
00200                                        parlist,
00201                                        "si_sinfo_utl_cube2ima", 
00202                                        "SINFONI", 
00203                                        KEY_VALUE_HPRO_DID),
00204       "Problem in the product DFS-compliance") ;
00205    
00206     /* Save the file */
00207     check_nomsg(cpl_propertylist_erase_regexp(plist, "^CTYPE3",0));
00208     check_nomsg(cpl_propertylist_erase_regexp(plist, "^CRPIX3",0));
00209     check_nomsg(cpl_propertylist_erase_regexp(plist, "^CRVAL3",0));
00210     check_nomsg(cpl_propertylist_erase_regexp(plist, "^CDELT3",0));
00211     check_nomsg(cpl_propertylist_erase_regexp(plist, "^CUNIT3",0));
00212 
00213     check(cpl_image_save(img, 
00214                          name_o, 
00215                          CPL_BPP_DEFAULT, 
00216                          plist,
00217                          CPL_IO_DEFAULT),
00218       "Could not save product");
00219 
00220     check_nomsg(sinfo_free_propertylist(&plist)) ;
00221   
00222     /* Log the saved file in the input frameset */
00223     check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00224     check_nomsg(sinfo_free_image(&img)) ;
00225  
00226     /* Return */
00227 
00228  cleanup:
00229        sinfo_free_imagelist(&cube);
00230        sinfo_free_propertylist(&plist);    
00231        sinfo_free_image(&img);
00232 
00233     if (cpl_error_get_code()) 
00234         return -1 ;
00235     else 
00236         return 0 ;
00237 }

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