midiAppendPropertylist.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  midiAppendPropertylist.c
00007 * Description:  Contains a routine to add a property list
00008 *
00009 * History:
00010 * 04-June-07     (agabasch) Created
00011 *******************************************************************************
00012 ******************************************************************************/
00013 
00014 /******************************************************************************
00015 *   Include files
00016 ******************************************************************************/
00017 #include <stdio.h>
00018 #include <cpl.h>
00019 #include "midiAppendPropertylist.h"
00020 #include <config.h>
00021 #include <string.h>
00022 
00023 cpl_error_code appendPropertylist(const char *ProductFile, 
00024                                   cpl_frame_type type, 
00025                                   const char     *tag,
00026                                   cpl_frameset *frameset,
00027                                   cpl_parameterlist  *parlist)
00028 {
00029    cpl_image  * pImage        = NULL;
00030    cpl_frame  * pFrameProduct = NULL;
00031    cpl_propertylist  *   pHeader = NULL;
00032    
00033    
00034    const char *szProduct=ProductFile;
00035    const char *szRawFile=ProductFile;
00036    
00037    
00038    pImage = cpl_image_load(szRawFile, CPL_TYPE_FLOAT, 0, 0 );
00039    
00040    
00041 /*    pHeader = cpl_propertylist_new(); */
00042    pHeader = cpl_propertylist_load(szRawFile, 0 );
00043    
00044    
00045    pFrameProduct = cpl_frame_new();
00046    cpl_frame_set_filename( pFrameProduct, szProduct );
00047    cpl_frame_set_type    ( pFrameProduct, type );
00048    cpl_frame_set_tag     ( pFrameProduct, tag );
00049    cpl_frame_set_group   ( pFrameProduct, CPL_FRAME_GROUP_PRODUCT );
00050    cpl_frame_set_level   ( pFrameProduct, CPL_FRAME_LEVEL_FINAL );
00051    
00052    
00053 //   cpl_frame_set_group(cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
00054    
00055    
00056 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)   
00057    if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID, NULL))
00058    {
00059       cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
00060    }
00061 #else
00062    if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID))
00063    {
00064       cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
00065    }
00066 #endif
00067 
00068    
00069    /*   if (CPL_ERROR_NONE != irplib_image_save(frameset, */
00070 /*                                            parlist, */
00071 /*                                            frameset, */
00072 /*                                            pImage, */
00073 /*                                            CPL_BPP_IEEE_FLOAT, */
00074 /*                                            "Test", */
00075 /*                                            tag, */
00076 /*                                            pHeader, */
00077 /*                                            NULL, */
00078 /*                                            "Test", */
00079 /*                                            szProduct)) */
00080 /*    { */
00081 /*       cpl_msg_error(cpl_func,"Error in irplib_image_save: %s %s",tag,cpl_error_get_message()); */
00082 /*    } */
00083    
00084    
00085    
00086    
00087    if (CPL_ERROR_NONE != cpl_image_save(pImage, szProduct, CPL_BPP_IEEE_FLOAT, pHeader, CPL_IO_DEFAULT ))
00088    {
00089       cpl_msg_error(cpl_func,"Error in cpl_image_save");
00090    }
00091    
00092    if (CPL_ERROR_NONE != cpl_frameset_insert( frameset, pFrameProduct ))
00093    {
00094       cpl_msg_error(cpl_func,"Error in cpl_frameset_insert");
00095    }
00096    
00097    cpl_image_delete( pImage );
00098    cpl_propertylist_delete(pHeader ); 
00099    
00100    return 0;
00101 }
00102 
00103 cpl_error_code appendPropertylist_table(const char   *ProductFile, 
00104                                         cpl_frame_type     type, 
00105                                         const char         *tag,
00106                                         cpl_frameset       *frameset,
00107                                         cpl_parameterlist  *parlist)
00108 {
00109    
00110    int extnum,i;
00111    
00112 /*    cpl_table         * pTable         = NULL; */
00113    cpl_table        ** pTable_ext  = NULL;
00114    
00115    cpl_propertylist  * pHeader        = NULL;
00116    cpl_propertylist ** pHeader_ext = NULL;
00117    
00118    cpl_frame         * pFrameProduct  = NULL;
00119    cpl_frame         * pFrame_tmp  = NULL;
00120    
00121    
00122    const char *szProduct=ProductFile;
00123    const char *szRawFile=ProductFile;
00124    char * current_extension;
00125 
00126    pFrameProduct = cpl_frame_new();
00127    cpl_frame_set_filename( pFrameProduct, szProduct );
00128    cpl_frame_set_type    ( pFrameProduct, type );
00129    cpl_frame_set_tag     ( pFrameProduct, tag );
00130    cpl_frame_set_group   ( pFrameProduct, CPL_FRAME_GROUP_PRODUCT );
00131    cpl_frame_set_level   ( pFrameProduct, CPL_FRAME_LEVEL_FINAL );
00132    
00133 //   cpl_frame_set_group(cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
00134 
00135 
00136 /* read the number of extensions from the rawfile */
00137    pFrame_tmp = cpl_frame_new();
00138    cpl_frame_set_filename( pFrame_tmp, szRawFile );
00139    extnum=cpl_frame_get_nextensions(pFrame_tmp);
00140    cpl_frame_delete(pFrame_tmp);
00141    
00142    pHeader = cpl_propertylist_load(szRawFile, 0 );
00143 
00144 
00145    if (pHeader ==NULL)
00146    {
00147       cpl_msg_error(cpl_func,"Error loading the propertylist of %s",szRawFile); 
00148       return -1;
00149    }
00150    
00151    pTable_ext  = cpl_malloc((extnum) * sizeof(cpl_table *));
00152    pHeader_ext = cpl_malloc((extnum) * sizeof(cpl_propertylist *));
00153 
00154 
00155 
00156    for (i=0; i<extnum; i++)
00157    {
00158       pHeader_ext[i] = cpl_propertylist_load(szRawFile, i+1 );
00159       pTable_ext[i]  = cpl_table_load(szRawFile, i+1, 1 );
00160       
00161       if (pHeader_ext[i] ==NULL || pTable_ext[i]==NULL)
00162       {
00163          cpl_msg_error(cpl_func,"Error loading the extended header or table of %s",szRawFile); 
00164          
00165          for (i=0; i<extnum; i++)
00166          {
00167             cpl_propertylist_delete(pHeader_ext[i]);
00168             cpl_table_delete(pTable_ext[i]); 
00169          }
00170          cpl_free (pHeader_ext);          
00171          cpl_free (pTable_ext);          
00172          
00173          
00174          return -1;
00175       }
00176 
00177 /* ------------------------------------------------------------------- */
00178 /*       OI_FITS standard requires to saves the FLAG column as boolean */
00179       current_extension=(char *)cpl_propertylist_get_string(pHeader_ext[i],"EXTNAME");
00180       
00181       if (strcmp(current_extension,"OI_VIS")==0 || strcmp(current_extension,"OI_VIS2")==0){
00182          
00183          if (cpl_table_has_column(pTable_ext[i],"FLAG")){
00184             cpl_table_set_column_savetype(pTable_ext[i], "FLAG", CPL_TYPE_BOOL);
00185          }
00186       }
00187       
00188 /* ------------------------------------------------------------------- */
00189       
00190    }
00191    
00192 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)   
00193    if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID, NULL))
00194    {
00195       cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
00196    }
00197 #else
00198    if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID))
00199    {
00200       cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
00201    }
00202 #endif
00203 
00204 
00205    if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0],pHeader, pHeader_ext[0], szProduct, CPL_IO_DEFAULT ))
00206    {
00207       cpl_msg_error(cpl_func,"Error when saving primary header: %s %s",cpl_error_get_message(),cpl_error_get_where());
00208    }
00209 
00210    for (i=1; i<extnum; i++)
00211    {
00212       if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[i], NULL, pHeader_ext[i], szProduct, CPL_IO_EXTEND ))
00213       {
00214          cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
00215       }
00216 
00217    }
00218 
00219 
00220 
00221    if (CPL_ERROR_NONE != cpl_frameset_insert( frameset, pFrameProduct ))
00222    {
00223       cpl_msg_error(cpl_func,"Error in cpl_frameset_insert");
00224    }
00225    
00226 
00227     cpl_propertylist_delete(pHeader);
00228 
00229    for (i=0; i<extnum; i++)
00230    {
00231       cpl_propertylist_delete(pHeader_ext[i]);
00232       cpl_table_delete (pTable_ext[i]); 
00233    }
00234    cpl_free (pTable_ext); 
00235    cpl_free (pHeader_ext); 
00236    
00237 
00238    return 0;  
00239 
00240 }
00241 
00242 
00243 cpl_error_code appendPropertylist_statistics(const char         *InputFile, 
00244                                              const char         *ProductFile, 
00245                                              cpl_frame_type     type, 
00246                                              const char         *tag,
00247                                              cpl_frameset       *frameset,
00248                                              cpl_parameterlist  *parlist,
00249                                              struct plots       *plotfiles_lambda,
00250                                              struct plots       *plotfiles_frames,
00251                                              struct plots       *plotfiles_scans,
00252                                              int                 dimen_lambda,
00253                                              int                 dimen_frames,
00254                                              int                 dimen_scans)
00255 {
00256    
00257    int extnum,i;
00258    
00259 /*    cpl_table         * pTable             = NULL; */
00260    cpl_table        ** pTable_ext         = NULL;
00261    
00262    cpl_propertylist  * pHeader            = NULL;
00263    cpl_propertylist ** pHeader_ext        = NULL;
00264    cpl_propertylist ** pHeader_ext_append = NULL;
00265    
00266    cpl_frame         * pFrameProduct      = NULL;
00267    cpl_frame         * pFrame_tmp         = NULL;
00268    
00269    
00270    const char *szProduct=ProductFile;
00271    const char *szRawFile=InputFile;
00272    char * current_extension;
00273    
00274 
00275 
00276 /*    cpl_msg_info(cpl_func,"Entries: %s ",plotfiles_lambda[0].filename); */
00277 /*    cpl_msg_info(cpl_func,"dimen_lambda: %d ",dimen_lambda); */
00278    
00279 
00280    cpl_vector *vec_lambda;
00281    cpl_vector *vec_frames;
00282    cpl_vector *vec_scans;
00283    
00284 /*    cpl_vector  *vec; */
00285 
00286    cpl_table   *table_lambda; 
00287    cpl_table   *table_frames; 
00288    cpl_table   *table_scans; 
00289    
00290 
00291 /* Determine the size of the Table */
00292 
00293    vec_lambda= vector_read(plotfiles_lambda[0].filename) ;
00294    table_lambda=cpl_table_new(cpl_vector_get_size(vec_lambda));
00295    cpl_vector_delete(vec_lambda);
00296 
00297    vec_frames= vector_read(plotfiles_frames[0].filename) ;
00298    table_frames=cpl_table_new(cpl_vector_get_size(vec_frames));
00299    cpl_vector_delete(vec_frames);
00300 
00301    vec_scans= vector_read(plotfiles_scans[0].filename) ;
00302    table_scans=cpl_table_new(cpl_vector_get_size(vec_scans));
00303    cpl_vector_delete(vec_scans);
00304 
00305 
00306 /* Fill the Table */
00307    for(i=0; i<dimen_lambda; i++)   
00308    {
00309       vec_lambda= vector_read(plotfiles_lambda[i].filename) ;
00310       cpl_table_wrap_double(table_lambda, cpl_vector_get_data(vec_lambda), plotfiles_lambda[i].label) ;
00311       cpl_vector_unwrap(vec_lambda);
00312    }
00313 
00314    for(i=0; i<dimen_frames; i++)   
00315    {
00316       vec_frames= vector_read(plotfiles_frames[i].filename) ;
00317       cpl_table_wrap_double(table_frames, cpl_vector_get_data(vec_frames), plotfiles_frames[i].label) ;
00318       cpl_vector_unwrap(vec_frames);
00319    }
00320 
00321    for(i=0; i<dimen_scans; i++)   
00322    {
00323       vec_scans= vector_read(plotfiles_scans[i].filename) ;
00324       cpl_table_wrap_double(table_scans, cpl_vector_get_data(vec_scans), plotfiles_scans[i].label) ;
00325       cpl_vector_unwrap(vec_scans);
00326    }
00327 
00328 
00329 
00330 /* read the number of extensions from the rawfile */
00331    pFrame_tmp = cpl_frame_new();
00332    cpl_frame_set_filename( pFrame_tmp, szRawFile );
00333    extnum=cpl_frame_get_nextensions(pFrame_tmp);
00334    cpl_frame_delete(pFrame_tmp);
00335 
00336 
00337 
00338    pFrameProduct = cpl_frame_new();
00339    cpl_frame_set_filename( pFrameProduct, szProduct );
00340    cpl_frame_set_type    ( pFrameProduct, type );
00341    cpl_frame_set_tag     ( pFrameProduct, tag );
00342    cpl_frame_set_group   ( pFrameProduct, CPL_FRAME_GROUP_PRODUCT );
00343    cpl_frame_set_level   ( pFrameProduct, CPL_FRAME_LEVEL_FINAL );
00344    
00345 //   cpl_frame_set_group(cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
00346 
00347 
00348 /*   cpl_msg_error(cpl_func,"extnum %d",extnum);     */
00349 
00350 
00351    pHeader = cpl_propertylist_load(szRawFile, 0 );
00352    if (pHeader ==NULL)
00353    {
00354       cpl_msg_error(cpl_func,"Error loading the propertylist of %s",szRawFile); 
00355       return -1;
00356    }
00357    
00358    pTable_ext  = cpl_malloc((extnum) * sizeof(cpl_table *));
00359    pHeader_ext = cpl_malloc((extnum) * sizeof(cpl_propertylist *));
00360 
00361 
00362 
00363    for (i=0; i<extnum; i++)
00364    {
00365       pHeader_ext[i] = cpl_propertylist_load(szRawFile, i+1 );
00366       pTable_ext[i]  = cpl_table_load(szRawFile, i+1, 1 );
00367       
00368       if (pHeader_ext[i] ==NULL || pTable_ext[i]==NULL)
00369       {
00370          cpl_msg_error(cpl_func,"Error loading the extended header or table of %s",szRawFile); 
00371          
00372          for (i=0; i<extnum; i++)
00373          {
00374             cpl_propertylist_delete(pHeader_ext[i]);
00375             cpl_table_delete(pTable_ext[i]); 
00376          }
00377             cpl_free (pHeader_ext);
00378             cpl_free (pTable_ext); 
00379          
00380          
00381          
00382          return -1;
00383       }
00384       
00385 /* ------------------------------------------------------------------- */
00386 /*       OI_FITS standard requires to saves the FLAG column as boolean */
00387 
00388       current_extension=(char *)cpl_propertylist_get_string(pHeader_ext[i],"EXTNAME");
00389       
00390       if (strcmp(current_extension,"OI_VIS")==0 || strcmp(current_extension,"OI_VIS2")==0){
00391          
00392          if (cpl_table_has_column(pTable_ext[i],"FLAG")){
00393             cpl_table_set_column_savetype(pTable_ext[i], "FLAG", CPL_TYPE_BOOL);
00394          }
00395       }
00396       
00397 /* ------------------------------------------------------------------- */
00398 
00399    }
00400    
00401 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)   
00402    if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID, NULL))
00403    {
00404       cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
00405    }
00406 #else
00407    if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID))
00408    {
00409       cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
00410    }
00411 #endif
00412 
00413 
00414    if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0],pHeader, pHeader_ext[0], szProduct, CPL_IO_DEFAULT ))
00415    {
00416       cpl_msg_error(cpl_func,"Error when saving primary header: %s %s",cpl_error_get_message(),cpl_error_get_where());
00417    }
00418 
00419 
00420    for (i=1; i<extnum; i++)
00421    {
00422       if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[i], NULL, pHeader_ext[i], szProduct, CPL_IO_EXTEND ))
00423       {
00424          cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
00425       }
00426 
00427    }
00428 
00429 
00430 
00431 /* Create and append 3 new extensions */
00432    
00433    pHeader_ext_append = cpl_malloc(3 * sizeof(cpl_propertylist *));
00434    
00435    pHeader_ext_append[0]=cpl_propertylist_new();
00436    pHeader_ext_append[1]=cpl_propertylist_new();
00437    pHeader_ext_append[2]=cpl_propertylist_new();
00438    
00439 
00440    cpl_propertylist_append_string (pHeader_ext_append[0],"EXTNAME","STATISTICS1");
00441    cpl_propertylist_append_string (pHeader_ext_append[1],"EXTNAME","STATISTICS2");
00442    cpl_propertylist_append_string (pHeader_ext_append[2],"EXTNAME","STATISTICS3");
00443    
00444    if (CPL_ERROR_NONE != cpl_table_save(table_lambda, NULL, pHeader_ext_append[0], szProduct, CPL_IO_EXTEND ))
00445    {
00446       cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
00447    }
00448    
00449    
00450    if (CPL_ERROR_NONE != cpl_table_save(table_frames, NULL, pHeader_ext_append[1], szProduct, CPL_IO_EXTEND ))
00451    {
00452       cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
00453    }
00454    
00455    if (CPL_ERROR_NONE != cpl_table_save(table_scans, NULL, pHeader_ext_append[2], szProduct, CPL_IO_EXTEND ))
00456    {
00457       cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
00458    }
00459    
00460    
00461    
00462    if (CPL_ERROR_NONE != cpl_frameset_insert( frameset, pFrameProduct ))
00463    {
00464       cpl_msg_error(cpl_func,"Error in cpl_frameset_insert");
00465    }
00466    
00467    
00468    /* Free the memory */
00469    
00470    cpl_table_delete(table_lambda);
00471    cpl_table_delete(table_frames);
00472    cpl_table_delete(table_scans);
00473    
00474    
00475    
00476    
00477    
00478    
00479    cpl_propertylist_delete(pHeader);
00480    
00481    for (i=0; i<extnum; i++)
00482    {
00483       cpl_propertylist_delete(pHeader_ext[i]);
00484       cpl_table_delete(pTable_ext[i]); 
00485    }
00486    cpl_free (pHeader_ext);
00487    cpl_free (pTable_ext); 
00488    
00489    
00490    cpl_propertylist_delete (pHeader_ext_append[0]);
00491    cpl_propertylist_delete (pHeader_ext_append[1]);
00492    cpl_propertylist_delete (pHeader_ext_append[2]);
00493    cpl_free(pHeader_ext_append);    
00494    
00495    
00496    
00497    return 0;  
00498    
00499 }
00500 
00501 
00502 /* cpl_vector_read modified to work wit one column: "%*d %lg" -> "%lg" */
00503 
00504 /*----------------------------------------------------------------------------*/
00524 /*----------------------------------------------------------------------------*/
00525 cpl_vector * vector_read(const char * filename)
00526 {
00527     FILE       * in;
00528     cpl_vector * v;
00529     int          np = 0;
00530     int          size = 1000; /* Default size */
00531     char         line[1024];
00532     double       x;
00533 
00534     cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
00535 
00536     /* Open the file */
00537     in = fopen(filename, "r");
00538     cpl_ensure(in != NULL, CPL_ERROR_FILE_IO, NULL);
00539 
00540     /* Create and fill the vector */
00541     v = cpl_vector_new(size);
00542 
00543     while (fgets(line, 1024, in) != NULL) {
00544         if (line[0] != '#' && sscanf(line, "%lg", &x) == 1) {
00545             /* Found new element
00546                - increase vector size if necessary,
00547                - insert element at end and
00548                - increment size counter */
00549             if (np == size) cpl_vector_set_size(v, size *= 2);
00550             cpl_vector_set(v, np++, x);
00551         }
00552     }
00553 
00554     /* Check that the loop ended due to eof and not an error */
00555     if (ferror(in)) {
00556         fclose(in);
00557         cpl_vector_delete(v);
00558         cpl_ensure(0, CPL_ERROR_FILE_IO, NULL);
00559     }
00560 
00561     fclose(in);
00562 
00563     /* Check that the file was not empty and set the size to its true value */
00564     if (np == 0 || cpl_vector_set_size(v, np)) {
00565         cpl_vector_delete(v);
00566         cpl_ensure(0, CPL_ERROR_BAD_FILE_FORMAT, NULL);
00567     }
00568 
00569     return v;
00570 }

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