GRAVI Pipeline Reference Manual 0.1.1
gravi_all_p2vm.c
00001 /* $Id: gravi_all_p2vm.c,v 1.29 2009/02/10 09:16:12 llundin Exp $
00002  *
00003  * This file is part of the GRAVI 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: llundin $
00023  * $Date: 2009/02/10 09:16:12 $
00024  * $Revision: 1.29 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <cpl.h>
00037 #include <stdio.h>
00038 #include <string.h>
00039 
00040 #include "gravi_utils.h"
00041 #include "gravi_pfits.h"
00042 #include "gravi_dfs.h"
00043 #include "gravi_calib.h"
00044 
00045 #include "gravi_data.h"
00046 
00047 
00048 
00049 /*-----------------------------------------------------------------------------
00050                             Private function prototypes
00051  -----------------------------------------------------------------------------*/
00052 
00053 static int gravi_all_p2vm_create(cpl_plugin *);
00054 static int gravi_all_p2vm_exec(cpl_plugin *);
00055 static int gravi_all_p2vm_destroy(cpl_plugin *);
00056 static int gravi_all_p2vm(cpl_frameset *, const cpl_parameterlist *);
00057 
00058 /*-----------------------------------------------------------------------------
00059                             Static variables
00060  -----------------------------------------------------------------------------*/
00061 
00062 static char gravi_all_p2vm_description[] =
00063 "This recipe is used to compute the p2vm matrix.\n"
00064 "The given output FITS file contain a p2vm table with "
00065 "the values of the transmission, phase and coherence extract "
00066 "using the p2vm matrix \n"
00067 "The description should include the required FITS-files and\n"
00068 "their associated tags, e.g.\n"
00069 "GRAVI-GRAVI_ALL_P2VM-raw-file.fits " GRAVI_FT_P2VM "\n"
00070 "and any optional files, e.g.\n"
00071 "GRAVI-GRAVI_ALL_P2VM-flat-file.fits " P2VM_FT "\n"
00072 "\n"
00073 "Additionally, it should describe functionality of the expected output."
00074 "\n";
00075 
00076 /*-----------------------------------------------------------------------------
00077                                 Function code
00078  -----------------------------------------------------------------------------*/
00079 
00080 /*----------------------------------------------------------------------------*/
00090 /*----------------------------------------------------------------------------*/
00091 int cpl_plugin_get_info(cpl_pluginlist * list)
00092 {
00093     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe );
00094     cpl_plugin  *   plugin = &recipe->interface;
00095 
00096     if (cpl_plugin_init(plugin,
00097                     CPL_PLUGIN_API,
00098                     GRAVI_BINARY_VERSION,
00099                     CPL_PLUGIN_TYPE_RECIPE,
00100                     "gravi_all_p2vm",
00101                     "This recipe is used to compute p2vm matrix",
00102                     gravi_all_p2vm_description,
00103                     "Firstname Lastname",
00104                     PACKAGE_BUGREPORT,
00105                     gravi_get_license(),
00106                     gravi_all_p2vm_create,
00107                     gravi_all_p2vm_exec,
00108                     gravi_all_p2vm_destroy)) {    
00109         cpl_msg_error(cpl_func, "Plugin initialization failed");
00110         (void)cpl_error_set_where(cpl_func);                          
00111         return 1;                                               
00112     }                                                    
00113 
00114     if (cpl_pluginlist_append(list, plugin)) {                 
00115         cpl_msg_error(cpl_func, "Error adding plugin to list");
00116         (void)cpl_error_set_where(cpl_func);                         
00117         return 1;                                              
00118     }                                                          
00119     
00120     return 0;
00121 }
00122 
00123 /*----------------------------------------------------------------------------*/
00131 /*----------------------------------------------------------------------------*/
00132 static int gravi_all_p2vm_create(cpl_plugin * plugin)
00133 {
00134     cpl_recipe    * recipe;                                               
00135     cpl_parameter * p;
00136                                                                        
00137     /* Do not create the recipe if an error code is already set */     
00138     if (cpl_error_get_code() != CPL_ERROR_NONE) {                      
00139         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00140                       cpl_func, __LINE__, cpl_error_get_where());      
00141         return (int)cpl_error_get_code();                              
00142     }                                                                  
00143                                                                        
00144     if (plugin == NULL) {                                              
00145         cpl_msg_error(cpl_func, "Null plugin");                        
00146         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                 
00147     }                                                                  
00148                                                                        
00149     /* Verify plugin type */                                           
00150     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {       
00151         cpl_msg_error(cpl_func, "Plugin is not a recipe");             
00152         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);              
00153     }                                                                  
00154                                                                        
00155     /* Get the recipe */                                               
00156     recipe = (cpl_recipe *)plugin;                                     
00157                                                                        
00158     /* Create the parameters list in the cpl_recipe object */          
00159     recipe->parameters = cpl_parameterlist_new();                      
00160     if (recipe->parameters == NULL) {                                  
00161         cpl_msg_error(cpl_func, "Parameter list allocation failed");   
00162         cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);             
00163     }                                                                  
00164 
00165     /* Fill the parameters list */
00166 
00167     /* --the minimum wavelength */
00168     p = cpl_parameter_new_value("gravi.gravi_all_p2vm.preproc_param.preproc_file",
00169             CPL_TYPE_BOOL, "Save the preproc file", "gravi.gravi_all_p2vm", FALSE);
00170     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "preprocfile");
00171     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00172     cpl_parameterlist_append(recipe->parameters, p);
00173  
00174     /* --the minimum wavelength */
00175     p = cpl_parameter_new_value("gravi.gravi_all_p2vm.preproc_param.min_wave",
00176             CPL_TYPE_DOUBLE, "the minimum wavelength", "gravi.gravi_all_p2vm", 2.0);
00177     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "lambdamin");
00178     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00179     cpl_parameterlist_append(recipe->parameters, p);
00180     
00181     /* --the maximum wavelength */
00182     p = cpl_parameter_new_value("gravi.gravi_all_p2vm.preproc_param.max_wave",
00183             CPL_TYPE_DOUBLE, "the maximum wavelength", "gravi.gravi_all_p2vm", 2.6);
00184     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "lambdamax");
00185     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00186     cpl_parameterlist_append(recipe->parameters, p);    
00187     
00188     /* --the number of the spectral element to compute */
00189     p = cpl_parameter_new_value("gravi.gravi_all_p2vm.preproc_param.nspectrum",
00190             CPL_TYPE_INT, "the number of the spectral", "gravi.gravi_all_dark", 10);
00191     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "nlambda");
00192     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00193     cpl_parameterlist_append(recipe->parameters, p);
00194     
00195     /* --the width of the profile element */
00196     p = cpl_parameter_new_value("gravi.gravi_all_flat."
00197                 "profile_width", CPL_TYPE_INT, "profile width option",
00198                                         "gravi.gravi_all_flat", 5);
00199     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "profile_width");
00200     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00201     cpl_parameterlist_append(recipe->parameters, p);
00202 
00203     /* --the background correction */
00204     p = cpl_parameter_new_value("gravi.gravi_all_p2vm.preproc_param.correction",
00205             CPL_TYPE_STRING, "the background correction", "gravi.gravi_all_p2vm", "DARK");
00206     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "background");
00207     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00208     cpl_parameterlist_append(recipe->parameters, p);
00209     
00210     p = cpl_parameter_new_value("gravi."
00211             "flat_param.Bad_dark_threshold", CPL_TYPE_INT, "the rms factor for "
00212                     "dark bad pixel threshold", "gravi.gravi_all_flat", 5);
00213     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "Bad_dark_threshold");
00214     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00215     cpl_parameterlist_append(recipe->parameters, p);
00216 
00217     return 0;
00218 }
00219 
00220 /*----------------------------------------------------------------------------*/
00226 /*----------------------------------------------------------------------------*/
00227 static int gravi_all_p2vm_exec(cpl_plugin * plugin)
00228 {
00229 
00230     cpl_recipe * recipe;                                                   
00231     int recipe_status;                                                     
00232     cpl_errorstate initial_errorstate = cpl_errorstate_get();              
00233                                                                            
00234     /* Return immediately if an error code is already set */               
00235     if (cpl_error_get_code() != CPL_ERROR_NONE) {                          
00236         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",    
00237                       cpl_func, __LINE__, cpl_error_get_where());          
00238         return (int)cpl_error_get_code();                                  
00239     }                                                                      
00240                                                                            
00241     if (plugin == NULL) {                                                  
00242         cpl_msg_error(cpl_func, "Null plugin");                            
00243         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00244     }                                                                      
00245                                                                            
00246     /* Verify plugin type */                                               
00247     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {           
00248         cpl_msg_error(cpl_func, "Plugin is not a recipe");                 
00249         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);                  
00250     }                                                                      
00251                                                                            
00252     /* Get the recipe */                                                   
00253     recipe = (cpl_recipe *)plugin;                                         
00254                                                                            
00255     /* Verify parameter and frame lists */                                 
00256     if (recipe->parameters == NULL) {
00257         cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00258         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00259     }
00260     if (recipe->frames == NULL) {                                          
00261         cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");     
00262         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00263     }                                                                      
00264                                                                            
00265     /* Invoke the recipe */                                                
00266     recipe_status = gravi_all_p2vm(recipe->frames, recipe->parameters);
00267                                                                            
00268     /* Ensure DFS-compliance of the products */                            
00269     if (cpl_dfs_update_product_header(recipe->frames)) {                   
00270         if (!recipe_status) recipe_status = (int)cpl_error_get_code();                         
00271     }                                                                      
00272                                                                            
00273     if (!cpl_errorstate_is_equal(initial_errorstate)) {                    
00274         /* Dump the error history since recipe execution start.            
00275            At this point the recipe cannot recover from the error */       
00276         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);          
00277     }                                                                      
00278                                                                            
00279     return recipe_status;                                                  
00280 }
00281 
00282 /*----------------------------------------------------------------------------*/
00288 /*----------------------------------------------------------------------------*/
00289 static int gravi_all_p2vm_destroy(cpl_plugin * plugin)
00290 {
00291     cpl_recipe * recipe;                                          
00292                                                                   
00293     if (plugin == NULL) {                                         
00294         cpl_msg_error(cpl_func, "Null plugin");                   
00295         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);            
00296     }                                                             
00297                                                                   
00298     /* Verify plugin type */                                      
00299     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {  
00300         cpl_msg_error(cpl_func, "Plugin is not a recipe");        
00301         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);         
00302     }                                                             
00303                                                                   
00304     /* Get the recipe */                                          
00305     recipe = (cpl_recipe *)plugin;                                
00306                                                                   
00307     cpl_parameterlist_delete(recipe->parameters);             
00308                                                                   
00309     return 0;                                                    
00310 }
00311 
00312 
00313 /*----------------------------------------------------------------------------*/
00320 /*----------------------------------------------------------------------------*/
00321 static int gravi_all_p2vm(cpl_frameset            * frameset,
00322                           const cpl_parameterlist * parlist)
00323 {
00324     cpl_frameset * p2vm_frameset, * wave_frameset, * dark_frameset,
00325                  * gain_frameset;
00326     cpl_propertylist * applist, * primary_hdr, * primary_hdr_dark, * p2vm_plist,
00327                      * primary_hdr_wave, * bad_primary_hdr, * profile_primary_hdr;
00328     cpl_frame * frame;
00329     const cpl_parameter * p;
00330     double property;
00331     char * output, * out_dark, * out_wave, * preproc_name, * out_gain;
00332     const char * frame_tag, * filename, * insname;
00333     gravi_data * p2vm_map, * data, * dark_map, * wave_map,
00334                * profile_map, * badpix_map;
00335     gravi_data ** calib_datas;
00336     gravi_data ** preproc_data;
00337     gravi_data ** raw_data;
00338     dark_map = NULL, wave_map = NULL;
00339     int nb_frame, i, nb_frame_DW, nb_frame_gain, j, badpix;
00340     double gain;
00341     int * shutter, file_type = 1;
00342     double dit, darkmean, minwave, maxwave, darkrms;
00343 
00344     /* Identify the RAW and CALIB frames in the input frameset */
00345 
00346     cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00347                         cpl_error_get_code()) ;
00348 
00349     /*  - Extract a set of frame p2vm and set of frame dark wave */
00350     p2vm_frameset = gravi_frameset_extract_p2vm_data(frameset);
00351     wave_frameset = gravi_frameset_extract_wave_file(frameset);
00352     dark_frameset = gravi_frameset_extract_dark_file(frameset);
00353     gain_frameset = gravi_frameset_extract_flat_file(frameset);
00354 
00355     if (cpl_frameset_is_empty(p2vm_frameset)) {
00356        /* To use this recipe the frameset must contain at least
00357         * one p2vm frame. */
00358         cpl_frameset_delete(p2vm_frameset);
00359         cpl_frameset_delete(wave_frameset);
00360         cpl_frameset_delete(wave_frameset);
00361         cpl_frameset_delete(gain_frameset);
00362        return (int)cpl_error_set_message(cpl_func,  CPL_ERROR_INVALID_TYPE,
00363                                           "No p2vm frame on the frameset") ;
00364     }
00365 
00366 
00367     /* Get the number of the p2vm frame contained in the frameset */
00368     nb_frame = cpl_frameset_get_size(p2vm_frameset);
00369     preproc_data = cpl_malloc(nb_frame * sizeof(gravi_data *));
00370     calib_datas = cpl_malloc(3 * sizeof(gravi_data *));
00371 
00372      /* Identify and extract the dark and wave fits files */
00373     int test_dark = 0, test_wave = 0;
00374     /* Check and get the dark frame */
00375     if (cpl_frameset_is_empty(dark_frameset)) {
00376         frame = cpl_frameset_get_first(p2vm_frameset);
00377         for (i = 0; i < nb_frame; i++){
00378             filename = cpl_frame_get_filename(frame);
00379             data = gravi_data_load(filename);
00380             primary_hdr = gravi_data_get_propertylist(data,
00381                                                 GRAVI_PRIMARY_HDR_NAME_EXT);
00382             shutter = gravi_shutters_check(primary_hdr);
00383             if (shutter == NULL){
00384                 cpl_frameset_delete(dark_frameset);
00385                 cpl_frameset_delete(wave_frameset);
00386                 cpl_frameset_delete(gain_frameset);
00387                 cpl_free(preproc_data);
00388                 cpl_free(calib_datas);
00389                 cpl_frameset_delete(p2vm_frameset);
00390                 gravi_data_delete(data);
00391                 return (int)cpl_error_set_message(cpl_func,
00392                         CPL_ERROR_ILLEGAL_OUTPUT, "Shutter problem");
00393             }
00394             /* The dark file must contains all the shutter close */
00395             if((shutter[0] == 0) && (shutter[1] == 0) && (shutter[2] == 0)
00396                     && (shutter[3] == 0)){
00397                 cpl_msg_info (NULL, "This file %s is a dark file",
00398                                                                 filename);
00399                 dark_map = gravi_compute_dark(data);
00400                 if (cpl_error_get_code()) {
00401                     cpl_frameset_delete(dark_frameset);
00402                     cpl_frameset_delete(wave_frameset);
00403                     cpl_frameset_delete(gain_frameset);
00404                     cpl_free(preproc_data);
00405                     cpl_free(calib_datas);
00406                     gravi_data_delete(dark_map);
00407                     cpl_frameset_delete(p2vm_frameset);
00408                     cpl_propertylist_delete(applist);
00409                     gravi_data_delete(data);
00410                     return (int)cpl_error_set_message(cpl_func,
00411                             CPL_ERROR_ILLEGAL_OUTPUT,
00412                                    "Error while computing the dark_map");
00413                 }
00414 
00415                 /* Save the dark map */
00416                 cpl_frameset_insert(dark_frameset, cpl_frame_duplicate(frame));
00417                 frame_tag = cpl_frame_get_tag(frame) ;
00418                 primary_hdr_dark = gravi_data_get_propertylist(dark_map,
00419                                                     GRAVI_PRIMARY_HDR_NAME_EXT);
00420                 applist = cpl_propertylist_new();
00421                 dit = gravi_pfits_get_dit(primary_hdr_dark);
00422                 //insname = gravi_pfits_get_insname(primary_hdr_dark);
00423                 darkmean = cpl_propertylist_get_double(primary_hdr_dark,
00424                                                                 QC_MEANDARK);
00425                 darkrms = cpl_propertylist_get_double(primary_hdr_dark,
00426                                                                 QC_DARKRMS);
00427 
00428                 if (!strcmp(frame_tag, GRAVI_FT_P2VM))
00429                     cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00430                                                                     DARK_FT);
00431                 else if (!strcmp(frame_tag, GRAVI_SC_P2VM))
00432                     cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00433                                                                     DARK_SC);
00434 
00435                 cpl_propertylist_append_double(applist, GRAVI_DET_DIT, dit);
00436                 //cpl_propertylist_append_string(applist, "INSNAME", insname);
00437                 cpl_propertylist_append_double(applist, QC_MEANDARK, darkmean);
00438                 cpl_propertylist_append_double(applist, QC_DARKRMS, darkrms);
00439                 out_dark = "gravi_final_dark.fits";
00440 
00441                 if (gravi_data_save(dark_map, frameset, out_dark, parlist,
00442                                     dark_frameset, "gravi_all_p2vm", applist)
00443                                                           != CPL_ERROR_NONE){
00444 
00445                     cpl_frameset_delete(dark_frameset);
00446                     cpl_frameset_delete(wave_frameset);
00447                     cpl_frameset_delete(gain_frameset);
00448                     cpl_free(preproc_data);
00449                     cpl_free(calib_datas);
00450                     gravi_data_delete(dark_map);
00451                     cpl_frameset_delete(p2vm_frameset);
00452                     cpl_propertylist_delete(applist);
00453                     gravi_data_delete(data);
00454 
00455                     return (int) cpl_error_set_message(cpl_func,
00456                             CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the dark_map"
00457                                                       " on the output file");
00458                 }
00459                 cpl_propertylist_delete(applist);
00460 
00461                 test_dark = 1;
00462             }
00463             frame = cpl_frameset_get_next(p2vm_frameset);
00464             gravi_data_delete(data);
00465             cpl_free(shutter);
00466             if (test_dark)
00467                 break;
00468         }
00469     }
00470     else{
00471         frame = cpl_frameset_get_first(dark_frameset);
00472 
00473         frame_tag = cpl_frame_get_tag(frame) ;
00474         filename = cpl_frame_get_filename(frame);
00475         data = gravi_data_load(filename);
00476 
00477         if ((strcmp(frame_tag, DARK_FT) == 0) ||
00478                                        (strcmp(frame_tag, DARK_SC) == 0)){
00479             cpl_msg_info (NULL, "This file %s is a dark file already "
00480                                                     "computed", filename);
00481             dark_map = gravi_data_duplicate(data);
00482         }
00483         gravi_data_delete(data);
00484     }
00485 
00486     /* Check that the dark_map in the input frameset */
00487     if ((dark_map == NULL)){
00488         cpl_frameset_delete(p2vm_frameset);
00489         cpl_frameset_delete(dark_frameset);
00490         cpl_frameset_delete(wave_frameset);
00491         cpl_frameset_delete(gain_frameset);
00492         cpl_free(preproc_data);
00493         gravi_data_delete(dark_map);
00494         cpl_free(calib_datas);
00495         return (int)cpl_error_set_message(cpl_func,  CPL_ERROR_INVALID_TYPE,
00496                                   "No dark frames in the frameset");
00497     }
00498 
00499     /* Compute the bad pixel map */
00500     cpl_frameset * badpix_frameset = gravi_frameset_extract_badpix_file (frameset);
00501 
00502     if (cpl_frameset_is_empty(badpix_frameset)) {
00503 
00504         badpix_map = gravi_compute_badpix (dark_map, parlist);
00505         if ((badpix_map == NULL)){
00506             cpl_frameset_delete(p2vm_frameset);
00507             cpl_frameset_delete(dark_frameset);
00508             cpl_frameset_delete(wave_frameset);
00509             cpl_frameset_delete(gain_frameset);
00510             cpl_frameset_delete(badpix_frameset);
00511             cpl_free(preproc_data);
00512             gravi_data_delete(dark_map);
00513             gravi_data_delete(badpix_map);
00514             cpl_free(calib_datas);
00515 
00516             return (int)cpl_error_set_message(cpl_func,  CPL_ERROR_INVALID_TYPE,
00517                                       "Error during the compute of the bad pixel map");
00518         }
00519         bad_primary_hdr = gravi_data_get_propertylist(badpix_map,
00520                                                     GRAVI_PRIMARY_HDR_NAME_EXT);
00521         applist = cpl_propertylist_new();
00522         badpix = cpl_propertylist_get_int(bad_primary_hdr,
00523                                                         QC_BADPIX);
00524         cpl_propertylist_append_int (applist, QC_BADPIX, badpix);
00525         frame = cpl_frameset_get_first(p2vm_frameset);
00526         frame_tag = cpl_frame_get_tag(frame);
00527         if (!strcmp(frame_tag, GRAVI_FT_P2VM))
00528             cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00529                                                             BAD_FT);
00530         else if (!strcmp(frame_tag, GRAVI_SC_P2VM))
00531             cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00532                                                             BAD_SC);
00533 
00534         if (gravi_data_save(badpix_map, frameset, "gravi_badpix_map.fits", parlist,
00535                             dark_frameset, "gravi_all_p2vm", applist)
00536                                                   != CPL_ERROR_NONE){
00537 
00538             cpl_frameset_delete(dark_frameset);
00539             cpl_frameset_delete(wave_frameset);
00540             cpl_frameset_delete(gain_frameset);
00541             cpl_frameset_delete(badpix_frameset);
00542             cpl_free(preproc_data);
00543             cpl_free(calib_datas);
00544             gravi_data_delete(dark_map);
00545             gravi_data_delete(badpix_map);
00546             cpl_frameset_delete(p2vm_frameset);
00547             cpl_propertylist_delete(applist);
00548 
00549             return (int) cpl_error_set_message(cpl_func,
00550                     CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the badpix_map"
00551                                               " on the output file");
00552         }
00553         cpl_propertylist_delete(applist);
00554     }
00555     else {
00556         frame = cpl_frameset_get_first(badpix_frameset);
00557 
00558         filename = cpl_frame_get_filename(frame);
00559         badpix_map = gravi_data_load(filename);
00560 
00561         cpl_msg_info (NULL, "This file %s is a bad pixel file already "
00562                                                 "computed", filename);
00563 
00564     }
00565     cpl_frameset_delete(badpix_frameset);
00566     /* Identify the flat file */
00567     raw_data = cpl_malloc(nb_frame * sizeof(gravi_data *));
00568     nb_frame_gain = nb_frame;
00569     frame = cpl_frameset_get_first(p2vm_frameset);
00570 
00571     for (i = 0; i < nb_frame; i++){
00572         filename = cpl_frame_get_filename(frame);
00573         raw_data[i] = gravi_data_load(filename);
00574         frame = cpl_frameset_get_next(p2vm_frameset);
00575     }
00576 
00577     /* Compute the gain and extract the gain map*/
00578     gain = gravi_compute_gain(raw_data, nb_frame_gain, dark_map);
00579     /* Check that the gain map in the input frameset */
00580     if ((gain == 0)){
00581         cpl_frameset_delete(p2vm_frameset);
00582         cpl_frameset_delete(dark_frameset);
00583         cpl_frameset_delete(wave_frameset);
00584         cpl_free(preproc_data);
00585         gravi_data_delete(dark_map);
00586         gravi_data_delete(badpix_map);
00587         cpl_free(calib_datas);
00588         for (i = 0; i < nb_frame_gain; i++){
00589             gravi_data_delete(raw_data[i]);
00590         }
00591         cpl_free(raw_data);
00592         return (int)cpl_error_set_message(cpl_func,  CPL_ERROR_INVALID_TYPE,
00593                                   "Compute of the gain failed");
00594     }
00595 
00596     if (cpl_frameset_is_empty(gain_frameset)) {
00597 
00598         frame = cpl_frameset_get_first(p2vm_frameset);
00599         frame_tag = cpl_frame_get_tag(frame);
00600 
00601         if (!strcmp(frame_tag, GRAVI_SC_P2VM)){
00602             /* Compute the profile */
00603             profile_map = gravi_compute_profile(raw_data, dark_map, nb_frame_gain, parlist);
00604             if (cpl_error_get_code()){
00605                 cpl_frameset_delete(p2vm_frameset);
00606                 cpl_frameset_delete(dark_frameset);
00607                 cpl_frameset_delete(wave_frameset);
00608                 cpl_free(preproc_data);
00609                 gravi_data_delete(profile_map);
00610                 gravi_data_delete(dark_map);
00611                 gravi_data_delete(badpix_map);
00612                 cpl_free(calib_datas);
00613                 for (i = 0; i < nb_frame_gain; i++){
00614                     gravi_data_delete(raw_data[i]);
00615                 }
00616                 cpl_free(raw_data);
00617                 return (int)cpl_error_set_message(cpl_func,  CPL_ERROR_INVALID_TYPE,
00618                                           "Compute of the profile map failed");
00619             }
00620 
00621             file_type = 1;
00622         }
00623         else if (!strcmp(frame_tag, GRAVI_FT_P2VM)){
00624             profile_map = gravi_data_duplicate(dark_map);
00625             gravi_data_erase(profile_map, GRAVI_IMAGING_DATA_NAME_EXT);
00626 
00627             file_type = 0;
00628         }
00629 
00630         /* Save the profile map */
00631         applist = cpl_propertylist_new();
00632         cpl_propertylist_append_double (applist, QC_MEANGAIN, gain);
00633         if (!strcmp(frame_tag, GRAVI_FT_P2VM))
00634             cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00635                                                             PROFILE_FT);
00636         else if (!strcmp(frame_tag, GRAVI_SC_P2VM))
00637             cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00638                                                             PROFILE_SC);
00639 
00640         out_gain = "gravi_final_profile.fits";
00641         if (gravi_data_save(profile_map, frameset, out_gain, parlist,
00642                              p2vm_frameset, "gravi_all_p2vm", applist)
00643                                                    != CPL_ERROR_NONE){
00644 
00645             cpl_frameset_delete(wave_frameset);
00646             cpl_frameset_delete(dark_frameset);
00647             cpl_frameset_delete(gain_frameset);
00648             for (i = 0; i < nb_frame_gain; i++){
00649                 gravi_data_delete(raw_data[i]);
00650             }
00651             cpl_free(raw_data);
00652             cpl_free(preproc_data);
00653             cpl_free(calib_datas);
00654             gravi_data_delete(dark_map);
00655             gravi_data_delete(badpix_map);
00656             gravi_data_delete(profile_map);
00657             cpl_frameset_delete(p2vm_frameset);
00658             cpl_propertylist_delete(applist);
00659 
00660             return (int) cpl_error_set_message(cpl_func,
00661                     CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the profile_map"
00662                                               " on the output file");
00663         }
00664         cpl_propertylist_delete(applist);
00665 
00666 
00667     }
00668     else{
00669         nb_frame_gain = cpl_frameset_get_size(gain_frameset);
00670 
00671         frame = cpl_frameset_get_first(gain_frameset);
00672         frame_tag = cpl_frame_get_tag(frame);
00673         filename = cpl_frame_get_filename(frame);
00674         profile_map = gravi_data_load(filename);
00675         profile_primary_hdr = gravi_data_get_propertylist(profile_map,
00676                                                 GRAVI_PRIMARY_HDR_NAME_EXT);
00677 
00678         cpl_propertylist_append_double (profile_primary_hdr, QC_MEANGAIN, gain);
00679 
00680         if (!strcmp(frame_tag, FLAT_SC))
00681             file_type = 1;
00682         else if (!strcmp(frame_tag, FLAT_FT))
00683             file_type = 0;
00684     }
00685 
00686     for (i = 0; i < nb_frame_gain; i++){
00687         gravi_data_delete(raw_data[i]);
00688     }
00689     cpl_free(raw_data);
00690 
00691     /* Check and get the wave frame */
00692     if (cpl_frameset_is_empty(wave_frameset)) {
00693         frame = cpl_frameset_get_first(p2vm_frameset);
00694         for (i = 0; i < nb_frame; i++){
00695             filename = cpl_frame_get_filename(frame);
00696             data = gravi_data_load(filename);
00697             primary_hdr = gravi_data_get_propertylist(data,
00698                                                 GRAVI_PRIMARY_HDR_NAME_EXT);
00699             shutter = gravi_shutters_check(primary_hdr);
00700             if (shutter == NULL){
00701                 cpl_frameset_delete(dark_frameset);
00702                 cpl_frameset_delete(wave_frameset);
00703                 cpl_frameset_delete(gain_frameset);
00704                 gravi_data_delete(dark_map);
00705                 cpl_free(preproc_data);
00706                 cpl_free(calib_datas);
00707                 cpl_frameset_delete(p2vm_frameset);
00708                 gravi_data_delete(data);
00709                 gravi_data_delete(profile_map);
00710                 gravi_data_delete(badpix_map);
00711                 return (int)cpl_error_set_message(cpl_func,
00712                         CPL_ERROR_ILLEGAL_OUTPUT, "Shutter problem");
00713             }
00714             /* The wave file must contains all the shutters open */
00715             if ((shutter[0] == 1) && (shutter[1] == 1) && (shutter[2] == 1)
00716                     && (shutter[3] == 1)){
00717                 cpl_msg_info (NULL, "This file %s is a wave file", filename);
00718                 profile_primary_hdr = gravi_data_get_propertylist(profile_map,
00719                                                         GRAVI_PRIMARY_HDR_NAME_EXT);
00720                 gain = cpl_propertylist_get_double(profile_primary_hdr, QC_MEANGAIN);
00721                 wave_map = gravi_compute_wave(data, profile_map);
00722 
00723                 if (cpl_error_get_code()) {
00724                     cpl_frameset_delete(wave_frameset);
00725                     cpl_frameset_delete(dark_frameset);
00726                     cpl_frameset_delete(gain_frameset);
00727                     cpl_free(preproc_data);
00728                     cpl_free(calib_datas);
00729                     gravi_data_delete(dark_map);
00730                     gravi_data_delete(wave_map);
00731                     cpl_frameset_delete(p2vm_frameset);
00732                     gravi_data_delete(data);
00733                     gravi_data_delete(profile_map);
00734                     gravi_data_delete(badpix_map);
00735                     return (int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
00736                                               "Error while computing the wave_map");
00737                 }
00738                 /* Save the wave map */
00739 
00740                 cpl_frameset_insert(wave_frameset, cpl_frame_duplicate(frame));
00741                 frame_tag = cpl_frame_get_tag(frame) ;
00742                 applist = cpl_propertylist_new();
00743                 primary_hdr_wave = gravi_data_get_propertylist(wave_map,
00744                                                     GRAVI_PRIMARY_HDR_NAME_EXT);
00745                 dit = gravi_pfits_get_dit(primary_hdr_wave);
00746                 //insname = gravi_pfits_get_insname(primary_hdr_wave);
00747                 minwave = cpl_propertylist_get_double(primary_hdr_wave,
00748                                                                 QC_MINWAVE);
00749                 cpl_propertylist_append_double(applist, QC_MINWAVE, minwave);
00750                 if (!strcmp(frame_tag, GRAVI_FT_P2VM))
00751                     cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00752                                                                     WAVE_FT);
00753                 else if (!strcmp(frame_tag, GRAVI_SC_P2VM))
00754                     cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00755                                                                     WAVE_SC);
00756                 maxwave = cpl_propertylist_get_double(primary_hdr_wave,
00757                                                                  QC_MAXWAVE);
00758 
00759                 cpl_propertylist_append_double(applist, GRAVI_DET_DIT, dit);
00760                 cpl_propertylist_append_double(applist, QC_MAXWAVE, maxwave);
00761                 //cpl_propertylist_append_string(applist, "INSNAME", insname);
00762 
00763                 out_wave = "gravi_final_wave.fits";
00764                 if (gravi_data_save(wave_map, frameset, out_wave, parlist,
00765                                      wave_frameset, "gravi_all_p2vm", applist)
00766                                                            != CPL_ERROR_NONE){
00767 
00768                     cpl_frameset_delete(wave_frameset);
00769                     cpl_frameset_delete(dark_frameset);
00770                     cpl_frameset_delete(gain_frameset);
00771                     cpl_free(preproc_data);
00772                     cpl_free(calib_datas);
00773                     gravi_data_delete(dark_map);
00774                     gravi_data_delete(wave_map);
00775                     cpl_frameset_delete(p2vm_frameset);
00776                     cpl_propertylist_delete(applist);
00777                     gravi_data_delete(data);
00778                     gravi_data_delete(profile_map);
00779                     gravi_data_delete(badpix_map);
00780                     return (int) cpl_error_set_message(cpl_func,
00781                             CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the wave_map"
00782                                                       " on the output file");
00783                 }
00784 
00785                 cpl_propertylist_delete(applist);
00786                 test_wave = 1;
00787             }
00788             frame = cpl_frameset_get_next(p2vm_frameset);
00789             gravi_data_delete(data);
00790             cpl_free(shutter);
00791             if (test_wave)
00792                 break;
00793         }
00794     }
00795     else{
00796 
00797         frame = cpl_frameset_get_first(wave_frameset);
00798 
00799         frame_tag = cpl_frame_get_tag(frame) ;
00800         filename = cpl_frame_get_filename(frame);
00801         data = gravi_data_load(filename);
00802 
00803         if ((strcmp(frame_tag, WAVE_FT) == 0) ||
00804                                        (strcmp(frame_tag, WAVE_SC) == 0)){
00805             cpl_msg_info (NULL, "This file %s is a wave file already "
00806                     "                               computed", filename);
00807             wave_map = gravi_data_duplicate(data);
00808         }
00809 
00810         gravi_data_delete(data);
00811     }
00812 
00813 
00814     /* Check that wave_map exist in the input frameset */
00815     if ((wave_map == NULL)){
00816         cpl_frameset_delete(p2vm_frameset);
00817         cpl_frameset_delete(dark_frameset);
00818         cpl_frameset_delete(wave_frameset);
00819         cpl_frameset_delete(gain_frameset);
00820         cpl_free(preproc_data);
00821         cpl_free(calib_datas);
00822         gravi_data_delete(wave_map);
00823         gravi_data_delete(profile_map);
00824         gravi_data_delete(badpix_map);
00825         return (int)cpl_error_set_message(cpl_func,  CPL_ERROR_INVALID_TYPE,
00826                                   "No wave frames in the frameset");
00827     }
00828 
00829     calib_datas[0] = dark_map;
00830     calib_datas[1] = wave_map;
00831     calib_datas[2] = profile_map;
00832 
00833 
00834     p = cpl_parameterlist_find_const(parlist, "gravi.gravi_all_p2vm."
00835                 "preproc_param.preproc_file");
00836 
00837     frame = cpl_frameset_get_first(p2vm_frameset);
00838     for (i = 0; i < nb_frame; i++){
00839         filename = cpl_frame_get_filename(frame);
00840         cpl_msg_info (NULL, "Calibration of this file %s", filename);
00841         data = gravi_data_load(filename);
00842         preproc_data[i] = gravi_preproc(data, calib_datas, 3, parlist);
00843 
00844         if (cpl_error_get_code()) {
00845             cpl_frameset_delete(p2vm_frameset);
00846             cpl_frameset_delete(dark_frameset);
00847             cpl_frameset_delete(wave_frameset);
00848             cpl_frameset_delete(gain_frameset);
00849             gravi_data_delete(profile_map);
00850             for (j = 0; j <= i; j++){
00851                 gravi_data_delete(preproc_data[j]);
00852             }
00853             cpl_free(preproc_data);
00854             cpl_free(calib_datas);
00855             gravi_data_delete(dark_map);
00856             gravi_data_delete(wave_map);
00857             gravi_data_delete(data);
00858             gravi_data_delete(badpix_map);
00859             return (int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
00860                                       "Error while computing the preproc data_map");
00861         }
00862 
00863         /* Option save the proproc file */
00864         if (cpl_parameter_get_bool(p)){
00865             preproc_name = cpl_sprintf("gravi_preproc_%s", filename);
00866             gravi_data_save_data(preproc_data[i], preproc_name, CPL_IO_CREATE);
00867             cpl_free(preproc_name);
00868         }
00869         frame = cpl_frameset_get_next(p2vm_frameset);
00870         gravi_data_delete(data);
00871     }
00872     /* Initialization of the output name */
00873     output = "gravi_final_p2vm.fits";
00874 
00875 //  /*test*/
00876 //  frame = cpl_frameset_get_first(p2vm_frameset);
00877 //  for (i =0; i < nb_frame; i++){
00878 //      filename = cpl_frame_get_filename(frame);
00879 //      printf("Calibration of this file %s\n", filename);
00880 //      preproc_data[i] = gravi_data_load(filename);
00881 //      frame = cpl_frameset_get_next(p2vm_frameset);
00882 //  }
00883 
00884     /* Compute the p2vm table of a set of p2vm frames */
00885     p2vm_map = gravi_compute_p2vm(preproc_data, nb_frame, 0, file_type);
00886 
00887     if (cpl_error_get_code()) {
00888         cpl_frameset_delete(p2vm_frameset);
00889         cpl_frameset_delete(dark_frameset);
00890         cpl_frameset_delete(wave_frameset);
00891         cpl_frameset_delete(gain_frameset);
00892         gravi_data_delete(profile_map);
00893         for (i = 0; i < nb_frame; i++){
00894             gravi_data_delete(preproc_data[i]);
00895         }
00896         cpl_free(preproc_data);
00897         cpl_free(calib_datas);
00898         gravi_data_delete(dark_map);
00899         gravi_data_delete(wave_map);
00900         gravi_data_delete(p2vm_map);
00901         gravi_data_delete(badpix_map);
00902         return (int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
00903                                   "Error while computing the p2vm_map");
00904     }
00905 
00906     /* Create product frame, add DataFlow keywords, save the file, log the
00907      * saved file in the input frameset*/
00908     applist = cpl_propertylist_new();
00909     p2vm_plist = gravi_data_get_propertylist(p2vm_map, GRAVI_P2VM_DATA_EXT);
00910     property = cpl_propertylist_get_double(p2vm_plist, QC_MEANCOH);
00911 
00912     frame = cpl_frameset_get_first(p2vm_frameset);
00913     frame_tag = cpl_frame_get_tag(frame) ;
00914     if (!strcmp(frame_tag, GRAVI_FT_P2VM))
00915         cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, P2VM_FT);
00916     else if (!strcmp(frame_tag, GRAVI_SC_P2VM))
00917         cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, P2VM_SC);
00918     else
00919         return (int)cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
00920                                                "Invalid type of p2vm tag");
00921     cpl_propertylist_append_double(applist, QC_MEANCOH, property);
00922 
00923     if (gravi_data_save( p2vm_map, frameset, "gravi_final_p2vm.fits", parlist,
00924                               p2vm_frameset, "gravi_all_p2vm", applist)
00925                                                       != CPL_ERROR_NONE){
00926 
00927         cpl_frameset_delete(dark_frameset);
00928         cpl_frameset_delete(wave_frameset);
00929         cpl_frameset_delete(gain_frameset);
00930         gravi_data_delete(profile_map);
00931         for (i = 0; i < nb_frame; i++){
00932             gravi_data_delete(preproc_data[i]);
00933         }
00934         cpl_free(preproc_data);
00935         cpl_free(calib_datas);
00936         gravi_data_delete(dark_map);
00937         gravi_data_delete(wave_map);
00938         gravi_data_delete(p2vm_map);
00939         cpl_frameset_delete(p2vm_frameset);
00940         cpl_propertylist_delete(applist);
00941         gravi_data_delete(badpix_map);
00942         return (int) cpl_error_set_message(cpl_func,
00943                 CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the p2vm_map"
00944                                           " on the output file");
00945     }
00946 
00947     /* Deallocation of all variables */
00948     cpl_frameset_delete(wave_frameset);
00949     cpl_frameset_delete(dark_frameset);
00950     cpl_frameset_delete(gain_frameset);
00951     gravi_data_delete(profile_map);
00952     for (i = 0; i < nb_frame; i++){
00953         gravi_data_delete(preproc_data[i]);
00954     }
00955     cpl_free(preproc_data);
00956     cpl_free(calib_datas);
00957     gravi_data_delete(dark_map);
00958     gravi_data_delete(wave_map);
00959     gravi_data_delete(p2vm_map);
00960     cpl_frameset_delete(p2vm_frameset);
00961     cpl_propertylist_delete(applist);
00962     gravi_data_delete(badpix_map);
00963 
00964     return (int)cpl_error_get_code();
00965 }
00966 
00967