00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 #include "med_config.h"
00020 #include <med.h>
00021 #include <med_outils.h>
00022 #include <hdf5.h>
00023 #include <string.h>
00024 
00025 med_err _MED21datasetNumLire(med_idt pere,char *nom,med_type_champ type,
00026                            med_mode_switch interlace, med_size nbdim, med_size fixdim,
00027                            med_size psize, med_ssize * pfltab, med_int ngauss,
00028                            unsigned char *val,hid_t hdf_file)
00029 {
00030   med_idt    dataset, dataspace = 0, memspace = 0;
00031   med_size  start_mem[1],start_data[1],*pflmem=0,*pfldsk=0;
00032   med_size   stride[1],count[1],pcount[1],size[1],pflsize[1];
00033   med_err    ret;
00034   int        i,j,index,type_hdf;
00035   hid_t      datatype;
00036   size_t     typesize;
00037   int        dim, firstdim, dimutil, lastdim;
00038   med_mode_profil pflmod;
00039 
00040   
00041   if (  fixdim > nbdim ) 
00042     return -1;
00043  
00044   
00045   pflmod = MED_COMPACT;
00046 
00047   switch(type)
00048     {
00049     case MED_FLOAT64 :
00050       if (H5Tequal(hdf_file,H5T_IEEE_F64BE))
00051         type_hdf = H5T_IEEE_F64LE;
00052       if (H5Tequal(hdf_file,H5T_IEEE_F64LE))
00053         type_hdf = H5T_IEEE_F64BE;
00054       break;
00055 
00056     case MED_INT32 :
00057       type_hdf = H5T_NATIVE_INT;
00058       break;
00059 
00060     case MED_INT64 :
00061       type_hdf = H5T_NATIVE_LONG;
00062       break;
00063 
00064     default :
00065       return -1;
00066     }
00067 
00068   
00069   if ((dataset = H5Dopen(pere,nom)) < 0)
00070     return -1;
00071 
00072   
00073   if ( (datatype  = H5Dget_type(dataset )) < 0)  return -1;
00074   if ( (typesize  = H5Tget_size(datatype)) == 0) return -1;
00075   size[0] = H5Dget_storage_size(dataset) / typesize; 
00076   if ( H5Tclose(datatype) < 0) return -1;
00077 
00078   
00079   if ((dataspace = H5Screate_simple(1,size,NULL)) < 0)
00080     return -1;
00081   
00082   switch(interlace)
00083     {
00084     case MED_FULL_INTERLACE :
00085 
00086       
00087       if ( fixdim != MED_ALL) 
00088         { 
00089           firstdim = fixdim-1;
00090           lastdim  = fixdim;
00091           dimutil  = 1;
00092         } else  {
00093           firstdim = 0;
00094           lastdim = nbdim;
00095           dimutil  = nbdim; 
00096         }
00097 
00098       count [0] = (*size)/(nbdim);
00099       
00100 
00101       
00102       if ( psize == MED_NOPF ) {  
00103 
00104       
00105       if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
00106         return -1;
00107 
00108         stride[0] = nbdim;  
00109 
00110         for (dim=firstdim; dim < lastdim; dim++) {
00111                   
00112           start_mem[0] = dim;
00113           if ( (ret = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, start_mem, stride, 
00114                                            count, NULL)) <0)
00115             return -1; 
00116           
00117           start_data[0] = dim*count[0];
00118           if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL, 
00119                                            count, NULL)) <0)
00120             return -1; 
00121           
00122           if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,
00123                              H5P_DEFAULT, val)) < 0)
00124             return -1;
00125         }
00126         
00127       } else {
00128 
00129         pflsize [0] = psize*ngauss*nbdim;
00130         pcount  [0] = psize*ngauss*dimutil;
00131         pflmem     = (med_size *) malloc (sizeof(med_size)*pcount[0]);
00132         pfldsk     = (med_size *) malloc (sizeof(med_size)*pcount[0]);
00133         
00134         switch(pflmod)
00135           { 
00136           case MED_GLOBAL :
00137 
00138             
00139             if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
00140               return -1;
00141 
00142             for (dim=firstdim; dim < lastdim; dim++) {
00143               
00144               for (i=0; i < psize; i++)              
00145                 for (j=0; j < ngauss; j++) {         
00146                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
00147                   pflmem[index] = (pfltab[i]-1)*ngauss*nbdim + j*nbdim+dim;
00148                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;             
00149                 }
00150             }
00151             
00152             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pflmem ) ) <0) 
00153               return -1; 
00154             
00155             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pfldsk ) ) <0) 
00156               return -1; 
00157             
00158             break;
00159         
00160           case MED_COMPACT :
00161         
00162             
00163             
00164              
00165             
00166             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
00167               return -1;
00168             
00169             for (dim=firstdim; dim < lastdim; dim++) {
00170               
00171               for (i=0; i < psize; i++)              
00172                 for (j=0; j < ngauss; j++) {         
00173                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
00174                   pflmem[index] = i*ngauss*nbdim + j*nbdim+dim;
00175                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;             
00176                 }             
00177             }
00178             
00179             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pflmem ) ) <0) 
00180               return -1; 
00181             
00182             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pfldsk ) ) <0) 
00183               return -1; 
00184             
00185             break;
00186 
00187           default :
00188             return -1; 
00189           }
00190         
00191         if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
00192           return -1;
00193         
00194         free(pflmem);
00195         free(pfldsk);
00196       }
00197       
00198       break;
00199       
00200     case MED_NO_INTERLACE :
00201 
00202       
00203 
00204       count[0] = (*size)/nbdim;
00205       
00206       if ( psize == MED_NOPF ) {  
00207         
00208         if ( fixdim != MED_ALL) 
00209           start_data[0] = (fixdim-1)*count[0];
00210         else {
00211           count[0] = *size;
00212           start_data[0] =  0;
00213         };
00214         
00215         if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL, 
00216                                          count, NULL)) <0)
00217           return -1; 
00218         
00219         if ((ret = H5Dread(dataset,type_hdf,dataspace,dataspace,
00220                            H5P_DEFAULT, val)) < 0)
00221           return -1;
00222         
00223       } else {
00224 
00225         if ( fixdim != MED_ALL) 
00226           { 
00227             firstdim = fixdim-1;
00228             lastdim  = fixdim;
00229             dimutil  = 1;
00230           } else        {
00231             firstdim = 0;
00232             lastdim  = nbdim;
00233             dimutil  = nbdim; 
00234           }
00235 
00236         pflsize [0] = psize*ngauss*nbdim;       
00237         pcount  [0] = psize*ngauss*dimutil;         
00238         pfldsk      = (med_size *) malloc(sizeof(med_size)*pcount[0]);
00239         
00240         switch(pflmod)
00241           { 
00242           case MED_GLOBAL :
00243             
00244             for (dim=firstdim; dim < lastdim; dim++) {
00245               
00246               for (i=0; i < psize; i++)              
00247                 for (j=0; j < ngauss; j++) { 
00248                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
00249                   pfldsk[index] = dim*count[0]+(pfltab[i]-1)*ngauss+j;      
00250                 }
00251             }
00252             
00253             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,pcount[0], HDF5_SELECT_BUG pfldsk ) ) <0) 
00254               return -1;
00255             
00256             if ((ret = H5Dread(dataset,type_hdf,dataspace,dataspace,H5P_DEFAULT, val)) < 0)
00257               return -1;
00258               
00259             break;
00260             
00261           case MED_COMPACT :
00262             
00263             
00264             
00265              
00266 
00267             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
00268               return -1;
00269 
00270             pflmem     = (med_size *) malloc (sizeof(med_size)*pcount[0]);
00271             
00272             
00273 
00274             for (dim=firstdim; dim < lastdim; dim++) {
00275               
00276               for (i=0; i < psize; i++)              
00277                 for (j=0; j < ngauss; j++) {
00278                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
00279                   pflmem[index] = dim*(psize*ngauss) + (pfltab[i]-1)*ngauss+j;
00280                   pfldsk[index] = dim*count[0]  + (pfltab[i]-1)*ngauss+j;           
00281                 }
00282             }
00283             
00284             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pflmem ) ) <0) 
00285               return -1; 
00286             
00287             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,pcount[0], HDF5_SELECT_BUG pfldsk ) ) <0) 
00288               return -1;          
00289             
00290             if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
00291               return -1;
00292             
00293             break;
00294             
00295           default :
00296             return -1;      
00297             
00298           }
00299         
00300         free(pfldsk);
00301         
00302       };
00303       
00304       break;
00305       
00306     default :
00307       return -1;
00308     }
00309   
00310   
00311 
00312   if (memspace) 
00313     if ((ret = H5Sclose(memspace)) < 0)
00314       return -1;
00315 
00316   if ((ret = H5Sclose(dataspace)) < 0)
00317     return -1;
00318   
00319   if ((ret = H5Dclose(dataset)) < 0)
00320     return -1;      
00321 
00322   return 0;
00323 }