Back to index

salome-med  6.5.0
MEDdatasetNumLire.cxx
Go to the documentation of this file.
00001 /*************************************************************************
00002 * COPYRIGHT (C) 1999 - 2002  EDF R&D
00003 * THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
00004 * IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE 
00005 * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; 
00006 * EITHER VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
00007 *  
00008 * THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
00009 * WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
00010 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
00011 * LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
00012 *
00013 * YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE
00014 * ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE SOFTWARE FOUNDATION,
00015 * INC., 59 TEMPLE PLACE, SUITE 330, BOSTON, MA 02111-1307 USA
00016 *
00017 *************************************************************************/
00018 
00019 #include "med.hxx"
00020 #include "med_outils.hxx"
00021 #include <stdlib.h>
00022 #include "hdf5_version2api.hxx"
00023 
00024 /*
00025  * - Nom de la fonction : _MEDdatasetNumLire
00026  * - Description : lecture d'un dataset tableau numerique
00027  * - Parametres :
00028  *     - pere (IN)     : l'ID de l'objet HDF pere ou placer l'attribut
00029  *     - nom  (IN)     : le nom du dataset
00030  *     - type (IN)     : type numerique MED
00031  *     - interlace (IN) : Choix du type d'entrelacement demandé par l'appelant { MED_FULL_INTERLACE(x1,y1,z1,x2,...)) , MED_NO_INTERLACE(x1,x2,y1,y2,z1,z2) }
00032  *       - nbdim   (IN) : Dimension des éléments
00033  *       - fixdim  (IN) : MED_ALL ou n° de la dimension a enregistrer à partir de 1..oo
00034  *     - psize     (IN) : Taille du profil à utiliser, MED_NOPF si pas de profil
00035  *       - pfltab  (IN) : Tableau contenant les n° déléments à traiter (1....oo)
00036  *       - pflmod  (IN) : PARAMETRE A AJOUTER : Indique comment lire les informations en mémoire { MED_COMPACT, MED_GLOBALE }. 
00037  *       - ngauss  (IN) : Nombre de points de GAUSS par élément
00038  *     - val  (OUT)    : valeurs du tableau
00039  * - Resultat : 0 en cas de succes, -1 sinon
00040  *  Equivalent à l'ancienne routine si .....,MED_NO_INTERLACE,1,MED_ALL,MED_NOPF,0,1 (peu importe),....
00041  */ 
00042 
00043 namespace med_2_1{
00044 
00045 med_err 
00046 _MEDdatasetNumLire(med_idt pere,char *nom,med_type_champ type,
00047                    med_mode_switch interlace, med_size nbdim, med_size fixdim, 
00048                    med_size psize, med_ssize * pfltab, med_int ngauss,
00049                    unsigned char *val)
00050 {
00051   med_idt    dataset, dataspace = 0, memspace = 0;
00052 #ifdef HDF_NEW_API
00053   med_size  start_mem[1],start_data[1],*pflmem=0,*pfldsk=0;
00054 #else
00055   med_ssize  start_mem[1],start_data[1],*pflmem=0,*pfldsk=0;
00056 #endif
00057   med_size   stride[1],count[1],pcount[1],size[1],pflsize[1];
00058   med_err    ret;
00059   int        i,j,index,type_hdf;
00060   hid_t      datatype;
00061   size_t     typesize;
00062   int        dim, firstdim, dimutil, lastdim;
00063   med_mode_profil pflmod;
00064 
00065   /* Verify fixdim is between [0, nbdim] ( 0 is MED_ALL ) */
00066   if ( ( fixdim < 0 ) || ( fixdim > nbdim ) ) 
00067     return -1;
00068  
00069   /* block pflmod to MED_COMPACT (until med2.2) */
00070   pflmod = MED_COMPACT;
00071 
00072   switch(type)
00073     {
00074     case MED_REEL64 :
00075       /* 1) IA32 is LE but due to an (?HDF convertion BUG?) when using H5T_NATIVE_DOUBLE/MED_REEL64? under PCLINUX
00076             the file read under SGI is incorrect
00077          2) Compaq OSF/1 is LE, since we force SGI64,SUN4SOL2,HP to write double in LE even if they are BE, mips OSF/1 must be BE
00078          REM  : Be careful of compatibility between MED files when changing this (med2.2)
00079          3) PPRO_NT is added for med2.1.6 support under Win32 */
00080 #if defined(PCLINUX) || defined(OSF1) || defined(PPRO_NT) || defined(PCLINUX64) || defined(PCLINUX64_32)
00081       type_hdf = H5T_IEEE_F64BE;
00082 #else 
00083       type_hdf = H5T_IEEE_F64LE;
00084 #endif
00085       break;
00086 
00087     case MED_INT32 :
00088       type_hdf = H5T_NATIVE_INT;
00089       break;
00090 
00091     case MED_INT64 :
00092       type_hdf = H5T_NATIVE_LONG;
00093       break;
00094 
00095     default :
00096       return -1;
00097     }
00098 
00099   /* Ouverture du Dataset à lire */
00100   if ((dataset = H5Dopen(pere,nom)) < 0)
00101     return -1;
00102 
00103   /* Interrogation de la taille du dataset */
00104   if ( (datatype  = H5Dget_type(dataset )) < 0) return -1;
00105   if ( (typesize  = H5Tget_size(datatype)) < 0) return -1;
00106   size[0] = H5Dget_storage_size(dataset) / typesize; 
00107   if ( H5Tclose(datatype) < 0) return -1;
00108 
00109   /* Create dataspace */
00110   if ((dataspace = H5Screate_simple(1,size,NULL)) < 0)
00111     return -1;
00112   
00113   switch(interlace)
00114     {
00115     case MED_FULL_INTERLACE :
00116 
00117       /*Initialisation des indices de boucle du traitement de l'entrelacement en fonction de la dimension fixee*/
00118       if ( fixdim != MED_ALL) 
00119         { 
00120           firstdim = (int)fixdim-1;
00121           lastdim  = (int)fixdim;
00122           dimutil  = 1;
00123         } else  {
00124           firstdim = 0;
00125           lastdim = (int)nbdim;
00126           dimutil  = (int)nbdim; 
00127         }
00128 
00129       count [0] = (*size)/(nbdim);
00130       
00131 
00132       /*rem: Pas de vérification de l'assertion (*size)=n*nbdim */
00133       if ( psize == MED_NOPF ) {  
00134 
00135       /* Creation d'un data space mémoire de dimension 1, de longeur size, et de longeur maxi size */
00136       if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
00137         return -1;
00138 
00139         stride[0] = nbdim;  
00140 
00141         for (dim=firstdim; dim < lastdim; dim++) {
00142                   
00143           start_mem[0] = dim;
00144           if ( (ret = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, start_mem, stride, 
00145                                            count, NULL)) <0)
00146             return -1; 
00147           
00148           start_data[0] = dim*count[0];
00149           if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL, 
00150                                            count, NULL)) <0)
00151             return -1; 
00152           
00153           if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,
00154                              H5P_DEFAULT, val)) < 0)
00155             return -1;
00156         }
00157         
00158       } else {
00159 
00160         pflsize [0] = psize*ngauss*nbdim;
00161         pcount  [0] = psize*ngauss*dimutil;
00162 #ifdef HDF_NEW_API
00163         pflmem     = (med_size *) malloc (sizeof(med_size)*(size_t)pcount[0]);
00164         pfldsk     = (med_size *) malloc (sizeof(med_size)*(size_t)pcount[0]);
00165 #else
00166         pflmem     = (med_ssize *) malloc (sizeof(med_ssize)*(size_t)pcount[0]);
00167         pfldsk     = (med_ssize *) malloc (sizeof(med_ssize)*(size_t)pcount[0]);
00168 #endif
00169         
00170         switch(pflmod)
00171           { /* switch pflmod pour FULL_INTERLACE*/
00172           case MED_GLOBALE :
00173 
00174             /* Creation d'un data space mémoire de dimension 1, de longeur size, et de longeur maxi size */
00175             if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
00176               return -1;
00177 
00178             for (dim=firstdim; dim < lastdim; dim++) {
00179               
00180               for (i=0; i < psize; i++)              /* i balaye les élements du profil */
00181                 for (j=0; j < ngauss; j++) {         
00182                   index = i*ngauss+j + (dim-firstdim)*((int)psize*ngauss);
00183                   pflmem[index] = (pfltab[i]-1)*ngauss*nbdim + j*nbdim+dim;
00184                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;             
00185                 }
00186             }
00187             
00188 #ifdef HDF_NEW_API2
00189             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pflmem ) ) <0) 
00190               return -1; 
00191             
00192             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pfldsk ) ) <0) 
00193               return -1; 
00194 #elif defined HDF_NEW_API
00195             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pflmem ) ) <0) 
00196               return -1; 
00197             
00198             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pfldsk ) ) <0) 
00199               return -1; 
00200 #else
00201             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pflmem ) ) <0) 
00202               return -1; 
00203             
00204             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pfldsk ) ) <0) 
00205               return -1; 
00206 #endif
00207             
00208             break;
00209         
00210           case MED_COMPACT :
00211         
00212             /* Creation d'un data space mémoire de dimension 1, de la longeur du profil          */
00213             /* La dimension utilisée est ici nbdim, même pour un profil compact on suppose       */
00214             /*  que l'utilisateur a toutes les coordonées stockées, même si il en demande qu'une */ 
00215             
00216             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
00217               return -1;
00218             
00219             for (dim=firstdim; dim < lastdim; dim++) {
00220               
00221               for (i=0; i < psize; i++)              /* i balaye les élements du profil */
00222                 for (j=0; j < ngauss; j++) {         
00223                   index = i*ngauss+j + (dim-firstdim)*((int)psize*ngauss);
00224                   pflmem[index] = i*ngauss*nbdim + j*nbdim+dim;
00225                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;             
00226                 }             
00227             }
00228             
00229 #ifdef HDF_NEW_API2
00230             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pflmem ) ) <0) 
00231               return -1; 
00232             
00233             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pfldsk ) ) <0) 
00234               return -1; 
00235 #elif defined HDF_NEW_API
00236             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pflmem ) ) <0) 
00237               return -1; 
00238             
00239             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pfldsk ) ) <0) 
00240               return -1; 
00241 #else
00242             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pflmem ) ) <0) 
00243               return -1; 
00244             
00245             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pfldsk ) ) <0) 
00246               return -1; 
00247 #endif
00248             
00249             break;
00250 
00251           default :
00252             return -1; 
00253           }
00254         
00255         if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
00256           return -1;
00257         
00258         free(pflmem);
00259         free(pfldsk);
00260       }
00261      
00262       break;
00263       
00264     case MED_NO_INTERLACE :
00265 
00266       /*Initialisation des indices de boucle du traitement de l'entrelacement en fonction de la dimension fixee*/
00267 
00268       count[0] = (*size)/nbdim;
00269       
00270       if ( psize == MED_NOPF ) {  
00271         
00272         if ( fixdim != MED_ALL) 
00273           start_data[0] = (fixdim-1)*count[0];
00274         else {
00275           count[0] = *size;
00276           start_data[0] =  0;
00277         };
00278         
00279         if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL, 
00280                                          count, NULL)) <0)
00281           return -1; 
00282         
00283         if ((ret = H5Dread(dataset,type_hdf,dataspace,dataspace,
00284                            H5P_DEFAULT, val)) < 0)
00285           return -1;
00286         
00287       } else {
00288 
00289         if ( fixdim != MED_ALL) 
00290           { 
00291             firstdim = (int)fixdim-1;
00292             lastdim  = (int)fixdim;
00293             dimutil  = 1;
00294           } else        {
00295             firstdim = 0;
00296             lastdim  = (int)nbdim;
00297             dimutil  = (int)nbdim; 
00298           }
00299 
00300         pflsize [0] = psize*ngauss*nbdim;       
00301         pcount  [0] = psize*ngauss*dimutil; /* nom pas très coherent avec count !!! A revoir */      
00302 #ifdef HDF_NEW_API
00303         pfldsk      = (med_size *) malloc(sizeof(med_size)*(size_t)pcount[0]);
00304 #else
00305         pfldsk      = (med_ssize *) malloc(sizeof(med_ssize)*(size_t)pcount[0]);
00306 #endif
00307         
00308         switch(pflmod)
00309           { /*switch plfmod pour NO_INTERLACE */
00310           case MED_GLOBALE :
00311             
00312             for (dim=firstdim; dim < lastdim; dim++) {
00313               
00314               for (i=0; i < psize; i++)              /* i balaye le nbre d'élements du profil                */
00315                 for (j=0; j < ngauss; j++) { 
00316                   index = i*ngauss+j + (dim-firstdim)*((int)psize*ngauss);
00317                   pfldsk[index] = dim*count[0]+(pfltab[i]-1)*ngauss+j;      
00318                 }
00319             }
00320             
00321 #ifdef HDF_NEW_API2
00322             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t *) pfldsk ) ) <0) 
00323               return -1;
00324 #elif defined HDF_NEW_API
00325             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t **) pfldsk ) ) <0) 
00326               return -1;
00327 #else
00328             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hssize_t **) pfldsk ) ) <0) 
00329               return -1;
00330 #endif
00331             
00332             if ((ret = H5Dread(dataset,type_hdf,dataspace,dataspace,H5P_DEFAULT, val)) < 0)
00333               return -1;
00334               
00335             break;
00336             
00337           case MED_COMPACT :
00338             
00339             /* Creation d'un data space mémoire de dimension 1, de la longeur du profil          */
00340             /* La dimension utilisée est ici nbdim, même pour un profil compact on suppose       */
00341             /*  que l'utilisateur a toutes les coordonées stockées, même si il en demande qu'une */ 
00342 
00343             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
00344               return -1;
00345 
00346 #ifdef HDF_NEW_API
00347             pflmem     = (med_size *) malloc (sizeof(med_size)*(size_t)pcount[0]);
00348 #else
00349             pflmem     = (med_ssize *) malloc (sizeof(med_ssize)*(size_t)pcount[0]);
00350 #endif
00351             
00352             /* Le profil COMPACT est contigüe, mais il est possible que l'on selectionne uniquemenent une dimension*/
00353 
00354             index = 0;
00355             for (dim=firstdim; dim < lastdim; dim++) {
00356               
00357               for (i=0; i < psize; i++)              /* i balaye le nbre d'élements du profil                */
00358                 for (j=0; j < ngauss; j++) {
00359 //                index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
00360 //                pflmem[index] = dim*(psize*ngauss) + (pfltab[i]-1)*ngauss+j;
00361 //                pfldsk[index] = dim*count[0]  + (pfltab[i]-1)*ngauss+j;           
00362                   pflmem[index] = ( (dim*psize) + i )*ngauss + j;
00363                   pfldsk[index] = dim*count[0]  + (pfltab[i]-1)*ngauss+j;
00364                   index++;          
00365                 }
00366             }
00367             
00368 #ifdef HDF_NEW_API2
00369             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pflmem ) ) <0) 
00370               return -1; 
00371             
00372             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t *) pfldsk ) ) <0) 
00373               return -1;          
00374 #elif defined HDF_NEW_API
00375             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pflmem ) ) <0) 
00376               return -1; 
00377             
00378             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t **) pfldsk ) ) <0) 
00379               return -1;          
00380 #else
00381             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pflmem ) ) <0) 
00382               return -1; 
00383             
00384             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hssize_t **) pfldsk ) ) <0) 
00385               return -1;          
00386 #endif
00387             
00388             if ((ret = H5Dread(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
00389               return -1;
00390             
00391             break;
00392             
00393           default :
00394             return -1;      
00395             
00396           }
00397         
00398         free(pfldsk);
00399                 
00400       };
00401       
00402       break;
00403       
00404     default :
00405       return -1;
00406     }
00407   
00408   
00409 
00410   if (memspace) 
00411     if ((ret = H5Sclose(memspace)) < 0)
00412       return -1;
00413 
00414   if ((ret = H5Sclose(dataspace)) < 0)
00415     return -1;
00416   
00417   if ((ret = H5Dclose(dataset)) < 0)
00418     return -1;      
00419 
00420   return 0;
00421 }
00422 
00423 }