midi_profile.c

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

Generated on 11 Feb 2011 for MIDI Pipeline Reference Manual by  doxygen 1.6.1