midi_profile.c

00001 /* $Id: midi_profile.c,v 1.31 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.31 $
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 <fitsio.h>
00044 #include "midi_cplupgrade.h"
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Private function prototypes
00048  -----------------------------------------------------------------------------*/
00049 
00050 static int midi_profile_create(cpl_plugin *);
00051 static int midi_profile_exec(cpl_plugin *);
00052 static int midi_profile_destroy(cpl_plugin *);
00053 static int midi_profile(cpl_frameset *, const cpl_parameterlist *);
00054 static int midi_normalize_image_y(cpl_image * image);
00055 static int table_to_imglst_sky_target(const char * ,
00056                            const char * ,
00057                            cpl_imagelist * ,
00058                            cpl_imagelist * ,
00059                            cpl_table *);
00060 static int append_image_to_table(cpl_table * table, const char * columname, 
00061                                  cpl_image * image, int row);
00062 static int midi_copy_extension(const char * infile , const char * outfile, 
00063                                const char * extension_name); 
00064 static void midi_profile_qc_barycenter(cpl_propertylist * pro_list, int index,
00065         cpl_image * image, const int * x_start, const int * x_end);
00066 static void midi_profile_qc_maximum(cpl_propertylist * pro_list, int index,
00067         cpl_image * image, const int * x_start, const int * x_end);
00068 
00069 /*-----------------------------------------------------------------------------
00070                             Static variables
00071  -----------------------------------------------------------------------------*/
00072 
00073 static char midi_profile_description[] =
00074 "This recipe derives the 2D profile of the spectral dispersed signal in\n"
00075 "the SCI_PHOT and HIGH_SENS mode. It combines a set of AOPEN and BOPEN\n"
00076 "photometry files with the following tags:\n"
00077 "\n"
00078 "PHOTOM_SP_CALIB/PHOTOM_SP_SCIENCE \n"
00079 "or\n"
00080 "PHOTOM_HS_CALIB/PHOTOM_HS_SCIENCE.  \n"
00081 "\n"
00082 "The sky subtracted data are co-added and then a two pass threshold is\n"
00083 "applied in order to get a clean profile of the signal: In a first pass\n"
00084 "the standard deviation of the image is derived and all the pixels\n"
00085 "exceeding the latter are marked as they are supposed to belong to the\n"
00086 "source and not to the background. In a second pass the standard\n"
00087 "deviation is recalculated excluding these source-pixels. After that,\n"
00088 "all pixels of the image not exceeding (<threshold> times\n"
00089 "standard-deviation) are set to zero. Please note, that the <threshold>\n"
00090 "variable can be set as a recipe option in sigma units. Finally the\n"
00091 "integral of the profile perpendicular to the dispersion direction is\n"
00092 "normalized to unity.\n"
00093 
00094 "Input files:\n\n"
00095 "  DO category:                Type:       Explanation:         Required:\n"
00096 "  PHOTOM_SP_CALIB             Raw         Raw data frame            \n"
00097 "  or                                                                \n"
00098 "  PHOTOM_SP_SCIENCE           Raw         Raw data frame            \n"
00099 "  or                                                               Y\n"
00100 "  PHOTOM_HS_CALIB             Raw         Raw data frame            \n"
00101 "  or                                                                \n"
00102 "  PHOTOM_HS_SCIENCE           Raw         Raw data frame          \n\n"
00103 "Output files:\n\n"
00104 "  DO category:                Data type:  Explanation:\n"
00105 "  KAPPA_HS_MASK_PRISM         FITS table  Spectral profile: Main product\n"
00106 "  or \n"
00107 "  KAPPA_HS_MASK_GRISM         FITS table  Spectral profile: Main product \n\n"
00108 "  KAPPA_HS_MASK_PRISM_PROFILE FITS image  single profile: For diagnostics\n"
00109 "  or \n"
00110 "  KAPPA_HS_MASK_GRISM_PROFILE FITS image  single profile: For diagnostics\n\n"
00111 "  KAPPA_HS_MASK_PRISM_SIGNAL  FITS image  single signal:  For diagnostics\n"
00112 "  or \n"
00113 "  KAPPA_HS_MASK_GRISM_SIGNAL  FITS image  single signal:  For diagnostics\n";
00114 
00115 /*-----------------------------------------------------------------------------
00116                                 Function code
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_profile",
00149                     "Derives the Profile of the spectrum",
00150                     midi_profile_description,
00151                     "Armin Gabasch",
00152                     PACKAGE_BUGREPORT,
00153                     midi_get_license(),
00154                     midi_profile_create,
00155                     midi_profile_exec,
00156                     midi_profile_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_profile_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     p = cpl_parameter_new_value("midi.midi_profile.threshold",
00216             CPL_TYPE_DOUBLE, "The threshold to discriminate between source and background pixels in sigma units", 
00217                                 "midi.midi_profile",1.7);
00218     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "threshold");
00219     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00220     cpl_parameterlist_append(recipe->parameters, p);
00221 
00222 
00223     return 0;
00224 }
00225 
00226 /*----------------------------------------------------------------------------*/
00232 /*----------------------------------------------------------------------------*/
00233 static int midi_profile_exec(cpl_plugin * plugin)
00234 {
00235 
00236     cpl_recipe * recipe;                                                   
00237     int recipe_status;                                                     
00238     cpl_errorstate initial_errorstate = cpl_errorstate_get();              
00239                                                                            
00240     /* Return immediately if an error code is already set */               
00241     if (cpl_error_get_code() != CPL_ERROR_NONE) {                          
00242         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",    
00243                       cpl_func, __LINE__, cpl_error_get_where());          
00244         return (int)cpl_error_get_code();                                  
00245     }                                                                      
00246                                                                            
00247     if (plugin == NULL) {                                                  
00248         cpl_msg_error(cpl_func, "Null plugin");                            
00249         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00250     }                                                                      
00251                                                                            
00252     /* Verify plugin type */                                               
00253     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {           
00254         cpl_msg_error(cpl_func, "Plugin is not a recipe");                 
00255         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);                  
00256     }                                                                      
00257                                                                            
00258     /* Get the recipe */                                                   
00259     recipe = (cpl_recipe *)plugin;                                         
00260                                                                            
00261     /* Verify parameter and frame lists */                                 
00262     if (recipe->parameters == NULL) {                                      
00263         cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00264         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00265     }                                                                      
00266     if (recipe->frames == NULL) {                                          
00267         cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");     
00268         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00269     }                                                                      
00270                                                                            
00271     /* Invoke the recipe */                                                
00272     recipe_status = midi_profile(recipe->frames, recipe->parameters);
00273                                                                            
00274     /* Ensure DFS-compliance of the products */                            
00275     if (cpl_dfs_update_product_header(recipe->frames)) {                   
00276         if (!recipe_status) recipe_status = (int)cpl_error_get_code();                         
00277     }                                                                      
00278                                                                            
00279     if (!cpl_errorstate_is_equal(initial_errorstate)) {                    
00280         /* Dump the error history since recipe execution start.            
00281            At this point the recipe cannot recover from the error */       
00282         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);          
00283     }                                                                      
00284                                                                            
00285     return recipe_status;                                                  
00286 }
00287 
00288 /*----------------------------------------------------------------------------*/
00294 /*----------------------------------------------------------------------------*/
00295 static int midi_profile_destroy(cpl_plugin * plugin)
00296 {
00297     cpl_recipe * recipe;                                          
00298                                                                   
00299     if (plugin == NULL) {                                         
00300         cpl_msg_error(cpl_func, "Null plugin");                   
00301         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);            
00302     }                                                             
00303                                                                   
00304     /* Verify plugin type */                                      
00305     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {  
00306         cpl_msg_error(cpl_func, "Plugin is not a recipe");        
00307         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);         
00308     }                                                             
00309                                                                   
00310     /* Get the recipe */                                          
00311     recipe = (cpl_recipe *)plugin;                                
00312                                                                   
00313     cpl_parameterlist_delete(recipe->parameters);             
00314                                                                   
00315     return 0;                                                    
00316 }
00317 
00318 /*----------------------------------------------------------------------------*/
00325 /*----------------------------------------------------------------------------*/
00326 static int midi_profile(cpl_frameset            * frameset,
00327                             const cpl_parameterlist * parlist)
00328 {
00329    cpl_propertylist * plist=NULL;
00330    cpl_propertylist * pro_list=NULL;
00331 
00332    cpl_propertylist * qclist_all_extension;
00333    cpl_propertylist **pHeader_ext= NULL;
00334    
00335    const cpl_parameter   * param;
00336 
00337    cpl_frame        * cur_frame=NULL;
00338 
00339    cpl_table        * table=NULL;
00340    cpl_table        * profile_table=NULL;
00341    cpl_table       ** pTable_ext= NULL;
00342 
00343    cpl_imagelist    * imglst_AOPEN_DATA_S[4];
00344    cpl_imagelist    * imglst_AOPEN_DATA_T[4];
00345    cpl_imagelist    * imglst_BOPEN_DATA_S[4];
00346    cpl_imagelist    * imglst_BOPEN_DATA_T[4];
00347 
00348 
00349    cpl_image        * image_AOPEN_DATA_T[4];       
00350    cpl_image        * image_BOPEN_DATA_T[4];       
00351    cpl_image        * image_AOPEN_DATA_T_float[4];       
00352    cpl_image        * image_BOPEN_DATA_T_float[4];       
00353 
00354    cpl_mask         * image_AOPEN_DATA_T_mask[4];
00355    cpl_mask         * image_BOPEN_DATA_T_mask[4];
00356 
00357    cpl_image        * dummy_image;
00358 
00359    cpl_errorstate     prestate = cpl_errorstate_get();
00360 
00361    char             * tag=NULL;
00362    char             * dataname=NULL;
00363    char             * dataname1=NULL;
00364    char               gris_name[100]= ""; 
00365    char             * first_valid_frame=NULL;
00366 
00367 
00368    const char       * shutter_id =NULL; 
00369 
00370    int                i=0;
00371    int                ext_imaging_data=0;       
00372    int                ext_imaging_detector=0;      
00373    int                isPHOTAdata=0;
00374    int                isPHOTBdata=0;
00375    int                isSP=0;   
00376    int                isHS=0;   
00377    int                dimenDATA=0;
00378    int                extnum=1;
00379    int                isFirst=0;
00380 
00381    double             std_AOPEN [4];       
00382    double             std_BOPEN[4];       
00383    double             threshold=0.;
00384 
00385    int                maxloop=0;
00386    int                image_size_y=0;
00387 
00388    const int x_pos_grism_start[]={38, 52, 116, 176, 217};
00389    const int x_pos_grism_end[]  ={51, 69, 137, 201, 246};
00390 
00391    const int x_pos_prism_start[]={120, 113, 88, 60, 36};
00392    const int x_pos_prism_end[]  ={127, 122, 99, 73, 51};
00393 
00394    param = cpl_parameterlist_find_const(parlist,
00395                                         "midi.midi_profile.threshold");
00396    threshold = cpl_parameter_get_double(param);
00397    pTable_ext  = cpl_malloc((extnum) * sizeof(cpl_table *));
00398    pHeader_ext = cpl_malloc((extnum) * sizeof(cpl_propertylist *));
00399   
00400 
00401 
00402    /* Identify the RAW and CALIB frames in the input frameset */
00403    cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00404                    cpl_error_get_code());
00405    
00406    
00407 /* Check if SCI_PHOT or HIGH_SENS */
00408    cur_frame = cpl_frameset_get_first(frameset);
00409  
00410    while(cur_frame)
00411    {
00412       /*      Check the right tags */
00413       tag = (char*)cpl_frame_get_tag(cur_frame);
00414       if (strcmp(tag,MIDI_PHOTOM_SP_CALIB) ==0 || strcmp(tag,MIDI_PHOTOM_SP_SCIENCE)==0) {
00415          isSP=1;
00416          dimenDATA=4;   
00417       }
00418       if (strcmp(tag,MIDI_PHOTOM_HS_CALIB) ==0 || strcmp(tag,MIDI_PHOTOM_HS_SCIENCE)==0) {
00419          isHS=1;
00420          dimenDATA=2;   
00421       }
00422       /* cpl_msg_info(cpl_func,"tag: %s SP: %d HS:%d ",tag,isSP,isHS); */
00423 
00424       /* Get next frame from SOF */
00425       cur_frame = cpl_frameset_get_next( frameset );   
00426 
00427 
00428       
00429    }
00430    
00431    if (isHS>0 && isSP>0) {
00432       return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00433                                         "SOF has mixed data: SCI_PHOT and HIGH_SENS. This is not allowed!");
00434    }
00435 
00436 
00437    for (i=0; i<dimenDATA;i++){
00438 
00439       imglst_AOPEN_DATA_S[i]=cpl_imagelist_new();
00440       imglst_AOPEN_DATA_T[i]=cpl_imagelist_new();
00441       
00442       imglst_BOPEN_DATA_S[i]=cpl_imagelist_new();
00443       imglst_BOPEN_DATA_T[i]=cpl_imagelist_new();
00444 
00445    }
00446    
00447   
00448 
00449 /*       Walk through the whole Set of Frames SOF  */
00450 
00451 
00452 /*    Start  1   */
00453    
00454    cur_frame = cpl_frameset_get_first(frameset);
00455    if (cur_frame == NULL) {
00456       return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00457                                         "SOF does not have any file");
00458    }
00459    /* Append the images from the tables to the various imagelists */     
00460    while(cur_frame)
00461    {
00462       /*      Check the right tags */
00463       tag = (char*)cpl_frame_get_tag(cur_frame);
00464       if (strcmp(tag, MIDI_PHOTOM_SP_SCIENCE) && strcmp(tag, MIDI_PHOTOM_SP_CALIB) && 
00465           strcmp(tag, MIDI_PHOTOM_HS_SCIENCE) && strcmp(tag, MIDI_PHOTOM_HS_CALIB)) {
00466          cur_frame = cpl_frameset_get_next( frameset );   
00467          continue;
00468       }
00469       
00470       cpl_msg_info(cpl_func, "Processing file  %s",cpl_frame_get_filename(cur_frame));
00471       ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DATA");
00472       ext_imaging_detector=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DETECTOR");
00473 
00474 
00475       /* Read the IMAGING_DETECTOR extension from the first valid image */
00476       
00477       if(isFirst<1){
00478          isFirst=1;
00479 
00480       /* Save the name of the first valid frame */
00481          first_valid_frame=cpl_sprintf(cpl_frame_get_filename(cur_frame));         
00482 
00483          for (i=0; i<extnum; i++)
00484          {
00485             pHeader_ext[i] = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), ext_imaging_detector );
00486             pTable_ext[i]  = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_detector, 1 );
00487             
00488             if (pHeader_ext[i] ==NULL || pTable_ext[i]==NULL)
00489             {
00490                cpl_msg_error(cpl_func,"Error loading the extended header or table of %s",cpl_frame_get_filename(cur_frame)); 
00491                
00492                for (i=0; i<extnum; i++)
00493                {
00494                   cpl_propertylist_delete(pHeader_ext[i]);
00495                   cpl_table_delete(pTable_ext[i]); 
00496                }
00497                cpl_free (pHeader_ext);
00498                cpl_free (pTable_ext); 
00499                
00500                
00501                
00502                return -1;
00503             }
00504          }
00505       }
00506 
00507       
00508 /*       Check if the file is AOPEN or BOPEN */
00509       
00510       plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
00511       if (cpl_propertylist_has(plist, "ESO INS SHUT ID") == 1)
00512       {
00513          shutter_id=(cpl_propertylist_get_string(plist, "ESO INS SHUT ID"));
00514       }
00515       
00516 /*       Checking if PRISM or GRISM */
00517       if (cpl_propertylist_has(plist, "ESO INS GRIS NAME") == 1)
00518       {
00519          strcpy(gris_name,cpl_propertylist_get_string(plist, "ESO INS GRIS NAME"));
00520       }
00521       
00522       if (!cpl_errorstate_is_equal(prestate)) {
00523          return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Could not ...");
00524       }
00525       
00526       if (strcmp(shutter_id,"AOPEN")==0)
00527       {
00528 /*          PhotA Data found */
00529          isPHOTAdata=1;
00530          
00531          /* Load extension  Imaging Data  */ 
00532          table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00533          if (table == NULL) {
00534             return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00535                                               "Could not load the table");
00536          }
00537          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());      
00538 
00539 
00540          for (i=0; i<dimenDATA;i++){
00541  
00542             dataname=cpl_sprintf("DATA%d",i+1);           
00543 
00544             /*         cpl_msg_info(cpl_func, "Scanning for dataname ..."); */
00545             if (cpl_table_has_column(table,dataname)){
00546                table_to_imglst_sky_target(dataname,"TARTYP2",imglst_AOPEN_DATA_S[i],imglst_AOPEN_DATA_T[i],table);
00547             }
00548             
00549             cpl_msg_info(cpl_func, "Number of so far processed AOPEN %s patches: % " CPL_SIZE_FORMAT "",dataname,cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i]));
00550             cpl_free(dataname);
00551          }
00552          
00553          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00554          
00555          cpl_table_delete(table);
00556       }
00557       
00558       if (strcmp(shutter_id,"BOPEN")==0)
00559       {
00560 /*          PhotB Data found */
00561          isPHOTBdata=1;
00562          
00563          /* Load extension  Imaging Data  */ 
00564          table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00565          if (table == NULL) {
00566             return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00567                                               "Could not load the table");
00568          }
00569          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());      
00570          
00571 
00572          for (i=0; i<dimenDATA;i++){
00573  
00574             dataname=cpl_sprintf("DATA%d",i+1);           
00575             /*         cpl_msg_info(cpl_func, "Scanning for dataname ..."); */
00576             if (cpl_table_has_column(table,dataname)){
00577                table_to_imglst_sky_target(dataname,"TARTYP2",imglst_BOPEN_DATA_S[i],imglst_BOPEN_DATA_T[i],table);
00578             }
00579             
00580             cpl_msg_info(cpl_func, "Number of so far processed BOPEN %s patches: % " CPL_SIZE_FORMAT "",dataname,cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i]));
00581             cpl_free(dataname);
00582          }
00583          
00584          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00585          
00586          cpl_table_delete(table);
00587       }
00588       
00589       
00590       cpl_propertylist_delete(plist);
00591       
00592       /* Get next frame from SOF */
00593       cur_frame = cpl_frameset_get_next( frameset );   
00594       
00595    } /* while more frames */
00596    
00597    
00598    if (isPHOTAdata==0 || isPHOTBdata==0)
00599    {
00600       cpl_msg_error(cpl_func, "No suitable SetOfFrame fround");              
00601       (void)cpl_error_set_where(cpl_func);
00602       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "No suitable SetOfFrame fround");
00603       
00604    }
00605 
00606 
00607 /* /\* Save the single sky and target cubes *\/ */
00608 
00609 
00610 /*    for (i=0; i<dimenDATA;i++){ */
00611 /*       if (strcmp(gris_name,"GRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_GRISM"); */
00612 /*       if (strcmp(gris_name,"PRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_PRISM"); */
00613 /*       if (strcmp(gris_name,"GRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_GRISM"); */
00614 /*       if (strcmp(gris_name,"PRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_PRISM"); */
00615 
00616            
00617 /*       dataname1=cpl_sprintf("imglst_AOPEN_DATA%d_T.fits",i+1);            */
00618 /*       if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_T[i], */
00619 /*                              CPL_BPP_IEEE_FLOAT, "midi_profile", */
00620 /*                              dataname, NULL, NULL, */
00621 /*                              PACKAGE "/" PACKAGE_VERSION, */
00622 /*                              dataname1)) { */
00623 /*          /\* Propagate the error *\/ */
00624 /*          (void)cpl_error_set_where(cpl_func); */
00625 /*       } */
00626 /*       cpl_free(dataname1);       */
00627 /*       dataname1=cpl_sprintf("imglst_AOPEN_DATA%d_S.fits",i+1);            */
00628 /*       if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_S[i], */
00629 /*                              CPL_BPP_IEEE_FLOAT, "midi_profile", */
00630 /*                              dataname, NULL, NULL, */
00631 /*                              PACKAGE "/" PACKAGE_VERSION, */
00632 /*                              dataname1)) { */
00633 /*          /\* Propagate the error *\/ */
00634 /*          (void)cpl_error_set_where(cpl_func); */
00635 /*       } */
00636 /*       cpl_free(dataname1);       */
00637 
00638 
00639 /*       dataname1=cpl_sprintf("imglst_BOPEN_DATA%d_T.fits",i+1);            */
00640 /*       if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_T[i], */
00641 /*                              CPL_BPP_IEEE_FLOAT, "midi_profile", */
00642 /*                              dataname, NULL, NULL, */
00643 /*                        PACKAGE "/" PACKAGE_VERSION, */
00644 /*                              dataname1)) { */
00645 /*          /\* Propagate the error *\/ */
00646 /*          (void)cpl_error_set_where(cpl_func); */
00647 /*       } */
00648 /*       cpl_free(dataname1); */
00649 /*       dataname1=cpl_sprintf("imglst_BOPEN_DATA%d_S.fits",i+1);            */
00650 /*       if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_S[i], */
00651 /*                                  CPL_BPP_IEEE_FLOAT, "midi_profile", */
00652 /*                                  dataname, NULL, NULL, */
00653 /*                                  PACKAGE "/" PACKAGE_VERSION, */
00654 /*                                  dataname1)) { */
00655 /*          /\* Propagate the error *\/ */
00656 /*          (void)cpl_error_set_where(cpl_func); */
00657 /*       } */
00658 /*       cpl_free(dataname); */
00659       
00660 /*       cpl_free(dataname1);       */
00661       
00662 
00663 /*    } */
00664 
00665 
00666 
00667 
00668 
00669 
00670 /*    Subtract the sky frame from the target frame*/
00671    
00672    cpl_msg_info(cpl_func, "Doing sky subtraction ...");
00673 
00674 
00675    for (i=0; i<dimenDATA;i++){
00676       
00677       if(cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i])==cpl_imagelist_get_size(imglst_AOPEN_DATA_S[i])
00678          && cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i])==cpl_imagelist_get_size(imglst_BOPEN_DATA_S[i])
00679          )
00680       {
00681          cpl_imagelist_subtract(imglst_AOPEN_DATA_T[i],imglst_AOPEN_DATA_S[i]); 
00682          cpl_imagelist_subtract(imglst_BOPEN_DATA_T[i],imglst_BOPEN_DATA_S[i]); 
00683          
00684       }
00685       else 
00686       {
00687          cpl_msg_info(cpl_func, "The number of Skyframes and Targetframes differ");
00688          return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00689                                            "The number of Skyframes and Targetframes differ"); 
00690       }
00691       
00692    }
00693    
00694 
00695    cpl_msg_info(cpl_func, "Collapsing the images in the time domain ...");
00696 
00697 /*    Collapse the sky-subtracted targets */
00698    for (i=0; i<dimenDATA;i++){
00699 
00700       dummy_image=cpl_imagelist_collapse_create(imglst_AOPEN_DATA_T[i]);
00701       image_AOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
00702       cpl_image_delete(dummy_image);
00703 
00704       dummy_image=cpl_imagelist_collapse_create(imglst_BOPEN_DATA_T[i]);
00705       image_BOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
00706       cpl_image_delete(dummy_image);      
00707 
00708    }
00709 
00710    
00711 /* For HIGH_SENS: stack AOPEN and BOPEN DATA1/DATA2 frames */
00712 
00713    if (isHS>0) {
00714       cpl_image_add(image_AOPEN_DATA_T[0], image_BOPEN_DATA_T[0]);  
00715       cpl_image_add(image_AOPEN_DATA_T[1], image_BOPEN_DATA_T[1]);  
00716    }
00717  
00718 /* For SCI_PHOT: stack AOPEN and BOPEN DATA2/DATA3 frames */
00719 
00720    if (isSP>0) {
00721       cpl_image_add(image_AOPEN_DATA_T[1], image_BOPEN_DATA_T[1]);  
00722       cpl_image_add(image_AOPEN_DATA_T[2], image_BOPEN_DATA_T[2]);  
00723    }
00724 
00725 
00726    pro_list = cpl_propertylist_new();
00727 
00728 /* Save the images for diagnostics */
00729 
00730    if(1){
00731       for (i=0; i<dimenDATA;i++){
00732          if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM_SIGNAL");
00733          if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM_SIGNAL");
00734          if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM_SIGNAL");
00735          if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM_SIGNAL");
00736          
00737          
00738          dataname1=cpl_sprintf("image_AOPEN_DATA%d_signal.fits",i+1);           
00739          if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  image_AOPEN_DATA_T[i],
00740                                 CPL_BPP_IEEE_FLOAT, "midi_profile",
00741                                 pro_list, NULL,
00742                                 PACKAGE "/" PACKAGE_VERSION,
00743                                 dataname1)) {
00744             /* Propagate the error */
00745             (void)cpl_error_set_where(cpl_func);
00746          }
00747          cpl_free(dataname1);      
00748          
00749          
00750          dataname1=cpl_sprintf("image_BOPEN_DATA%d_signal.fits",i+1);           
00751          if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  image_BOPEN_DATA_T[i],
00752                                 CPL_BPP_IEEE_FLOAT, "midi_profile",
00753                                 pro_list, NULL,
00754                                 PACKAGE "/" PACKAGE_VERSION,
00755                                 dataname1)) {
00756             /* Propagate the error */
00757             (void)cpl_error_set_where(cpl_func);
00758          }
00759          cpl_free(dataname1);      
00760          
00761       }
00762    }
00763   
00764 
00765 /* Calculate the standard deviation */
00766 
00767    for (i=0; i<dimenDATA;i++){
00768       std_AOPEN[i]=cpl_image_get_stdev(image_AOPEN_DATA_T[i]);
00769       std_BOPEN[i]=cpl_image_get_stdev(image_BOPEN_DATA_T[i]);
00770       cpl_msg_info(cpl_func,"First pass STDEV of AOPEN DATA%d: %f", i+1, std_AOPEN[i]);
00771       cpl_msg_info(cpl_func,"First pass STDEV of BOPEN DATA%d: %f", i+1, std_BOPEN[i]);
00772 
00773    }
00774    
00775 
00776 
00777 /* Create a bad pixel mask and connect it with the image*/
00778    for (i=0; i<dimenDATA;i++){
00779       image_AOPEN_DATA_T_mask[i] = cpl_mask_threshold_image_create(image_AOPEN_DATA_T[i], std_AOPEN[i], FLT_MAX);   
00780       image_BOPEN_DATA_T_mask[i] = cpl_mask_threshold_image_create(image_BOPEN_DATA_T[i], std_BOPEN[i], FLT_MAX);   
00781       cpl_image_reject_from_mask(image_AOPEN_DATA_T[i], image_AOPEN_DATA_T_mask[i]) ;    
00782       cpl_image_reject_from_mask(image_BOPEN_DATA_T[i], image_BOPEN_DATA_T_mask[i]) ;    
00783       
00784    }
00785 
00786 /* Re Calculate the standard deviation using the mask file*/
00787 
00788    cpl_msg_info(cpl_func," ");
00789    for (i=0; i<dimenDATA;i++){
00790       std_AOPEN[i]=cpl_image_get_stdev(image_AOPEN_DATA_T[i]);
00791       std_BOPEN[i]=cpl_image_get_stdev(image_BOPEN_DATA_T[i]);
00792       cpl_msg_info(cpl_func,"Second pass STDEV of AOPEN DATA%d: %f", i+1, std_AOPEN[i]);
00793       cpl_msg_info(cpl_func,"Second pass STDEV of BOPEN DATA%d: %f", i+1, std_BOPEN[i]);
00794    }
00795 
00796 /* un-connect the mask and free the memory*/
00797    for (i=0; i<dimenDATA;i++){
00798       cpl_image_accept_all(image_AOPEN_DATA_T[i]) ; 
00799       cpl_image_accept_all(image_BOPEN_DATA_T[i]) ; 
00800 
00801       cpl_mask_delete(image_AOPEN_DATA_T_mask[i]);
00802       cpl_mask_delete(image_BOPEN_DATA_T_mask[i]);
00803    }
00804 
00805    
00806    
00807 
00808 
00809 /* Threshold the image */
00810 
00811    for (i=0; i<dimenDATA;i++){
00812       cpl_image_threshold(image_AOPEN_DATA_T[i],threshold*std_AOPEN[i],FLT_MAX,0.,FLT_MAX);
00813       cpl_image_threshold(image_BOPEN_DATA_T[i],threshold*std_BOPEN[i],FLT_MAX,0.,FLT_MAX);
00814 
00815    }
00816    
00817 
00818 /* Normalize the profile */
00819 
00820    for (i=0; i<dimenDATA;i++){
00821       midi_normalize_image_y(image_AOPEN_DATA_T[i]);
00822       midi_normalize_image_y(image_BOPEN_DATA_T[i]);
00823       
00824    }
00825 
00826 /* Cast images do float as the midi core library expects a float profile*/
00827 /* ONLY THE TABLE IS SAVED AS FLOAT !!!! */
00828 
00829    for (i=0; i<dimenDATA;i++){
00830       image_AOPEN_DATA_T_float[i]=cpl_image_cast(image_AOPEN_DATA_T[i],CPL_TYPE_FLOAT);
00831       image_BOPEN_DATA_T_float[i]=cpl_image_cast(image_BOPEN_DATA_T[i],CPL_TYPE_FLOAT);
00832       
00833    }
00834 
00835 /* Derive QC parameters */
00836 
00837    cpl_msg_info(cpl_func, "Deriving QC parameters ...");
00838    image_size_y=cpl_image_get_size_y(image_AOPEN_DATA_T[0]);
00839 
00840    if (isHS>0) { 
00841       maxloop=2;
00842    }
00843    else {
00844       maxloop=4;
00845    }
00846 
00847    /* Compute the barycenter in y and the maximum... */
00848    for (i = 0; i < maxloop; i++){
00849 
00850        if (strcmp(gris_name,"GRISM")==0)
00851        {
00852            if(isSP>0 && i==0){
00853                /* Take data from BOBEN image as AOPEN has no flux */
00854                midi_profile_qc_barycenter(pro_list, i, image_BOPEN_DATA_T[i],
00855                        x_pos_grism_start,x_pos_grism_end);
00856                midi_profile_qc_maximum(pro_list, i, image_BOPEN_DATA_T[i],
00857                        x_pos_grism_start,x_pos_grism_end);
00858            }
00859            else{
00860                midi_profile_qc_barycenter(pro_list, i, image_AOPEN_DATA_T[i],
00861                        x_pos_grism_start,x_pos_grism_end);
00862                midi_profile_qc_maximum(pro_list, i, image_AOPEN_DATA_T[i],
00863                        x_pos_grism_start,x_pos_grism_end);
00864            }
00865        }
00866 
00867        if (strcmp(gris_name,"PRISM")==0)
00868        {
00869            if(isSP>0 && i==0){
00870                /* Take data from BOBEN image as AOPEN has no flux */
00871                midi_profile_qc_barycenter(pro_list, i, image_BOPEN_DATA_T[i],
00872                        x_pos_prism_start,x_pos_prism_end);
00873                midi_profile_qc_maximum(pro_list, i, image_BOPEN_DATA_T[i],
00874                        x_pos_prism_start,x_pos_prism_end);
00875            }
00876            else{
00877                midi_profile_qc_barycenter(pro_list, i, image_AOPEN_DATA_T[i],
00878                        x_pos_prism_start,x_pos_prism_end);
00879                midi_profile_qc_maximum(pro_list, i, image_AOPEN_DATA_T[i],
00880                        x_pos_prism_start,x_pos_prism_end);
00881            }
00882        }
00883    }
00884 
00885 
00886       
00887 /* Save the result */
00888 
00889 
00890 /* Save it to a fits table */
00891    
00892    profile_table=cpl_table_new(1);
00893    
00894 /* Propertylist to write to the extension */
00895    qclist_all_extension = cpl_propertylist_new();
00896    cpl_propertylist_append_string (qclist_all_extension, "EXTNAME", "IMAGING_DATA");
00897    
00898    if (isHS>0) { 
00899       append_image_to_table(profile_table,"DATA1",image_AOPEN_DATA_T_float[0],0);
00900       append_image_to_table(profile_table,"DATA2",image_AOPEN_DATA_T_float[1],0);
00901    }
00902    
00903    if (isSP>0) { 
00904       append_image_to_table(profile_table,"DATA1",image_BOPEN_DATA_T_float[0],0);
00905       append_image_to_table(profile_table,"DATA2",image_AOPEN_DATA_T_float[1],0);
00906       append_image_to_table(profile_table,"DATA3",image_AOPEN_DATA_T_float[2],0);
00907       append_image_to_table(profile_table,"DATA4",image_AOPEN_DATA_T_float[3],0);
00908    }
00909    
00910    
00911    if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM");
00912    if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM");
00913    if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM");
00914    if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM");
00915    
00916    if (strcmp(gris_name,"GRISM")==0)
00917    {
00918       cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, profile_table, 
00919                          qclist_all_extension, "midi_profile",
00920                          pro_list, NULL, 
00921                          PACKAGE "/" PACKAGE_VERSION,
00922                          "midi_profile_grism.fits");
00923       cpl_table_delete(profile_table);   
00924       
00925       /* Append the IMAGING_DETECTOR table from the first valid image */
00926       /*  midi_copy_extension() is a workaround to cpl_table_save() as
00927        *  the latter is modifiing the content during saving */
00928 
00929       midi_copy_extension(first_valid_frame, "midi_profile_grism.fits", "IMAGING_DETECTOR");
00930 
00931 /*       if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0], NULL, pHeader_ext[0], "midi_profile_grism.fits", CPL_IO_EXTEND )) */
00932 /*       { */
00933 /*          cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where()); */
00934 /*       } */
00935    }
00936    
00937    if (strcmp(gris_name,"PRISM")==0)
00938    {
00939       cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, profile_table, 
00940                          qclist_all_extension, "midi_profile",
00941                          pro_list, NULL, 
00942                          PACKAGE "/" PACKAGE_VERSION,
00943                          "midi_profile_prism.fits");
00944       cpl_table_delete(profile_table);    
00945       
00946       /* Append the IMAGING_DETECTOR table from the first valid image */
00947       /*  midi_copy_extension() is a workaround to cpl_table_save() as
00948        *  the latter is modifiing the content during saving */
00949       
00950       midi_copy_extension(first_valid_frame, "midi_profile_prism.fits", "IMAGING_DETECTOR");
00951       
00952 /*       if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0], NULL, pHeader_ext[0], "midi_profile_prism.fits", CPL_IO_EXTEND )) */
00953 /*       { */
00954 /*          cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where()); */
00955 /*       } */
00956    }
00957    
00958 
00959    cpl_propertylist_delete(qclist_all_extension);
00960 /*    cpl_free(dataname);  */
00961    
00962    for (i=0; i<extnum; i++)
00963    {
00964       cpl_propertylist_delete(pHeader_ext[i]);
00965       cpl_table_delete(pTable_ext[i]); 
00966    }
00967    cpl_free (pHeader_ext);
00968    cpl_free (pTable_ext); 
00969    cpl_free(first_valid_frame);   
00970    
00971    
00972 /* Save the images for diagnostics */
00973    if(1){
00974       for (i=0; i<dimenDATA;i++){
00975          if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM_PROFILE");
00976          if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM_PROFILE");
00977          if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM_PROFILE");
00978          if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM_PROFILE");
00979          
00980          
00981          dataname1=cpl_sprintf("image_AOPEN_DATA%d_profile.fits",i+1);           
00982          if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  image_AOPEN_DATA_T[i],
00983                                 CPL_BPP_IEEE_FLOAT, "midi_profile",
00984                                 pro_list, NULL,
00985                                 PACKAGE "/" PACKAGE_VERSION,
00986                                 dataname1)) {
00987             /* Propagate the error */
00988             (void)cpl_error_set_where(cpl_func);
00989          }
00990          cpl_free(dataname1);      
00991          
00992          
00993          dataname1=cpl_sprintf("image_BOPEN_DATA%d_profile.fits",i+1);           
00994          if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  image_BOPEN_DATA_T[i],
00995                                 CPL_BPP_IEEE_FLOAT, "midi_profile",
00996                                 pro_list, NULL,
00997                                 PACKAGE "/" PACKAGE_VERSION,
00998                                 dataname1)) {
00999             /* Propagate the error */
01000             (void)cpl_error_set_where(cpl_func);
01001          }
01002 /*          cpl_free(dataname); */
01003          cpl_free(dataname1);      
01004          
01005       }
01006    }
01007 
01008 
01009 /* Free the memory */
01010 /*    cpl_propertylist_delete(qclist_all_extension); */
01011    cpl_propertylist_delete(pro_list);
01012 
01013 
01014    for (i=0; i<dimenDATA;i++){
01015       cpl_image_delete(image_AOPEN_DATA_T[i]);
01016       cpl_image_delete(image_BOPEN_DATA_T[i]);
01017       cpl_image_delete(image_AOPEN_DATA_T_float[i]);
01018       cpl_image_delete(image_BOPEN_DATA_T_float[i]);
01019    }
01020    
01021 
01022 
01023 
01024 
01025 
01026 /* /\* Save the image lists for diagnostics *\/ */
01027 /*    if(0){ */
01028 /*       for (i=0; i<dimenDATA;i++){ */
01029 /*          if (strcmp(gris_name,"GRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_GRISM_DIAGNOSTIC"); */
01030 /*          if (strcmp(gris_name,"PRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_PRISM_DIAGNOSTIC"); */
01031 /*          if (strcmp(gris_name,"GRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_GRISM_DIAGNOSTIC"); */
01032 /*          if (strcmp(gris_name,"PRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_PRISM_DIAGNOSTIC"); */
01033 
01034            
01035 /*          dataname1=cpl_sprintf("imglst_AOPEN_DATA%d.fits",i+1);            */
01036 /*          if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_T[i], */
01037 /*                                     CPL_BPP_IEEE_FLOAT, "midi_profile", */
01038 /*                                     dataname, NULL, NULL, */
01039 /*                                     PACKAGE "/" PACKAGE_VERSION, */
01040 /*                                     dataname1)) { */
01041 /*             /\* Propagate the error *\/ */
01042 /*             (void)cpl_error_set_where(cpl_func); */
01043 /*          } */
01044 /*          cpl_free(dataname1);       */
01045 
01046 
01047 /*          dataname1=cpl_sprintf("imglst_BOPEN_DATA%d.fits",i+1);            */
01048 /*          if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_T[i], */
01049 /*                                     CPL_BPP_IEEE_FLOAT, "midi_profile", */
01050 /*                                     dataname, NULL, NULL, */
01051 /*                                     PACKAGE "/" PACKAGE_VERSION, */
01052 /*                                     dataname1)) { */
01053 /*             /\* Propagate the error *\/ */
01054 /*             (void)cpl_error_set_where(cpl_func); */
01055 /*          } */
01056 /*          cpl_free(dataname); */
01057 /*          cpl_free(dataname1);       */
01058       
01059       
01060 
01061 
01062 /*       } */
01063 
01064 /*    } */
01065 
01066 
01067 /* Free the memory */
01068    for (i=0; i<dimenDATA;i++){
01069       cpl_imagelist_delete(imglst_AOPEN_DATA_T[i]);
01070       cpl_imagelist_delete(imglst_AOPEN_DATA_S[i]);
01071       
01072       cpl_imagelist_delete(imglst_BOPEN_DATA_T[i]);
01073       cpl_imagelist_delete(imglst_BOPEN_DATA_S[i]);
01074       
01075    }
01076    
01077 
01078 
01079 
01080    /* Free memory */
01081 
01082    
01083    return (int)cpl_error_get_code();
01084 }
01085 
01086 
01087 
01088 static int table_to_imglst_sky_target(const char * columname,
01089                     const char * columntype,
01090                     cpl_imagelist * imglst_sky,
01091                     cpl_imagelist * imglst_target,
01092                     cpl_table * table)
01093 {
01094 
01095    int dimenDATA; 
01096    int   i,csky=0, ctarget=0,csky_tmp=0, ctarget_tmp=0,first_unknown=0;
01097    cpl_array * array_data=NULL;
01098    cpl_image * image_data_int=NULL;
01099    cpl_errorstate          prestate = cpl_errorstate_get();
01100    char ** target_type;     
01101    cpl_type ctype;   
01102    int   flag_issky=0;
01103    int   flag_istarget=0;
01104 
01105    cpl_imagelist * imglst_sky_tmp=NULL;
01106    cpl_imagelist * imglst_target_tmp=NULL;
01107 
01108    imglst_sky_tmp=cpl_imagelist_new();
01109    imglst_target_tmp=cpl_imagelist_new();
01110 
01111    /* Load extension  Imaging Data  */ 
01112    
01113    dimenDATA=cpl_table_get_column_dimensions(table, columname);
01114    cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
01115    if (dimenDATA != 2) {
01116       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
01117                                         "DATA has a wrong dimension");
01118    }
01119    /* Read target type T(arget),S(ky),B(ackground) and store them in the image list */
01120    /* Loop over all "images" stored in DATA and add them to the imagelist */
01121    
01122    
01123    if (cpl_table_has_column(table, columntype))
01124    {
01125       target_type=cpl_table_get_data_string(table, columntype);
01126       
01127    }
01128    else 
01129    {
01130       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
01131                                         "TYPE of the Column not found");
01132    }
01133 /*    cpl_msg_info(cpl_func, "Number of images in the Table: %d",cpl_table_get_nrow(table)); */
01134 /*    cpl_msg_info(cpl_func, "Extracting %d images: please wait ...",cpl_table_get_nrow(table)); */
01135    
01136    
01137    ctype=cpl_table_get_column_type(table, columname);   
01138 /*    cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
01139 
01140 
01141 
01142    csky=cpl_imagelist_get_size(imglst_sky);
01143    
01144    ctarget=cpl_imagelist_get_size(imglst_target);
01145    
01146 /*    cpl_msg_info(cpl_func, "Length of imagelist S T : %d %d",csky,ctarget ); */
01147 
01148 
01149 
01150    flag_issky=0;
01151    flag_istarget=0;
01152 
01153 /* Find the first "U" image and discard the images befor  */
01154    
01155    for (i=0; i<cpl_table_get_nrow(table);i++){
01156       if(strcmp(target_type[i],"U")== 0){
01157          first_unknown=i;
01158          break;
01159       }
01160    }
01161 
01162 /*    cpl_msg_info(cpl_func, "First unknown: %d",first_unknown); */
01163    
01164    for (i=first_unknown; i<cpl_table_get_nrow(table);i++){
01165       
01166       array_data=(cpl_array *)cpl_table_get_array(table,columname, i); 
01167       if(ctype&CPL_TYPE_INT){
01168          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) );
01169       }
01170       if(ctype&CPL_TYPE_FLOAT){
01171          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) );
01172       }
01173       /* Cast the image to float */
01174 /*       image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
01175       
01176 /*        Append the image to the imagelists */
01177       
01178       if(strcmp(target_type[i],"S")== 0){
01179          cpl_imagelist_set(imglst_sky_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),csky_tmp++);
01180          flag_issky=1;
01181          cpl_image_unwrap(image_data_int);
01182          continue;
01183       }
01184       if(strcmp(target_type[i],"T")== 0){
01185          cpl_imagelist_set(imglst_target_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),ctarget_tmp++);
01186          flag_istarget=1;
01187          cpl_image_unwrap(image_data_int);
01188          continue;
01189       }
01190 
01191       /*Unwrap processed image manualy if nor Target nor Sky*/
01192       if(image_data_int!=NULL){
01193          cpl_image_unwrap(image_data_int);
01194       }
01195       if(flag_issky >0 && flag_istarget >0){
01196          /*Collapse the temporary imagelist and add the image to the "final" imagelist */
01197          cpl_imagelist_set(imglst_sky,cpl_imagelist_collapse_create(imglst_sky_tmp),csky++);
01198          cpl_imagelist_set(imglst_target,cpl_imagelist_collapse_create(imglst_target_tmp),ctarget++);
01199          csky_tmp=0;
01200          ctarget_tmp=0;
01201          flag_issky=0;
01202          flag_istarget=0;
01203 
01204          /*Empty imagelist and free the image buffer*/
01205          while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
01206             cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
01207          }
01208          while(cpl_imagelist_get_size(imglst_target_tmp)>0){
01209             cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
01210          }
01211 
01212       }
01213       
01214    }
01215  
01216    
01217 /*          Empty imagelist and free the image buffer*/
01218    while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
01219       cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
01220    }
01221    while(cpl_imagelist_get_size(imglst_target_tmp)>0){
01222       cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
01223    }
01224    
01225    cpl_imagelist_delete(imglst_sky_tmp);
01226    cpl_imagelist_delete(imglst_target_tmp);
01227    
01228    
01229    
01230    return (int)cpl_error_get_code();
01231 }
01232 
01233 
01234 /*----------------------------------------------------------------------------*/
01240 /*----------------------------------------------------------------------------*/
01241 
01242 int midi_normalize_image_y(cpl_image * image)
01243    {
01244 
01245    int         nx_image=0;
01246    int         ny_image=0;
01247    int         xpos=0; 
01248    int         ypos=0;
01249 
01250    double *    pimage=NULL;
01251    double *    pimage_norm=NULL;
01252 
01253    cpl_image * image_norm;
01254    
01255    cpl_msg_info(cpl_func, "Normalizing the integral of the profile to unity ...");         
01256 
01257    image_norm=cpl_image_collapse_create(image,0);
01258    
01259    nx_image=cpl_image_get_size_x(image);
01260    ny_image=cpl_image_get_size_y(image);
01261    
01262    pimage     = cpl_image_get_data_double(image);
01263    pimage_norm= cpl_image_get_data_double(image_norm);
01264 
01265    
01266    for (xpos=0; xpos<nx_image; ++xpos)
01267    {
01268       for(ypos=0; ypos<ny_image;++ypos)
01269       {
01270          if (pimage_norm[xpos]>0)  pimage[xpos+nx_image*ypos]  /= pimage_norm[xpos];
01271          if (pimage_norm[xpos]<=0) pimage[xpos+nx_image*ypos] = 0.;
01272       }
01273    }
01274    
01275    
01276    cpl_image_delete(image_norm);
01277    
01278    return 0;
01279 }
01280 
01281 /*----------------------------------------------------------------------------*/
01290 /*----------------------------------------------------------------------------*/
01291 
01292 static int append_image_to_table(cpl_table * table, const char * columname, cpl_image * image, int row)
01293 {
01294    
01295    cpl_array        * array_dimension=NULL;   
01296    cpl_array        * array_dummy=NULL;
01297    
01298    array_dimension=cpl_array_new(2,CPL_TYPE_INT);
01299    cpl_array_set(array_dimension, 0,cpl_image_get_size_x(image));
01300    cpl_array_set(array_dimension, 1,cpl_image_get_size_y(image));
01301    
01302    cpl_table_new_column_array(table, columname, CPL_TYPE_FLOAT, cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
01303    cpl_table_set_column_dimensions(table,columname,array_dimension);
01304    array_dummy = cpl_array_wrap_float(cpl_image_get_data_float(image), cpl_image_get_size_x(image)*cpl_image_get_size_y(image));    
01305    cpl_table_set_array(table, columname, row, array_dummy);
01306    cpl_array_unwrap(array_dummy);
01307    
01308    
01309    cpl_array_delete(array_dimension);
01310    
01311    return 0;  
01312 }
01313 
01314 /*----------------------------------------------------------------------------*/
01322 /*----------------------------------------------------------------------------*/
01323 
01324 static int midi_copy_extension(const char * infile , const char * outfile, const char * extension_name)   
01325 {
01326    fitsfile * fptrin=NULL;
01327    fitsfile * fptrout=NULL;
01328    int status=0;
01329    
01330    fits_open_diskfile(&fptrin, infile, READONLY, &status);
01331    fits_open_diskfile(&fptrout, outfile, READWRITE, &status);
01332    fits_movnam_hdu(fptrin, ANY_HDU, (char *)extension_name, 0, &status);
01333    fits_copy_hdu(fptrin, fptrout, 0, &status);
01334    fits_close_file(fptrin, &status);
01335    fits_close_file(fptrout, &status);
01336    
01337    if  (status != 0){
01338       cpl_msg_error(cpl_func,"A problem occured while copying the EXTENSION: %s", extension_name);      
01339       return 1;
01340    }
01341    else{
01342       return 0;
01343    }
01344 }
01345 
01346 /*----------------------------------------------------------------------------*/
01357 /*----------------------------------------------------------------------------*/
01358 static void midi_profile_qc_barycenter(cpl_propertylist * pro_list, int index,
01359         cpl_image * image, const int * x_start, const int * x_end)
01360 {
01361     char * qcname=NULL;
01362     int i=0;
01363     int image_size_y=0;
01364 
01365     image_size_y=cpl_image_get_size_y(image);
01366     for (i = 0; i < 5; i++){
01367 
01368         qcname=cpl_sprintf("ESO QC Y%d CENT BINNED%d",index+1,i+1);
01369         cpl_propertylist_update_double(pro_list, qcname,
01370                 cpl_image_get_centroid_y_window(image, x_start[i], 1,
01371                         x_end[i], image_size_y));
01372         cpl_free(qcname);
01373     }
01374     return;
01375 }
01376 
01377 /*----------------------------------------------------------------------------*/
01388 /*----------------------------------------------------------------------------*/
01389 static void midi_profile_qc_maximum(cpl_propertylist * pro_list, int index,
01390         cpl_image * image, const int * x_start, const int * x_end)
01391 {
01392     char * qcname=NULL;
01393     char * qcname_collapsed=NULL;
01394     int i=0;
01395     int image_size_y=0;
01396     cpl_image * image_collapsed=NULL;
01397 
01398     image_size_y=cpl_image_get_size_y(image);
01399 
01400     for (i = 0; i < 5; i++){
01401         image_collapsed=cpl_image_collapse_window_create(image, x_start[i], 1,
01402                 x_end[i], image_size_y,1);
01403         qcname=cpl_sprintf("ESO QC Y%d MAX BIN%d",index+1,i+1);
01404         qcname_collapsed=cpl_sprintf("ESO QC Y%d MAXSUM BIN%d",index+1,i+1);
01405 
01406         cpl_propertylist_update_double(pro_list, qcname,
01407                 cpl_image_get_max_window(image, x_start[i], 1,
01408                         x_end[i], image_size_y));
01409         cpl_propertylist_update_double(pro_list, qcname_collapsed,
01410                 cpl_image_get_max_window(image_collapsed, 1, 1,
01411                         1, image_size_y));
01412 
01413 
01414         cpl_free(qcname);
01415         cpl_free(qcname_collapsed);
01416         cpl_image_delete(image_collapsed);
01417     }
01418     return;
01419 }
01420 

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