utl_cube2ima.c

00001 /* $Id: utl_cube2ima.c,v 1.8 2005/07/28 09:11:08 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/07/28 09:11:08 $
00024  * $Revision: 1.8 $
00025  * $Name:  $
00026  */
00027 
00028 
00029 /*-----------------------------------------------------------------------------
00030                                 Includes
00031  -----------------------------------------------------------------------------*/
00032 
00033  
00034 #include "eclipse.h" 
00035 #include "fitshead.h"
00036 #include "cube_ops.h"
00037 #include "utl_cube2ima.h"
00038 #include "sinfoni_key_names.h"
00039 /*-----------------------------------------------------------------------------
00040                             Functions prototypes
00041  -----------------------------------------------------------------------------*/
00042 
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Static variables
00046  -----------------------------------------------------------------------------*/
00047 
00048 /*-----------------------------------------------------------------------------
00049                                 Functions code
00050  -----------------------------------------------------------------------------*/
00051 
00052 
00053 /*----------------------------------------------------------------------------*/
00060 /*----------------------------------------------------------------------------*/
00061 int si_utl_cube2ima(
00062         cpl_parameterlist   *   parlist, 
00063         cpl_frameset        *   framelist)
00064 {
00065     const char          *   fctid = "si_utl_cube2ima" ;
00066     cpl_parameter       *   param =NULL ;
00067     const char          *   name_i =NULL;
00068     const char          *   name_o =NULL;
00069 
00070     double                  ws=0 ;
00071     double                  we=0 ;
00072     double                  wc=0 ;
00073     double                  wd=0 ;
00074 
00075 
00076     int naxis3=0;
00077     double lams=0;
00078     double lame=0;
00079 
00080     cpl_frame           *   frm_cub=NULL ;
00081  
00082     cpl_propertylist    *   plist=NULL ;
00083     cpl_image           *   image=NULL ;
00084     cpl_image           *   imagew=NULL ;
00085     cpl_frame           *   product_frame=NULL;
00086 
00087     OneCube * cube=NULL;
00088     OneImage * img=NULL;
00089     fits_header * header=NULL;
00090 
00091 
00092     /* HOW TO RETRIEVE INPUT PARAMETERS */
00093     /* --stropt */
00094     name_o ="out_ima.fits";
00095 
00096     /* --doubleopt */
00097     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube2ima.ws");
00098     ws = cpl_parameter_get_double(param) ;
00099 
00100     /* --doubleopt */
00101     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube2ima.we");
00102     we = cpl_parameter_get_double(param) ;
00103 
00104     /* Identify the RAW and CALIB frames in the input frameset */
00105     if (sinfoni_dfs_set_groups(framelist)) {
00106         cpl_msg_error(fctid, "Cannot identify RAW and CALIB frames") ;
00107         return -1 ;
00108     }
00109 
00110     /* HOW TO ACCESS INPUT DATA */
00111     if ((frm_cub = cpl_frameset_find(framelist, SI_UTL_CUBE2IMA_CUBE))==NULL) {
00112         cpl_msg_error(fctid, "SOF does not have a file tagged as %s",SI_UTL_CUBE2IMA_CUBE);
00113         return -1 ;
00114     }
00115     name_i = cpl_frame_get_filename(frm_cub);
00116     naxis3=spiffi_get_naxis3((char*)name_i);
00117     wd=spiffi_get_dispersion((char*)name_i);
00118     wc=spiffi_get_cenLambda((char*)name_i);
00119     lams=wc-naxis3*wd/2+wd;
00120     lame=lams+(naxis3-2)*wd;
00121     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube2ima.ws");
00122     if(ws != cpl_parameter_get_default_double(param)) {
00123       if (ws < lams) {
00124         cpl_msg_warning(fctid,"ws =%f too low set it to %f",ws,lams);
00125         ws=lams;
00126       }
00127     } else {
00128       ws=lams;
00129     }
00130 
00131     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_cube2ima.we");
00132     if(we != cpl_parameter_get_default_double(param)) {
00133       if (we > lame) {
00134         cpl_msg_warning(fctid,"we =%f too low set it to %f",we,lame);
00135         we=lame;
00136       }
00137     } else {
00138       we=lame;
00139     }
00140 
00141     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cub), 
00142                     0)) == NULL) {
00143         cpl_msg_error(fctid, "Cannot read the FITS header") ;
00144         return -1 ;
00145     }
00146 
00147     /* recipe's algorithm */
00148     cube   = load_cube((char*)name_i);
00149     header = fits_read_header((char*)name_i);
00150 
00151 
00152     if(NULL == (img  = averageCubeToImageBetweenWaves(cube,wd,wc,ws,we)) ) {
00153        cpl_msg_error(fctid, "Cannot average cube to image in wavelength range [%f,%f] um",ws,we) ;
00154        destroy_cube(cube);
00155        fits_header_destroy(header);
00156        cpl_propertylist_delete(plist);    
00157        return -1 ;
00158     }   
00159 
00160 
00161     /*save_image_to_fits_hdr_dump(img,(char*)name_o, header, -32 ); */   
00162     imagew=cpl_image_wrap_float(img->lx,img->ly,img->data);
00163     image=cpl_image_duplicate(imagew);
00164     cpl_image_unwrap(imagew);
00165 
00166 
00167 
00168     /* HOW TO SAVE A PRODUCT ON DISK  */
00169 
00170     /* Create product frame */
00171     product_frame = cpl_frame_new();
00172     cpl_frame_set_filename(product_frame, name_o) ;
00173     cpl_frame_set_tag(product_frame, SI_UTL_CUBE2IMA_PROIMA) ;
00174     cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00175     cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00176     cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00177 
00178     if (cpl_error_get_code()) {
00179 
00180         cpl_msg_error(fctid, "Error while initialising the product frame") ;
00181         cpl_propertylist_delete(plist) ;
00182         cpl_frame_delete(product_frame) ;
00183         cpl_image_delete(image) ;
00184         return -1 ;
00185     }
00186     
00187     /* Add DataFlow keywords */
00188     cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG");
00189     if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00190             "si_utl_cube2ima", "SINFONI", KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00191         cpl_msg_error(fctid, "Problem in the product DFS-compliance") ;
00192         cpl_propertylist_delete(plist) ;
00193         cpl_frame_delete(product_frame) ;
00194         cpl_image_delete(image) ;
00195         return -1 ;
00196     }
00197    
00198     /* Save the file */
00199     cpl_propertylist_erase_regexp(plist, "^CTYPE3");
00200     cpl_propertylist_erase_regexp(plist, "^CRPIX3");
00201     cpl_propertylist_erase_regexp(plist, "^CRVAL3");
00202     cpl_propertylist_erase_regexp(plist, "^CDELT3");
00203     cpl_propertylist_erase_regexp(plist, "^CUNIT3");
00204 
00205     if (cpl_image_save(image, name_o, CPL_BPP_DEFAULT, plist,
00206                        CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00207         cpl_msg_error(fctid, "Could not save product");
00208         cpl_propertylist_delete(plist) ;
00209         cpl_frame_delete(product_frame) ;
00210         cpl_image_delete(image) ;
00211         return -1 ;
00212     }
00213     cpl_propertylist_delete(plist) ;
00214  
00215     /* Log the saved file in the input frameset */
00216     cpl_frameset_insert(framelist, product_frame) ;
00217     destroy_cube(cube);
00218     destroy_image(img);
00219     fits_header_destroy(header);
00220     cpl_image_delete(image) ;
00221     
00222     /* Return */
00223     if (cpl_error_get_code()) 
00224         return -1 ;
00225     else 
00226         return 0 ;
00227 }

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