Salome HOME
aea72c6babf738fabd86b9cb48e36813ff287be0
[modules/med.git] / src / MEDWrapper / V2_1 / MEDchampLire.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 /*************************************************************************
21 * COPYRIGHT (C) 1999 - 2002  EDF R&D
22 * THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
23 * IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE 
24 * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; 
25 * EITHER VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
26 *  
27 * THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
28 * WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
29 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
30 * LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
31 *
32 * YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE
33 * ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE SOFTWARE FOUNDATION,
34 * INC., 59 TEMPLE PLACE, SUITE 330, BOSTON, MA 02111-1307 USA
35 *
36 *************************************************************************/
37
38 #include "med.hxx"
39 #include "med_outils.hxx"
40
41 #include <string.h>
42 #include <stdlib.h>
43
44  /*La taille de val allouée par l'utilisateur doit prendre en compte le nbre de points de gauss et le nbre de composantes*/
45
46 namespace med_2_1{
47
48 med_err 
49 MEDchampLire(med_idt fid,char *maa, char *cha, unsigned char *val,med_mode_switch interlace,med_int numco,
50              char *profil,med_entite_maillage type_ent, med_geometrie_element type_geo,
51              med_int numdt, med_int numo)
52      /* VERIFIER LA POSSIBILITE DE RELIRE L'UNITE DE PAS DE TEMPS (DS CHAMPINFO) */
53 {
54   med_err ret;
55   med_idt chid, datagroup1, datagroup2;
56   med_int ncomp, chtype, ngauss, i, locnumdt, pfluse;
57   char nomdatagroup1[2*MED_TAILLE_NOM_ENTITE+2],nomdatagroup2[2*MED_MAX_PARA+1];
58   char tmp1[MED_TAILLE_NOM_ENTITE+1], pfltmp[MED_TAILLE_NOM+1];
59   char chemin[MED_TAILLE_CHA+MED_TAILLE_NOM+1];
60   med_size   psize;
61   med_int   *pfltabtmp=0;
62   med_ssize *pfltab=0;
63   char maatmp[MED_TAILLE_NOM+1];
64   
65   /*
66    * On inhibe le gestionnaire d'erreur HDF 5
67    */
68   _MEDmodeErreurVerrouiller();
69
70   /* 
71    * Si le Data Group cha n'existe pas => erreur
72    */
73   strcpy(chemin,MED_CHA);
74   strcat(chemin,cha);
75   if ((chid = _MEDdatagroupOuvrir(fid,chemin)) < 0)
76     return -1;
77
78  /* 
79    * Si le Data Group  de niveau 1 <type_ent>[.<type_geo>] n'existe pas => erreur
80    */
81   if ((ret = _MEDnomEntite(nomdatagroup1,type_ent)) < 0)
82     return -1;
83   if ((type_ent != MED_NOEUD))
84     {
85       if ((ret = _MEDnomGeometrie(tmp1,type_geo)) < 0)
86         return -1;
87       strcat(nomdatagroup1,".");
88       strcat(nomdatagroup1,tmp1);
89     }
90   datagroup1 = 0;
91   if ( (datagroup1 = _MEDdatagroupOuvrir(chid,nomdatagroup1)) < 0 )
92     return -1;
93
94   /*
95    * Si le Data Group de niveau 2 <numdtt>.<numoo> n'existe pas => erreur
96    */
97   if ( numdt == MED_NOPDT) locnumdt = MED_NOPDT; else locnumdt = numdt;
98   sprintf(nomdatagroup2,"%*li%*li",MED_MAX_PARA,(long ) locnumdt,MED_MAX_PARA,(long ) numo);
99   
100   datagroup2 = 0;   
101   if ( (datagroup2 = _MEDdatagroupOuvrir(datagroup1,nomdatagroup2)) < 0)    
102     return -1;   
103
104   /* Lecture du nbre de composantes du champ */
105
106   if ((ret = _MEDattrEntierLire(chid,MED_NOM_NCO,&ncomp)) < 0)
107     return -1;
108   
109   /* Gestion des profils*/
110
111   /*
112    * Lire le profil demandé
113    */
114
115   if ((ret = _MEDattrStringLire(datagroup2,MED_NOM_PFL,MED_TAILLE_NOM,pfltmp)) < 0)
116     return -1;
117   
118   if ( pfluse = (strcmp(pfltmp,MED_NOPFLi) && strcmp(pfltmp,"")) ) /* le test "" pour des raisons de compatibilité */
119     {
120       strcpy(profil,pfltmp);
121       if ( (i = MEDnValProfil(fid,profil)) < 0 )
122         return -1;
123       else
124         psize = i;
125
126       pfltabtmp = (med_int *)   malloc (sizeof(med_int)*psize);
127       pfltab = (med_ssize *) malloc (sizeof(med_ssize)*psize);
128       if ((ret = MEDprofilLire(fid,pfltabtmp,profil)) < 0)
129         return -1;
130       for (i=0;i<psize;i++)
131         pfltab[i] = (med_ssize) pfltabtmp[i];
132        
133     }
134   else {
135     psize = MED_NOPF;
136     strcpy(profil,MED_NOPFL);
137   }
138   
139   /*
140    * Lire le nom de maillage associé au champ
141    */
142   if ((ret = _MEDattrStringLire(datagroup2,MED_NOM_MAI,MED_TAILLE_NOM,maatmp)) < 0)
143     return -1;
144
145   if (strcmp(maa,maatmp)) 
146     return -1;
147   
148   /* Lit le nbre de points de Gauss  */ 
149   /* (attribut MED_NOM_NGAU) propre au <type_ent>[.<type_geo>] pour simplifier la relecture */
150   if ( (ret = _MEDattrEntierLire(datagroup2,MED_NOM_NGA,&ngauss)) < 0 )
151     return -1;
152
153   /*
154    * Lecture du champ
155    */
156
157   if ((ret = _MEDattrEntierLire(chid,MED_NOM_TYP,&chtype)) < 0)
158     return -1;
159
160   switch(chtype)
161     {
162     case MED_REEL64 :
163       if ((ret =  _MEDdatasetNumLire(datagroup2,MED_NOM_CO,MED_REEL64,
164                                      interlace,ncomp,numco,
165                                      psize,pfltab,ngauss,val))< 0)
166         return -1;
167       break;
168
169     case MED_INT32 :
170 #if defined(IRIX64)||defined(OSF1) 
171      if ((ret =  _MEDdatasetNumLire(datagroup2,MED_NOM_CO,MED_INT64,
172                                      interlace,ncomp,numco,
173                                      psize,pfltab,ngauss,val))< 0)
174         return -1;
175 #else
176      if ((ret =  _MEDdatasetNumLire(datagroup2,MED_NOM_CO,MED_INT32,
177                                      interlace,ncomp,numco,
178                                      psize, pfltab,ngauss,val))< 0)
179         return -1;
180 #endif
181      break;
182
183     case MED_INT64 :
184 #if defined(IRIX64)||defined(OSF1) 
185      if ((ret =  _MEDdatasetNumLire(datagroup2,MED_NOM_CO,MED_INT64,
186                                      interlace,ncomp,numco,
187                                      psize,pfltab,ngauss,val))< 0)
188         return -1;
189 #else
190      return -1;
191 #endif
192       break;      
193
194     default :
195       return -1;
196     }
197
198   /*
199    * On ferme tout 
200    */
201   if ( pfluse ) { free(pfltab); free(pfltabtmp);}
202
203   if ((ret = _MEDdatagroupFermer(datagroup2)) < 0)
204     return -1;
205   if ((ret = _MEDdatagroupFermer(datagroup1)) < 0)
206     return -1;
207   if ((ret = _MEDdatagroupFermer(chid)) < 0)
208     return -1;
209     
210   return 0;     
211 }
212
213 }