Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEM / MEDMEM_MEDMEMchampLire.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "MEDMEM_MEDMEMchampLire.hxx"
24 /*
25  * En attendant une correction de la gestion du mode d'accès au fichier dans MEDfichier
26  * on intègre la correction ici.
27  */
28
29 namespace med_2_3 {
30   extern "C" {
31
32 # define ICI                    {                                       \
33       fflush(stdout);                                                   \
34       fprintf(stderr, "%s [%d] : " , __FILE__ , __LINE__ ) ;            \
35       fflush(stderr) ;                                                  \
36     }
37
38 # define ISCRUTE_MED(entier)        {                                   \
39       ICI ;                                                             \
40       fprintf(stderr,"%s = %d\n",#entier,entier) ;                      \
41       fflush(stderr) ;                                                  \
42     }
43
44 # define SSCRUTE_MED(chaine)        {                                   \
45       ICI ;                                                             \
46       fprintf(stderr,"%s = \"%s\"\n",#chaine,chaine) ;                  \
47       fflush(stderr) ;                                                  \
48     }
49 # define MESSAGE_MED(chaine)        {                                   \
50       ICI ;                                                             \
51       fprintf(stderr,"%s\n",chaine) ;                                   \
52       fflush(stderr) ;                                                  \
53     }
54
55     extern void _MEDmodeErreurVerrouiller(void);
56
57 #include <med.h>
58 #include <med_outils.h>
59
60 #include <string.h>
61 #include <stdlib.h>
62
63     /*
64      * - Nom de la fonction : MEDchampLire
65      * - Description : Lecture d'un Champ Résultat
66      * - Parametres :
67      *     - fid      (IN)  : ID du fichier HDF courant
68      *     - maa      (IN)  : le nom du maillage sur lequel s'applique le champ
69      *     - cha      (IN)  : le nom du champ
70      *     - val      (OUT) : valeurs du champ à lire
71      *     - interlace(IN)  : entrelacement voulu en mémoire {MED_FULL_INTERLACE,MED_NO_INTERLACE}
72      *     - numco    (IN)  : n° de la composante à lire (MED_ALL si toutes)
73      *     - profil   (OUT) : nom du profil utilisé (MED_NOPFL si inutilisé)
74      *     - pflmod   (IN)  : Indique comment lire les informations en mémoire { MED_COMPACT, MED_GLOBALE }.
75      *     - type_ent (IN)  : entité concerné par le champ {MED_NOEUD,MED_ARETE,MED_FACE,MED_MAILLE}
76      *     - type_geo (IN)  : type géométrique de l'entité concerné {MED_POINT,MED_SEG2 ......}
77      *     - numdt    (IN)  : n° du pas de temps (MED_NOPDT si aucun)
78      *     - numo     (IN)  : n° d'ordre utilisé MED_NONOR si inutile
79      * - Resultat : 0 en cas de succes, -1 sinon
80      */
81
82     /* REM : La taille de val allouée par l'utilisateur doit prendre en compte le nbre de points de gauss et le nbre de composantes*/
83
84     med_err
85     MEDMEMchampLire(med_idt fid,char *maa, char *cha, unsigned char *val,med_mode_switch interlace,med_int numco,
86                  char * locname, char *profil, med_mode_profil pflmod,
87                  med_entite_maillage type_ent, med_geometrie_element type_geo,
88                  med_int numdt, med_int numo)
89
90     {
91       med_err ret=-1;
92       med_idt gid=0, datagroup1=0, datagroup2=0,datagroup3=0;
93       med_int ncomp=0, chtype=0, ngauss=0, i=0, pfluse=0;
94       char nomdatagroup1[2*MED_TAILLE_NOM_ENTITE+2]="",nomdatagroup2[2*MED_MAX_PARA+1]="";
95       char tmp1[MED_TAILLE_NOM_ENTITE+1]="", pfltmp[MED_TAILLE_NOM+1]="";
96       char chemin[MED_TAILLE_CHA+MED_TAILLE_NOM+1]="";
97       med_size   psize=0;
98       med_int   *pfltabtmp=0;
99       med_ssize *pfltab=0;
100
101       /*
102        * On inhibe le gestionnaire d'erreur HDF 5
103        */
104       _MEDmodeErreurVerrouiller();
105
106       /*
107        * Si le Data Group cha n'existe pas => erreur
108        */
109       strcpy(chemin,MED_CHA);
110       strcat(chemin,cha);
111       if ((gid = _MEDdatagroupOuvrir(fid,chemin)) < 0)
112         goto ERROR;
113
114       /* Lecture du nbre de composantes du champ */
115
116       if (_MEDattrEntierLire(gid,MED_NOM_NCO,&ncomp) < 0)
117         goto ERROR;
118
119       /*
120        * Si le Data Group  de niveau 1 <type_ent>[.<type_geo>] n'existe pas => erreur
121        */
122       if (_MEDnomEntite(nomdatagroup1,type_ent) < 0)
123         goto ERROR;
124       if ((type_ent != MED_NOEUD))
125         {
126           if (_MEDnomGeometrie(tmp1,type_geo) < 0)
127             goto ERROR;
128           strcat(nomdatagroup1,".");
129           strcat(nomdatagroup1,tmp1);
130         }
131       datagroup1 = 0;
132       if ( (datagroup1 = _MEDdatagroupOuvrir(gid,nomdatagroup1)) < 0 )
133         goto ERROR;
134
135       /*
136        * Si le Data Group de niveau 2 <numdt>.<numoo> n'existe pas => erreur
137        */
138       sprintf(nomdatagroup2,"%*li%*li",MED_MAX_PARA,(long ) numdt,MED_MAX_PARA,(long ) numo);
139
140       datagroup2 = 0;
141       if ( (datagroup2 = _MEDdatagroupOuvrir(datagroup1,nomdatagroup2)) < 0)
142         goto ERROR;
143
144       /*
145        * Ouvre le datagroup de niveau 3 <maa>
146        */
147
148       if ( ! strcmp(maa,MED_NOREF) )
149         if (_MEDattrStringLire(datagroup2,MED_NOM_MAI,MED_TAILLE_NOM,maa) < 0)
150           goto ERROR;
151
152       datagroup3 = 0;
153       if ( (datagroup3 = _MEDdatagroupOuvrir(datagroup2,maa)) < 0 )
154         goto ERROR;
155
156       /* Gestion des profils*/
157
158       /*
159        * Lire le profil
160        */
161
162       if (_MEDattrStringLire(datagroup3,MED_NOM_PFL,MED_TAILLE_NOM,pfltmp) < 0)
163         goto ERROR;
164
165       if ( pfluse = (strcmp(pfltmp,MED_NOPFLi) && strcmp(pfltmp,"")) ) /* le test MED_NOPFLi pour des raisons de compatibilité */
166         {
167           strcpy(profil,pfltmp);
168           if ( (i = MEDnValProfil(fid,profil)) < 0 )
169             goto ERROR;
170           else
171             psize = i;
172
173           pfltabtmp = (med_int *)   malloc (sizeof(med_int)*psize);
174           pfltab = (med_ssize *) malloc (sizeof(med_ssize)*psize);
175           if (MEDprofilLire(fid,pfltabtmp,profil) < 0)
176             goto ERROR;
177           for (i=0;i<psize;i++)
178             pfltab[i] = (med_ssize) pfltabtmp[i];
179
180         }
181       else {
182         psize = MED_NOPF;
183         strcpy(profil,MED_NOPFL);
184       }
185
186
187       /* Lire le nbre des points de GAUSS*/
188       if (_MEDattrEntierLire(datagroup3,MED_NOM_NGA,&ngauss) < 0) {
189         MESSAGE_MED("Erreur à la lecture de l'attribut MED_NOM_NGA : ");
190         ISCRUTE_MED(ngauss);goto ERROR;
191       };
192
193       /* Lire l'identificateur de localisation des points de GAUSS*/
194       if ( _MEDattrStringLire(datagroup3,MED_NOM_GAU,MED_TAILLE_NOM,locname) < 0) {
195         MESSAGE_MED("Erreur à la lecture de l'attribut MED_NOM_GAU : ");
196         SSCRUTE_MED(locname); goto ERROR;
197       }
198
199       /*
200        * Lecture du champ
201        */
202
203       if (_MEDattrEntierLire(gid,MED_NOM_TYP,&chtype) < 0)
204         goto ERROR;
205
206       switch(chtype)
207         {
208         case MED_FLOAT64 :
209           if ( _MEDdatasetNumLire(datagroup3,MED_NOM_CO,MED_FLOAT64,
210                                   interlace,ncomp,numco,
211                                   psize,pflmod,pfltab,ngauss,val)< 0)
212             goto ERROR;
213           break;
214
215         case MED_INT32 :
216 #if defined(F77INT64)
217           if ( _MEDdatasetNumLire(datagroup3,MED_NOM_CO,MED_INT64,
218                                   interlace,ncomp,numco,
219                                   psize,pflmod,pfltab,ngauss,val)< 0)
220             goto ERROR;
221 #else
222           if ( _MEDdatasetNumLire(datagroup3,MED_NOM_CO,MED_INT32,
223                                   interlace,ncomp,numco,
224                                   psize,pflmod,pfltab,ngauss,val)< 0)
225             goto ERROR;
226 #endif
227           break;
228
229         case MED_INT64 :
230 #if defined(F77INT64)
231           if ( _MEDdatasetNumLire(datagroup3,MED_NOM_CO,MED_INT64,
232                                   interlace,ncomp,numco,
233                                   psize,pflmod,pfltab,ngauss,val)< 0)
234             goto ERROR;
235 #else
236           goto ERROR;
237 #endif
238           break;
239
240         default :
241           goto ERROR;
242         }
243
244       /*
245        * On ferme tout
246        */
247
248       ret = 0;
249
250     ERROR:
251
252       if ( pfluse ) { free(pfltab); free(pfltabtmp);}
253
254       if (datagroup3>0)     if (_MEDdatagroupFermer(datagroup3) < 0) {
255         MESSAGE_MED("Impossible de fermer le datagroup : ");
256         ISCRUTE_MED(datagroup3); ret = -1;
257       }
258
259       if (datagroup2>0)     if (_MEDdatagroupFermer(datagroup2) < 0) {
260         MESSAGE_MED("Impossible de fermer le datagroup : ");
261         ISCRUTE_MED(datagroup2); ret = -1;
262       }
263
264       if (datagroup1>0)     if (_MEDdatagroupFermer(datagroup1) < 0) {
265         MESSAGE_MED("Impossible de fermer le datagroup : ");
266         ISCRUTE_MED(datagroup1); ret = -1;
267       }
268
269       if (gid>0)     if (_MEDdatagroupFermer(gid) < 0) {
270         MESSAGE_MED("Impossible de fermer le datagroup : ");
271         ISCRUTE_MED(gid); ret = -1;
272       }
273
274       return ret;
275     }
276
277
278 #undef MESSAGE_MED
279 #undef SSCRUTE_MED
280 #undef ISCRUTE_MED
281
282   }
283 }
284
285