# MED21datasetNumLire.c

Aller à la documentation de ce fichier.

    00001 /*  This file is part of MED.
    00002  *
    00003  *  COPYRIGHT (C) 1999 - 2011  EDF R&D, CEA/DEN
    00004  *  MED is free software: you can redistribute it and/or modify
    00005  *  it under the terms of the GNU Lesser General Public License as published by
    00006  *  the Free Software Foundation, either version 3 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  MED is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Lesser General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU Lesser General Public License
    00015  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
    00016  */
    00017
    00018
    00019 #ifndef MESGERR
    00020 #define MESGERR 1
    00021 #endif
    00022
    00023 #include <med.h>
    00024 #include <med_config.h>
    00025 #include <med_outils.h>
    00026 #include <hdf5.h>
    00027 #include <string.h>
    00028
    00029 med_err _MED21datasetNumLire(med_idt pere,char *nom,med_type_champ type,
    00030                            med_mode_switch interlace, med_size nbdim, med_size fixdim,
    00031                            med_size psize, med_ssize * pfltab, med_int ngauss,
    00032                            unsigned char *val,hid_t hdf_file)
    00033 {
    00034   med_idt    dataset, dataspace = 0, memspace = 0;
    00035   med_size  start_mem[1],start_data[1],*pflmem=0,*pfldsk=0;
    00036   med_size   stride[1],count[1],pcount[1],size[1],pflsize[1];
    00037   med_err    ret;
    00038   int        i,j,index,type_hdf;
    00039   hid_t      datatype;
    00040   size_t     typesize;
    00041   int        dim, firstdim, dimutil, lastdim;
    00042   med_mode_profil pflmod;
    00043
    00044   /* Verify fixdim is between [0, nbdim] ( 0 is MED_ALL ) */
    00045   if (  fixdim > nbdim )
    00046     return -1;
    00047
    00048   /* block pflmod to MED_COMPACT (until med2.2) */
    00049   pflmod = MED_COMPACT;
    00050
    00051   switch(type)
    00052     {
    00053     case MED_FLOAT64 :
    00054       if (H5Tequal(hdf_file,H5T_IEEE_F64BE))
    00055         type_hdf = H5T_IEEE_F64LE;
    00056       if (H5Tequal(hdf_file,H5T_IEEE_F64LE))
    00057         type_hdf = H5T_IEEE_F64BE;
    00058       break;
    00059
    00060     case MED_INT32 :
    00061       type_hdf = H5T_NATIVE_INT;
    00062       break;
    00063
    00064     case MED_INT64 :
    00065       type_hdf = H5T_NATIVE_LONG;
    00066       break;
    00067
    00068     default :
    00069       return -1;
    00070     }
    00071
    00072   /* Ouverture du Dataset à lire */
    00073   if ((dataset = H5Dopen(pere,nom)) < 0)
    00074     return -1;
    00075
    00076   /* Interrogation de la taille du dataset */
    00077   if ( (datatype  = H5Dget_type(dataset )) < 0)  return -1;
    00078   if ( (typesize  = H5Tget_size(datatype)) == 0) return -1;
    00079   size[0] = H5Dget_storage_size(dataset) / typesize;
    00080   if ( H5Tclose(datatype) < 0) return -1;
    00081
    00082   /* Create dataspace */
    00083   if ((dataspace = H5Screate_simple(1,size,NULL)) < 0)
    00084     return -1;
    00085
    00086   switch(interlace)
    00087     {
    00088     case MED_FULL_INTERLACE :
    00089
    00090       /*Initialisation des indices de boucle du traitement de l'entrelacement en fonction de la dimension fixee*/
    00091       if ( fixdim != MED_ALL)
    00092         {
    00093           firstdim = fixdim-1;
    00094           lastdim  = fixdim;
    00095           dimutil  = 1;
    00096         } else  {
    00097           firstdim = 0;
    00098           lastdim = nbdim;
    00099           dimutil  = nbdim;
    00100         }
    00101
    00102       count [0] = (*size)/(nbdim);
    00103
    00104
    00105       /*rem: Pas de vérification de l'assertion (*size)=n*nbdim */
    00106       if ( psize == MED_NOPF ) {
    00107
    00108       /* Creation d'un data space mémoire de dimension 1, de longeur size, et de longeur maxi size */
    00109       if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
    00110         return -1;
    00111
    00112         stride[0] = nbdim;
    00113
    00114         for (dim=firstdim; dim < lastdim; dim++) {
    00115
    00116           start_mem[0] = dim;
    00117           if ( (ret = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, start_mem, stride,
    00118                                            count, NULL)) <0)
    00119             return -1;
    00120
    00121           start_data[0] = dim*count[0];
    00122           if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL,
    00123                                            count, NULL)) <0)
    00124             return -1;
    00125
    00126           if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,
    00127                              H5P_DEFAULT, val)) < 0)
    00128             return -1;
    00129         }
    00130
    00131       } else {
    00132
    00133         pflsize [0] = psize*ngauss*nbdim;
    00134         pcount  [0] = psize*ngauss*dimutil;
    00135         pflmem     = (med_size *) malloc (sizeof(med_size)*pcount[0]);
    00136         pfldsk     = (med_size *) malloc (sizeof(med_size)*pcount[0]);
    00137
    00138         switch(pflmod)
    00139           { /* switch pflmod pour FULL_INTERLACE*/
    00140           case MED_GLOBAL :
    00141
    00142             /* Creation d'un data space mémoire de dimension 1, de longeur size, et de longeur maxi size */
    00143             if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
    00144               return -1;
    00145
    00146             for (dim=firstdim; dim < lastdim; dim++) {
    00147
    00148               for (i=0; i < psize; i++)              /* i balaye les élements du profil */
    00149                 for (j=0; j < ngauss; j++) {
    00150                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
    00151                   pflmem[index] = (pfltab[i]-1)*ngauss*nbdim + j*nbdim+dim;
    00152                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;
    00153                 }
    00154             }
    00155
    00156             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pflmem ) ) <0)
    00157               return -1;
    00158
    00159             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pfldsk ) ) <0)
    00160               return -1;
    00161
    00162             break;
    00163
    00164           case MED_COMPACT :
    00165
    00166             /* Creation d'un data space mémoire de dimension 1, de la longeur du profil          */
    00167             /* La dimension utilisée est ici nbdim, même pour un profil compact on suppose       */
    00168             /*  que l'utilisateur a toutes les coordonées stockées, même si il en demande qu'une */
    00169
    00170             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
    00171               return -1;
    00172
    00173             for (dim=firstdim; dim < lastdim; dim++) {
    00174
    00175               for (i=0; i < psize; i++)              /* i balaye les élements du profil */
    00176                 for (j=0; j < ngauss; j++) {
    00177                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
    00178                   pflmem[index] = i*ngauss*nbdim + j*nbdim+dim;
    00179                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;
    00180                 }
    00181             }
    00182
    00183             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pflmem ) ) <0)
    00184               return -1;
    00185
    00186             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pfldsk ) ) <0)
    00187               return -1;
    00188
    00189             break;
    00190
    00191           default :
    00192             return -1;
    00193           }
    00194
    00195         if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
    00196           return -1;
    00197
    00198         free(pflmem);
    00199         free(pfldsk);
    00200       }
    00201
    00202       break;
    00203
    00204     case MED_NO_INTERLACE :
    00205
    00206       /*Initialisation des indices de boucle du traitement de l'entrelacement en fonction de la dimension fixee*/
    00207
    00208       count[0] = (*size)/nbdim;
    00209
    00210       if ( psize == MED_NOPF ) {
    00211
    00212         if ( fixdim != MED_ALL)
    00213           start_data[0] = (fixdim-1)*count[0];
    00214         else {
    00215           count[0] = *size;
    00216           start_data[0] =  0;
    00217         };
    00218
    00219         if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL,
    00220                                          count, NULL)) <0)
    00221           return -1;
    00222
    00223         if ((ret = H5Dread(dataset,type_hdf,dataspace,dataspace,
    00224                            H5P_DEFAULT, val)) < 0)
    00225           return -1;
    00226
    00227       } else {
    00228
    00229         if ( fixdim != MED_ALL)
    00230           {
    00231             firstdim = fixdim-1;
    00232             lastdim  = fixdim;
    00233             dimutil  = 1;
    00234           } else        {
    00235             firstdim = 0;
    00236             lastdim  = nbdim;
    00237             dimutil  = nbdim;
    00238           }
    00239
    00240         pflsize [0] = psize*ngauss*nbdim;
    00241         pcount  [0] = psize*ngauss*dimutil; /* nom pas très coherent avec count !!! A revoir */
    00242         pfldsk      = (med_size *) malloc(sizeof(med_size)*pcount[0]);
    00243
    00244         switch(pflmod)
    00245           { /*switch plfmod pour NO_INTERLACE */
    00246           case MED_GLOBAL :
    00247
    00248             for (dim=firstdim; dim < lastdim; dim++) {
    00249
    00250               for (i=0; i < psize; i++)              /* i balaye le nbre d'élements du profil                */
    00251                 for (j=0; j < ngauss; j++) {
    00252                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
    00253                   pfldsk[index] = dim*count[0]+(pfltab[i]-1)*ngauss+j;
    00254                 }
    00255             }
    00256
    00257             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,pcount[0], HDF5_SELECT_BUG pfldsk ) ) <0)
    00258               return -1;
    00259
    00260             if ((ret = H5Dread(dataset,type_hdf,dataspace,dataspace,H5P_DEFAULT, val)) < 0)
    00261               return -1;
    00262
    00263             break;
    00264
    00265           case MED_COMPACT :
    00266
    00267             /* Creation d'un data space mémoire de dimension 1, de la longeur du profil          */
    00268             /* La dimension utilisée est ici nbdim, même pour un profil compact on suppose       */
    00269             /*  que l'utilisateur a toutes les coordonées stockées, même si il en demande qu'une */
    00270
    00271             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
    00272               return -1;
    00273
    00274             pflmem     = (med_size *) malloc (sizeof(med_size)*pcount[0]);
    00275
    00276             /* Le profil COMPACT est contigüe, mais il est possible que l'on selectionne uniquemenent une dimension*/
    00277
    00278             for (dim=firstdim; dim < lastdim; dim++) {
    00279
    00280               for (i=0; i < psize; i++)              /* i balaye le nbre d'élements du profil                */
    00281                 for (j=0; j < ngauss; j++) {
    00282                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
    00283                   pflmem[index] = dim*(psize*ngauss) + (pfltab[i]-1)*ngauss+j;
    00284                   pfldsk[index] = dim*count[0]  + (pfltab[i]-1)*ngauss+j;
    00285                 }
    00286             }
    00287
    00288             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, pcount[0], HDF5_SELECT_BUG pflmem ) ) <0)
    00289               return -1;
    00290
    00291             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,pcount[0], HDF5_SELECT_BUG pfldsk ) ) <0)
    00292               return -1;
    00293
    00294             if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
    00295               return -1;
    00296
    00297             break;
    00298
    00299           default :
    00300             return -1;
    00301
    00302           }
    00303
    00304         free(pfldsk);
    00305
    00306       };
    00307
    00308       break;
    00309
    00310     default :
    00311       return -1;
    00312     }
    00313
    00314
    00315
    00316   if (memspace)
    00317     if ((ret = H5Sclose(memspace)) < 0)
    00318       return -1;
    00319
    00320   if ((ret = H5Sclose(dataspace)) < 0)
    00321     return -1;
    00322
    00323   if ((ret = H5Dclose(dataset)) < 0)
    00324     return -1;
    00325
    00326   return 0;
    00327 }