]> SALOME platform Git repositories - modules/kernel.git/blob - src/MEDWrapper/V2_1/MEDdatasetNumEcrire.cxx
Salome HOME
PR: merge from tag mergeto_trunk_18Jan05
[modules/kernel.git] / src / MEDWrapper / V2_1 / MEDdatasetNumEcrire.cxx
1 /*************************************************************************
2 * COPYRIGHT (C) 1999 - 2002  EDF R&D
3 * THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
4 * IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE 
5 * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; 
6 * EITHER VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
7 *  
8 * THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
9 * WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
11 * LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
12 *
13 * YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE
14 * ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE SOFTWARE FOUNDATION,
15 * INC., 59 TEMPLE PLACE, SUITE 330, BOSTON, MA 02111-1307 USA
16 *
17 *************************************************************************/
18
19 #include "med.hxx"
20 #include "med_outils.hxx"
21
22 /*
23  * - Nom de la fonction : _MEDdatasetNumEcrire
24  * - Description : ecriture d'un dataset tableau numerique
25  * - Parametres :
26  *     - pere (IN)      : l'ID de l'objet HDF pere ou placer l'attribut
27  *     - nom  (IN)      : le nom du dataset
28  *     - type (IN)      : type numerique MED { MED_REEL64 , MED_INT32 , MED_INT64 }
29  *     - 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) }
30  *       - nbdim   (IN) : Dimension des éléments
31  *       - fixdim  (IN) : MED_ALL ou n° de la dimension a enregistrer
32  *     - psize     (IN) : Taille du profil à utiliser, MED_NOPF si pas de profil 
33  *                        (référence les élements, cette taille ne prend pas en compte le nombre de pts de gauss ni la dimension )  
34  *       - pfltab  (IN) : Tableau contenant les n° déléments à traiter (1....oo)
35  *       - pflmod  (IN) : PARAMETRE A AJOUTER : Indique comment lire les informations en mémoire { MED_COMPACT, MED_GLOBALE }. 
36  *       - ngauss  (IN) : Nombre de points de GAUSS par élément
37  *     - size (IN)     : Taille du tableau de valeurs
38  *                        (référence tous les élements, cette taille  prend en compte le nombre de pts de gauss et la dimension )  
39  *     - val  (IN)     : valeurs du tableau
40  *     - mode (IN)     : mode d'ecriture MED (MED_ECRI | MED_REMP)
41  * - Resultat : 0 en cas de succes, -1 sinon
42  */ 
43
44 namespace med_2_1{
45
46 med_err 
47 _MEDdatasetNumEcrire(med_idt pere,char *nom, med_type_champ type,
48                      med_mode_switch interlace, med_size nbdim, med_size fixdim, 
49                      med_size psize, med_ssize * pfltab, med_int ngauss,
50                      med_size *size,  unsigned char *val, med_mode_acces mode)
51 {
52   med_idt    dataset, dataspace = 0, memspace = 0;
53   med_ssize  start_mem[1],start_data[1],*pflmem,*pfldsk;
54   med_size   stride[1],count[1],pcount[1],pflsize[1];
55   med_err    ret;
56   int        i,j,index,type_hdf;
57   int        dim, firstdim, dimutil, lastdim ;
58   med_mode_profil pflmod;
59
60   /* Verify fixdim is between [0, nbdim] ( 0 is MED_ALL ) */
61   if ( ( fixdim < 0 ) || ( fixdim > nbdim ) ) 
62     return -1;
63
64   /* block pflmod to MED_COMPACT (until med2.2) */
65   pflmod = MED_COMPACT;
66
67   switch(type) 
68     {
69     case MED_REEL64 :
70       /* 1) IA32 is LE but due to an (?HDF convertion BUG?) when using H5T_NATIVE_DOUBLE/MED_REEL64? under PCLINUX
71          the file read under SGI is incorrect
72          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
73          REM  : Be careful of compatibility between MED files when changing this (med2.2)                    */
74 #if defined(PCLINUX) || defined(OSF1)
75       type_hdf = H5T_IEEE_F64BE;
76 #else     
77       type_hdf = H5T_IEEE_F64LE;
78 #endif
79       break;
80
81     case MED_INT32 :
82 #if defined(PCLINUX)
83       type_hdf = H5T_STD_I32BE;
84       if ((H5Tconvert(H5T_NATIVE_INT,H5T_STD_I32BE,(hsize_t)*size,(void *)val,NULL,NULL)) < 0) 
85           return -1;
86 #else
87       type_hdf = H5T_NATIVE_INT;
88 #endif
89       break;
90  
91     case MED_INT64 :
92       type_hdf = H5T_NATIVE_LONG;
93       break;
94
95     default :
96       return -1;
97     }
98
99
100   if ((dataset = H5Dopen(pere,nom)) < 0)
101     {
102       /* Whatever the size of the profil is we create a dataset with the size of the value array               */
103       /* Then if we used the MED_REMP mode we can append a new dimension to a previous one in the dataset      */
104       /* When we'll use the compression mode, the space used by unused values would be easily compressed       */
105   
106       if ((dataspace = H5Screate_simple(1,size,NULL)) < 0)
107         return -1;
108       if ((dataset = H5Dcreate(pere,nom,type_hdf,dataspace,
109                                H5P_DEFAULT)) < 0)
110         return -1;      
111     }
112   else
113     if (mode != MED_REMP)
114       {
115         H5Dclose(dataset);
116         return -1;
117       }
118     else
119       if ((dataspace = H5Dget_space(dataset)) <0)
120         return -1;
121
122
123   switch(interlace) 
124     {  /* switch Interlace */
125     case MED_FULL_INTERLACE :
126       
127       /*Initialisation des indices de boucle du traitement de l'entrelacement en fonction de la dimension fixee*/
128       if ( fixdim != MED_ALL) 
129         { 
130           firstdim = fixdim-1;
131           lastdim  = fixdim;
132           dimutil  = 1;
133         } else  {
134           firstdim = 0;
135           lastdim  = nbdim;
136           dimutil  = nbdim; 
137         }
138
139       count [0] = (*size)/(nbdim);
140
141
142       if ( psize == MED_NOPF ) {  
143
144         /* Creation d'un data space mémoire de dimension 1, de longeur size, et de longeur maxi size */
145         if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
146           return -1;
147         
148         stride[0] = nbdim;  
149
150         for (dim=firstdim; dim < lastdim; dim++) {
151           
152           start_mem[0] = dim;
153           if ( (ret = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, start_mem, stride, 
154                                            count, NULL)) <0)
155             return -1; 
156           
157           start_data[0] = dim*count[0];
158           if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL, 
159                                            count, NULL)) <0)
160             return -1; 
161           
162           if ((ret = H5Dwrite(dataset,type_hdf,memspace,dataspace,
163                               H5P_DEFAULT, val)) < 0)
164             return -1;
165         }
166         
167       } else { /* psize != MED_NOPF */
168         
169         pflsize [0] = psize*ngauss*nbdim;
170         pcount  [0] = psize*ngauss*dimutil;
171         pflmem      = (med_ssize *) malloc (sizeof(med_ssize)*pcount[0]);
172         pfldsk      = (med_ssize *) malloc (sizeof(med_ssize)*pcount[0]);
173         
174         switch(pflmod)
175           { /* switch pflmod pout FULL_INTERLACE*/
176           case MED_GLOBALE :
177
178             /* Creation d'un data space mémoire de dimension 1, de longeur size, et de longeur maxi size */
179             if ( (memspace = H5Screate_simple (1, size, NULL)) <0)
180               return -1;
181
182             for (dim=firstdim; dim < lastdim; dim++) {
183               
184               for (i=0; i < psize; i++)              /* i balaye les élements du profil */
185                 for (j=0; j < ngauss; j++) {         
186                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
187                   pflmem[index] = (pfltab[i]-1)*ngauss*nbdim + j*nbdim+dim;
188                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;             
189                 }
190             }
191              
192             if ( (ret = H5Sselect_elements(memspace,H5S_SELECT_SET, pcount[0], (const hssize_t **) pflmem ) ) <0) 
193               return -1; 
194               
195             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, pcount[0], (const hssize_t **) pfldsk ) ) <0) 
196               return -1; 
197             
198             break;
199             
200           case MED_COMPACT :
201
202             /* Creation d'un data space mémoire de dimension 1, de la longeur du profil          */
203             /* La dimension utilisée est ici nbdim, même pour un profil compact on suppose       */
204             /*  que l'utilisateur a toutes les coordonées stockées, même si il en demande qu'une */ 
205
206             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
207               return -1;
208             
209             for (dim=firstdim; dim < lastdim; dim++) {
210               
211               for (i=0; i < psize; i++)              /* i balaye les élements du profil */
212                 for (j=0; j < ngauss; j++) {         
213                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
214                   pflmem[index] = i*ngauss*nbdim + j*nbdim+dim;
215                   pfldsk[index] = dim*count[0] + (pfltab[i]-1)*ngauss+j;             
216                 }
217             }
218             
219             if ( (ret = H5Sselect_elements(memspace,H5S_SELECT_SET, pcount[0], (const hssize_t **) pflmem ) ) <0) 
220               return -1; 
221             
222             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET, pcount[0], (const hssize_t **) pfldsk ) ) <0) 
223               return -1; 
224              
225             break;
226           
227           default :
228             return -1; 
229           }
230
231         if ((ret = H5Dwrite(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
232           return -1;
233         
234         free(pflmem);
235         free(pfldsk);
236       }
237       
238       
239       break;
240       
241     case MED_NO_INTERLACE :
242
243       /*Initialisation des indices de boucle du traitement de l'entrelacement en fonction de la dimension fixee*/
244
245       count[0] = (*size)/nbdim;
246
247       if ( psize == MED_NOPF ) {  
248         
249         if ( fixdim != MED_ALL) 
250           start_data[0] = (fixdim-1)*count[0];
251         else {
252           count[0] = *size;
253           start_data[0] =  0;
254         };
255         
256         if ( (ret = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, start_data, NULL, 
257                                          count, NULL)) <0)
258           return -1; 
259         
260         if ((ret = H5Dwrite(dataset,type_hdf,dataspace,dataspace,
261                             H5P_DEFAULT, val)) < 0)
262           return -1;
263         
264       } else {
265
266         if ( fixdim != MED_ALL) 
267           { 
268             firstdim = fixdim-1;
269             lastdim  = fixdim;
270             dimutil  = 1;
271           } else        {
272             firstdim = 0;
273             lastdim  = nbdim;
274             dimutil  = nbdim; 
275           }
276         
277         pflsize [0] = psize*ngauss*nbdim;
278         pcount  [0] = psize*ngauss*dimutil; /* nom pas très coherent avec count !!! A revoir */ 
279         pfldsk     = (med_ssize *) malloc(sizeof(med_ssize)*pcount[0]);
280
281         switch(pflmod)
282           { /*switch plfmod pour NO_INTERLACE */
283           case MED_GLOBALE :
284             
285             for (dim=firstdim; dim < lastdim; dim++) {
286               
287               for (i=0; i < psize; i++)              /* i balaye le nbre d'élements du profil                */
288                 for (j=0; j < ngauss; j++) { 
289                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
290                   pfldsk[index] = dim*count[0]+(pfltab[i]-1)*ngauss+j;      
291                 }
292             }
293             
294             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,pcount[0], (const hssize_t **) pfldsk ) ) <0) 
295               return -1;
296             
297             if ((ret = H5Dwrite(dataset,type_hdf,dataspace,dataspace,H5P_DEFAULT, val)) < 0)
298               return -1;
299             
300             break;
301             
302           case MED_COMPACT :
303             
304             /* Creation d'un data space mémoire de dimension 1, de la longeur du profil          */
305             /* La dimension utilisée est ici nbdim, même pour un profil compact on suppose       */
306             /*  que l'utilisateur a toutes les coordonées stockées, même si il en demande qu'une */ 
307
308             if ( (memspace = H5Screate_simple (1, pflsize, NULL)) <0)
309               return -1;
310
311             pflmem     = (med_ssize *) malloc (sizeof(med_ssize)*pcount[0]);
312             
313             /* Le profil COMPACT est contigüe, mais il est possible que l'on selectionne uniquemenent une dimension*/
314             
315             for (dim=firstdim; dim < lastdim; dim++) {
316               
317               for (i=0; i < psize; i++)              /* i balaye le nbre d'élements du profil                */
318                 for (j=0; j < ngauss; j++) {
319                   index = i*ngauss+j + (dim-firstdim)*(psize*ngauss);
320                   pflmem[index] = dim*(psize*ngauss) + (pfltab[i]-1)*ngauss+j;
321                   pfldsk[index] = dim*count[0]  + (pfltab[i]-1)*ngauss+j;           
322                 }
323             }
324              
325             if ( (ret = H5Sselect_elements(memspace ,H5S_SELECT_SET,pcount[0], (const hssize_t **) pflmem ) ) <0) 
326               return -1; 
327               
328             if ( (ret = H5Sselect_elements(dataspace,H5S_SELECT_SET,pcount[0], (const hssize_t **) pfldsk ) ) <0) 
329               return -1;
330            
331             if ((ret = H5Dwrite(dataset,type_hdf,memspace,dataspace,H5P_DEFAULT, val)) < 0)
332               return -1;
333
334             free(pflmem);
335             
336             break;
337             
338           default :
339             return -1;      
340             
341           }
342    
343         free(pfldsk);
344         
345       };
346
347       break;
348       
349     default :
350       return -1;
351     }
352   
353   
354   if (memspace) 
355     if ((ret = H5Sclose(memspace)) < 0)
356       return -1;
357   
358   if ((ret = H5Sclose(dataspace)) < 0)
359     return -1;
360   
361   if ((ret = H5Dclose(dataset)) < 0)
362     return -1;      
363
364 #if defined(PCLINUX)
365   if (type == MED_INT32)
366       if ((H5Tconvert(H5T_STD_I32BE,H5T_NATIVE_INT,(hsize_t)*size,(void *)val,NULL,NULL)) < 0) 
367           return -1;
368 #endif 
369   
370   return 0;
371 }
372
373 }