Back to index

salome-med  6.5.0
MEDdatasetNumEcrire.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 : _MEDdatasetNumEcrire
00026  * - Description : ecriture 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 { MED_REEL64 , MED_INT32 , MED_INT64 }
00031  *     - interlace (IN) : Choix du type d'entrelacement utilisé 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
00034  *     - psize     (IN) : Taille du profil à utiliser, MED_NOPF si pas de profil 
00035  *                        (référence les élements, cette taille ne prend pas en compte le nombre de pts de gauss ni la dimension )  
00036  *       - pfltab  (IN) : Tableau contenant les n° déléments à traiter (1....oo)
00037  *       - pflmod  (IN) : PARAMETRE A AJOUTER : Indique comment lire les informations en mémoire { MED_COMPACT, MED_GLOBALE }. 
00038  *       - ngauss  (IN) : Nombre de points de GAUSS par élément
00039  *     - size (IN)     : Taille du tableau de valeurs
00040  *                        (référence tous les élements, cette taille  prend en compte le nombre de pts de gauss et la dimension )  
00041  *     - val  (IN)     : valeurs du tableau
00042  *     - mode (IN)     : mode d'ecriture MED (MED_ECRI | MED_REMP)
00043  * - Resultat : 0 en cas de succes, -1 sinon
00044  */ 
00045 
00046 namespace med_2_1{
00047 
00048 med_err 
00049 _MEDdatasetNumEcrire(med_idt pere,char *nom, med_type_champ type,
00050                      med_mode_switch interlace, med_size nbdim, med_size fixdim, 
00051                      med_size psize, med_ssize * pfltab, med_int ngauss,
00052                      med_size *size,  unsigned char *val, med_mode_acces mode)
00053 {
00054   med_idt    dataset, dataspace = 0, memspace = 0;
00055 #ifdef HDF_NEW_API
00056   med_size  start_mem[1],start_data[1],*pflmem,*pfldsk;
00057 #else
00058   med_ssize  start_mem[1],start_data[1],*pflmem,*pfldsk;
00059 #endif
00060   med_size   stride[1],count[1],pcount[1],pflsize[1];
00061   med_err    ret;
00062   int        i,j,index,type_hdf;
00063   int        dim, firstdim, dimutil, lastdim ;
00064   med_mode_profil pflmod;
00065 
00066   /* Verify fixdim is between [0, nbdim] ( 0 is MED_ALL ) */
00067   if ( ( fixdim < 0 ) || ( fixdim > nbdim ) ) 
00068     return -1;
00069 
00070   /* block pflmod to MED_COMPACT (until med2.2) */
00071   pflmod = MED_COMPACT;
00072 
00073   switch(type) 
00074     {
00075     case MED_REEL64 :
00076       /* 1) IA32 is LE but due to an (?HDF convertion BUG?) when using H5T_NATIVE_DOUBLE/MED_REEL64? under PCLINUX
00077          the file read under SGI is incorrect
00078          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
00079          REM  : Be careful of compatibility between MED files when changing this (med2.2)                    */
00080 #if defined(PCLINUX) || defined(PCLINUX64) || defined(PCLINUX64_32) || defined(OSF1) || defined( PPRO_NT )
00081       type_hdf = H5T_IEEE_F64BE;
00082 #else     
00083       type_hdf = H5T_IEEE_F64LE;
00084 #endif
00085       break;
00086 
00087     case MED_INT32 :
00088 #if defined(PCLINUX) || defined(PCLINUX64) || defined(PCLINUX64_32)
00089       type_hdf = H5T_STD_I32BE;
00090       if ((H5Tconvert(H5T_NATIVE_INT,H5T_STD_I32BE,(hsize_t)*size,(void *)val,NULL,(hid_t)0)) < 0) 
00091           return -1;
00092 #else
00093       type_hdf = H5T_NATIVE_INT;
00094 #endif
00095       break;
00096  
00097     case MED_INT64 :
00098       type_hdf = H5T_NATIVE_LONG;
00099       break;
00100 
00101     default :
00102       return -1;
00103     }
00104 
00105 
00106   if ((dataset = H5Dopen(pere,nom)) < 0)
00107     {
00108       /* Whatever the size of the profil is we create a dataset with the size of the value array               */
00109       /* Then if we used the MED_REMP mode we can append a new dimension to a previous one in the dataset      */
00110       /* When we'll use the compression mode, the space used by unused values would be easily compressed       */
00111   
00112       if ((dataspace = H5Screate_simple(1,size,NULL)) < 0)
00113         return -1;
00114       if ((dataset = H5Dcreate(pere,nom,type_hdf,dataspace,
00115                                H5P_DEFAULT)) < 0)
00116         return -1;      
00117     }
00118   else
00119     if (mode != MED_REMP)
00120       {
00121         H5Dclose(dataset);
00122         return -1;
00123       }
00124     else
00125       if ((dataspace = H5Dget_space(dataset)) <0)
00126         return -1;
00127 
00128 
00129   switch(interlace) 
00130     {  /* switch Interlace */
00131     case MED_FULL_INTERLACE :
00132       
00133       /*Initialisation des indices de boucle du traitement de l'entrelacement en fonction de la dimension fixee*/
00134       if ( fixdim != MED_ALL) 
00135         { 
00136           firstdim = (int)fixdim-1;
00137           lastdim  = (int)fixdim;
00138           dimutil  = 1;
00139         } else  {
00140           firstdim = 0;
00141           lastdim  = (int)nbdim;
00142           dimutil  = (int)nbdim; 
00143         }
00144 
00145       count [0] = (*size)/(nbdim);
00146 
00147 
00148       if ( psize == MED_NOPF ) {  
00149 
00150         /* Creation d'un data space mémoire de dimension 1, de longeur size, et de longeur maxi size */
00151         if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
00152           return -1;
00153         
00154         stride[0] = nbdim;  
00155 
00156         for (dim=firstdim; dim < lastdim; dim++) {
00157           
00158           start_mem[0] = dim;
00159           if ( (ret = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, start_mem, stride, 
00160                                            count, NULL)) <0)
00161             return -1; 
00162           
00163           start_data[0] = dim*count[0];
00164           if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL, 
00165                                            count, NULL)) <0)
00166             return -1; 
00167           
00168           if ((ret = H5Dwrite(dataset,type_hdf,memspace,dataspace,
00169                               H5P_DEFAULT, val)) < 0)
00170             return -1;
00171         }
00172         
00173       } else { /* psize != MED_NOPF */
00174         
00175         pflsize [0] = psize*ngauss*nbdim;
00176         pcount  [0] = psize*ngauss*dimutil;
00177 #ifdef HDF_NEW_API
00178         pflmem      = (med_size *) malloc (sizeof(med_size)*(size_t)pcount[0]);
00179         pfldsk      = (med_size *) malloc (sizeof(med_size)*(size_t)pcount[0]);
00180 #else
00181         pflmem      = (med_ssize *) malloc (sizeof(med_ssize)*(size_t)pcount[0]);
00182         pfldsk      = (med_ssize *) malloc (sizeof(med_ssize)*(size_t)pcount[0]);
00183 #endif
00184         
00185         switch(pflmod)
00186           { /* switch pflmod pout FULL_INTERLACE*/
00187           case MED_GLOBALE :
00188 
00189             /* Creation d'un data space mémoire de dimension 1, de longeur size, et de longeur maxi size */
00190             if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
00191               return -1;
00192 
00193             for (dim=firstdim; dim < lastdim; dim++) {
00194               
00195               for (i=0; i < psize; i++)              /* i balaye les élements du profil */
00196                 for (j=0; j < ngauss; j++) {         
00197                   index = i*ngauss+j + (dim-firstdim)*((int)psize*ngauss);
00198                   pflmem[index] = (pfltab[i]-1)*ngauss*nbdim + j*nbdim+dim;
00199                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;             
00200                 }
00201             }
00202              
00203 #ifdef HDF_NEW_API2
00204             if ( (ret = H5Sselect_elements(memspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pflmem ) ) <0) 
00205               return -1; 
00206               
00207             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pfldsk ) ) <0) 
00208               return -1; 
00209 #elif defined HDF_NEW_API
00210             if ( (ret = H5Sselect_elements(memspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pflmem ) ) <0) 
00211               return -1; 
00212               
00213             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pfldsk ) ) <0) 
00214               return -1; 
00215 #else
00216             if ( (ret = H5Sselect_elements(memspace,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pflmem ) ) <0) 
00217               return -1; 
00218               
00219             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pfldsk ) ) <0) 
00220               return -1; 
00221 #endif
00222             
00223             break;
00224             
00225           case MED_COMPACT :
00226 
00227             /* Creation d'un data space mémoire de dimension 1, de la longeur du profil          */
00228             /* La dimension utilisée est ici nbdim, même pour un profil compact on suppose       */
00229             /*  que l'utilisateur a toutes les coordonées stockées, même si il en demande qu'une */ 
00230 
00231             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
00232               return -1;
00233             
00234             for (dim=firstdim; dim < lastdim; dim++) {
00235               
00236               for (i=0; i < psize; i++)              /* i balaye les élements du profil */
00237                 for (j=0; j < ngauss; j++) {         
00238                   index = i*ngauss+j + (dim-firstdim)*((int)psize*ngauss);
00239                   pflmem[index] = i*ngauss*nbdim + j*nbdim+dim;
00240                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;             
00241                 }
00242             }
00243             
00244 #ifdef HDF_NEW_API2
00245             if ( (ret = H5Sselect_elements(memspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pflmem ) ) <0) 
00246               return -1; 
00247             
00248             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t *) pfldsk ) ) <0) 
00249               return -1; 
00250 #elif defined HDF_NEW_API
00251             if ( (ret = H5Sselect_elements(memspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pflmem ) ) <0) 
00252               return -1; 
00253             
00254             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hsize_t **) pfldsk ) ) <0) 
00255               return -1; 
00256 #else
00257             if ( (ret = H5Sselect_elements(memspace,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pflmem ) ) <0) 
00258               return -1; 
00259             
00260             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, (size_t)pcount[0], (const hssize_t **) pfldsk ) ) <0) 
00261               return -1; 
00262 #endif
00263              
00264             break;
00265           
00266           default :
00267             return -1; 
00268           }
00269 
00270         if ((ret = H5Dwrite(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
00271           return -1;
00272         
00273         free(pflmem);
00274         free(pfldsk);
00275       }
00276       
00277       
00278       break;
00279       
00280     case MED_NO_INTERLACE :
00281 
00282       /*Initialisation des indices de boucle du traitement de l'entrelacement en fonction de la dimension fixee*/
00283 
00284       count[0] = (*size)/nbdim;
00285 
00286       if ( psize == MED_NOPF ) {  
00287         
00288         if ( fixdim != MED_ALL) 
00289           start_data[0] = (fixdim-1)*count[0];
00290         else {
00291           count[0] = *size;
00292           start_data[0] =  0;
00293         };
00294         
00295         if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL, 
00296                                          count, NULL)) <0)
00297           return -1; 
00298         
00299         if ((ret = H5Dwrite(dataset,type_hdf,dataspace,dataspace,
00300                             H5P_DEFAULT, val)) < 0)
00301           return -1;
00302         
00303       } else {
00304 
00305         if ( fixdim != MED_ALL) 
00306           { 
00307             firstdim = (int)fixdim-1;
00308             lastdim  = (int)fixdim;
00309             dimutil  = 1;
00310           } else        {
00311             firstdim = 0;
00312             lastdim  = (int)nbdim;
00313             dimutil  = (int)nbdim; 
00314           }
00315         
00316         pflsize [0] = psize*ngauss*nbdim;
00317         pcount  [0] = psize*ngauss*dimutil; /* nom pas très coherent avec count !!! A revoir */      
00318 #ifdef HDF_NEW_API
00319         pfldsk     = (med_size *) malloc(sizeof(med_size)*(size_t)pcount[0]);
00320 #else
00321         pfldsk     = (med_ssize *) malloc(sizeof(med_ssize)*(size_t)pcount[0]);
00322 #endif
00323 
00324         switch(pflmod)
00325           { /*switch plfmod pour NO_INTERLACE */
00326           case MED_GLOBALE :
00327             
00328             for (dim=firstdim; dim < lastdim; dim++) {
00329               
00330               for (i=0; i < psize; i++)              /* i balaye le nbre d'élements du profil                */
00331                 for (j=0; j < ngauss; j++) { 
00332                   index = i*ngauss+j + (dim-firstdim)*((int)psize*ngauss);
00333                   pfldsk[index] = dim*count[0]+(pfltab[i]-1)*ngauss+j;      
00334                 }
00335             }
00336             
00337 #ifdef HDF_NEW_API2
00338             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t *) pfldsk ) ) <0) 
00339               return -1;
00340 #elif defined HDF_NEW_API
00341             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t **) pfldsk ) ) <0) 
00342               return -1;
00343 #else
00344             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hssize_t **) pfldsk ) ) <0) 
00345               return -1;
00346 #endif
00347             
00348             if ((ret = H5Dwrite(dataset,type_hdf,dataspace,dataspace,H5P_DEFAULT, val)) < 0)
00349               return -1;
00350             
00351             break;
00352             
00353           case MED_COMPACT :
00354             
00355             /* Creation d'un data space mémoire de dimension 1, de la longeur du profil          */
00356             /* La dimension utilisée est ici nbdim, même pour un profil compact on suppose       */
00357             /*  que l'utilisateur a toutes les coordonées stockées, même si il en demande qu'une */ 
00358 
00359             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
00360               return -1;
00361 
00362 #ifdef HDF_NEW_API
00363             pflmem     = (med_size *) malloc (sizeof(med_size)*(size_t)pcount[0]);
00364 #else
00365             pflmem     = (med_ssize *) malloc (sizeof(med_ssize)*(size_t)pcount[0]);
00366 #endif
00367             
00368             /* Le profil COMPACT est contigüe, mais il est possible que l'on selectionne uniquemenent une dimension*/
00369             
00370             for (dim=firstdim; dim < lastdim; dim++) {
00371               
00372               for (i=0; i < psize; i++)              /* i balaye le nbre d'élements du profil                */
00373                 for (j=0; j < ngauss; j++) {
00374                   index = i*ngauss+j + (dim-firstdim)*((int)psize*ngauss);
00375                   pflmem[index] = dim*(psize*ngauss) + (pfltab[i]-1)*ngauss+j;
00376                   pfldsk[index] = dim*count[0]  + (pfltab[i]-1)*ngauss+j;           
00377                 }
00378             }
00379              
00380 #ifdef HDF_NEW_API2
00381             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t *) pflmem ) ) <0) 
00382               return -1; 
00383               
00384             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t *) pfldsk ) ) <0) 
00385               return -1;
00386 #elif defined HDF_NEW_API
00387             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t **) pflmem ) ) <0) 
00388               return -1; 
00389               
00390             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hsize_t **) pfldsk ) ) <0) 
00391               return -1;
00392 #else
00393             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET,(size_t)pcount[0], (const hssize_t **) pflmem ) ) <0) 
00394               return -1; 
00395               
00396             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,(size_t)pcount[0], (const hssize_t **) pfldsk ) ) <0) 
00397               return -1;
00398 #endif
00399            
00400             if ((ret = H5Dwrite(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
00401               return -1;
00402 
00403             free(pflmem);
00404             
00405             break;
00406             
00407           default :
00408             return -1;      
00409             
00410           }
00411    
00412         free(pfldsk);
00413         
00414       };
00415 
00416       break;
00417       
00418     default :
00419       return -1;
00420     }
00421   
00422   
00423   if (memspace) 
00424     if ((ret = H5Sclose(memspace)) < 0)
00425       return -1;
00426   
00427   if ((ret = H5Sclose(dataspace)) < 0)
00428     return -1;
00429   
00430   if ((ret = H5Dclose(dataset)) < 0)
00431     return -1;      
00432 
00433 #if defined(PCLINUX) || defined(PCLINUX64) || defined(PCLINUX64_32)
00434   if (type == MED_INT32)
00435       if ((H5Tconvert(H5T_STD_I32BE,H5T_NATIVE_INT,(hsize_t)*size,(void *)val,NULL,(hid_t)0)) < 0) 
00436           return -1;
00437 #endif 
00438   
00439   return 0;
00440 }
00441 
00442 }