midi_profile.c

00001 /* $Id: midi_profile.c,v 1.32 2012/04/10 13:55:16 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: 2012/04/10 13:55:16 $
00024  * $Revision: 1.32 $
00025  * $Name: midi-2_8_2 $
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   
00398    /* Check if SOF is empty */
00399    cur_frame = cpl_frameset_get_first(frameset);
00400    if (cur_frame == NULL) {
00401        return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00402                "SOF does not have any file");
00403    }
00404 
00405 
00406    pTable_ext  = cpl_malloc((extnum) * sizeof(cpl_table *));
00407    pHeader_ext = cpl_malloc((extnum) * sizeof(cpl_propertylist *));
00408 
00409 
00410    /* Identify the RAW and CALIB frames in the input frameset */
00411    cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00412            cpl_error_get_code());
00413 
00414    /* Check if SCI_PHOT or HIGH_SENS */
00415    cur_frame = cpl_frameset_get_first(frameset);
00416 
00417    while(cur_frame)
00418    {
00419       /*      Check the right tags */
00420       tag = (char*)cpl_frame_get_tag(cur_frame);
00421       if (strcmp(tag,MIDI_PHOTOM_SP_CALIB) ==0 || strcmp(tag,MIDI_PHOTOM_SP_SCIENCE)==0) {
00422          isSP=1;
00423          dimenDATA=4;   
00424       }
00425       if (strcmp(tag,MIDI_PHOTOM_HS_CALIB) ==0 || strcmp(tag,MIDI_PHOTOM_HS_SCIENCE)==0) {
00426          isHS=1;
00427          dimenDATA=2;   
00428       }
00429       /* cpl_msg_info(cpl_func,"tag: %s SP: %d HS:%d ",tag,isSP,isHS); */
00430 
00431       /* Get next frame from SOF */
00432       cur_frame = cpl_frameset_get_next( frameset );   
00433 
00434 
00435       
00436    }
00437    
00438    if (isHS>0 && isSP>0) {
00439       return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00440                                         "SOF has mixed data: SCI_PHOT and HIGH_SENS. This is not allowed!");
00441    }
00442 
00443 
00444    for (i=0; i<dimenDATA;i++){
00445 
00446       imglst_AOPEN_DATA_S[i]=cpl_imagelist_new();
00447       imglst_AOPEN_DATA_T[i]=cpl_imagelist_new();
00448       
00449       imglst_BOPEN_DATA_S[i]=cpl_imagelist_new();
00450       imglst_BOPEN_DATA_T[i]=cpl_imagelist_new();
00451 
00452    }
00453    
00454   
00455 
00456 /*       Walk through the whole Set of Frames SOF  */
00457 
00458 
00459 /*    Start  1   */
00460    
00461    cur_frame = cpl_frameset_get_first(frameset);
00462    if (cur_frame == NULL) {
00463       return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00464                                         "SOF does not have any file");
00465    }
00466    /* Append the images from the tables to the various imagelists */     
00467    while(cur_frame)
00468    {
00469       /*      Check the right tags */
00470       tag = (char*)cpl_frame_get_tag(cur_frame);
00471       if (strcmp(tag, MIDI_PHOTOM_SP_SCIENCE) && strcmp(tag, MIDI_PHOTOM_SP_CALIB) && 
00472           strcmp(tag, MIDI_PHOTOM_HS_SCIENCE) && strcmp(tag, MIDI_PHOTOM_HS_CALIB)) {
00473          cur_frame = cpl_frameset_get_next( frameset );   
00474          continue;
00475       }
00476       
00477       cpl_msg_info(cpl_func, "Processing file  %s",cpl_frame_get_filename(cur_frame));
00478       ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DATA");
00479       ext_imaging_detector=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DETECTOR");
00480 
00481 
00482       /* Read the IMAGING_DETECTOR extension from the first valid image */
00483       
00484       if(isFirst<1){
00485          isFirst=1;
00486 
00487       /* Save the name of the first valid frame */
00488          first_valid_frame=cpl_sprintf(cpl_frame_get_filename(cur_frame));         
00489 
00490          for (i=0; i<extnum; i++)
00491          {
00492             pHeader_ext[i] = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), ext_imaging_detector );
00493             pTable_ext[i]  = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_detector, 1 );
00494             
00495             if (pHeader_ext[i] ==NULL || pTable_ext[i]==NULL)
00496             {
00497                cpl_msg_error(cpl_func,"Error loading the extended header or table of %s",cpl_frame_get_filename(cur_frame)); 
00498                
00499                for (i=0; i<extnum; i++)
00500                {
00501                   cpl_propertylist_delete(pHeader_ext[i]);
00502                   cpl_table_delete(pTable_ext[i]); 
00503                }
00504                cpl_free (pHeader_ext);
00505                cpl_free (pTable_ext); 
00506                
00507                
00508                
00509                return -1;
00510             }
00511          }
00512       }
00513 
00514       
00515 /*       Check if the file is AOPEN or BOPEN */
00516       
00517       plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
00518       if (cpl_propertylist_has(plist, "ESO INS SHUT ID") == 1)
00519       {
00520          shutter_id=(cpl_propertylist_get_string(plist, "ESO INS SHUT ID"));
00521       }
00522       
00523 /*       Checking if PRISM or GRISM */
00524       if (cpl_propertylist_has(plist, "ESO INS GRIS NAME") == 1)
00525       {
00526          strcpy(gris_name,cpl_propertylist_get_string(plist, "ESO INS GRIS NAME"));
00527       }
00528       
00529       if (!cpl_errorstate_is_equal(prestate)) {
00530          return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Could not ...");
00531       }
00532       
00533       if (strcmp(shutter_id,"AOPEN")==0)
00534       {
00535 /*          PhotA Data found */
00536          isPHOTAdata=1;
00537          
00538          /* Load extension  Imaging Data  */ 
00539          table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00540          if (table == NULL) {
00541             return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00542                                               "Could not load the table");
00543          }
00544          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());      
00545 
00546 
00547          for (i=0; i<dimenDATA;i++){
00548  
00549             dataname=cpl_sprintf("DATA%d",i+1);           
00550 
00551             /*         cpl_msg_info(cpl_func, "Scanning for dataname ..."); */
00552             if (cpl_table_has_column(table,dataname)){
00553                table_to_imglst_sky_target(dataname,"TARTYP2",imglst_AOPEN_DATA_S[i],imglst_AOPEN_DATA_T[i],table);
00554             }
00555             
00556             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]));
00557             cpl_free(dataname);
00558          }
00559          
00560          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00561          
00562          cpl_table_delete(table);
00563       }
00564       
00565       if (strcmp(shutter_id,"BOPEN")==0)
00566       {
00567 /*          PhotB Data found */
00568          isPHOTBdata=1;
00569          
00570          /* Load extension  Imaging Data  */ 
00571          table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
00572          if (table == NULL) {
00573             return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00574                                               "Could not load the table");
00575          }
00576          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());      
00577          
00578 
00579          for (i=0; i<dimenDATA;i++){
00580  
00581             dataname=cpl_sprintf("DATA%d",i+1);           
00582             /*         cpl_msg_info(cpl_func, "Scanning for dataname ..."); */
00583             if (cpl_table_has_column(table,dataname)){
00584                table_to_imglst_sky_target(dataname,"TARTYP2",imglst_BOPEN_DATA_S[i],imglst_BOPEN_DATA_T[i],table);
00585             }
00586             
00587             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]));
00588             cpl_free(dataname);
00589          }
00590          
00591          cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00592          
00593          cpl_table_delete(table);
00594       }
00595       
00596       
00597       cpl_propertylist_delete(plist);
00598       
00599       /* Get next frame from SOF */
00600       cur_frame = cpl_frameset_get_next( frameset );   
00601       
00602    } /* while more frames */
00603    
00604    
00605    if (isPHOTAdata==0 || isPHOTBdata==0)
00606    {
00607       cpl_msg_error(cpl_func, "No suitable SetOfFrame fround");              
00608       (void)cpl_error_set_where(cpl_func);
00609       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "No suitable SetOfFrame fround");
00610       
00611    }
00612 
00613 
00614 /* /\* Save the single sky and target cubes *\/ */
00615 
00616 
00617 /*    for (i=0; i<dimenDATA;i++){ */
00618 /*       if (strcmp(gris_name,"GRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_GRISM"); */
00619 /*       if (strcmp(gris_name,"PRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_PRISM"); */
00620 /*       if (strcmp(gris_name,"GRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_GRISM"); */
00621 /*       if (strcmp(gris_name,"PRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_PRISM"); */
00622 
00623            
00624 /*       dataname1=cpl_sprintf("imglst_AOPEN_DATA%d_T.fits",i+1);            */
00625 /*       if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_T[i], */
00626 /*                              CPL_BPP_IEEE_FLOAT, "midi_profile", */
00627 /*                              dataname, NULL, NULL, */
00628 /*                              PACKAGE "/" PACKAGE_VERSION, */
00629 /*                              dataname1)) { */
00630 /*          /\* Propagate the error *\/ */
00631 /*          (void)cpl_error_set_where(cpl_func); */
00632 /*       } */
00633 /*       cpl_free(dataname1);       */
00634 /*       dataname1=cpl_sprintf("imglst_AOPEN_DATA%d_S.fits",i+1);            */
00635 /*       if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_S[i], */
00636 /*                              CPL_BPP_IEEE_FLOAT, "midi_profile", */
00637 /*                              dataname, NULL, NULL, */
00638 /*                              PACKAGE "/" PACKAGE_VERSION, */
00639 /*                              dataname1)) { */
00640 /*          /\* Propagate the error *\/ */
00641 /*          (void)cpl_error_set_where(cpl_func); */
00642 /*       } */
00643 /*       cpl_free(dataname1);       */
00644 
00645 
00646 /*       dataname1=cpl_sprintf("imglst_BOPEN_DATA%d_T.fits",i+1);            */
00647 /*       if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_T[i], */
00648 /*                              CPL_BPP_IEEE_FLOAT, "midi_profile", */
00649 /*                              dataname, NULL, NULL, */
00650 /*                        PACKAGE "/" PACKAGE_VERSION, */
00651 /*                              dataname1)) { */
00652 /*          /\* Propagate the error *\/ */
00653 /*          (void)cpl_error_set_where(cpl_func); */
00654 /*       } */
00655 /*       cpl_free(dataname1); */
00656 /*       dataname1=cpl_sprintf("imglst_BOPEN_DATA%d_S.fits",i+1);            */
00657 /*       if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_S[i], */
00658 /*                                  CPL_BPP_IEEE_FLOAT, "midi_profile", */
00659 /*                                  dataname, NULL, NULL, */
00660 /*                                  PACKAGE "/" PACKAGE_VERSION, */
00661 /*                                  dataname1)) { */
00662 /*          /\* Propagate the error *\/ */
00663 /*          (void)cpl_error_set_where(cpl_func); */
00664 /*       } */
00665 /*       cpl_free(dataname); */
00666       
00667 /*       cpl_free(dataname1);       */
00668       
00669 
00670 /*    } */
00671 
00672 
00673 
00674 
00675 
00676 
00677 /*    Subtract the sky frame from the target frame*/
00678    
00679    cpl_msg_info(cpl_func, "Doing sky subtraction ...");
00680 
00681 
00682    for (i=0; i<dimenDATA;i++){
00683       
00684       if(cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i])==cpl_imagelist_get_size(imglst_AOPEN_DATA_S[i])
00685          && cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i])==cpl_imagelist_get_size(imglst_BOPEN_DATA_S[i])
00686          )
00687       {
00688          cpl_imagelist_subtract(imglst_AOPEN_DATA_T[i],imglst_AOPEN_DATA_S[i]); 
00689          cpl_imagelist_subtract(imglst_BOPEN_DATA_T[i],imglst_BOPEN_DATA_S[i]); 
00690          
00691       }
00692       else 
00693       {
00694          cpl_msg_info(cpl_func, "The number of Skyframes and Targetframes differ");
00695          return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00696                                            "The number of Skyframes and Targetframes differ"); 
00697       }
00698       
00699    }
00700    
00701 
00702    cpl_msg_info(cpl_func, "Collapsing the images in the time domain ...");
00703 
00704 /*    Collapse the sky-subtracted targets */
00705    for (i=0; i<dimenDATA;i++){
00706 
00707       dummy_image=cpl_imagelist_collapse_create(imglst_AOPEN_DATA_T[i]);
00708       image_AOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
00709       cpl_image_delete(dummy_image);
00710 
00711       dummy_image=cpl_imagelist_collapse_create(imglst_BOPEN_DATA_T[i]);
00712       image_BOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
00713       cpl_image_delete(dummy_image);      
00714 
00715    }
00716 
00717    
00718 /* For HIGH_SENS: stack AOPEN and BOPEN DATA1/DATA2 frames */
00719 
00720    if (isHS>0) {
00721       cpl_image_add(image_AOPEN_DATA_T[0], image_BOPEN_DATA_T[0]);  
00722       cpl_image_add(image_AOPEN_DATA_T[1], image_BOPEN_DATA_T[1]);  
00723    }
00724  
00725 /* For SCI_PHOT: stack AOPEN and BOPEN DATA2/DATA3 frames */
00726 
00727    if (isSP>0) {
00728       cpl_image_add(image_AOPEN_DATA_T[1], image_BOPEN_DATA_T[1]);  
00729       cpl_image_add(image_AOPEN_DATA_T[2], image_BOPEN_DATA_T[2]);  
00730    }
00731 
00732 
00733    pro_list = cpl_propertylist_new();
00734 
00735 /* Save the images for diagnostics */
00736 
00737    if(1){
00738       for (i=0; i<dimenDATA;i++){
00739          if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM_SIGNAL");
00740          if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM_SIGNAL");
00741          if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM_SIGNAL");
00742          if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM_SIGNAL");
00743          
00744          
00745          dataname1=cpl_sprintf("image_AOPEN_DATA%d_signal.fits",i+1);           
00746          if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  image_AOPEN_DATA_T[i],
00747                                 CPL_BPP_IEEE_FLOAT, "midi_profile",
00748                                 pro_list, NULL,
00749                                 PACKAGE "/" PACKAGE_VERSION,
00750                                 dataname1)) {
00751             /* Propagate the error */
00752             (void)cpl_error_set_where(cpl_func);
00753          }
00754          cpl_free(dataname1);      
00755          
00756          
00757          dataname1=cpl_sprintf("image_BOPEN_DATA%d_signal.fits",i+1);           
00758          if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  image_BOPEN_DATA_T[i],
00759                                 CPL_BPP_IEEE_FLOAT, "midi_profile",
00760                                 pro_list, NULL,
00761                                 PACKAGE "/" PACKAGE_VERSION,
00762                                 dataname1)) {
00763             /* Propagate the error */
00764             (void)cpl_error_set_where(cpl_func);
00765          }
00766          cpl_free(dataname1);      
00767          
00768       }
00769    }
00770   
00771 
00772 /* Calculate the standard deviation */
00773 
00774    for (i=0; i<dimenDATA;i++){
00775       std_AOPEN[i]=cpl_image_get_stdev(image_AOPEN_DATA_T[i]);
00776       std_BOPEN[i]=cpl_image_get_stdev(image_BOPEN_DATA_T[i]);
00777       cpl_msg_info(cpl_func,"First pass STDEV of AOPEN DATA%d: %f", i+1, std_AOPEN[i]);
00778       cpl_msg_info(cpl_func,"First pass STDEV of BOPEN DATA%d: %f", i+1, std_BOPEN[i]);
00779 
00780    }
00781    
00782 
00783 
00784 /* Create a bad pixel mask and connect it with the image*/
00785    for (i=0; i<dimenDATA;i++){
00786       image_AOPEN_DATA_T_mask[i] = cpl_mask_threshold_image_create(image_AOPEN_DATA_T[i], std_AOPEN[i], FLT_MAX);   
00787       image_BOPEN_DATA_T_mask[i] = cpl_mask_threshold_image_create(image_BOPEN_DATA_T[i], std_BOPEN[i], FLT_MAX);   
00788       cpl_image_reject_from_mask(image_AOPEN_DATA_T[i], image_AOPEN_DATA_T_mask[i]) ;    
00789       cpl_image_reject_from_mask(image_BOPEN_DATA_T[i], image_BOPEN_DATA_T_mask[i]) ;    
00790       
00791    }
00792 
00793 /* Re Calculate the standard deviation using the mask file*/
00794 
00795    cpl_msg_info(cpl_func," ");
00796    for (i=0; i<dimenDATA;i++){
00797       std_AOPEN[i]=cpl_image_get_stdev(image_AOPEN_DATA_T[i]);
00798       std_BOPEN[i]=cpl_image_get_stdev(image_BOPEN_DATA_T[i]);
00799       cpl_msg_info(cpl_func,"Second pass STDEV of AOPEN DATA%d: %f", i+1, std_AOPEN[i]);
00800       cpl_msg_info(cpl_func,"Second pass STDEV of BOPEN DATA%d: %f", i+1, std_BOPEN[i]);
00801    }
00802 
00803 /* un-connect the mask and free the memory*/
00804    for (i=0; i<dimenDATA;i++){
00805       cpl_image_accept_all(image_AOPEN_DATA_T[i]) ; 
00806       cpl_image_accept_all(image_BOPEN_DATA_T[i]) ; 
00807 
00808       cpl_mask_delete(image_AOPEN_DATA_T_mask[i]);
00809       cpl_mask_delete(image_BOPEN_DATA_T_mask[i]);
00810    }
00811 
00812    
00813    
00814 
00815 
00816 /* Threshold the image */
00817 
00818    for (i=0; i<dimenDATA;i++){
00819       cpl_image_threshold(image_AOPEN_DATA_T[i],threshold*std_AOPEN[i],FLT_MAX,0.,FLT_MAX);
00820       cpl_image_threshold(image_BOPEN_DATA_T[i],threshold*std_BOPEN[i],FLT_MAX,0.,FLT_MAX);
00821 
00822    }
00823    
00824 
00825 /* Normalize the profile */
00826 
00827    for (i=0; i<dimenDATA;i++){
00828       midi_normalize_image_y(image_AOPEN_DATA_T[i]);
00829       midi_normalize_image_y(image_BOPEN_DATA_T[i]);
00830       
00831    }
00832 
00833 /* Cast images do float as the midi core library expects a float profile*/
00834 /* ONLY THE TABLE IS SAVED AS FLOAT !!!! */
00835 
00836    for (i=0; i<dimenDATA;i++){
00837       image_AOPEN_DATA_T_float[i]=cpl_image_cast(image_AOPEN_DATA_T[i],CPL_TYPE_FLOAT);
00838       image_BOPEN_DATA_T_float[i]=cpl_image_cast(image_BOPEN_DATA_T[i],CPL_TYPE_FLOAT);
00839       
00840    }
00841 
00842 /* Derive QC parameters */
00843 
00844    cpl_msg_info(cpl_func, "Deriving QC parameters ...");
00845    image_size_y=cpl_image_get_size_y(image_AOPEN_DATA_T[0]);
00846 
00847    if (isHS>0) { 
00848       maxloop=2;
00849    }
00850    else {
00851       maxloop=4;
00852    }
00853 
00854    /* Compute the barycenter in y and the maximum... */
00855    for (i = 0; i < maxloop; i++){
00856 
00857        if (strcmp(gris_name,"GRISM")==0)
00858        {
00859            if(isSP>0 && i==0){
00860                /* Take data from BOBEN image as AOPEN has no flux */
00861                midi_profile_qc_barycenter(pro_list, i, image_BOPEN_DATA_T[i],
00862                        x_pos_grism_start,x_pos_grism_end);
00863                midi_profile_qc_maximum(pro_list, i, image_BOPEN_DATA_T[i],
00864                        x_pos_grism_start,x_pos_grism_end);
00865            }
00866            else{
00867                midi_profile_qc_barycenter(pro_list, i, image_AOPEN_DATA_T[i],
00868                        x_pos_grism_start,x_pos_grism_end);
00869                midi_profile_qc_maximum(pro_list, i, image_AOPEN_DATA_T[i],
00870                        x_pos_grism_start,x_pos_grism_end);
00871            }
00872        }
00873 
00874        if (strcmp(gris_name,"PRISM")==0)
00875        {
00876            if(isSP>0 && i==0){
00877                /* Take data from BOBEN image as AOPEN has no flux */
00878                midi_profile_qc_barycenter(pro_list, i, image_BOPEN_DATA_T[i],
00879                        x_pos_prism_start,x_pos_prism_end);
00880                midi_profile_qc_maximum(pro_list, i, image_BOPEN_DATA_T[i],
00881                        x_pos_prism_start,x_pos_prism_end);
00882            }
00883            else{
00884                midi_profile_qc_barycenter(pro_list, i, image_AOPEN_DATA_T[i],
00885                        x_pos_prism_start,x_pos_prism_end);
00886                midi_profile_qc_maximum(pro_list, i, image_AOPEN_DATA_T[i],
00887                        x_pos_prism_start,x_pos_prism_end);
00888            }
00889        }
00890    }
00891 
00892 
00893       
00894 /* Save the result */
00895 
00896 
00897 /* Save it to a fits table */
00898    
00899    profile_table=cpl_table_new(1);
00900    
00901 /* Propertylist to write to the extension */
00902    qclist_all_extension = cpl_propertylist_new();
00903    cpl_propertylist_append_string (qclist_all_extension, "EXTNAME", "IMAGING_DATA");
00904    
00905    if (isHS>0) { 
00906       append_image_to_table(profile_table,"DATA1",image_AOPEN_DATA_T_float[0],0);
00907       append_image_to_table(profile_table,"DATA2",image_AOPEN_DATA_T_float[1],0);
00908    }
00909    
00910    if (isSP>0) { 
00911       append_image_to_table(profile_table,"DATA1",image_BOPEN_DATA_T_float[0],0);
00912       append_image_to_table(profile_table,"DATA2",image_AOPEN_DATA_T_float[1],0);
00913       append_image_to_table(profile_table,"DATA3",image_AOPEN_DATA_T_float[2],0);
00914       append_image_to_table(profile_table,"DATA4",image_AOPEN_DATA_T_float[3],0);
00915    }
00916    
00917    
00918    if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM");
00919    if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM");
00920    if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM");
00921    if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM");
00922    
00923    if (strcmp(gris_name,"GRISM")==0)
00924    {
00925       cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, profile_table, 
00926                          qclist_all_extension, "midi_profile",
00927                          pro_list, NULL, 
00928                          PACKAGE "/" PACKAGE_VERSION,
00929                          "midi_profile_grism.fits");
00930       cpl_table_delete(profile_table);   
00931       
00932       /* Append the IMAGING_DETECTOR table from the first valid image */
00933       /*  midi_copy_extension() is a workaround to cpl_table_save() as
00934        *  the latter is modifiing the content during saving */
00935 
00936       midi_copy_extension(first_valid_frame, "midi_profile_grism.fits", "IMAGING_DETECTOR");
00937 
00938 /*       if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0], NULL, pHeader_ext[0], "midi_profile_grism.fits", CPL_IO_EXTEND )) */
00939 /*       { */
00940 /*          cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where()); */
00941 /*       } */
00942    }
00943    
00944    if (strcmp(gris_name,"PRISM")==0)
00945    {
00946       cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, profile_table, 
00947                          qclist_all_extension, "midi_profile",
00948                          pro_list, NULL, 
00949                          PACKAGE "/" PACKAGE_VERSION,
00950                          "midi_profile_prism.fits");
00951       cpl_table_delete(profile_table);    
00952       
00953       /* Append the IMAGING_DETECTOR table from the first valid image */
00954       /*  midi_copy_extension() is a workaround to cpl_table_save() as
00955        *  the latter is modifiing the content during saving */
00956       
00957       midi_copy_extension(first_valid_frame, "midi_profile_prism.fits", "IMAGING_DETECTOR");
00958       
00959 /*       if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0], NULL, pHeader_ext[0], "midi_profile_prism.fits", CPL_IO_EXTEND )) */
00960 /*       { */
00961 /*          cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where()); */
00962 /*       } */
00963    }
00964    
00965 
00966    cpl_propertylist_delete(qclist_all_extension);
00967 /*    cpl_free(dataname);  */
00968    
00969    for (i=0; i<extnum; i++)
00970    {
00971       cpl_propertylist_delete(pHeader_ext[i]);
00972       cpl_table_delete(pTable_ext[i]); 
00973    }
00974    cpl_free (pHeader_ext);
00975    cpl_free (pTable_ext); 
00976    cpl_free(first_valid_frame);   
00977    
00978    
00979 /* Save the images for diagnostics */
00980    if(1){
00981       for (i=0; i<dimenDATA;i++){
00982          if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM_PROFILE");
00983          if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM_PROFILE");
00984          if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM_PROFILE");
00985          if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM_PROFILE");
00986          
00987          
00988          dataname1=cpl_sprintf("image_AOPEN_DATA%d_profile.fits",i+1);           
00989          if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  image_AOPEN_DATA_T[i],
00990                                 CPL_BPP_IEEE_FLOAT, "midi_profile",
00991                                 pro_list, NULL,
00992                                 PACKAGE "/" PACKAGE_VERSION,
00993                                 dataname1)) {
00994             /* Propagate the error */
00995             (void)cpl_error_set_where(cpl_func);
00996          }
00997          cpl_free(dataname1);      
00998          
00999          
01000          dataname1=cpl_sprintf("image_BOPEN_DATA%d_profile.fits",i+1);           
01001          if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,  image_BOPEN_DATA_T[i],
01002                                 CPL_BPP_IEEE_FLOAT, "midi_profile",
01003                                 pro_list, NULL,
01004                                 PACKAGE "/" PACKAGE_VERSION,
01005                                 dataname1)) {
01006             /* Propagate the error */
01007             (void)cpl_error_set_where(cpl_func);
01008          }
01009 /*          cpl_free(dataname); */
01010          cpl_free(dataname1);      
01011          
01012       }
01013    }
01014 
01015 
01016 /* Free the memory */
01017 /*    cpl_propertylist_delete(qclist_all_extension); */
01018    cpl_propertylist_delete(pro_list);
01019 
01020 
01021    for (i=0; i<dimenDATA;i++){
01022       cpl_image_delete(image_AOPEN_DATA_T[i]);
01023       cpl_image_delete(image_BOPEN_DATA_T[i]);
01024       cpl_image_delete(image_AOPEN_DATA_T_float[i]);
01025       cpl_image_delete(image_BOPEN_DATA_T_float[i]);
01026    }
01027    
01028 
01029 
01030 
01031 
01032 
01033 /* /\* Save the image lists for diagnostics *\/ */
01034 /*    if(0){ */
01035 /*       for (i=0; i<dimenDATA;i++){ */
01036 /*          if (strcmp(gris_name,"GRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_GRISM_DIAGNOSTIC"); */
01037 /*          if (strcmp(gris_name,"PRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_PRISM_DIAGNOSTIC"); */
01038 /*          if (strcmp(gris_name,"GRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_GRISM_DIAGNOSTIC"); */
01039 /*          if (strcmp(gris_name,"PRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_PRISM_DIAGNOSTIC"); */
01040 
01041            
01042 /*          dataname1=cpl_sprintf("imglst_AOPEN_DATA%d.fits",i+1);            */
01043 /*          if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_T[i], */
01044 /*                                     CPL_BPP_IEEE_FLOAT, "midi_profile", */
01045 /*                                     dataname, NULL, NULL, */
01046 /*                                     PACKAGE "/" PACKAGE_VERSION, */
01047 /*                                     dataname1)) { */
01048 /*             /\* Propagate the error *\/ */
01049 /*             (void)cpl_error_set_where(cpl_func); */
01050 /*          } */
01051 /*          cpl_free(dataname1);       */
01052 
01053 
01054 /*          dataname1=cpl_sprintf("imglst_BOPEN_DATA%d.fits",i+1);            */
01055 /*          if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_T[i], */
01056 /*                                     CPL_BPP_IEEE_FLOAT, "midi_profile", */
01057 /*                                     dataname, NULL, NULL, */
01058 /*                                     PACKAGE "/" PACKAGE_VERSION, */
01059 /*                                     dataname1)) { */
01060 /*             /\* Propagate the error *\/ */
01061 /*             (void)cpl_error_set_where(cpl_func); */
01062 /*          } */
01063 /*          cpl_free(dataname); */
01064 /*          cpl_free(dataname1);       */
01065       
01066       
01067 
01068 
01069 /*       } */
01070 
01071 /*    } */
01072 
01073 
01074 /* Free the memory */
01075    for (i=0; i<dimenDATA;i++){
01076       cpl_imagelist_delete(imglst_AOPEN_DATA_T[i]);
01077       cpl_imagelist_delete(imglst_AOPEN_DATA_S[i]);
01078       
01079       cpl_imagelist_delete(imglst_BOPEN_DATA_T[i]);
01080       cpl_imagelist_delete(imglst_BOPEN_DATA_S[i]);
01081       
01082    }
01083    
01084 
01085 
01086 
01087    /* Free memory */
01088 
01089    
01090    return (int)cpl_error_get_code();
01091 }
01092 
01093 
01094 
01095 static int table_to_imglst_sky_target(const char * columname,
01096                     const char * columntype,
01097                     cpl_imagelist * imglst_sky,
01098                     cpl_imagelist * imglst_target,
01099                     cpl_table * table)
01100 {
01101 
01102    int dimenDATA; 
01103    int   i,csky=0, ctarget=0,csky_tmp=0, ctarget_tmp=0,first_unknown=0;
01104    cpl_array * array_data=NULL;
01105    cpl_image * image_data_int=NULL;
01106    cpl_errorstate          prestate = cpl_errorstate_get();
01107    char ** target_type;     
01108    cpl_type ctype;   
01109    int   flag_issky=0;
01110    int   flag_istarget=0;
01111 
01112    cpl_imagelist * imglst_sky_tmp=NULL;
01113    cpl_imagelist * imglst_target_tmp=NULL;
01114 
01115    imglst_sky_tmp=cpl_imagelist_new();
01116    imglst_target_tmp=cpl_imagelist_new();
01117 
01118    /* Load extension  Imaging Data  */ 
01119    
01120    dimenDATA=cpl_table_get_column_dimensions(table, columname);
01121    cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
01122    if (dimenDATA != 2) {
01123       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
01124                                         "DATA has a wrong dimension");
01125    }
01126    /* Read target type T(arget),S(ky),B(ackground) and store them in the image list */
01127    /* Loop over all "images" stored in DATA and add them to the imagelist */
01128    
01129    
01130    if (cpl_table_has_column(table, columntype))
01131    {
01132       target_type=cpl_table_get_data_string(table, columntype);
01133       
01134    }
01135    else 
01136    {
01137       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
01138                                         "TYPE of the Column not found");
01139    }
01140 /*    cpl_msg_info(cpl_func, "Number of images in the Table: %d",cpl_table_get_nrow(table)); */
01141 /*    cpl_msg_info(cpl_func, "Extracting %d images: please wait ...",cpl_table_get_nrow(table)); */
01142    
01143    
01144    ctype=cpl_table_get_column_type(table, columname);   
01145 /*    cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
01146 
01147 
01148 
01149    csky=cpl_imagelist_get_size(imglst_sky);
01150    
01151    ctarget=cpl_imagelist_get_size(imglst_target);
01152    
01153 /*    cpl_msg_info(cpl_func, "Length of imagelist S T : %d %d",csky,ctarget ); */
01154 
01155 
01156 
01157    flag_issky=0;
01158    flag_istarget=0;
01159 
01160 /* Find the first "U" image and discard the images befor  */
01161    
01162    for (i=0; i<cpl_table_get_nrow(table);i++){
01163       if(strcmp(target_type[i],"U")== 0){
01164          first_unknown=i;
01165          break;
01166       }
01167    }
01168 
01169 /*    cpl_msg_info(cpl_func, "First unknown: %d",first_unknown); */
01170    
01171    for (i=first_unknown; i<cpl_table_get_nrow(table);i++){
01172       
01173       array_data=(cpl_array *)cpl_table_get_array(table,columname, i); 
01174       if(ctype&CPL_TYPE_INT){
01175          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) );
01176       }
01177       if(ctype&CPL_TYPE_FLOAT){
01178          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) );
01179       }
01180       /* Cast the image to float */
01181 /*       image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
01182       
01183 /*        Append the image to the imagelists */
01184       
01185       if(strcmp(target_type[i],"S")== 0){
01186          cpl_imagelist_set(imglst_sky_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),csky_tmp++);
01187          flag_issky=1;
01188          cpl_image_unwrap(image_data_int);
01189          continue;
01190       }
01191       if(strcmp(target_type[i],"T")== 0){
01192          cpl_imagelist_set(imglst_target_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),ctarget_tmp++);
01193          flag_istarget=1;
01194          cpl_image_unwrap(image_data_int);
01195          continue;
01196       }
01197 
01198       /*Unwrap processed image manualy if nor Target nor Sky*/
01199       if(image_data_int!=NULL){
01200          cpl_image_unwrap(image_data_int);
01201       }
01202       if(flag_issky >0 && flag_istarget >0){
01203          /*Collapse the temporary imagelist and add the image to the "final" imagelist */
01204          cpl_imagelist_set(imglst_sky,cpl_imagelist_collapse_create(imglst_sky_tmp),csky++);
01205          cpl_imagelist_set(imglst_target,cpl_imagelist_collapse_create(imglst_target_tmp),ctarget++);
01206          csky_tmp=0;
01207          ctarget_tmp=0;
01208          flag_issky=0;
01209          flag_istarget=0;
01210 
01211          /*Empty imagelist and free the image buffer*/
01212          while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
01213             cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
01214          }
01215          while(cpl_imagelist_get_size(imglst_target_tmp)>0){
01216             cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
01217          }
01218 
01219       }
01220       
01221    }
01222  
01223    
01224 /*          Empty imagelist and free the image buffer*/
01225    while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
01226       cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
01227    }
01228    while(cpl_imagelist_get_size(imglst_target_tmp)>0){
01229       cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
01230    }
01231    
01232    cpl_imagelist_delete(imglst_sky_tmp);
01233    cpl_imagelist_delete(imglst_target_tmp);
01234    
01235    
01236    
01237    return (int)cpl_error_get_code();
01238 }
01239 
01240 
01241 /*----------------------------------------------------------------------------*/
01247 /*----------------------------------------------------------------------------*/
01248 
01249 int midi_normalize_image_y(cpl_image * image)
01250    {
01251 
01252    int         nx_image=0;
01253    int         ny_image=0;
01254    int         xpos=0; 
01255    int         ypos=0;
01256 
01257    double *    pimage=NULL;
01258    double *    pimage_norm=NULL;
01259 
01260    cpl_image * image_norm;
01261    
01262    cpl_msg_info(cpl_func, "Normalizing the integral of the profile to unity ...");         
01263 
01264    image_norm=cpl_image_collapse_create(image,0);
01265    
01266    nx_image=cpl_image_get_size_x(image);
01267    ny_image=cpl_image_get_size_y(image);
01268    
01269    pimage     = cpl_image_get_data_double(image);
01270    pimage_norm= cpl_image_get_data_double(image_norm);
01271 
01272    
01273    for (xpos=0; xpos<nx_image; ++xpos)
01274    {
01275       for(ypos=0; ypos<ny_image;++ypos)
01276       {
01277          if (pimage_norm[xpos]>0)  pimage[xpos+nx_image*ypos]  /= pimage_norm[xpos];
01278          if (pimage_norm[xpos]<=0) pimage[xpos+nx_image*ypos] = 0.;
01279       }
01280    }
01281    
01282    
01283    cpl_image_delete(image_norm);
01284    
01285    return 0;
01286 }
01287 
01288 /*----------------------------------------------------------------------------*/
01297 /*----------------------------------------------------------------------------*/
01298 
01299 static int append_image_to_table(cpl_table * table, const char * columname, cpl_image * image, int row)
01300 {
01301    
01302    cpl_array        * array_dimension=NULL;   
01303    cpl_array        * array_dummy=NULL;
01304    
01305    array_dimension=cpl_array_new(2,CPL_TYPE_INT);
01306    cpl_array_set(array_dimension, 0,cpl_image_get_size_x(image));
01307    cpl_array_set(array_dimension, 1,cpl_image_get_size_y(image));
01308    
01309    cpl_table_new_column_array(table, columname, CPL_TYPE_FLOAT, cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
01310    cpl_table_set_column_dimensions(table,columname,array_dimension);
01311    array_dummy = cpl_array_wrap_float(cpl_image_get_data_float(image), cpl_image_get_size_x(image)*cpl_image_get_size_y(image));    
01312    cpl_table_set_array(table, columname, row, array_dummy);
01313    cpl_array_unwrap(array_dummy);
01314    
01315    
01316    cpl_array_delete(array_dimension);
01317    
01318    return 0;  
01319 }
01320 
01321 /*----------------------------------------------------------------------------*/
01329 /*----------------------------------------------------------------------------*/
01330 
01331 static int midi_copy_extension(const char * infile , const char * outfile, const char * extension_name)   
01332 {
01333    fitsfile * fptrin=NULL;
01334    fitsfile * fptrout=NULL;
01335    int status=0;
01336    
01337    fits_open_diskfile(&fptrin, infile, READONLY, &status);
01338    fits_open_diskfile(&fptrout, outfile, READWRITE, &status);
01339    fits_movnam_hdu(fptrin, ANY_HDU, (char *)extension_name, 0, &status);
01340    fits_copy_hdu(fptrin, fptrout, 0, &status);
01341    fits_close_file(fptrin, &status);
01342    fits_close_file(fptrout, &status);
01343    
01344    if  (status != 0){
01345       cpl_msg_error(cpl_func,"A problem occured while copying the EXTENSION: %s", extension_name);      
01346       return 1;
01347    }
01348    else{
01349       return 0;
01350    }
01351 }
01352 
01353 /*----------------------------------------------------------------------------*/
01364 /*----------------------------------------------------------------------------*/
01365 static void midi_profile_qc_barycenter(cpl_propertylist * pro_list, int index,
01366         cpl_image * image, const int * x_start, const int * x_end)
01367 {
01368     char * qcname=NULL;
01369     int i=0;
01370     int image_size_y=0;
01371 
01372     image_size_y=cpl_image_get_size_y(image);
01373     for (i = 0; i < 5; i++){
01374 
01375         qcname=cpl_sprintf("ESO QC Y%d CENT BINNED%d",index+1,i+1);
01376         cpl_propertylist_update_double(pro_list, qcname,
01377                 cpl_image_get_centroid_y_window(image, x_start[i], 1,
01378                         x_end[i], image_size_y));
01379         cpl_free(qcname);
01380     }
01381     return;
01382 }
01383 
01384 /*----------------------------------------------------------------------------*/
01395 /*----------------------------------------------------------------------------*/
01396 static void midi_profile_qc_maximum(cpl_propertylist * pro_list, int index,
01397         cpl_image * image, const int * x_start, const int * x_end)
01398 {
01399     char * qcname=NULL;
01400     char * qcname_collapsed=NULL;
01401     int i=0;
01402     int image_size_y=0;
01403     cpl_image * image_collapsed=NULL;
01404 
01405     image_size_y=cpl_image_get_size_y(image);
01406 
01407     for (i = 0; i < 5; i++){
01408         image_collapsed=cpl_image_collapse_window_create(image, x_start[i], 1,
01409                 x_end[i], image_size_y,1);
01410         qcname=cpl_sprintf("ESO QC Y%d MAX BIN%d",index+1,i+1);
01411         qcname_collapsed=cpl_sprintf("ESO QC Y%d MAXSUM BIN%d",index+1,i+1);
01412 
01413         cpl_propertylist_update_double(pro_list, qcname,
01414                 cpl_image_get_max_window(image, x_start[i], 1,
01415                         x_end[i], image_size_y));
01416         cpl_propertylist_update_double(pro_list, qcname_collapsed,
01417                 cpl_image_get_max_window(image_collapsed, 1, 1,
01418                         1, image_size_y));
01419 
01420 
01421         cpl_free(qcname);
01422         cpl_free(qcname_collapsed);
01423         cpl_image_delete(image_collapsed);
01424     }
01425     return;
01426 }
01427 

Generated on 5 Mar 2013 for MIDI Pipeline Reference Manual by  doxygen 1.6.1