midi_kappamatrix.c

00001 /* $Id: midi_kappamatrix.c,v 1.30 2011/11/21 09:40:20 agabasch Exp $
00002  *
00003  * This file is part of the MIDI 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: agabasch $
00023  * $Date: 2011/11/21 09:40:20 $
00024  * $Revision: 1.30 $
00025  * $Name: midi-2_8_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <cpl.h>
00037 
00038 #include "midi_utils.h"
00039 #include "midi_pfits.h"
00040 #include "midi_dfs.h"
00041 #include "string.h"
00042 #include "midiTableToFits.h"
00043 #include "midi_cplupgrade.h"
00044 
00045 /*-----------------------------------------------------------------------------
00046                             Private function prototypes
00047  -----------------------------------------------------------------------------*/
00048 
00049 static int midi_kappamatrix_create(cpl_plugin *);
00050 static int midi_kappamatrix_exec(cpl_plugin *);
00051 static int midi_kappamatrix_destroy(cpl_plugin *);
00052 static int midi_kappamatrix(cpl_frameset *, const cpl_parameterlist *);
00053 static int table_to_imglst_sky_target(const char * ,
00054                            const char * ,
00055                            cpl_imagelist * ,
00056                            cpl_imagelist * ,
00057                            cpl_table *);
00058 static int table_to_imglst_mask(const char *,
00059                                 cpl_imagelist *,
00060                                 cpl_table *);
00061 static void kappa_to_ascii(const cpl_image * , 
00062                           const cpl_image * ,
00063                           const cpl_image * ,
00064                           const cpl_image * ,
00065                           const char * );
00066 
00067 
00068 
00069 
00070 /*-----------------------------------------------------------------------------
00071                             Static variables
00072  -----------------------------------------------------------------------------*/
00073 
00074 static char midi_kappamatrix_description[] =
00075 "This recipe derives the Kappa Matrix in the SCI_PHOT mode following\n"
00076 "the definition in A&A 425 1161. It uses a set of AOPEN and BOPEN\n"
00077 "photometry files as well as a spatial profile of the spectrum. The\n"
00078 "spectrum is extracted using the Optimal Extraction algorithm\n" 
00079 "(Horne 1986, PASP 98 209) adapted to NIR data, i.e. background dominated\n"
00080 "images. The errors of the kappa matrix are not calculated but set a\n"
00081 "priori to 5 per cent. "
00082 "The main output is a fits table with the PRO\n"
00083 "CATG keyword set to MIDI_KAPPAMATRIX_(G|P)RISM. For diagnostic reasons the\n"
00084 "different kappa matrixes are also saved as single fits images\n\n"
00085 
00086 
00087 "Input files:\n\n"
00088 "  DO category:                Type:       Explanation:         Required:\n"
00089 "  PHOTOM_SP_CALIB             Raw         Raw data frame            \n"
00090 "  or                                                               Y\n"
00091 "  PHOTOM_SP_SCIENCE           Raw         Raw data frame          \n\n"
00092 "  KAPPA_SP_MASK_PRISM         Calib       Spectral profile          \n"
00093 "  or                                                               Y\n"
00094 "  KAPPA_SP_MASK_GRISM         Calib       Spectral profile        \n\n"
00095 "Output files:\n\n"
00096 "  DO category:                Data type:  Explanation:\n"
00097 "  MIDI_KAPPAMATRIX_PRISM      FITS table  Kappa matrix: Main product  \n"
00098 "  or                                                                  \n"
00099 "  MIDI_KAPPAMATRIX_GRISM      FITS table  Kappa matrix: Main product \n\n"
00100 "  MIDI_KAPPAMATRIX11          FITS image  Kappa matrix: For diagnostics\n"
00101 "  MIDI_KAPPAMATRIX11_FILTERED FITS image  Kappa matrix: For diagnostics\n"
00102 "  MIDI_KAPPAMATRIX11_NOMASK   FITS image  Kappa matrix: For diagnostics\n"
00103 "  MIDI_KAPPAMATRIX12          FITS image  Kappa matrix: For diagnostics\n"
00104 "  MIDI_KAPPAMATRIX12_FILTERED FITS image  Kappa matrix: For diagnostics\n"
00105 "  MIDI_KAPPAMATRIX12_NOMASK   FITS image  Kappa matrix: For diagnostics\n"
00106 "  MIDI_KAPPAMATRIX21          FITS image  Kappa matrix: For diagnostics\n"
00107 "  MIDI_KAPPAMATRIX21_FILTERED FITS image  Kappa matrix: For diagnostics\n"
00108 "  MIDI_KAPPAMATRIX21_NOMASK   FITS image  Kappa matrix: For diagnostics\n"
00109 "  MIDI_KAPPAMATRIX22          FITS image  Kappa matrix: For diagnostics\n"
00110 "  MIDI_KAPPAMATRIX22_FILTERED FITS image  Kappa matrix: For diagnostics\n"
00111 "  MIDI_KAPPAMATRIX22_NOMASK   FITS image  Kappa matrix: For diagnostics\n\n";
00112 
00113 
00114 /*-----------------------------------------------------------------------------
00115                                 Function code
00116  -----------------------------------------------------------------------------*/
00117 
00118 
00119 /*----------------------------------------------------------------------------*/
00124 /*----------------------------------------------------------------------------*/
00125 
00128 /*----------------------------------------------------------------------------*/
00138 /*----------------------------------------------------------------------------*/
00139 int cpl_plugin_get_info(cpl_pluginlist * list)
00140 {
00141     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe );
00142     cpl_plugin  *   plugin = &recipe->interface;
00143 
00144     if (cpl_plugin_init(plugin,
00145                     CPL_PLUGIN_API,
00146                     MIDI_BINARY_VERSION,
00147                     CPL_PLUGIN_TYPE_RECIPE,
00148                     "midi_kappamatrix",
00149                     "Derives the Kappamatrix for the SCI_PHOT mode",
00150                     midi_kappamatrix_description,
00151                     "Armin Gabasch",
00152                     PACKAGE_BUGREPORT,
00153                     midi_get_license(),
00154                     midi_kappamatrix_create,
00155                     midi_kappamatrix_exec,
00156                     midi_kappamatrix_destroy)) {    
00157         cpl_msg_error(cpl_func, "Plugin initialization failed");
00158         (void)cpl_error_set_where(cpl_func);                          
00159         return 1;                                               
00160     }                                                    
00161 
00162     if (cpl_pluginlist_append(list, plugin)) {                 
00163         cpl_msg_error(cpl_func, "Error adding plugin to list");
00164         (void)cpl_error_set_where(cpl_func);                         
00165         return 1;                                              
00166     }                                                          
00167     
00168     return 0;
00169 }
00170 
00171 /*----------------------------------------------------------------------------*/
00179 /*----------------------------------------------------------------------------*/
00180 static int midi_kappamatrix_create(cpl_plugin * plugin)
00181 {
00182     cpl_recipe    * recipe;                                               
00183     cpl_parameter * p;
00184                                                                        
00185     /* Do not create the recipe if an error code is already set */     
00186     if (cpl_error_get_code() != CPL_ERROR_NONE) {                      
00187         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00188                       cpl_func, __LINE__, cpl_error_get_where());      
00189         return (int)cpl_error_get_code();                              
00190     }                                                                  
00191                                                                        
00192     if (plugin == NULL) {                                              
00193         cpl_msg_error(cpl_func, "Null plugin");                        
00194         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                 
00195     }                                                                  
00196                                                                        
00197     /* Verify plugin type */                                           
00198     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {       
00199         cpl_msg_error(cpl_func, "Plugin is not a recipe");             
00200         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);              
00201     }                                                                  
00202                                                                        
00203     /* Get the recipe */                                               
00204     recipe = (cpl_recipe *)plugin;                                     
00205                                                                        
00206     /* Create the parameters list in the cpl_recipe object */          
00207     recipe->parameters = cpl_parameterlist_new();                      
00208     if (recipe->parameters == NULL) {                                  
00209         cpl_msg_error(cpl_func, "Parameter list allocation failed");   
00210         cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);             
00211     }                                                                  
00212 
00213     /* Fill the parameters list */
00214 
00215     /* --stropt */
00216 /*     p = cpl_parameter_new_value("midi.midi_kappamatrix.outputfilename",  */
00217 /*             CPL_TYPE_STRING, "Output Filename (placeholder; not used for the moment!!)", "midi.midi_kappamatrix","kappamatrix.fits"); */
00218 /*     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "outfile"); */
00219 /*     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); */
00220 /*     cpl_parameterlist_append(recipe->parameters, p); */
00221 
00222     p = cpl_parameter_new_value("midi.midi_kappamatrix.medianwindow", 
00223             CPL_TYPE_INT,
00224             "The window size of the median filter. No influence on the main product",
00225             "midi.midi_kappamatrix",9);
00226     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "medianwindow");
00227     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00228     cpl_parameterlist_append(recipe->parameters, p);
00229 
00230 
00231 /*     /\* --boolopt *\/ */
00232 /*     p = cpl_parameter_new_value("midi.midi_kappamatrix.bool_option",  */
00233 /*             CPL_TYPE_BOOL, "a flag", "midi.midi_kappamatrix", TRUE); */
00234 /*     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "boolopt"); */
00235 /*     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); */
00236 /*     cpl_parameterlist_append(recipe->parameters, p); */
00237  
00238     return 0;
00239 }
00240 
00241 /*----------------------------------------------------------------------------*/
00247 /*----------------------------------------------------------------------------*/
00248 static int midi_kappamatrix_exec(cpl_plugin * plugin)
00249 {
00250 
00251     cpl_recipe * recipe;                                                   
00252     int recipe_status;                                                     
00253     cpl_errorstate initial_errorstate = cpl_errorstate_get();              
00254                                                                            
00255     /* Return immediately if an error code is already set */               
00256     if (cpl_error_get_code() != CPL_ERROR_NONE) {                          
00257         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",    
00258                       cpl_func, __LINE__, cpl_error_get_where());          
00259         return (int)cpl_error_get_code();                                  
00260     }                                                                      
00261                                                                            
00262     if (plugin == NULL) {                                                  
00263         cpl_msg_error(cpl_func, "Null plugin");                            
00264         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00265     }                                                                      
00266                                                                            
00267     /* Verify plugin type */                                               
00268     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {           
00269         cpl_msg_error(cpl_func, "Plugin is not a recipe");                 
00270         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);                  
00271     }                                                                      
00272                                                                            
00273     /* Get the recipe */                                                   
00274     recipe = (cpl_recipe *)plugin;                                         
00275                                                                            
00276     /* Verify parameter and frame lists */                                 
00277     if (recipe->parameters == NULL) {                                      
00278         cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00279         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00280     }                                                                      
00281     if (recipe->frames == NULL) {                                          
00282         cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");     
00283         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00284     }                                                                      
00285                                                                            
00286     /* Invoke the recipe */                                                
00287     recipe_status = midi_kappamatrix(recipe->frames, recipe->parameters);
00288                                                                            
00289     /* Ensure DFS-compliance of the products */                            
00290     if (cpl_dfs_update_product_header(recipe->frames)) {                   
00291         if (!recipe_status) recipe_status = (int)cpl_error_get_code();                         
00292     }                                                                      
00293                                                                            
00294     if (!cpl_errorstate_is_equal(initial_errorstate)) {                    
00295         /* Dump the error history since recipe execution start.            
00296            At this point the recipe cannot recover from the error */       
00297         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);          
00298     }                                                                      
00299                                                                            
00300     return recipe_status;                                                  
00301 }
00302 
00303 /*----------------------------------------------------------------------------*/
00309 /*----------------------------------------------------------------------------*/
00310 static int midi_kappamatrix_destroy(cpl_plugin * plugin)
00311 {
00312     cpl_recipe * recipe;                                          
00313                                                                   
00314     if (plugin == NULL) {                                         
00315         cpl_msg_error(cpl_func, "Null plugin");                   
00316         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);            
00317     }                                                             
00318                                                                   
00319     /* Verify plugin type */                                      
00320     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {  
00321         cpl_msg_error(cpl_func, "Plugin is not a recipe");        
00322         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);         
00323     }                                                             
00324                                                                   
00325     /* Get the recipe */                                          
00326     recipe = (cpl_recipe *)plugin;                                
00327                                                                   
00328     cpl_parameterlist_delete(recipe->parameters);             
00329                                                                   
00330     return 0;                                                    
00331 }
00332 
00333 /*----------------------------------------------------------------------------*/
00340 /*----------------------------------------------------------------------------*/
00341 static int midi_kappamatrix(cpl_frameset            * frameset,
00342                             const cpl_parameterlist * parlist)
00343 {
00344    const cpl_parameter   * param;
00345    cpl_frame             * cur_frame;
00346    cpl_table             * table=NULL;
00347    cpl_table             * kappamatrix_table=NULL;
00348    cpl_propertylist      * plist;
00349 
00350    cpl_propertylist      * qclist11;
00351    cpl_propertylist      * qclist12;
00352    cpl_propertylist      * qclist21;
00353    cpl_propertylist      * qclist22;
00354    cpl_propertylist      * qclist11_nomask;
00355    cpl_propertylist      * qclist12_nomask;
00356    cpl_propertylist      * qclist21_nomask;
00357    cpl_propertylist      * qclist22_nomask;
00358    cpl_propertylist      * qclist11_filtered;
00359    cpl_propertylist      * qclist12_filtered;
00360    cpl_propertylist      * qclist21_filtered;
00361    cpl_propertylist      * qclist22_filtered;
00362    cpl_propertylist      * qclist_all;
00363    cpl_propertylist      * qclist_all_extension;
00364    cpl_mask              * mask=NULL;
00365 
00366    const char            * shutter_id =NULL; 
00367    cpl_errorstate          prestate = cpl_errorstate_get();
00368    char                  * tag=NULL;
00369 /*   cpl_matrix            * kernel ;   */
00370    char                  * cubename;
00371 
00372    char     gris_name[100]= ""; 
00373    int      ext_imaging_data;    
00374    int      isPHOTAdata=0;
00375    int      isPHOTBdata=0;
00376    double * pmask_grism_DATA1=NULL;
00377    double * pmask_grism_DATA1_norm=NULL;
00378    double * pmask_grism_DATA2=NULL;
00379    double * pmask_grism_DATA2_norm=NULL;
00380    double * pmask_grism_DATA3=NULL;
00381    double * pmask_grism_DATA3_norm=NULL;
00382    double * pmask_grism_DATA4=NULL;
00383    double * pmask_grism_DATA4_norm=NULL;
00384 
00385    double * pmask_prism_DATA1=NULL;
00386    double * pmask_prism_DATA1_norm=NULL;
00387    double * pmask_prism_DATA2=NULL;
00388    double * pmask_prism_DATA2_norm=NULL;
00389    double * pmask_prism_DATA3=NULL;
00390    double * pmask_prism_DATA3_norm=NULL;
00391    double * pmask_prism_DATA4=NULL;
00392    double * pmask_prism_DATA4_norm=NULL;
00393 
00394    double kappamatrix11_stdev=0.;
00395    double kappamatrix11_stdev_nomask=0.;
00396    double kappamatrix11_stdev_filtered=0.;
00397    double kappamatrix12_stdev=0.;
00398    double kappamatrix12_stdev_nomask=0.;
00399    double kappamatrix12_stdev_filtered=0.;
00400    double kappamatrix21_stdev=0.;
00401    double kappamatrix21_stdev_nomask=0.;
00402    double kappamatrix21_stdev_filtered=0.;
00403    double kappamatrix22_stdev=0.;
00404    double kappamatrix22_stdev_nomask=0.;
00405    double kappamatrix22_stdev_filtered=0.;
00406 
00407    double kappamatrix11_median=0.;
00408    double kappamatrix11_median_nomask=0.;
00409    double kappamatrix11_median_filtered=0.;
00410    double kappamatrix12_median=0.;
00411    double kappamatrix12_median_nomask=0.;
00412    double kappamatrix12_median_filtered=0.;
00413    double kappamatrix21_median=0.;
00414    double kappamatrix21_median_nomask=0.;
00415    double kappamatrix21_median_filtered=0.;
00416    double kappamatrix22_median=0.;
00417    double kappamatrix22_median_nomask=0.;
00418    double kappamatrix22_median_filtered=0.;
00419    
00420 
00421    int nx_DATA1=0,nx_DATA2=0,nx_DATA3=0,nx_DATA4=0; 
00422    int ny_DATA1=0,ny_DATA2=0,ny_DATA3=0,ny_DATA4=0; 
00423 
00424 
00425       int xpos=0, ypos=0;
00426 
00427 
00428    cpl_image * image_AOPEN_DATA2_T = NULL;       
00429    cpl_image * image_AOPEN_DATA3_T = NULL;       
00430    cpl_image * image_AOPEN_DATA4_T = NULL;       
00431 
00432    cpl_image * image_BOPEN_DATA1_T = NULL;       
00433    cpl_image * image_BOPEN_DATA2_T = NULL;       
00434    cpl_image * image_BOPEN_DATA3_T = NULL;       
00435 
00436    cpl_image * image_AOPEN_DATA2_T_collapsed = NULL;
00437    cpl_image * image_AOPEN_DATA3_T_collapsed = NULL;
00438    cpl_image * image_AOPEN_DATA4_T_collapsed = NULL;
00439    cpl_image * image_BOPEN_DATA1_T_collapsed = NULL;
00440    cpl_image * image_BOPEN_DATA2_T_collapsed = NULL;
00441    cpl_image * image_BOPEN_DATA3_T_collapsed = NULL;
00442 
00443    cpl_image * image_AOPEN_DATA2_T_collapsed_nomask = NULL;
00444    cpl_image * image_AOPEN_DATA3_T_collapsed_nomask = NULL;
00445    cpl_image * image_AOPEN_DATA4_T_collapsed_nomask = NULL;
00446    cpl_image * image_BOPEN_DATA1_T_collapsed_nomask = NULL;
00447    cpl_image * image_BOPEN_DATA2_T_collapsed_nomask = NULL;
00448    cpl_image * image_BOPEN_DATA3_T_collapsed_nomask = NULL;
00449 
00450 
00451    cpl_image * mask_grism_DATA1_collapsed = NULL;
00452    cpl_image * mask_grism_DATA2_collapsed = NULL;
00453    cpl_image * mask_grism_DATA3_collapsed = NULL;
00454    cpl_image * mask_grism_DATA4_collapsed = NULL;
00455 
00456    cpl_image * mask_prism_DATA1_collapsed = NULL;
00457    cpl_image * mask_prism_DATA2_collapsed = NULL;
00458    cpl_image * mask_prism_DATA3_collapsed = NULL;
00459    cpl_image * mask_prism_DATA4_collapsed = NULL;
00460 
00461 
00462    
00463    cpl_image * kappamatrix11 = NULL;
00464    cpl_image * kappamatrix12 = NULL;
00465    cpl_image * kappamatrix21 = NULL;
00466    cpl_image * kappamatrix22 = NULL;
00467    cpl_image * kappamatrix11_nomask = NULL;
00468    cpl_image * kappamatrix12_nomask = NULL;
00469    cpl_image * kappamatrix21_nomask = NULL;
00470    cpl_image * kappamatrix22_nomask = NULL;
00471    cpl_image * kappamatrix11_filtered = NULL;
00472    cpl_image * kappamatrix12_filtered = NULL;
00473    cpl_image * kappamatrix21_filtered = NULL;
00474    cpl_image * kappamatrix22_filtered = NULL;
00475    
00476    cpl_image * mask_grism_DATA1 = NULL;
00477    cpl_image * mask_grism_DATA2 = NULL;
00478    cpl_image * mask_grism_DATA3 = NULL;
00479    cpl_image * mask_grism_DATA4 = NULL;
00480    
00481    cpl_image * mask_grism_DATA1_norm = NULL;
00482    cpl_image * mask_grism_DATA2_norm = NULL;
00483    cpl_image * mask_grism_DATA3_norm = NULL;
00484    cpl_image * mask_grism_DATA4_norm = NULL;
00485    
00486    cpl_image * mask_prism_DATA1 = NULL;
00487    cpl_image * mask_prism_DATA2 = NULL;
00488    cpl_image * mask_prism_DATA3 = NULL;
00489    cpl_image * mask_prism_DATA4 = NULL;
00490    
00491    
00492    cpl_image * mask_prism_DATA1_norm = NULL;
00493    cpl_image * mask_prism_DATA2_norm = NULL;
00494    cpl_image * mask_prism_DATA3_norm = NULL;
00495    cpl_image * mask_prism_DATA4_norm = NULL;
00496   
00497 /*    cpl_imagelist * imglst_AOPEN_DATA1_S; */
00498 /*    cpl_imagelist * imglst_AOPEN_DATA1_T; */
00499    cpl_imagelist * imglst_AOPEN_DATA2_S;
00500    cpl_imagelist * imglst_AOPEN_DATA2_T;
00501    cpl_imagelist * imglst_AOPEN_DATA3_S;
00502    cpl_imagelist * imglst_AOPEN_DATA3_T;
00503    cpl_imagelist * imglst_AOPEN_DATA4_S;
00504    cpl_imagelist * imglst_AOPEN_DATA4_T;
00505 
00506    cpl_imagelist * imglst_BOPEN_DATA1_S;
00507    cpl_imagelist * imglst_BOPEN_DATA1_T;
00508    cpl_imagelist * imglst_BOPEN_DATA2_S;
00509    cpl_imagelist * imglst_BOPEN_DATA2_T;
00510    cpl_imagelist * imglst_BOPEN_DATA3_S;
00511    cpl_imagelist * imglst_BOPEN_DATA3_T;
00512 /*    cpl_imagelist * imglst_BOPEN_DATA4_S; */
00513 /*    cpl_imagelist * imglst_BOPEN_DATA4_T; */
00514 
00515    cpl_imagelist * imglst_mask_prism;
00516    cpl_imagelist * imglst_mask_grism;
00517 
00518    int medianwindow=0;
00519    int lowlimit=0;
00520    int highlimit=0;
00521    
00522 /*    imglst_AOPEN_DATA1_S=cpl_imagelist_new(); */
00523 /*    imglst_AOPEN_DATA1_T=cpl_imagelist_new(); */
00524    imglst_AOPEN_DATA2_S=cpl_imagelist_new();
00525    imglst_AOPEN_DATA2_T=cpl_imagelist_new();
00526    imglst_AOPEN_DATA3_S=cpl_imagelist_new();
00527    imglst_AOPEN_DATA3_T=cpl_imagelist_new();
00528    imglst_AOPEN_DATA4_S=cpl_imagelist_new();
00529    imglst_AOPEN_DATA4_T=cpl_imagelist_new();
00530    
00531    imglst_BOPEN_DATA1_S=cpl_imagelist_new();
00532    imglst_BOPEN_DATA1_T=cpl_imagelist_new();
00533    imglst_BOPEN_DATA2_S=cpl_imagelist_new();
00534    imglst_BOPEN_DATA2_T=cpl_imagelist_new();
00535    imglst_BOPEN_DATA3_S=cpl_imagelist_new();
00536    imglst_BOPEN_DATA3_T=cpl_imagelist_new();
00537 /*    imglst_BOPEN_DATA4_S=cpl_imagelist_new(); */
00538 /*    imglst_BOPEN_DATA4_T=cpl_imagelist_new(); */
00539 
00540    imglst_mask_prism=cpl_imagelist_new();
00541    imglst_mask_grism=cpl_imagelist_new();
00542 
00543    
00544    
00545    /* RETRIEVE THE INPUT PARAMETERS */
00546 /*    param = cpl_parameterlist_find_const(parlist, "midi.midi_kappamatrix.outputfilename"); */
00547 /*    outputfilename = cpl_parameter_get_string(param); */
00548  
00549    param = cpl_parameterlist_find_const(parlist,
00550                                         "midi.midi_kappamatrix.medianwindow");
00551    medianwindow = cpl_parameter_get_int(param);
00552 
00553    if(medianwindow%2 == 0)/*  even number  */
00554    {
00555       cpl_msg_warning(cpl_func, "The window size of the median filter is not odd,");
00556       cpl_msg_warning(cpl_func, "therefore the size is increased by unity");
00557       medianwindow=medianwindow+1;   
00558    }
00559 
00560    if(medianwindow > 81)
00561    {
00562       cpl_msg_warning(cpl_func, "The window size of the median filter exceeds the maximal supported value,");
00563       cpl_msg_warning(cpl_func, "therefore the size is reset to 81");
00564       medianwindow=81;   
00565    }
00566 
00567    if(medianwindow < 0)
00568    {
00569       cpl_msg_warning(cpl_func, "The window size of the median filter must be positive,");
00570       cpl_msg_warning(cpl_func, "therefore the size is reset to 1");
00571       medianwindow=1;   
00572    }
00573 
00574 
00575   
00576    if (!cpl_errorstate_is_equal(prestate)) {
00577       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Could not retrieve the input parameters");
00578    }
00579    
00580    /* Identify the RAW and CALIB frames in the input frameset */
00581    cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00582                    cpl_error_get_code());
00583    
00584    
00585    /* 
00586       Walk through the whole Set of Frames SOF and search for KAPPA_SP_SCIENCE and/or KAPPA_SP_CALIB 
00587    */
00588    
00589    
00590    
00591 /*    Start  1   */
00592    
00593    cur_frame = cpl_frameset_get_first(frameset);
00594    if (cur_frame == NULL) {
00595       return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00596                                         "SOF does not have any file");
00597    }
00598    /* Append the images from the tables to the various imagelists */     
00599    while(cur_frame)
00600    {
00601       /*      Check the right tags */
00602       tag = (char*)cpl_frame_get_tag(cur_frame);
00603       if (strcmp(tag, MIDI_PHOTOM_SP_CALIB) && strcmp(tag, MIDI_PHOTOM_SP_SCIENCE)) {
00604          cur_frame = cpl_frameset_get_next( frameset );   
00605          continue;
00606       }
00607       
00608       cpl_msg_info(cpl_func, "Processing file  %s",cpl_frame_get_filename(cur_frame));
00609       ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DATA");
00610       
00611 /*       Check if the file is AOPEN or BOPEN */
00612       
00613       plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
00614       if (cpl_propertylist_has(plist, "ESO INS SHUT ID") == 1)
00615       {
00616          shutter_id=(cpl_propertylist_get_string(plist, "ESO INS SHUT ID"));
00617       }
00618       
00619 /*       Checking if PRISM or GRISM */
00620       if (cpl_propertylist_has(plist, "ESO INS GRIS NAME") == 1)
00621       {
00622          strcpy(gris_name,cpl_propertylist_get_string(plist, "ESO INS GRIS NAME"));
00623       }
00624       
00625       if (!cpl_errorstate_is_equal(prestate)) {
00626          return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Could not ...");
00627       }
00628       
00629       if (strcmp(shutter_id,"AOPEN")==0)
00630       {
00631 /*          PhotA Data found */
00632          isPHOTAdata=1;
00633          
00634          /* Load extension  Imaging Data  */ 
00635          table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00636          if (table == NULL) {
00637             return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00638                                               "Could not load the table");
00639          }
00640          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());      
00641          
00642 /*          No Signal in DATA1 for AOPEN */
00643          
00644          /*         cpl_msg_info(cpl_func, "Scanning for DATA2 ..."); */
00645          if (cpl_table_has_column(table,"DATA2")){
00646             table_to_imglst_sky_target("DATA2","TARTYP2",imglst_AOPEN_DATA2_S,imglst_AOPEN_DATA2_T,table);
00647          }
00648 /*          cpl_msg_info(cpl_func, "Scanning for DATA3 ..."); */
00649          if (cpl_table_has_column(table,"DATA3")){
00650             table_to_imglst_sky_target("DATA3","TARTYP2",imglst_AOPEN_DATA3_S,imglst_AOPEN_DATA3_T,table);
00651          }
00652 /*          cpl_msg_info(cpl_func, "Scanning for DATA4 ..."); */
00653          if (cpl_table_has_column(table,"DATA4")){
00654             table_to_imglst_sky_target("DATA4","TARTYP2",imglst_AOPEN_DATA4_S,imglst_AOPEN_DATA4_T,table);
00655          }
00656          
00657          cpl_msg_info(cpl_func, "Number of so far processed AOPEN patches: % " CPL_SIZE_FORMAT " ",cpl_imagelist_get_size(imglst_AOPEN_DATA2_T));
00658          
00659          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00660          
00661          cpl_table_delete(table);
00662       }
00663       
00664       if (strcmp(shutter_id,"BOPEN")==0)
00665       {
00666 /*          PhotB Data found */
00667          isPHOTBdata=1;
00668          
00669          /* Load extension  Imaging Data  */ 
00670          table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00671          if (table == NULL) {
00672             return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00673                                               "Could not load the table");
00674          }
00675          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());      
00676          
00677          
00678          /*         cpl_msg_info(cpl_func, "Scanning for DATA1 ..."); */
00679          if (cpl_table_has_column(table,"DATA1")){
00680             table_to_imglst_sky_target("DATA1","TARTYP2",imglst_BOPEN_DATA1_S,imglst_BOPEN_DATA1_T,table);
00681          }
00682          
00683 /*          cpl_msg_info(cpl_func, "Scanning for DATA2 ..."); */
00684          if (cpl_table_has_column(table,"DATA2")){
00685             table_to_imglst_sky_target("DATA2","TARTYP2",imglst_BOPEN_DATA2_S,imglst_BOPEN_DATA2_T,table);
00686          }
00687 /*          cpl_msg_info(cpl_func, "Scanning for DATA3 ..."); */
00688          if (cpl_table_has_column(table,"DATA3")){
00689             table_to_imglst_sky_target("DATA3","TARTYP2",imglst_BOPEN_DATA3_S,imglst_BOPEN_DATA3_T,table);
00690          }
00691          
00692          
00693 /*          No Signal in DATA4 for BOPEN */
00694          
00695          cpl_msg_info(cpl_func, "Number of so far processed BOPEN patches: % " CPL_SIZE_FORMAT " ",cpl_imagelist_get_size(imglst_BOPEN_DATA2_T));
00696          
00697          
00698          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00699          
00700          cpl_table_delete(table);
00701       }
00702       
00703       
00704       cpl_propertylist_delete(plist);
00705       
00706       /* Get next frame from SOF */
00707       cur_frame = cpl_frameset_get_next( frameset );   
00708       
00709    } /* while more frames */
00710 
00711 
00712    if (isPHOTAdata==0 || isPHOTBdata==0)
00713    {
00714       cpl_msg_error(cpl_func, "No suitable SetOfFrame fround");              
00715       (void)cpl_error_set_where(cpl_func);
00716       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Mask for filter name PRISM needed but not found");
00717       
00718    }
00719    
00720    
00721 /*    Subtract the sky frame from the target frame*/
00722    
00723    cpl_msg_info(cpl_func, "Doing sky subtraction ...");
00724    
00725    if(cpl_imagelist_get_size(imglst_AOPEN_DATA2_T)==cpl_imagelist_get_size(imglst_AOPEN_DATA2_S)
00726       && cpl_imagelist_get_size(imglst_BOPEN_DATA1_T)==cpl_imagelist_get_size(imglst_BOPEN_DATA1_S)
00727       )
00728    {
00729       cpl_imagelist_subtract(imglst_AOPEN_DATA2_T,imglst_AOPEN_DATA2_S); 
00730       cpl_imagelist_subtract(imglst_AOPEN_DATA3_T,imglst_AOPEN_DATA3_S); 
00731       cpl_imagelist_subtract(imglst_AOPEN_DATA4_T,imglst_AOPEN_DATA4_S); 
00732       cpl_imagelist_subtract(imglst_BOPEN_DATA1_T,imglst_BOPEN_DATA1_S); 
00733       cpl_imagelist_subtract(imglst_BOPEN_DATA2_T,imglst_BOPEN_DATA2_S); 
00734       cpl_imagelist_subtract(imglst_BOPEN_DATA3_T,imglst_BOPEN_DATA3_S); 
00735    }
00736    else 
00737    {
00738       cpl_msg_info(cpl_func, "The number of Skyframes and Targetframes differ");
00739       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00740                                         "The number of Skyframes and Targetframes differ"); 
00741    }
00742    
00743    
00744    cpl_msg_info(cpl_func, "Collapsing the images in the time domain ...");
00745    
00746 /*    Collapse the sky-subtracted targets */
00747    image_AOPEN_DATA2_T=cpl_imagelist_collapse_create(imglst_AOPEN_DATA2_T);
00748    image_AOPEN_DATA3_T=cpl_imagelist_collapse_create(imglst_AOPEN_DATA3_T);
00749    image_AOPEN_DATA4_T=cpl_imagelist_collapse_create(imglst_AOPEN_DATA4_T);
00750    image_BOPEN_DATA1_T=cpl_imagelist_collapse_create(imglst_BOPEN_DATA1_T);
00751    image_BOPEN_DATA2_T=cpl_imagelist_collapse_create(imglst_BOPEN_DATA2_T);
00752    image_BOPEN_DATA3_T=cpl_imagelist_collapse_create(imglst_BOPEN_DATA3_T);
00753    
00754 
00755    cpl_imagelist_delete(imglst_AOPEN_DATA2_T);
00756    cpl_imagelist_delete(imglst_AOPEN_DATA3_T);
00757    cpl_imagelist_delete(imglst_AOPEN_DATA4_T);
00758    cpl_imagelist_delete(imglst_AOPEN_DATA2_S);
00759    cpl_imagelist_delete(imglst_AOPEN_DATA3_S);
00760    cpl_imagelist_delete(imglst_AOPEN_DATA4_S);
00761    
00762    cpl_imagelist_delete(imglst_BOPEN_DATA1_T);
00763    cpl_imagelist_delete(imglst_BOPEN_DATA2_T);
00764    cpl_imagelist_delete(imglst_BOPEN_DATA3_T);
00765    cpl_imagelist_delete(imglst_BOPEN_DATA1_S);
00766    cpl_imagelist_delete(imglst_BOPEN_DATA2_S);
00767    cpl_imagelist_delete(imglst_BOPEN_DATA3_S);
00768    
00769 /*    END  1   */
00770    
00771    
00772 /* Extract the masks from the table and store them in a cpl_image */
00773    
00774 /*    The masks are stored in the two imagelists imglst_mask_grism and imglst_mask_prism */
00775 /*    !! DATA1 is read twice, therefore DATA1 is stored in imglst_mask_grism[1] and so on !! */
00776    
00777    cpl_msg_info(cpl_func, "Extracting the masks ...");
00778    
00779    cur_frame = cpl_frameset_get_first(frameset);
00780    if (cur_frame == NULL) {
00781       return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00782                                         "SOF does not have any file");
00783    }
00784    
00785    while(cur_frame)
00786    {
00787       ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DATA");
00788       
00789       /*      Check the right tags */
00790       tag = (char*)cpl_frame_get_tag(cur_frame);
00791       
00792       if (!strcmp(tag, MIDI_KAPPA_SP_MASK_GRISM)) {
00793          table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00794          if (table == NULL) {
00795             return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00796                                               "Could not load the table");
00797          }
00798          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());      
00799          
00800          table_to_imglst_mask("DATA1",imglst_mask_grism,table); 
00801          table_to_imglst_mask("DATA1",imglst_mask_grism,table); 
00802          table_to_imglst_mask("DATA2",imglst_mask_grism,table); 
00803          table_to_imglst_mask("DATA3",imglst_mask_grism,table); 
00804          table_to_imglst_mask("DATA4",imglst_mask_grism,table); 
00805          
00806 /* Normalizing the mask to 1*/
00807          
00808          cpl_msg_info(cpl_func, "Normalizing the integral of the masks (GRISM) to unity ...");         
00809          mask_grism_DATA1=cpl_image_cast(cpl_imagelist_get(imglst_mask_grism, 1),CPL_TYPE_DOUBLE);
00810          mask_grism_DATA2=cpl_image_cast(cpl_imagelist_get(imglst_mask_grism, 2),CPL_TYPE_DOUBLE);
00811          mask_grism_DATA3=cpl_image_cast(cpl_imagelist_get(imglst_mask_grism, 3),CPL_TYPE_DOUBLE);
00812          mask_grism_DATA4=cpl_image_cast(cpl_imagelist_get(imglst_mask_grism, 4),CPL_TYPE_DOUBLE);
00813           
00814          mask_grism_DATA1_norm=cpl_image_collapse_create(mask_grism_DATA1,0);
00815          mask_grism_DATA2_norm=cpl_image_collapse_create(mask_grism_DATA2,0);
00816          mask_grism_DATA3_norm=cpl_image_collapse_create(mask_grism_DATA3,0);
00817          mask_grism_DATA4_norm=cpl_image_collapse_create(mask_grism_DATA4,0);
00818           
00819          nx_DATA1=cpl_image_get_size_x(mask_grism_DATA1);
00820          ny_DATA1=cpl_image_get_size_y(mask_grism_DATA1);
00821          nx_DATA2=cpl_image_get_size_x(mask_grism_DATA2);
00822          ny_DATA2=cpl_image_get_size_y(mask_grism_DATA2);
00823          nx_DATA3=cpl_image_get_size_x(mask_grism_DATA3);
00824          ny_DATA3=cpl_image_get_size_y(mask_grism_DATA3);
00825          nx_DATA4=cpl_image_get_size_x(mask_grism_DATA4);
00826          ny_DATA4=cpl_image_get_size_y(mask_grism_DATA4);
00827          
00828          pmask_grism_DATA1     = cpl_image_get_data_double(mask_grism_DATA1);
00829          pmask_grism_DATA1_norm= cpl_image_get_data_double(mask_grism_DATA1_norm);
00830          pmask_grism_DATA2     = cpl_image_get_data_double(mask_grism_DATA2);
00831          pmask_grism_DATA2_norm= cpl_image_get_data_double(mask_grism_DATA2_norm);
00832          pmask_grism_DATA3     = cpl_image_get_data_double(mask_grism_DATA3);
00833          pmask_grism_DATA3_norm= cpl_image_get_data_double(mask_grism_DATA3_norm);
00834          pmask_grism_DATA4     = cpl_image_get_data_double(mask_grism_DATA4);
00835          pmask_grism_DATA4_norm= cpl_image_get_data_double(mask_grism_DATA4_norm);
00836          
00837          
00838          
00839          for (xpos=0; xpos<nx_DATA1; ++xpos)
00840          {
00841             for(ypos=0; ypos<ny_DATA1;++ypos)
00842             {
00843                pmask_grism_DATA1[xpos+nx_DATA1*ypos]  /= pmask_grism_DATA1_norm[xpos];
00844             }
00845          }
00846          
00847          for (xpos=0; xpos<nx_DATA2; ++xpos)
00848          {
00849             for(ypos=0; ypos<ny_DATA2;++ypos)
00850             {
00851                pmask_grism_DATA2[xpos+nx_DATA2*ypos]  /= pmask_grism_DATA2_norm[xpos];
00852             }
00853          }
00854          
00855          
00856          for (xpos=0; xpos<nx_DATA3; ++xpos)
00857          {
00858             for(ypos=0; ypos<ny_DATA3;++ypos)
00859             {
00860                pmask_grism_DATA3[xpos+nx_DATA3*ypos]  /= pmask_grism_DATA3_norm[xpos];
00861             }
00862          }
00863          
00864          
00865          for (xpos=0; xpos<nx_DATA4; ++xpos)
00866          {
00867             for(ypos=0; ypos<ny_DATA4;++ypos)
00868             {
00869                pmask_grism_DATA4[xpos+nx_DATA4*ypos]  /= pmask_grism_DATA4_norm[xpos];
00870             }
00871          }
00872          
00873          cpl_image_delete(mask_grism_DATA1_norm);
00874          cpl_image_delete(mask_grism_DATA2_norm);
00875          cpl_image_delete(mask_grism_DATA3_norm);
00876          cpl_image_delete(mask_grism_DATA4_norm);
00877          
00878          cpl_table_delete(table);
00879 
00880       }
00881       
00882       if (!strcmp(tag, MIDI_KAPPA_SP_MASK_PRISM)) {
00883          table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00884          if (table == NULL) {
00885             return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00886                                               "Could not load the table");
00887          }
00888          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());      
00889          
00890          table_to_imglst_mask("DATA1",imglst_mask_prism,table); 
00891          table_to_imglst_mask("DATA1",imglst_mask_prism,table); 
00892          table_to_imglst_mask("DATA2",imglst_mask_prism,table); 
00893          table_to_imglst_mask("DATA3",imglst_mask_prism,table); 
00894          table_to_imglst_mask("DATA4",imglst_mask_prism,table); 
00895 
00896 
00897 /* Normalizing the mask to 1*/
00898          
00899          cpl_msg_info(cpl_func, "Normalizing the integral of the masks (PRISM) to unity ...");               
00900          mask_prism_DATA1=cpl_image_cast(cpl_imagelist_get(imglst_mask_prism, 1),CPL_TYPE_DOUBLE);
00901          mask_prism_DATA2=cpl_image_cast(cpl_imagelist_get(imglst_mask_prism, 2),CPL_TYPE_DOUBLE);
00902          mask_prism_DATA3=cpl_image_cast(cpl_imagelist_get(imglst_mask_prism, 3),CPL_TYPE_DOUBLE);
00903          mask_prism_DATA4=cpl_image_cast(cpl_imagelist_get(imglst_mask_prism, 4),CPL_TYPE_DOUBLE);
00904           
00905          mask_prism_DATA1_norm=cpl_image_collapse_create(mask_prism_DATA1,0);
00906          mask_prism_DATA2_norm=cpl_image_collapse_create(mask_prism_DATA2,0);
00907          mask_prism_DATA3_norm=cpl_image_collapse_create(mask_prism_DATA3,0);
00908          mask_prism_DATA4_norm=cpl_image_collapse_create(mask_prism_DATA4,0);
00909           
00910          nx_DATA1=cpl_image_get_size_x(mask_prism_DATA1);
00911          ny_DATA1=cpl_image_get_size_y(mask_prism_DATA1);
00912          nx_DATA2=cpl_image_get_size_x(mask_prism_DATA2);
00913          ny_DATA2=cpl_image_get_size_y(mask_prism_DATA2);
00914          nx_DATA3=cpl_image_get_size_x(mask_prism_DATA3);
00915          ny_DATA3=cpl_image_get_size_y(mask_prism_DATA3);
00916          nx_DATA4=cpl_image_get_size_x(mask_prism_DATA4);
00917          ny_DATA4=cpl_image_get_size_y(mask_prism_DATA4);
00918          
00919          pmask_prism_DATA1     = cpl_image_get_data_double(mask_prism_DATA1);
00920          pmask_prism_DATA1_norm= cpl_image_get_data_double(mask_prism_DATA1_norm);
00921          pmask_prism_DATA2     = cpl_image_get_data_double(mask_prism_DATA2);
00922          pmask_prism_DATA2_norm= cpl_image_get_data_double(mask_prism_DATA2_norm);
00923          pmask_prism_DATA3     = cpl_image_get_data_double(mask_prism_DATA3);
00924          pmask_prism_DATA3_norm= cpl_image_get_data_double(mask_prism_DATA3_norm);
00925          pmask_prism_DATA4     = cpl_image_get_data_double(mask_prism_DATA4);
00926          pmask_prism_DATA4_norm= cpl_image_get_data_double(mask_prism_DATA4_norm);
00927          
00928          
00929          
00930          for (xpos=0; xpos<nx_DATA1; ++xpos)
00931          {
00932             for(ypos=0; ypos<ny_DATA1;++ypos)
00933             {
00934                pmask_prism_DATA1[xpos+nx_DATA1*ypos]  /= pmask_prism_DATA1_norm[xpos];
00935             }
00936          }
00937          
00938          for (xpos=0; xpos<nx_DATA2; ++xpos)
00939          {
00940             for(ypos=0; ypos<ny_DATA2;++ypos)
00941             {
00942                pmask_prism_DATA2[xpos+nx_DATA2*ypos]  /= pmask_prism_DATA2_norm[xpos];
00943             }
00944          }
00945          
00946          
00947          for (xpos=0; xpos<nx_DATA3; ++xpos)
00948          {
00949             for(ypos=0; ypos<ny_DATA3;++ypos)
00950             {
00951                pmask_prism_DATA3[xpos+nx_DATA3*ypos]  /= pmask_prism_DATA3_norm[xpos];
00952             }
00953          }
00954          
00955          
00956          for (xpos=0; xpos<nx_DATA4; ++xpos)
00957          {
00958             for(ypos=0; ypos<ny_DATA4;++ypos)
00959             {
00960                pmask_prism_DATA4[xpos+nx_DATA4*ypos]  /= pmask_prism_DATA4_norm[xpos];
00961             }
00962          }
00963          
00964          cpl_image_delete(mask_prism_DATA1_norm);
00965          cpl_image_delete(mask_prism_DATA2_norm);
00966          cpl_image_delete(mask_prism_DATA3_norm);
00967          cpl_image_delete(mask_prism_DATA4_norm);
00968          
00969          
00970          cpl_table_delete(table);
00971       }
00972       
00973       /* Get next frame from SOF */
00974       cur_frame = cpl_frameset_get_next( frameset );   
00975       
00976    } /* while more frames */
00977 
00978 
00979 /*  Check if the mask is the right one    */
00980    
00981    if (strcmp(gris_name,"GRISM")==0 && cpl_imagelist_get_size(imglst_mask_grism) <1)
00982    {
00983       cpl_msg_error(cpl_func, "Mask for  GRISM needed but not found");              
00984       (void)cpl_error_set_where(cpl_func);
00985       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Mask for filter name GRISM needed but not found");
00986    }
00987    
00988    else if (strcmp(gris_name,"PRISM")==0 && cpl_imagelist_get_size(imglst_mask_prism) <1)
00989    {
00990       cpl_msg_error(cpl_func, "Mask for PRISM needed but not found");              
00991       (void)cpl_error_set_where(cpl_func);
00992       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Mask for filter name PRISM needed but not found");
00993    }
00994 
00995 
00996    cpl_msg_info(cpl_func, "Extracting the signal without using a mask ...");
00997 
00998    image_AOPEN_DATA2_T_collapsed_nomask=cpl_image_collapse_create(image_AOPEN_DATA2_T,0);
00999    image_AOPEN_DATA3_T_collapsed_nomask=cpl_image_collapse_create(image_AOPEN_DATA3_T,0);
01000    image_AOPEN_DATA4_T_collapsed_nomask=cpl_image_collapse_create(image_AOPEN_DATA4_T,0);
01001    image_BOPEN_DATA1_T_collapsed_nomask=cpl_image_collapse_create(image_BOPEN_DATA1_T,0);
01002    image_BOPEN_DATA2_T_collapsed_nomask=cpl_image_collapse_create(image_BOPEN_DATA2_T,0);
01003    image_BOPEN_DATA3_T_collapsed_nomask=cpl_image_collapse_create(image_BOPEN_DATA3_T,0);
01004 
01005 
01006 
01007 /* Multiply the image by the mask and collapse in the y-direction */
01008 
01009 cpl_msg_info(cpl_func, "Extracting the signal using Horne 1986, PASP 98 209 ...");
01010 cpl_msg_info(cpl_func, "and assuming that the frames are sky-dominated: V(x)-> V0 ...");
01011 
01012 
01013 if (strcmp(gris_name,"GRISM")==0){
01014    cpl_msg_info(cpl_func, "Calculating the kappa matrix for the GRISM Data...");
01015    
01016 /*    Multiply the images by the Mask */
01017    cpl_msg_info(cpl_func, "1) Multiplying the images by the mask ...");
01018    cpl_image_multiply(image_AOPEN_DATA2_T,mask_grism_DATA2);
01019    cpl_image_multiply(image_AOPEN_DATA3_T,mask_grism_DATA3);
01020    cpl_image_multiply(image_AOPEN_DATA4_T,mask_grism_DATA4);
01021    cpl_image_multiply(image_BOPEN_DATA1_T,mask_grism_DATA1);
01022    cpl_image_multiply(image_BOPEN_DATA2_T,mask_grism_DATA2);
01023    cpl_image_multiply(image_BOPEN_DATA3_T,mask_grism_DATA3);
01024    
01025 /*   Collapse the images in the y-direction */
01026    cpl_msg_info(cpl_func, "2) Integrating the images perpendicular to the dispersion direction ...");
01027    image_AOPEN_DATA2_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA2_T,0);
01028    image_AOPEN_DATA3_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA3_T,0);
01029    image_AOPEN_DATA4_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA4_T,0);
01030    image_BOPEN_DATA1_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA1_T,0);
01031    image_BOPEN_DATA2_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA2_T,0);
01032    image_BOPEN_DATA3_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA3_T,0);
01033    
01034 /* Derive P^2 following Horne et al. */
01035 
01036    cpl_msg_info(cpl_func, "3) Deriving mask^2 ");
01037 
01038    cpl_image_power(mask_grism_DATA1,2.0);
01039    cpl_image_power(mask_grism_DATA2,2.0);
01040    cpl_image_power(mask_grism_DATA3,2.0);
01041    cpl_image_power(mask_grism_DATA4,2.0);
01042 
01043 
01044 /* Summing up P^2 in y direction*/
01045   cpl_msg_info(cpl_func, "4) Integrating mask^2 perpendicular to the dispersion direction");
01046   mask_grism_DATA1_collapsed=cpl_image_collapse_create(mask_grism_DATA1,0);
01047   mask_grism_DATA2_collapsed=cpl_image_collapse_create(mask_grism_DATA2,0);
01048   mask_grism_DATA3_collapsed=cpl_image_collapse_create(mask_grism_DATA3,0);
01049   mask_grism_DATA4_collapsed=cpl_image_collapse_create(mask_grism_DATA4,0);
01050 
01051 
01052   cpl_msg_info(cpl_func, "5) Extracting the final flux by dividing 2) through 4)");  
01053   cpl_image_divide(image_AOPEN_DATA2_T_collapsed,mask_grism_DATA2_collapsed);
01054   cpl_image_divide(image_AOPEN_DATA3_T_collapsed,mask_grism_DATA3_collapsed);
01055   cpl_image_divide(image_AOPEN_DATA4_T_collapsed,mask_grism_DATA4_collapsed);
01056   cpl_image_divide(image_BOPEN_DATA1_T_collapsed,mask_grism_DATA1_collapsed);
01057   cpl_image_divide(image_BOPEN_DATA2_T_collapsed,mask_grism_DATA2_collapsed);
01058   cpl_image_divide(image_BOPEN_DATA3_T_collapsed,mask_grism_DATA3_collapsed);
01059 
01060 /* Free the memory */
01061   cpl_image_delete(mask_grism_DATA1_collapsed);
01062   cpl_image_delete(mask_grism_DATA2_collapsed);
01063   cpl_image_delete(mask_grism_DATA3_collapsed);
01064   cpl_image_delete(mask_grism_DATA4_collapsed);
01065 
01066   }
01067 /* Free the memory */
01068   if(mask_grism_DATA1 != NULL)cpl_image_delete(mask_grism_DATA1);
01069   if(mask_grism_DATA2 != NULL)cpl_image_delete(mask_grism_DATA2);
01070   if(mask_grism_DATA3 != NULL)cpl_image_delete(mask_grism_DATA3);
01071   if(mask_grism_DATA4 != NULL)cpl_image_delete(mask_grism_DATA4);
01072 
01073 
01074 
01075 if (strcmp(gris_name,"PRISM")==0){
01076    cpl_msg_info(cpl_func, "Calculating the kappa matrix for the PRISM Data...");
01077    
01078 /*    Multiply the images by the Mask */
01079    cpl_msg_info(cpl_func, "1) Multiplying the images by the mask ...");
01080    cpl_image_multiply(image_AOPEN_DATA2_T,mask_prism_DATA2);
01081    cpl_image_multiply(image_AOPEN_DATA3_T,mask_prism_DATA3);
01082    cpl_image_multiply(image_AOPEN_DATA4_T,mask_prism_DATA4);
01083    cpl_image_multiply(image_BOPEN_DATA1_T,mask_prism_DATA1);
01084    cpl_image_multiply(image_BOPEN_DATA2_T,mask_prism_DATA2);
01085    cpl_image_multiply(image_BOPEN_DATA3_T,mask_prism_DATA3);
01086 
01087 /*   Collapse the images in the y-direction */
01088    cpl_msg_info(cpl_func, "2) Integrating the images perpendicular to the dispersion direction ...");
01089    image_AOPEN_DATA2_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA2_T,0);
01090    image_AOPEN_DATA3_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA3_T,0);
01091    image_AOPEN_DATA4_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA4_T,0);
01092    image_BOPEN_DATA1_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA1_T,0);
01093    image_BOPEN_DATA2_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA2_T,0);
01094    image_BOPEN_DATA3_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA3_T,0);
01095    
01096 /* Derive P^2 following Horne et al. */
01097 
01098    cpl_msg_info(cpl_func, "3) Deriving mask^2 ");
01099 
01100    cpl_image_power(mask_prism_DATA1,2.0);
01101    cpl_image_power(mask_prism_DATA2,2.0);
01102    cpl_image_power(mask_prism_DATA3,2.0);
01103    cpl_image_power(mask_prism_DATA4,2.0);
01104 
01105 
01106 /* Summing up P^2 in y direction*/
01107 
01108   cpl_msg_info(cpl_func, "4) Integrating mask^2 perpendicular to the dispersion direction");
01109   mask_prism_DATA1_collapsed=cpl_image_collapse_create(mask_prism_DATA1,0);
01110   mask_prism_DATA2_collapsed=cpl_image_collapse_create(mask_prism_DATA2,0);
01111   mask_prism_DATA3_collapsed=cpl_image_collapse_create(mask_prism_DATA3,0);
01112   mask_prism_DATA4_collapsed=cpl_image_collapse_create(mask_prism_DATA4,0);
01113   
01114   cpl_msg_info(cpl_func, "5) Extracting the final flux by dividing 2) through 4)");  
01115   cpl_image_divide(image_AOPEN_DATA2_T_collapsed,mask_prism_DATA2_collapsed);
01116   cpl_image_divide(image_AOPEN_DATA3_T_collapsed,mask_prism_DATA3_collapsed);
01117   cpl_image_divide(image_AOPEN_DATA4_T_collapsed,mask_prism_DATA4_collapsed);
01118   cpl_image_divide(image_BOPEN_DATA1_T_collapsed,mask_prism_DATA1_collapsed);
01119   cpl_image_divide(image_BOPEN_DATA2_T_collapsed,mask_prism_DATA2_collapsed);
01120   cpl_image_divide(image_BOPEN_DATA3_T_collapsed,mask_prism_DATA3_collapsed);
01121 
01122 /* Free the memory */
01123   cpl_image_delete(mask_prism_DATA1_collapsed);
01124   cpl_image_delete(mask_prism_DATA2_collapsed);
01125   cpl_image_delete(mask_prism_DATA3_collapsed);
01126   cpl_image_delete(mask_prism_DATA4_collapsed);
01127 }
01128 
01129 /* Free the memory */
01130   if(mask_prism_DATA1 != NULL)cpl_image_delete(mask_prism_DATA1);
01131   if(mask_prism_DATA2 != NULL)cpl_image_delete(mask_prism_DATA2);
01132   if(mask_prism_DATA3 != NULL)cpl_image_delete(mask_prism_DATA3);
01133   if(mask_prism_DATA4 != NULL)cpl_image_delete(mask_prism_DATA4);
01134 
01135 
01136 /* Derive the Kappa matrix as defined in the VINCI paper (A&A 425 1161)  */
01137 cpl_msg_info(cpl_func, "Deriving the KAPPA matrix following  A&A 425 1161 ...");
01138 
01139 kappamatrix11=cpl_image_divide_create (image_BOPEN_DATA2_T_collapsed, image_BOPEN_DATA1_T_collapsed);
01140 kappamatrix12=cpl_image_divide_create (image_AOPEN_DATA2_T_collapsed, image_AOPEN_DATA4_T_collapsed);
01141 kappamatrix21=cpl_image_divide_create (image_BOPEN_DATA3_T_collapsed, image_BOPEN_DATA1_T_collapsed);
01142 kappamatrix22=cpl_image_divide_create (image_AOPEN_DATA3_T_collapsed, image_AOPEN_DATA4_T_collapsed);
01143 
01144 kappamatrix11_nomask=cpl_image_divide_create (image_BOPEN_DATA2_T_collapsed_nomask, image_BOPEN_DATA1_T_collapsed_nomask);
01145 kappamatrix12_nomask=cpl_image_divide_create (image_AOPEN_DATA2_T_collapsed_nomask, image_AOPEN_DATA4_T_collapsed_nomask);
01146 kappamatrix21_nomask=cpl_image_divide_create (image_BOPEN_DATA3_T_collapsed_nomask, image_BOPEN_DATA1_T_collapsed_nomask);
01147 kappamatrix22_nomask=cpl_image_divide_create (image_AOPEN_DATA3_T_collapsed_nomask, image_AOPEN_DATA4_T_collapsed_nomask);
01148 
01149    
01150 /* Free the memory */
01151 
01152 cpl_image_delete(image_AOPEN_DATA2_T);
01153 cpl_image_delete(image_AOPEN_DATA3_T);       
01154 cpl_image_delete(image_AOPEN_DATA4_T);       
01155 
01156 cpl_image_delete(image_BOPEN_DATA1_T);       
01157 cpl_image_delete(image_BOPEN_DATA2_T);       
01158 cpl_image_delete(image_BOPEN_DATA3_T);       
01159 
01160 cpl_image_delete(image_AOPEN_DATA2_T_collapsed);
01161 cpl_image_delete(image_AOPEN_DATA3_T_collapsed);
01162 cpl_image_delete(image_AOPEN_DATA4_T_collapsed);
01163 
01164 cpl_image_delete(image_BOPEN_DATA1_T_collapsed);
01165 cpl_image_delete(image_BOPEN_DATA2_T_collapsed);
01166 cpl_image_delete(image_BOPEN_DATA3_T_collapsed);
01167 
01168 cpl_image_delete(image_AOPEN_DATA2_T_collapsed_nomask);
01169 cpl_image_delete(image_AOPEN_DATA3_T_collapsed_nomask);
01170 cpl_image_delete(image_AOPEN_DATA4_T_collapsed_nomask);
01171 
01172 cpl_image_delete(image_BOPEN_DATA1_T_collapsed_nomask);
01173 cpl_image_delete(image_BOPEN_DATA2_T_collapsed_nomask);
01174 cpl_image_delete(image_BOPEN_DATA3_T_collapsed_nomask);
01175 
01176 /* Smooth the kappa matrix */
01177 
01178 /* Create the kernel */
01179 
01180 
01181 /*
01182 kernel = cpl_matrix_new(1, medianwindow);
01183 cpl_matrix_fill(kernel, 1.0);
01184 */
01185 
01186 cpl_msg_info(cpl_func, "Smoothing the KAPPA matrix ...");
01187 
01188 mask = cpl_mask_new(medianwindow,1);
01189 cpl_mask_not(mask);
01190 
01191 kappamatrix11_filtered=cpl_image_duplicate(kappamatrix11);
01192 kappamatrix12_filtered=cpl_image_duplicate(kappamatrix12);
01193 kappamatrix21_filtered=cpl_image_duplicate(kappamatrix21);
01194 kappamatrix22_filtered=cpl_image_duplicate(kappamatrix22);
01195 
01196 
01197 cpl_image_filter_mask(kappamatrix11_filtered,kappamatrix11, mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
01198 cpl_image_filter_mask(kappamatrix12_filtered,kappamatrix12, mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
01199 cpl_image_filter_mask(kappamatrix21_filtered,kappamatrix21, mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
01200 cpl_image_filter_mask(kappamatrix22_filtered,kappamatrix22, mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
01201 
01202 cpl_mask_delete(mask);
01203 /*
01204 
01205 kappamatrix11_filtered=cpl_image_filter_median(kappamatrix11, kernel);
01206 kappamatrix12_filtered=cpl_image_filter_median(kappamatrix12, kernel);
01207 kappamatrix21_filtered=cpl_image_filter_median(kappamatrix21, kernel);
01208 kappamatrix22_filtered=cpl_image_filter_median(kappamatrix22, kernel);
01209 */
01210 
01211 
01212 /* Deriving QC Parameters  */
01213 
01214 if (strcmp(gris_name,"GRISM")==0)
01215 {
01216    lowlimit=140;
01217    highlimit=160;
01218 }
01219 
01220 else
01221 {
01222    lowlimit=65;
01223    highlimit=85;
01224 }
01225 
01226 
01227 
01228    /* Derives the standard deviation */
01229    kappamatrix11_stdev=cpl_image_get_stdev_window (kappamatrix11,lowlimit,1,highlimit,1);
01230    kappamatrix11_stdev_nomask=cpl_image_get_stdev_window (kappamatrix11_nomask,lowlimit,1,highlimit,1);
01231    kappamatrix11_stdev_filtered=cpl_image_get_stdev_window (kappamatrix11_filtered,lowlimit,1,highlimit,1);
01232 
01233    kappamatrix12_stdev=cpl_image_get_stdev_window (kappamatrix12,lowlimit,1,highlimit,1);
01234    kappamatrix12_stdev_nomask=cpl_image_get_stdev_window (kappamatrix12_nomask,lowlimit,1,highlimit,1);
01235    kappamatrix12_stdev_filtered=cpl_image_get_stdev_window (kappamatrix12_filtered,lowlimit,1,highlimit,1);
01236 
01237    kappamatrix21_stdev=cpl_image_get_stdev_window (kappamatrix21,lowlimit,1,highlimit,1);
01238    kappamatrix21_stdev_nomask=cpl_image_get_stdev_window (kappamatrix21_nomask,lowlimit,1,highlimit,1);
01239    kappamatrix21_stdev_filtered=cpl_image_get_stdev_window (kappamatrix21_filtered,lowlimit,1,highlimit,1);
01240 
01241    kappamatrix22_stdev=cpl_image_get_stdev_window (kappamatrix22,lowlimit,1,highlimit,1);
01242    kappamatrix22_stdev_nomask=cpl_image_get_stdev_window (kappamatrix22_nomask,lowlimit,1,highlimit,1);
01243    kappamatrix22_stdev_filtered=cpl_image_get_stdev_window (kappamatrix22_filtered,lowlimit,1,highlimit,1);
01244 
01245    /* Derives the median */
01246    kappamatrix11_median=cpl_image_get_median_window (kappamatrix11,lowlimit,1,highlimit,1);
01247    kappamatrix11_median_nomask=cpl_image_get_median_window (kappamatrix11_nomask,lowlimit,1,highlimit,1);
01248    kappamatrix11_median_filtered=cpl_image_get_median_window (kappamatrix11_filtered,lowlimit,1,highlimit,1);
01249 
01250    kappamatrix12_median=cpl_image_get_median_window (kappamatrix12,lowlimit,1,highlimit,1);
01251    kappamatrix12_median_nomask=cpl_image_get_median_window (kappamatrix12_nomask,lowlimit,1,highlimit,1);
01252    kappamatrix12_median_filtered=cpl_image_get_median_window (kappamatrix12_filtered,lowlimit,1,highlimit,1);
01253 
01254    kappamatrix21_median=cpl_image_get_median_window (kappamatrix21,lowlimit,1,highlimit,1);
01255    kappamatrix21_median_nomask=cpl_image_get_median_window (kappamatrix21_nomask,lowlimit,1,highlimit,1);
01256    kappamatrix21_median_filtered=cpl_image_get_median_window (kappamatrix21_filtered,lowlimit,1,highlimit,1);
01257 
01258    kappamatrix22_median=cpl_image_get_median_window (kappamatrix22,lowlimit,1,highlimit,1);
01259    kappamatrix22_median_nomask=cpl_image_get_median_window (kappamatrix22_nomask,lowlimit,1,highlimit,1);
01260    kappamatrix22_median_filtered=cpl_image_get_median_window (kappamatrix22_filtered,lowlimit,1,highlimit,1);
01261 
01262 
01263    
01264 
01265 
01266 cpl_msg_info(cpl_func, "Saving the KAPPA matrix(es) ...");
01267 
01268 
01269     /* Save the KAPPA matrix(es) and add QC parameters  */
01270     qclist11 = cpl_propertylist_new();
01271     qclist12 = cpl_propertylist_new();
01272     qclist21 = cpl_propertylist_new();
01273     qclist22 = cpl_propertylist_new();
01274 
01275     qclist11_nomask = cpl_propertylist_new();
01276     qclist12_nomask = cpl_propertylist_new();
01277     qclist21_nomask = cpl_propertylist_new();
01278     qclist22_nomask = cpl_propertylist_new();
01279 
01280     qclist11_filtered = cpl_propertylist_new();
01281     qclist12_filtered = cpl_propertylist_new();
01282     qclist21_filtered = cpl_propertylist_new();
01283     qclist22_filtered = cpl_propertylist_new();
01284 
01285    /* Adding the standard median */
01286     cpl_propertylist_append_double(qclist11, "ESO QC KAPPA MEDIAN 11", kappamatrix11_median);
01287     cpl_propertylist_append_double(qclist12, "ESO QC KAPPA MEDIAN 12", kappamatrix12_median);
01288     cpl_propertylist_append_double(qclist21, "ESO QC KAPPA MEDIAN 21", kappamatrix21_median);
01289     cpl_propertylist_append_double(qclist22, "ESO QC KAPPA MEDIAN 22", kappamatrix22_median);
01290 
01291     cpl_propertylist_append_double(qclist11_nomask, "ESO QC KAPPA NOMASK MEDIAN 11", kappamatrix11_median_nomask);
01292     cpl_propertylist_append_double(qclist12_nomask, "ESO QC KAPPA NOMASK MEDIAN 12", kappamatrix12_median_nomask);
01293     cpl_propertylist_append_double(qclist21_nomask, "ESO QC KAPPA NOMASK MEDIAN 21", kappamatrix21_median_nomask);
01294     cpl_propertylist_append_double(qclist22_nomask, "ESO QC KAPPA NOMASK MEDIAN 22", kappamatrix22_median_nomask);
01295 
01296     cpl_propertylist_append_double(qclist11_filtered, "ESO QC KAPPA FILTERED MEDIAN 11", kappamatrix11_median_filtered);
01297     cpl_propertylist_append_double(qclist12_filtered, "ESO QC KAPPA FILTERED MEDIAN 12", kappamatrix12_median_filtered);
01298     cpl_propertylist_append_double(qclist21_filtered, "ESO QC KAPPA FILTERED MEDIAN 21", kappamatrix21_median_filtered);
01299     cpl_propertylist_append_double(qclist22_filtered, "ESO QC KAPPA FILTERED MEDIAN 22", kappamatrix22_median_filtered);
01300 
01301    /* Adding the standard deviation */
01302     cpl_propertylist_append_double(qclist11, "ESO QC KAPPA STDEV 11", kappamatrix11_stdev);
01303     cpl_propertylist_append_double(qclist12, "ESO QC KAPPA STDEV 12", kappamatrix12_stdev);
01304     cpl_propertylist_append_double(qclist21, "ESO QC KAPPA STDEV 21", kappamatrix21_stdev);
01305     cpl_propertylist_append_double(qclist22, "ESO QC KAPPA STDEV 22", kappamatrix22_stdev);
01306 
01307     cpl_propertylist_append_double(qclist11_nomask, "ESO QC KAPPA NOMASK STDEV 11", kappamatrix11_stdev_nomask);
01308     cpl_propertylist_append_double(qclist12_nomask, "ESO QC KAPPA NOMASK STDEV 12", kappamatrix12_stdev_nomask);
01309     cpl_propertylist_append_double(qclist21_nomask, "ESO QC KAPPA NOMASK STDEV 21", kappamatrix21_stdev_nomask);
01310     cpl_propertylist_append_double(qclist22_nomask, "ESO QC KAPPA NOMASK STDEV 22", kappamatrix22_stdev_nomask);
01311 
01312     cpl_propertylist_append_double(qclist11_filtered, "ESO QC KAPPA FILTERED STDEV 11", kappamatrix11_stdev_filtered);
01313     cpl_propertylist_append_double(qclist12_filtered, "ESO QC KAPPA FILTERED STDEV 12", kappamatrix12_stdev_filtered);
01314     cpl_propertylist_append_double(qclist21_filtered, "ESO QC KAPPA FILTERED STDEV 21", kappamatrix21_stdev_filtered);
01315     cpl_propertylist_append_double(qclist22_filtered, "ESO QC KAPPA FILTERED STDEV 22", kappamatrix22_stdev_filtered);
01316 
01317 
01318 
01319 cpl_propertylist_update_string(qclist11, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX11");           
01320 cpl_propertylist_update_string(qclist12, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX12");           
01321 cpl_propertylist_update_string(qclist21, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX21");           
01322 cpl_propertylist_update_string(qclist22, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX22");           
01323 
01324 cpl_propertylist_update_string(qclist11_nomask, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX11_NOMASK");             
01325 cpl_propertylist_update_string(qclist12_nomask, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX12_NOMASK");             
01326 cpl_propertylist_update_string(qclist21_nomask, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX21_NOMASK");             
01327 cpl_propertylist_update_string(qclist22_nomask, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX22_NOMASK");             
01328 
01329 cpl_propertylist_update_string(qclist11_filtered, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX11_FILTERED");             
01330 cpl_propertylist_update_string(qclist12_filtered, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX12_FILTERED");             
01331 cpl_propertylist_update_string(qclist21_filtered, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX21_FILTERED");             
01332 cpl_propertylist_update_string(qclist22_filtered, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX22_FILTERED");             
01333 
01334 
01335 
01336 
01337 
01338 
01339 
01340 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix11,
01341                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01342                        qclist11, NULL,
01343                        PACKAGE "/" PACKAGE_VERSION,
01344                        "midi_kappamatrix11.fits")) {
01345    /* Propagate the error */
01346    (void)cpl_error_set_where(cpl_func);
01347 }
01348 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix12,
01349                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01350                        qclist12, NULL,
01351                        PACKAGE "/" PACKAGE_VERSION,
01352                        "midi_kappamatrix12.fits")) {
01353    /* Propagate the error */
01354    (void)cpl_error_set_where(cpl_func);
01355 }
01356 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix21,
01357                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01358                        qclist21, NULL,
01359                        PACKAGE "/" PACKAGE_VERSION,
01360                        "midi_kappamatrix21.fits")) {
01361    /* Propagate the error */
01362    (void)cpl_error_set_where(cpl_func);
01363 }
01364 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix22,
01365                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01366                        qclist22, NULL,
01367                        PACKAGE "/" PACKAGE_VERSION,
01368                        "midi_kappamatrix22.fits")) {
01369    /* Propagate the error */
01370    (void)cpl_error_set_where(cpl_func);
01371 }
01372 
01373 
01374 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix11_nomask,
01375                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01376                        qclist11_nomask, NULL,
01377                        PACKAGE "/" PACKAGE_VERSION,
01378                        "midi_kappamatrix11_nomask.fits")) {
01379    /* Propagate the error */
01380    (void)cpl_error_set_where(cpl_func);
01381 }
01382 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix12_nomask,
01383                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01384                        qclist12_nomask, NULL,
01385                        PACKAGE "/" PACKAGE_VERSION,
01386                        "midi_kappamatrix12_nomask.fits")) {
01387    /* Propagate the error */
01388    (void)cpl_error_set_where(cpl_func);
01389 }
01390 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix21_nomask,
01391                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01392                        qclist21_nomask, NULL,
01393                        PACKAGE "/" PACKAGE_VERSION,
01394                        "midi_kappamatrix21_nomask.fits")) {
01395    /* Propagate the error */
01396    (void)cpl_error_set_where(cpl_func);
01397 }
01398 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix22_nomask,
01399                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01400                        qclist22_nomask, NULL,
01401                        PACKAGE "/" PACKAGE_VERSION,
01402                        "midi_kappamatrix22_nomask.fits")) {
01403    /* Propagate the error */
01404    (void)cpl_error_set_where(cpl_func);
01405 }
01406 
01407 
01408 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix11_filtered,
01409                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01410                        qclist11_filtered, NULL,
01411                        PACKAGE "/" PACKAGE_VERSION,
01412                        "midi_kappamatrix11_filtered.fits")) {
01413    /* Propagate the error */
01414    (void)cpl_error_set_where(cpl_func);
01415 }
01416 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix12_filtered,
01417                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01418                        qclist12_filtered, NULL,
01419                        PACKAGE "/" PACKAGE_VERSION,
01420                        "midi_kappamatrix12_filtered.fits")) {
01421    /* Propagate the error */
01422    (void)cpl_error_set_where(cpl_func);
01423 }
01424 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix21_filtered,
01425                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01426                        qclist21_filtered, NULL,
01427                        PACKAGE "/" PACKAGE_VERSION,
01428                        "midi_kappamatrix21_filtered.fits")) {
01429    /* Propagate the error */
01430    (void)cpl_error_set_where(cpl_func);
01431 }
01432 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  kappamatrix22_filtered,
01433                        CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
01434                        qclist22_filtered, NULL,
01435                        PACKAGE "/" PACKAGE_VERSION,
01436                        "midi_kappamatrix22_filtered.fits")) {
01437    /* Propagate the error */
01438    (void)cpl_error_set_where(cpl_func);
01439 }
01440 
01441 
01442 /* Save it to a fits table */
01443 
01444 kappamatrix_table=cpl_table_new(cpl_image_get_size_x(kappamatrix11));
01445 cpl_table_new_column(kappamatrix_table, "kappam11",  CPL_TYPE_FLOAT);
01446 cpl_table_new_column(kappamatrix_table, "kappam11_error",  CPL_TYPE_FLOAT);
01447 cpl_table_new_column(kappamatrix_table, "kappam12",  CPL_TYPE_FLOAT);
01448 cpl_table_new_column(kappamatrix_table, "kappam12_error",  CPL_TYPE_FLOAT);
01449 cpl_table_new_column(kappamatrix_table, "kappam21",  CPL_TYPE_FLOAT);
01450 cpl_table_new_column(kappamatrix_table, "kappam21_error",  CPL_TYPE_FLOAT);
01451 cpl_table_new_column(kappamatrix_table, "kappam22",  CPL_TYPE_FLOAT);
01452 cpl_table_new_column(kappamatrix_table, "kappam22_error",  CPL_TYPE_FLOAT);
01453 
01454 cpl_table_copy_data_float(kappamatrix_table, "kappam11", cpl_image_get_data_float((kappamatrix11)));
01455 cpl_table_copy_data_float(kappamatrix_table, "kappam12", cpl_image_get_data_float((kappamatrix12)));
01456 cpl_table_copy_data_float(kappamatrix_table, "kappam21", cpl_image_get_data_float((kappamatrix21)));
01457 cpl_table_copy_data_float(kappamatrix_table, "kappam22", cpl_image_get_data_float((kappamatrix22)));
01458 
01459 cpl_table_fill_column_window_float (kappamatrix_table, "kappam11_error", 0, cpl_image_get_size_x(kappamatrix11), 0.05);
01460 cpl_table_fill_column_window_float (kappamatrix_table, "kappam12_error", 0, cpl_image_get_size_x(kappamatrix12), 0.05);
01461 cpl_table_fill_column_window_float (kappamatrix_table, "kappam21_error", 0, cpl_image_get_size_x(kappamatrix21), 0.05);
01462 cpl_table_fill_column_window_float (kappamatrix_table, "kappam22_error", 0, cpl_image_get_size_x(kappamatrix22), 0.05);
01463 
01464 qclist_all = cpl_propertylist_new();
01465 cpl_propertylist_append(qclist_all,qclist11);
01466 cpl_propertylist_append(qclist_all,qclist12);
01467 cpl_propertylist_append(qclist_all,qclist21);
01468 cpl_propertylist_append(qclist_all,qclist22);
01469 cpl_propertylist_erase_regexp(qclist_all, CPL_DFS_PRO_CATG,0);
01470 
01471 
01472 /* Propertylist to write to the extension */
01473 qclist_all_extension = cpl_propertylist_new();
01474 cpl_propertylist_append_string (qclist_all_extension, "EXTNAME", "MIDI_KAPPAMATRIX");
01475 
01476 
01477 if (strcmp(gris_name,"GRISM")==0)
01478 {
01479 
01480 cpl_propertylist_update_string(qclist_all, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX_GRISM");
01481 
01482 cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, kappamatrix_table, 
01483                       qclist_all_extension, "midi_kappamatrix",
01484                       qclist_all, NULL, 
01485                       PACKAGE "/" PACKAGE_VERSION,
01486                       "midi_kappamatrix_grism.fits");
01487    cpl_table_delete(kappamatrix_table);   
01488 }
01489 
01490 if (strcmp(gris_name,"PRISM")==0)
01491 {
01492 cpl_propertylist_update_string(qclist_all, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX_PRISM");
01493 cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, kappamatrix_table, 
01494                       qclist_all_extension, "midi_kappamatrix",
01495                       qclist_all, NULL, 
01496                       PACKAGE "/" PACKAGE_VERSION,
01497                       "midi_kappamatrix_prism.fits");
01498    cpl_table_delete(kappamatrix_table);    
01499 }
01500 
01501 /* Save it to an ascii file */
01502 
01503 
01504 
01505 if (strcmp(gris_name,"GRISM")==0)
01506 {
01507    cubename = cpl_sprintf("%s_%s%s", "transferRatios", "GRISM", ".dat");
01508    kappa_to_ascii( kappamatrix11, kappamatrix12, kappamatrix21, kappamatrix22, cubename);
01509    cpl_free(cubename);
01510 
01511    cubename = cpl_sprintf("%s_%s%s", "transferRatios", "GRISM", "_nomask.dat");
01512    kappa_to_ascii( kappamatrix11_nomask, kappamatrix12_nomask, kappamatrix21_nomask, kappamatrix22_nomask, cubename);
01513    cpl_free(cubename);
01514 
01515    cubename = cpl_sprintf("%s_%s%s", "transferRatios", "GRISM", "_filtered.dat");
01516    kappa_to_ascii( kappamatrix11_filtered, kappamatrix12_filtered, kappamatrix21_filtered, kappamatrix22_filtered, cubename);
01517    cpl_free(cubename);
01518 
01519 
01520 }
01521 
01522 if (strcmp(gris_name,"PRISM")==0)
01523 {
01524    cubename = cpl_sprintf("%s_%s%s", "transferRatios", "PRISM", ".dat");
01525    kappa_to_ascii( kappamatrix11, kappamatrix12, kappamatrix21, kappamatrix22, cubename);
01526    cpl_free(cubename);
01527 
01528    cubename = cpl_sprintf("%s_%s%s", "transferRatios", "PRISM", "_nomask.dat");
01529    kappa_to_ascii( kappamatrix11_nomask, kappamatrix12_nomask, kappamatrix21_nomask, kappamatrix22_nomask, cubename);
01530    cpl_free(cubename);
01531 
01532    cubename = cpl_sprintf("%s_%s%s", "transferRatios", "PRISM", "_filtered.dat");
01533    kappa_to_ascii( kappamatrix11_filtered, kappamatrix12_filtered, kappamatrix21_filtered, kappamatrix22_filtered, cubename);
01534    cpl_free(cubename);
01535 
01536 }
01537 
01538 
01539 
01540 
01541 /* Free the memory */
01542 cpl_image_delete(kappamatrix11); 
01543 cpl_image_delete(kappamatrix12); 
01544 cpl_image_delete(kappamatrix21); 
01545 cpl_image_delete(kappamatrix22); 
01546 
01547 cpl_image_delete(kappamatrix11_nomask); 
01548 cpl_image_delete(kappamatrix12_nomask); 
01549 cpl_image_delete(kappamatrix21_nomask); 
01550 cpl_image_delete(kappamatrix22_nomask); 
01551 
01552 cpl_image_delete(kappamatrix11_filtered); 
01553 cpl_image_delete(kappamatrix12_filtered); 
01554 cpl_image_delete(kappamatrix21_filtered); 
01555 cpl_image_delete(kappamatrix22_filtered); 
01556 
01557 
01558 /*cpl_matrix_delete(kernel) ;*/
01559 
01560    while(cpl_imagelist_get_size(imglst_mask_prism)>0){
01561       cpl_image_delete(cpl_imagelist_unset(imglst_mask_prism,0));
01562    }
01563 
01564    while(cpl_imagelist_get_size(imglst_mask_grism)>0){
01565       cpl_image_delete(cpl_imagelist_unset(imglst_mask_grism,0));
01566    }
01567 
01568 
01569 cpl_imagelist_delete(imglst_mask_prism);
01570 cpl_imagelist_delete(imglst_mask_grism);
01571 
01572 
01573 
01574 
01575 
01576 cpl_propertylist_delete(qclist11);
01577 cpl_propertylist_delete(qclist12);
01578 cpl_propertylist_delete(qclist21);
01579 cpl_propertylist_delete(qclist22);
01580 cpl_propertylist_delete(qclist11_nomask);
01581 cpl_propertylist_delete(qclist12_nomask);
01582 cpl_propertylist_delete(qclist21_nomask);
01583 cpl_propertylist_delete(qclist22_nomask);
01584 cpl_propertylist_delete(qclist11_filtered);
01585 cpl_propertylist_delete(qclist12_filtered);
01586 cpl_propertylist_delete(qclist21_filtered);
01587 cpl_propertylist_delete(qclist22_filtered);
01588 cpl_propertylist_delete(qclist_all);
01589 cpl_propertylist_delete(qclist_all_extension);
01590 
01591 
01592 return (int)cpl_error_get_code();
01593 }
01594 
01595 
01596 
01597 static int table_to_imglst_sky_target(const char * columname,
01598                     const char * columntype,
01599                     cpl_imagelist * imglst_sky,
01600                     cpl_imagelist * imglst_target,
01601                     cpl_table * table)
01602 {
01603 
01604    int dimenDATA; 
01605    int   i,csky=0, ctarget=0,csky_tmp=0, ctarget_tmp=0,first_unknown=0;
01606    cpl_array * array_data=NULL;
01607    cpl_image * image_data_int=NULL;
01608    cpl_errorstate          prestate = cpl_errorstate_get();
01609    char ** target_type;     
01610    cpl_type ctype;   
01611    int   flag_issky=0;
01612    int   flag_istarget=0;
01613 
01614    cpl_imagelist * imglst_sky_tmp=NULL;
01615    cpl_imagelist * imglst_target_tmp=NULL;
01616 
01617    imglst_sky_tmp=cpl_imagelist_new();
01618    imglst_target_tmp=cpl_imagelist_new();
01619 
01620    /* Load extension  Imaging Data  */ 
01621    
01622    dimenDATA=cpl_table_get_column_dimensions(table, columname);
01623    cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
01624    if (dimenDATA != 2) {
01625       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
01626                                         "DATA has a wrong dimension");
01627    }
01628    /* Read target type T(arget),S(ky),B(ackground) and store them in the image list */
01629    /* Loop over all "images" stored in DATA and add them to the imagelist */
01630    
01631    
01632    if (cpl_table_has_column(table, columntype))
01633    {
01634       target_type=cpl_table_get_data_string(table, columntype);
01635       
01636    }
01637    else 
01638    {
01639       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
01640                                         "TYPE of the Column not found");
01641    }
01642 /*    cpl_msg_info(cpl_func, "Number of images in the Table: %d",cpl_table_get_nrow(table)); */
01643 /*    cpl_msg_info(cpl_func, "Extracting %d images: please wait ...",cpl_table_get_nrow(table)); */
01644    
01645    
01646    ctype=cpl_table_get_column_type(table, columname);   
01647 /*    cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
01648 
01649 
01650 
01651    csky=cpl_imagelist_get_size(imglst_sky);
01652    
01653    ctarget=cpl_imagelist_get_size(imglst_target);
01654    
01655 /*    cpl_msg_info(cpl_func, "Length of imagelist S T : %d %d",csky,ctarget ); */
01656 
01657 
01658 
01659    flag_issky=0;
01660    flag_istarget=0;
01661 
01662 /* Find the first "U" image and discard the images befor  */
01663    
01664    for (i=0; i<cpl_table_get_nrow(table);i++){
01665       if(strcmp(target_type[i],"U")== 0){
01666          first_unknown=i;
01667          break;
01668       }
01669    }
01670 
01671 /*    cpl_msg_info(cpl_func, "First unknown: %d",first_unknown); */
01672    
01673    for (i=first_unknown; i<cpl_table_get_nrow(table);i++){
01674       
01675       array_data=(cpl_array *)cpl_table_get_array(table,columname, i); 
01676       if(ctype&CPL_TYPE_INT){
01677          image_data_int=cpl_image_wrap_int(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_int(array_data) );
01678       }
01679       if(ctype&CPL_TYPE_FLOAT){
01680          image_data_int=cpl_image_wrap_float(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_float(array_data) );
01681       }
01682       /* Cast the image to float */
01683 /*       image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
01684       
01685 /*        Append the image to the imagelists */
01686       
01687       if(strcmp(target_type[i],"S")== 0){
01688          cpl_imagelist_set(imglst_sky_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),csky_tmp++);
01689          flag_issky=1;
01690          cpl_image_unwrap(image_data_int);
01691          continue;
01692       }
01693       if(strcmp(target_type[i],"T")== 0){
01694          cpl_imagelist_set(imglst_target_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),ctarget_tmp++);
01695          flag_istarget=1;
01696          cpl_image_unwrap(image_data_int);
01697          continue;
01698       }
01699 
01700       /*Unwrap processed image manualy if nor Target nor Sky*/
01701       if(image_data_int!=NULL){
01702          cpl_image_unwrap(image_data_int);
01703       }
01704       if(flag_issky >0 && flag_istarget >0){
01705          /*Collapse the temporary imagelist and add the image to the "final" imagelist */
01706          cpl_imagelist_set(imglst_sky,cpl_imagelist_collapse_create(imglst_sky_tmp),csky++);
01707          cpl_imagelist_set(imglst_target,cpl_imagelist_collapse_create(imglst_target_tmp),ctarget++);
01708          csky_tmp=0;
01709          ctarget_tmp=0;
01710          flag_issky=0;
01711          flag_istarget=0;
01712 
01713          /*Empty imagelist and free the image buffer*/
01714          while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
01715             cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
01716          }
01717          while(cpl_imagelist_get_size(imglst_target_tmp)>0){
01718             cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
01719          }
01720 
01721       }
01722       
01723    }
01724  
01725    
01726 /*          Empty imagelist and free the image buffer*/
01727    while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
01728       cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
01729    }
01730    while(cpl_imagelist_get_size(imglst_target_tmp)>0){
01731       cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
01732    }
01733    
01734    cpl_imagelist_delete(imglst_sky_tmp);
01735    cpl_imagelist_delete(imglst_target_tmp);
01736    
01737    
01738    
01739    return (int)cpl_error_get_code();
01740 }
01741 
01742 
01743 
01744 
01745 
01746 
01747 
01748 static int table_to_imglst_mask(const char * columname,
01749                     cpl_imagelist * imglst,
01750                     cpl_table * table)
01751 {
01752 
01753    int dimenDATA; 
01754    int   i=0, counter=0;
01755    cpl_array * array_data=NULL;
01756    cpl_image * image_data_int=NULL;
01757    cpl_errorstate          prestate = cpl_errorstate_get();
01758    cpl_type ctype;   
01759 
01760    /* Load extension  Imaging Data  */ 
01761    
01762    dimenDATA=cpl_table_get_column_dimensions(table, columname);
01763    cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
01764    if (dimenDATA != 2) {
01765       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
01766                                         "DATA has a wrong dimension");
01767    }
01768    /* Read target type T(arget),S(ky),B(ackground) and store them in the image list */
01769    /* Loop over all "images" stored in DATA and add them to the imagelist */
01770    
01771    
01772 /*    cpl_msg_info(cpl_func, "Number of images in the Table: %d",cpl_table_get_nrow(table)); */
01773 /*    cpl_msg_info(cpl_func, "Extracting %d images: please wait ...",cpl_table_get_nrow(table)); */
01774    
01775    
01776    ctype=cpl_table_get_column_type(table, columname);   
01777 /*    cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
01778    
01779 
01780 
01781    counter=cpl_imagelist_get_size(imglst);
01782 
01783 
01784    for (i=0; i<cpl_table_get_nrow(table);i++){
01785       
01786       array_data=(cpl_array *)cpl_table_get_array(table,columname, i); 
01787       if(ctype&CPL_TYPE_INT){
01788          image_data_int=cpl_image_wrap_int(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_int(array_data) );
01789       }
01790       if(ctype&CPL_TYPE_FLOAT){
01791          image_data_int=cpl_image_wrap_float(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_float(array_data) );
01792       }
01793       /* Cast the image to float */
01794 /*       image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
01795       
01796 /*        Append the image to the imagelists */
01797       
01798       cpl_imagelist_set(imglst,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),counter++);
01799 
01800       /*Unwrap processed image manualy if nor Target nor Sky*/
01801       if(image_data_int!=NULL){
01802          cpl_image_unwrap(image_data_int);
01803       }
01804       
01805       
01806    }
01807    
01808    
01809 
01810    return (int)cpl_error_get_code();
01811 }
01812 
01813 
01814 
01815 
01816 
01817 
01818 
01819 static void kappa_to_ascii(const cpl_image * kappa11, 
01820                           const cpl_image * kappa12,
01821                           const cpl_image * kappa21,
01822                           const cpl_image * kappa22,
01823                           const char * filename)
01824 {
01825    FILE * fp ;
01826    int pis_rejected;
01827    int i;
01828     if ( filename == NULL)
01829     {
01830         cpl_msg_error(cpl_func,"please give the filename!") ;
01831         return ;
01832     }
01833 
01834     if ( NULL == (fp = fopen ( filename, "w" ) ) )
01835     {
01836         cpl_msg_error(cpl_func,"cannot open %s", filename) ;
01837         return ;
01838     }
01839 
01840 
01841     fprintf (fp, "%d  \n",cpl_image_get_size_x(kappa11));
01842  
01843     for ( i = 1 ; i <= cpl_image_get_size_x(kappa11) ; i++ )
01844     {
01845        fprintf (fp, "%f 0.05 %f 0.05  %f 0.05  %f  0.05 \n", 
01846                 cpl_image_get(kappa11, i, 1,&pis_rejected), 
01847                 cpl_image_get(kappa12, i, 1,&pis_rejected),
01848                 cpl_image_get(kappa21, i, 1,&pis_rejected), 
01849                 cpl_image_get(kappa22, i, 1,&pis_rejected)) ;
01850     }
01851     fclose (fp ) ;
01852 }
01853 
01854 

Generated on 15 Mar 2012 for MIDI Pipeline Reference Manual by  doxygen 1.6.1