midi_cplutils.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  midicplutils.c
00007 * Description:  reads a table column into an imagelist
00008 *
00009 * History:
00010 * 01-Mai-09     (agabasch) Created
00011 *******************************************************************************
00012 ******************************************************************************/
00013 
00014 /******************************************************************************
00015 *   Include files
00016 ******************************************************************************/
00017 
00018 #include "midi_cplutils.h"
00019 #include <cpl.h>
00020 #include <string.h>
00021 #include "midi_cplupgrade.h"
00022 
00023 int timetable_to_cpltable(const char * columname,
00024                           cpl_table * intable,
00025                           cpl_table * outtable)
00026 {
00027    
00028    int              dimen_in=0, dimen_out=0;
00029    cpl_table      * dummy_table=NULL;
00030    cpl_errorstate   prestate = cpl_errorstate_get();
00031    
00032    /* Load extension  Imaging Data  */ 
00033    
00034    
00035    dimen_out=cpl_table_get_nrow(outtable);         
00036    dimen_in =cpl_table_get_nrow(intable);      
00037    
00038    dummy_table=cpl_table_new(dimen_in);
00039    cpl_table_duplicate_column(dummy_table, columname, intable, columname);
00040    cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());   
00041 
00042 /*    cpl_table_dump_structure      (intable,NULL);  */
00043 /*    cpl_table_dump_structure      (outtable,NULL);     */
00044 /*    cpl_table_dump_structure      (dummy_table,NULL);  */
00045 
00046    cpl_table_insert(outtable, dummy_table, dimen_out+1);   
00047    if(dummy_table!=NULL){
00048       cpl_table_delete(dummy_table);
00049    }
00050    cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00051    
00052    return (int)cpl_error_get_code();
00053 }
00054 
00055 
00056 
00057 
00058 int table_to_imglst(const char * columname,
00059                     cpl_imagelist * imglst,
00060                     cpl_table * table)
00061 {
00062    
00063    int             dimenDATA=0; 
00064    int             i=0, counter=0;
00065    cpl_array     * array_data=NULL;
00066    cpl_image     * image_data_int=NULL;
00067    cpl_errorstate  prestate = cpl_errorstate_get();
00068    cpl_type        ctype;   
00069    
00070    /* Load extension  Imaging Data  */ 
00071    
00072    dimenDATA=cpl_table_get_column_dimensions(table, columname);
00073    cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00074    if (dimenDATA != 2) {
00075       return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00076                                         "DATA has a wrong dimension");
00077    }
00078    ctype=cpl_table_get_column_type(table, columname);   
00079 /*    cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
00080    
00081    counter=cpl_imagelist_get_size(imglst);
00082 
00083 
00084    for (i=0; i<cpl_table_get_nrow(table);i++){
00085       
00086       array_data=(cpl_array *)cpl_table_get_array(table,columname, i); 
00087       if(ctype&CPL_TYPE_INT){
00088          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) );
00089       }
00090       if(ctype&CPL_TYPE_FLOAT){
00091          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) );
00092       }
00093       /* Cast the image to float */
00094 /*       image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
00095       
00096 /*        Append the image to the imagelists */
00097       
00098       cpl_imagelist_set(imglst,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),counter++);
00099 
00100       /*Unwrap processed image manualy if nor Target nor Sky*/
00101       if(image_data_int!=NULL){
00102          cpl_image_unwrap(image_data_int);
00103       }
00104       
00105       
00106    }
00107    
00108    
00109 
00110    return (int)cpl_error_get_code();
00111 }
00112 /*----------------------------------------------------------------------------*/
00119 /*----------------------------------------------------------------------------*/
00120 
00121 int midi_check_sof(cpl_frameset * frameset, const char * tag)
00122 {
00123    char                  * tag_file=NULL;
00124    cpl_frame             * cur_frame=NULL;
00125    int                     NumberOfFiles=0;
00126    
00127    cur_frame = cpl_frameset_get_first(frameset);
00128    if (cur_frame == NULL) {
00129       return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00130                                         "SOF does not have any file");
00131    }
00132    
00133 /*    Walk through the whole Set of Frames SOF */
00134    
00135    NumberOfFiles=0;   
00136    
00137    while(cur_frame)
00138    {
00139       tag_file = (char*)cpl_frame_get_tag(cur_frame);
00140       
00141       if (strcmp(tag_file, tag)) {
00142          cur_frame = cpl_frameset_get_next( frameset );   
00143          continue;
00144       }
00145       if (strcmp(tag_file, tag)==0) {
00146          NumberOfFiles+=1;
00147       }
00148       cur_frame = cpl_frameset_get_next( frameset );  
00149    }
00150    
00151    return NumberOfFiles;
00152    
00153 }
00154 /*----------------------------------------------------------------------------*/
00202 /*----------------------------------------------------------------------------*/
00203 
00204 cpl_polynomial * midi_polynomial_fit_1d_create(
00205         const cpl_vector    *   x_pos,
00206         const cpl_vector    *   values,
00207         int                     degree,
00208         double              *   mse)
00209 {
00210     cpl_polynomial * fit1d;
00211     cpl_matrix     * samppos;
00212     cpl_vector     * fitresidual;
00213 
00214     cpl_size degree_tmp=0;
00215 
00216     fit1d = cpl_polynomial_new(1);
00217     samppos = cpl_matrix_wrap(1, cpl_vector_get_size(x_pos),
00218                 cpl_vector_get_data((cpl_vector *)x_pos));
00219     fitresidual = cpl_vector_new(cpl_vector_get_size(x_pos));
00220     cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
00221             CPL_FALSE, NULL, &degree_tmp);
00222 
00223     degree=(int)(degree_tmp);
00224 
00225     cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
00226                                         samppos, NULL);
00227     cpl_matrix_unwrap(samppos);
00228 
00229     if(mse!=NULL){
00230         *mse = cpl_vector_product(fitresidual, fitresidual)
00231                     /(double)(cpl_vector_get_size(fitresidual));
00232     }
00233     cpl_vector_delete(fitresidual);
00234     return fit1d;
00235 }

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