Salome HOME
Minor: removing directive after an #endif (triggered a warning)
[modules/med.git] / src / MEDLoader / MEDLoader.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but 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
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDLoader.hxx"
22 #include "MEDLoaderBase.hxx"
23 #include "MEDFileUtilities.hxx"
24 #include "MEDFileMesh.hxx"
25 #include "MEDFileField.hxx"
26 #include "CellModel.hxx"
27 #include "MEDCouplingUMesh.hxx"
28 #include "MEDCouplingMemArray.hxx"
29 #include "MEDCouplingFieldDouble.hxx"
30 #include "MEDCouplingGaussLocalization.hxx"
31 #include "MEDCouplingAutoRefCountObjectPtr.hxx"
32
33 #include "InterpKernelAutoPtr.hxx"
34
35 #include "med.h"
36
37 #include <string>
38 #include <limits>
39 #include <cstring>
40 #include <sstream>
41 #include <fstream>
42 #include <numeric>
43 #include <iterator>
44 #include <algorithm>
45
46 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
47                                                    MED_SEG2,
48                                                    MED_SEG3,
49                                                    MED_SEG4,
50                                                    MED_TRIA3,
51                                                    MED_QUAD4,
52                                                    MED_TRIA6,
53                                                    MED_TRIA7,
54                                                    MED_QUAD8,
55                                                    MED_QUAD9,
56                                                    MED_TETRA4,
57                                                    MED_PYRA5,
58                                                    MED_PENTA6,
59                                                    MED_HEXA8,
60                                                    MED_OCTA12,
61                                                    MED_TETRA10,
62                                                    MED_PYRA13,
63                                                    MED_PENTA15,
64                                                    MED_HEXA20,
65                                                    MED_HEXA27,
66                                                    MED_POLYGON,
67                                                    MED_POLYHEDRON };
68
69 med_geometry_type typmainoeud[1] = { MED_NONE };
70
71 INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1,
72                                                                     INTERP_KERNEL::NORM_SEG2,
73                                                                     INTERP_KERNEL::NORM_SEG3,
74                                                                     INTERP_KERNEL::NORM_SEG4,
75                                                                     INTERP_KERNEL::NORM_TRI3,
76                                                                     INTERP_KERNEL::NORM_QUAD4,
77                                                                     INTERP_KERNEL::NORM_TRI6,
78                                                                     INTERP_KERNEL::NORM_TRI7,
79                                                                     INTERP_KERNEL::NORM_QUAD8,
80                                                                     INTERP_KERNEL::NORM_QUAD9,
81                                                                     INTERP_KERNEL::NORM_TETRA4,
82                                                                     INTERP_KERNEL::NORM_PYRA5,
83                                                                     INTERP_KERNEL::NORM_PENTA6,
84                                                                     INTERP_KERNEL::NORM_HEXA8,
85                                                                     INTERP_KERNEL::NORM_HEXGP12,
86                                                                     INTERP_KERNEL::NORM_TETRA10,
87                                                                     INTERP_KERNEL::NORM_PYRA13,
88                                                                     INTERP_KERNEL::NORM_PENTA15,
89                                                                     INTERP_KERNEL::NORM_HEXA20,
90                                                                     INTERP_KERNEL::NORM_HEXA27,
91                                                                     INTERP_KERNEL::NORM_POLYGON,
92                                                                     INTERP_KERNEL::NORM_POLYHED };
93
94 med_geometry_type typmai3[34] = { MED_POINT1,//0
95                                   MED_SEG2,//1
96                                   MED_SEG3,//2
97                                   MED_TRIA3,//3
98                                   MED_QUAD4,//4
99                                   MED_POLYGON,//5
100                                   MED_TRIA6,//6
101                                   MED_TRIA7,//7
102                                   MED_QUAD8,//8
103                                   MED_QUAD9,//9
104                                   MED_SEG4,//10
105                                   MED_NONE,//11
106                                   MED_NONE,//12
107                                   MED_NONE,//13
108                                   MED_TETRA4,//14
109                                   MED_PYRA5,//15
110                                   MED_PENTA6,//16
111                                   MED_NONE,//17
112                                   MED_HEXA8,//18
113                                   MED_NONE,//19
114                                   MED_TETRA10,//20
115                                   MED_NONE,//21
116                                   MED_OCTA12,//22
117                                   MED_PYRA13,//23
118                                   MED_NONE,//24
119                                   MED_PENTA15,//25
120                                   MED_NONE,//26
121                                   MED_HEXA27,//27
122                                   MED_NONE,//28
123                                   MED_NONE,//29
124                                   MED_HEXA20,//30
125                                   MED_POLYHEDRON,//31
126                                   MED_NONE,//32
127                                   MED_NONE//33
128 };
129
130 double MEDLoader::_EPS_FOR_NODE_COMP=1.e-12;
131
132 int MEDLoader::_COMP_FOR_CELL=0;
133
134 int MEDLoader::_TOO_LONG_STR=0;
135
136 using namespace ParaMEDMEM;
137
138 /// @cond INTERNAL
139
140 namespace MEDLoaderNS
141 {
142   int readUMeshDimFromFile(const char *fileName, const char *meshName, std::vector<int>& possibilities);
143   void dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity);
144   void writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch);
145   med_int getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception);
146   std::vector<std::string> getMeshNamesFid(med_idt fid);
147 }
148
149 /// @endcond
150
151
152 /// @cond INTERNAL
153
154 /*!
155  * This method returns a first quick overview of mesh with name 'meshName' into the file 'fileName'.
156  * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
157  * @return the maximal mesh dimension of specified mesh. If nothing found -1 is returned.
158  */
159 int MEDLoaderNS::readUMeshDimFromFile(const char *fileName, const char *meshName, std::vector<int>& possibilities)
160 {
161   possibilities.clear();
162   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
163   int ret;
164   std::set<int> poss;
165   char nommaa[MED_NAME_SIZE+1];
166   char maillage_description[MED_COMMENT_SIZE+1];
167   med_mesh_type type_maillage;
168   med_int Sdim,Mdim;
169   std::string trueMeshName;
170   med_int meshId=getIdFromMeshName(fid,meshName,trueMeshName);
171   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
172   med_sorting_type sortingType;
173   med_int nstep;
174   med_axis_type axisType;
175   int naxis=MEDmeshnAxis(fid,meshId);
176   INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
177   INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
178   MEDmeshInfo(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit);
179   // limitation
180   if(nstep!=1)
181     {
182       throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
183     } 
184   med_int numdt,numit;
185   med_float dt;
186   MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
187   // endlimitation
188   for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
189     {
190       med_geometry_type curMedType=typmai[i];
191       med_bool changement,transformation;
192       int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
193       int curNbOfElemF=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);//limitation
194       int curNbOfElem;
195       med_entity_type whichEntity;
196       MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
197       if(curNbOfElem>0)
198         {
199           INTERP_KERNEL::NormalizedCellType type=typmai2[i];
200           int curDim=(int)INTERP_KERNEL::CellModel::GetCellModel(type).getDimension();
201           poss.insert(curDim);
202         }
203     }
204   if(!poss.empty())
205     {
206       ret=*poss.rbegin();
207       for(std::set<int>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();it++)
208         possibilities.push_back(*it-ret);
209     }
210   else
211     ret=-2;
212   return ret;
213 }
214
215 med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception)
216 {
217   if(meshName==0)
218     {
219       std::vector<std::string> meshes=getMeshNamesFid(fid);
220       if(meshes.empty())
221         throw INTERP_KERNEL::Exception("No mesh in file");
222       trueMeshName=meshes[0];
223       return 1;
224     }
225   std::string meshNameStr(meshName);
226   std::vector<std::string> meshes=getMeshNamesFid(fid);
227   if(meshes.empty())
228     throw INTERP_KERNEL::Exception("No mesh in file");
229   std::vector<std::string>::iterator iter=std::find(meshes.begin(),meshes.end(),meshNameStr);
230   if(iter==meshes.end())
231     {
232       std::ostringstream os2;
233       os2 << "MeshName '" << meshName << "' not in file : meshes available : ";
234       std::copy(meshes.begin(),meshes.end(),std::ostream_iterator<std::string>(os2," "));
235       throw INTERP_KERNEL::Exception(os2.str().c_str());
236     }
237   trueMeshName=meshName;
238   return iter-meshes.begin()+1;
239 }
240
241 std::vector<std::string> MEDLoaderNS::getMeshNamesFid(med_idt fid)
242 {
243   med_mesh_type type_maillage;
244   char maillage_description[MED_COMMENT_SIZE+1];
245   char dtunit[MED_COMMENT_SIZE+1];
246   med_int space_dim;
247   med_int mesh_dim;
248   char nommaa[MED_NAME_SIZE+1];
249   med_axis_type axistype;
250   med_sorting_type stype;
251   med_int n=MEDnMesh(fid);
252   std::vector<std::string> ret(n);
253   for(int i=0;i<n;i++)
254     {
255       int naxis=MEDmeshnAxis(fid,i+1);
256       INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
257       INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
258       int nstep;
259       MEDmeshInfo(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit);
260       std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
261       ret[i]=cur;
262     }
263   return ret;
264 }
265
266 /*!
267  * This methods allows to merger all entities and to considerate only cell types.
268  */
269 void MEDLoaderNS::dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity)
270 {
271   if(nbOfElemCell>=nbOfElemFace)
272     {
273       whichEntity=MED_CELL;
274       nbOfElem=nbOfElemCell;
275     }
276   else
277     {
278       whichEntity=MED_CELL;
279       nbOfElem=nbOfElemFace;
280     }
281 }
282
283 /// @endcond
284
285 bool MEDLoader::HasXDR()
286 {
287 #ifdef HAS_XDR
288   return true;
289 #else
290   return false;
291 #endif
292 }
293
294 /*!
295  * This method sets the epsilon value used for node comparison when trying to buid a profile for a field on node/cell on an already written mesh.
296  */
297 void MEDLoader::SetEpsilonForNodeComp(double val)
298 {
299   _EPS_FOR_NODE_COMP=val;
300 }
301
302 /*!
303  * This method sets the policy comparison when trying to fit the already written mesh on a field. The semantic of the policy is specified in MEDCouplingUMesh::zipConnectivityTraducer.
304  */
305 void MEDLoader::SetCompPolicyForCell(int val)
306 {
307   _COMP_FOR_CELL=val;
308 }
309
310 /*!
311  * This method set the behaviour of MEDLoader when a too long string is seen in datastructure before copy it in MED file.
312  * By default (0) an exception is thrown. If equal to 1 a warning is emitted in std_err but no exception is thrown.
313  */
314 void MEDLoader::SetTooLongStrPolicy(int val)
315 {
316   _TOO_LONG_STR=val;
317 }
318
319 /*!
320  * Given a 'fileName' and a 'meshName' this method returns global information concerning this mesh.
321  * It returns, in this order :
322  * - number of cells sorted by dimension and by geometry type. The first entry in the vector is the maximal dimension, the 2nd in the vector is the maximal dimension-1...
323  * - the mesh dimension
324  * - the space dimension
325  * - the number of nodes
326  */
327 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > MEDLoader::GetUMeshGlobalInfo(const char *fileName, const char *meshName, int &meshDim, int& spaceDim, int& numberOfNodes)
328 {
329   CheckFileForRead(fileName);
330   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
331   std::set<int> poss;
332   char nommaa[MED_NAME_SIZE+1];
333   char maillage_description[MED_COMMENT_SIZE+1];
334   med_mesh_type type_maillage;
335   std::string trueMeshName;
336   med_int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName);
337   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
338   med_sorting_type sortingType;
339   med_int nstep;
340   med_axis_type axisType;
341   int naxis=MEDmeshnAxis(fid,meshId);
342   INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
343   INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
344   MEDmeshInfo(fid,meshId,nommaa,&spaceDim,&meshDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit); 
345   if(type_maillage!=MED_UNSTRUCTURED_MESH)
346     {
347       std::ostringstream oss; oss << "MEDLoader::GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
348       oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
349       throw INTERP_KERNEL::Exception(oss.str().c_str());
350     }
351   // limitation
352   if(nstep!=1)
353     throw INTERP_KERNEL::Exception("MEDLoader::GetUMeshGlobalInfo : multisteps on mesh not managed !");
354   med_int numdt,numit;
355   med_float dt;
356   MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
357   // endlimitation
358   std::vector<int> dims;
359   std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
360   med_bool changement,transformation;
361   for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
362     {
363       med_geometry_type curMedType=typmai[i];
364       int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
365       if(curNbOfElemM>0)
366         {
367           INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
368           int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
369           dims.push_back(mdimCell);
370           geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
371         }
372     }
373   int maxLev=*std::max_element(dims.begin(),dims.end());
374   int lowLev=*std::min_element(dims.begin(),dims.end());
375   int nbOfLevels=maxLev-lowLev+1;
376   std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
377   for(std::size_t i=0;i<dims.size();i++)
378     {
379       ret[maxLev-dims[i]].push_back(geoTypes[i]);
380     }
381   numberOfNodes=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
382   return ret;
383 }
384
385 void MEDLoader::CheckFileForRead(const char *fileName)
386 {
387   MEDFileUtilities::CheckFileForRead(fileName);
388 }
389
390 std::vector<std::string> MEDLoader::GetMeshNames(const char *fileName)
391 {
392   CheckFileForRead(fileName);
393   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
394   std::vector<std::string> ret=MEDLoaderNS::getMeshNamesFid(fid);
395   return ret;
396 }
397
398 std::vector< std::pair<std::string,std::string> > MEDLoader::GetComponentsNamesOfField(const char *fileName, const char *fieldName)
399 {
400   CheckFileForRead(fileName);
401   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
402   med_int nbFields=MEDnField(fid);
403   std::vector<std::string> fields(nbFields);
404   med_field_type typcha;
405   for(int i=0;i<nbFields;i++)
406     {
407       med_int ncomp=MEDfieldnComponent(fid,i+1);
408       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
409       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
410       INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
411       med_int nbPdt;
412       med_bool localmesh;
413       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
414       INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
415       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
416       std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
417       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
418       if(curFieldName==fieldName)
419         {
420           std::vector< std::pair<std::string,std::string> > ret(ncomp);
421           for(int j=0;j<ncomp;j++)
422             ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
423                                                       MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
424           return ret;
425         }
426       fields[i]=curFieldName;
427     }
428   std::ostringstream oss; oss << "MEDLoader::GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
429   oss << "Possible field names are : " << std::endl;
430   std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
431   throw INTERP_KERNEL::Exception(oss.str().c_str());
432 }
433
434 std::vector<std::string> MEDLoader::GetMeshNamesOnField(const char *fileName, const char *fieldName)
435 {
436   CheckFileForRead(fileName);
437   std::vector<std::string> ret;
438   //
439   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
440   med_int nbFields=MEDnField(fid);
441   //
442   med_field_type typcha;
443   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
444   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
445   med_bool localmesh;
446   //
447   for(int i=0;i<nbFields;i++)
448     {
449       med_int ncomp=MEDfieldnComponent(fid,i+1);
450       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
451       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
452       med_int nbPdt;
453       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
454       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
455       std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
456       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
457       if(curFieldName==fieldName)
458         ret.push_back(meshName);
459     }
460   return ret;
461 }
462
463 std::vector<std::string> MEDLoader::GetMeshFamiliesNames(const char *fileName, const char *meshName)
464 {
465   CheckFileForRead(fileName);
466   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
467   med_int nfam=MEDnFamily(fid,meshName);
468   std::vector<std::string> ret(nfam);
469   char nomfam[MED_NAME_SIZE+1];
470   med_int numfam;
471   for(int i=0;i<nfam;i++)
472     {
473       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
474       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
475       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
476       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
477       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
478       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
479       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
480       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
481       ret[i]=cur;
482     }
483   return ret;
484 }
485
486
487 std::vector<std::string> MEDLoader::GetMeshFamiliesNamesOnGroup(const char *fileName, const char *meshName, const char *grpName)
488 {
489   CheckFileForRead(fileName);
490   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
491   med_int nfam=MEDnFamily(fid,meshName);
492   std::vector<std::string> ret;
493   char nomfam[MED_NAME_SIZE+1];
494   med_int numfam;
495   for(int i=0;i<nfam;i++)
496     {
497       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
498       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
499       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
500       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
501       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
502       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
503       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
504       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
505       for(int j=0;j<ngro;j++)
506         {
507           std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
508           if(cur2==grpName)
509             ret.push_back(cur);
510         }
511     }
512   return ret;
513 }
514
515 std::vector<std::string> MEDLoader::GetMeshGroupsNamesOnFamily(const char *fileName, const char *meshName, const char *famName)
516 {
517   CheckFileForRead(fileName);
518   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
519   med_int nfam=MEDnFamily(fid,meshName);
520   std::vector<std::string> ret;
521   char nomfam[MED_NAME_SIZE+1];
522   med_int numfam;
523   bool found=false;
524   for(int i=0;i<nfam && !found;i++)
525     {
526       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
527       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
528       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
529       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
530       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
531       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
532       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
533       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
534       found=(cur==famName);
535       if(found)
536         for(int j=0;j<ngro;j++)
537           {
538             std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
539             ret.push_back(cur2);
540           }
541     }
542   if(!found)
543     {
544       std::ostringstream oss;
545       oss << "MEDLoader::GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
546       throw INTERP_KERNEL::Exception(oss.str().c_str());
547     }
548   return ret;
549 }
550
551   
552 std::vector<std::string> MEDLoader::GetMeshGroupsNames(const char *fileName, const char *meshName)
553 {
554   CheckFileForRead(fileName);
555   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
556   med_int nfam=MEDnFamily(fid,meshName);
557   std::vector<std::string> ret;
558   char nomfam[MED_NAME_SIZE+1];
559   med_int numfam;
560   for(int i=0;i<nfam;i++)
561     {
562       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
563       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
564       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
565       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
566       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
567       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
568       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
569       for(int j=0;j<ngro;j++)
570         {
571           std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
572           if(std::find(ret.begin(),ret.end(),cur)==ret.end())
573             ret.push_back(cur);
574         }
575     }
576   return ret;
577 }
578 std::vector<ParaMEDMEM::TypeOfField> MEDLoader::GetTypesOfField(const char *fileName, const char *meshName, const char *fieldName)
579 {
580   CheckFileForRead(fileName);
581   std::vector<ParaMEDMEM::TypeOfField> ret;
582   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
583   med_int nbFields=MEDnField(fid);
584   //
585   med_field_type typcha;
586   //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
587   med_int numdt=0,numo=0;
588   med_float dt=0.0;
589   char pflname[MED_NAME_SIZE+1]="";
590   char locname[MED_NAME_SIZE+1]="";
591   char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
592   char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
593   med_bool localmesh;
594   //
595   for(int i=0;i<nbFields;i++)
596     {
597       med_int ncomp=MEDfieldnComponent(fid,i+1);
598       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
599       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
600       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
601       med_int nbPdt;
602       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
603       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
604       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
605       if(curMeshName==meshName)
606         {
607           if(curFieldName==fieldName)
608             {
609               int profilesize,nbi;
610               if(nbPdt>0)
611                 {
612                   bool found=false;
613                   for(int ii=0;ii<nbPdt && !found;ii++)
614                     {
615                       MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
616                       med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
617                                                                 pflname,&profilesize,locname,&nbi);
618                       if(nbOfVal>0)
619                         {
620                           ret.push_back(ON_NODES);
621                           found=true;
622                         }
623                     }
624                 }
625               bool found=false;
626               for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
627                 {
628                   if(nbPdt>0)
629                     {
630                       MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
631                       med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
632                                                                 pflname,&profilesize,locname,&nbi);
633                       if(nbOfVal>0)
634                         {
635                           found=true;
636                           ret.push_back(ON_CELLS);
637                         }
638                     }
639                 }
640             }
641         }
642     }
643   delete [] maa_ass;
644   delete [] nomcha;
645   return ret;
646 }
647
648 std::vector<std::string> MEDLoader::GetAllFieldNames(const char *fileName)
649 {
650   CheckFileForRead(fileName);
651   std::vector<std::string> ret;
652   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
653   med_int nbFields=MEDnField(fid);
654   med_field_type typcha;
655   for(int i=0;i<nbFields;i++)
656     {
657       med_int ncomp=MEDfieldnComponent(fid,i+1);
658       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
659       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
660       INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
661       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
662       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
663       med_int nbPdt;
664       med_bool localmesh;
665       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
666       ret.push_back(std::string(nomcha));
667     }
668   return ret;
669 }
670
671 std::vector<std::string> MEDLoader::GetAllFieldNamesOnMesh(const char *fileName, const char *meshName)
672 {
673   CheckFileForRead(fileName);
674   std::vector<std::string> ret;
675   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
676   med_int nbFields=MEDnField(fid);
677   //
678   med_field_type typcha;
679   char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
680   char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
681   //
682   for(int i=0;i<nbFields;i++)
683     {
684       med_int ncomp=MEDfieldnComponent(fid,i+1);
685       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
686       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
687       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
688       med_int nbPdt;
689       med_bool localmesh;
690       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
691       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
692       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
693       //
694       if(curMeshName==meshName)
695         ret.push_back(curFieldName);
696     }
697   delete [] maa_ass;
698   delete [] nomcha;
699   return ret;
700 }
701
702 std::vector<std::string> MEDLoader::GetFieldNamesOnMesh(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName)
703 {
704   CheckFileForRead(fileName);
705   switch(type)
706     {
707     case ON_CELLS:
708       return GetCellFieldNamesOnMesh(fileName,meshName);
709     case ON_NODES:
710       return GetNodeFieldNamesOnMesh(fileName,meshName);
711     default:
712       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
713     } 
714 }
715
716 std::vector<std::string> MEDLoader::GetCellFieldNamesOnMesh(const char *fileName, const char *meshName)
717 {
718   CheckFileForRead(fileName);
719   std::vector<std::string> ret;
720   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
721   med_int nbFields=MEDnField(fid);
722   //
723   med_field_type typcha;
724   //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
725   med_int numdt=0,numo=0;
726   med_float dt=0.0;
727   char pflname[MED_NAME_SIZE+1]="";
728   char locname[MED_NAME_SIZE+1]="";
729   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
730   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
731   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
732   med_bool localmesh;
733   med_int nbPdt;
734   //
735   for(int i=0;i<nbFields;i++)
736     {
737       med_int ncomp=MEDfieldnComponent(fid,i+1);
738       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
739       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
740       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
741       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
742       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
743       int profilesize,nbi;
744       if(curMeshName==meshName)
745         {
746           bool found=false;
747           for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
748             {
749               if(nbPdt>0)
750                 {
751                   MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
752                   med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
753                                                             pflname,&profilesize,locname,&nbi);
754                   if(nbOfVal>0)
755                     {
756                       found=true;
757                       ret.push_back(curFieldName);
758                     }
759                 }
760             }
761         }
762     }
763   return ret;
764 }
765
766 std::vector<std::string> MEDLoader::GetNodeFieldNamesOnMesh(const char *fileName, const char *meshName)
767 {
768   CheckFileForRead(fileName);
769   std::vector<std::string> ret;
770   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
771   med_int nbFields=MEDnField(fid);
772   char pflname[MED_NAME_SIZE+1]="";
773   char locname[MED_NAME_SIZE+1]="";
774   //
775   med_field_type typcha;
776   med_int numdt=0,numo=0;
777   med_float dt=0.0;
778   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
779   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
780   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
781   med_bool localmesh;
782   //
783   for(int i=0;i<nbFields;i++)
784     {
785       med_int ncomp=MEDfieldnComponent(fid,i+1);
786       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
787       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
788       med_int nbPdt;
789       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
790       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
791       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
792       if(nbPdt>0)
793         {
794           int profilesize,nbi;
795           MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
796           med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
797                                                     pflname,&profilesize,locname,&nbi);
798           if(curMeshName==meshName && nbOfVal>0)
799             {
800               ret.push_back(curFieldName);
801             }
802         }
803     }
804   return ret;
805 }
806
807 std::vector< std::pair< std::pair<int,int>, double> > MEDLoader::GetAllFieldIterations(const char *fileName, const char *fieldName)
808 {
809   CheckFileForRead(fileName);
810   std::vector< std::pair< std::pair<int,int>, double > > ret;
811   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
812   med_int nbFields=MEDnField(fid);
813   //
814   med_field_type typcha;
815   med_int numdt=0,numo=0;
816   med_float dt=0.0;
817   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
818   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
819   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
820   med_bool localmesh;
821   //
822   std::ostringstream oss; oss << "MEDLoader::GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
823   for(int i=0;i<nbFields;i++)
824     {
825       med_int ncomp=MEDfieldnComponent(fid,i+1);
826       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
827       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
828       med_int nbPdt;
829       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
830       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
831       if(curFieldName==fieldName)
832         {
833           for(int k=0;k<nbPdt;k++)
834             {
835               MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
836               ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
837             }
838           return ret;
839         }
840       else
841         {
842           oss << "\"" << curFieldName << "\"";
843           if(i!=nbFields-1) oss << ", ";
844         }
845     }
846   oss << " !";
847   throw INTERP_KERNEL::Exception(oss.str().c_str());
848 }
849
850 double MEDLoader::GetTimeAttachedOnFieldIteration(const char *fileName, const char *fieldName, int iteration, int order)
851 {
852   CheckFileForRead(fileName);
853   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
854   med_int nbFields=MEDnField(fid);
855   //
856   med_field_type typcha;
857   med_int numdt=0,numo=0;
858   med_float dt=0.0;
859   med_bool local;
860   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
861   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
862   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
863   //
864   bool found=false;
865   bool found2=false;
866   double ret=std::numeric_limits<double>::max();
867   for(int i=0;i<nbFields && !found;i++)
868     {
869       med_int ncomp=MEDfieldnComponent(fid,i+1);
870       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
871       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
872       med_int nbPdt;
873       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt);
874       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
875       if(curFieldName==fieldName)
876         {
877           found=true;
878           for(int k=0;k<nbPdt;k++)
879             {
880               MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
881               if(numdt==iteration && numo==order)
882                 {
883                   found2=true;
884                   ret=dt;
885                 }
886             }
887         }
888     }
889   if(!found || !found2)
890     {
891       std::ostringstream oss;
892       oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
893       throw INTERP_KERNEL::Exception(oss.str().c_str());
894     }
895   return ret;
896 }
897
898 std::vector< std::pair<int,int> > MEDLoader::GetFieldIterations(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, const char *fieldName)
899 {
900   CheckFileForRead(fileName);
901   switch(type)
902     {
903     case ON_CELLS:
904       return GetCellFieldIterations(fileName,meshName,fieldName);
905     case ON_NODES:
906       return GetNodeFieldIterations(fileName,meshName,fieldName);
907     default:
908       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
909     }
910 }
911
912 std::vector< std::pair<int,int> > MEDLoader::GetCellFieldIterations(const char *fileName, const char *meshName, const char *fieldName)
913 {
914   CheckFileForRead(fileName);
915   std::string meshNameCpp(meshName);
916   std::vector< std::pair<int,int> > ret;
917   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
918   med_int nbFields=MEDnField(fid);
919   //
920   med_field_type typcha;
921   med_int numdt=0,numo=0;
922   med_float dt=0.0;
923   char pflname[MED_NAME_SIZE+1]="";
924   char locname[MED_NAME_SIZE+1]="";
925   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
926   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
927   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
928   med_bool localmesh;
929   //
930   std::ostringstream oss; oss << "MEDLoader::GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
931   std::set<std::string> s2;
932   for(int i=0;i<nbFields;i++)
933     {
934       med_int ncomp=MEDfieldnComponent(fid,i+1);
935       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
936       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
937       med_int nbPdt;
938       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
939       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
940       if(curFieldName==fieldName)
941         {
942           bool found=false;
943           for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
944             {
945               for(int k=0;k<nbPdt;k++)
946                 {
947                   int profilesize,nbi;
948                   MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
949                   med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
950                                                             pflname,&profilesize,locname,&nbi);
951                   std::string maa_ass_cpp(maa_ass);
952                   if(nbOfVal>0)
953                     {
954                       if(meshNameCpp==maa_ass_cpp)
955                         {
956                           found=true;
957                           ret.push_back(std::make_pair(numdt,numo));
958                         }
959                       else
960                         s2.insert(maa_ass_cpp);
961                     }
962                 }
963             }
964         }
965       else
966         {
967           oss << "\"" << curFieldName << "\"";
968           if(i!=nbFields-1) oss << ", ";
969         }
970     }
971   if(ret.empty())
972     {
973       if(!s2.empty())
974         {
975           oss << ". Cell Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
976           std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
977         }
978       oss << " !";
979       throw INTERP_KERNEL::Exception(oss.str().c_str());
980     }
981   return ret;
982 }
983
984 std::vector< std::pair<int,int> > MEDLoader::GetNodeFieldIterations(const char *fileName, const char *meshName, const char *fieldName)
985 {
986   CheckFileForRead(fileName);
987   std::string meshNameCpp(meshName);
988   std::vector< std::pair<int,int> > ret;
989   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
990   med_int nbFields=MEDnField(fid);
991   //
992   med_field_type typcha;
993   med_int numdt=0,numo=0;
994   med_float dt=0.0;
995   char pflname[MED_NAME_SIZE+1]="";
996   char locname[MED_NAME_SIZE+1]="";
997   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
998   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
999   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1000   med_bool localmesh;
1001   //
1002   std::ostringstream oss; oss << "MEDLoader::GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
1003   std::set<std::string> s2;
1004   for(int i=0;i<nbFields;i++)
1005     {
1006       med_int ncomp=MEDfieldnComponent(fid,i+1);
1007       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1008       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1009       med_int nbPdt;
1010       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
1011       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1012       if(curFieldName==fieldName)
1013         {
1014           for(int k=0;k<nbPdt;k++)
1015             {
1016               int profilesize,nbi;
1017               MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
1018               med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1019                                                         pflname,&profilesize,locname,&nbi);
1020                std::string maa_ass_cpp(maa_ass);
1021                if(nbOfVal>0)
1022                  {
1023                    if(meshNameCpp==maa_ass_cpp)
1024                      { ret.push_back(std::make_pair(numdt,numo)); }
1025                    else
1026                      s2.insert(maa_ass_cpp);
1027                  }
1028             }
1029         }
1030       else
1031         {
1032           oss << "\"" << curFieldName << "\"";
1033           if(i!=nbFields-1) oss << ", ";
1034         }
1035     }
1036   if(ret.empty())
1037     {
1038       if(!s2.empty())
1039         {
1040           oss << ". Node Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1041           std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1042         }
1043       oss << " !";
1044       throw INTERP_KERNEL::Exception(oss.str().c_str());
1045     }
1046   return ret;
1047 }
1048
1049 MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, const char *meshName, int meshDimRelToMax)
1050 {
1051   CheckFileForRead(fileName);
1052   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1053   MEDFileMesh *mmPtr(mm);
1054   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1055   if(!mmuPtr)
1056     {
1057       std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !";
1058       throw INTERP_KERNEL::Exception(oss.str().c_str());
1059     }
1060  return  mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1061 }
1062
1063 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, int meshDimRelToMax)
1064 {
1065   CheckFileForRead(fileName);
1066   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName);
1067   MEDFileMesh *mmPtr(mm);
1068   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1069   if(!mmuPtr)
1070     {
1071       std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1072       throw INTERP_KERNEL::Exception(oss.str().c_str());
1073     }
1074  return  mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1075 }
1076
1077 int MEDLoader::ReadUMeshDimFromFile(const char *fileName, const char *meshName)
1078 {
1079   CheckFileForRead(fileName);
1080   std::vector<int> poss;
1081   return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
1082 }
1083
1084 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFamilies(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<std::string>& fams)
1085 {
1086   CheckFileForRead(fileName);
1087   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1088   MEDFileMesh *mmPtr(mm);
1089   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1090   if(!mmuPtr)
1091     {
1092       std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1093       throw INTERP_KERNEL::Exception(oss.str().c_str());
1094     }
1095     return mmuPtr->getFamilies(meshDimRelToMax,fams,true);
1096 }
1097
1098 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromGroups(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<std::string>& grps)
1099 {
1100   CheckFileForRead(fileName);
1101   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1102   MEDFileMesh *mmPtr(mm);
1103   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1104   if(!mmuPtr)
1105     {
1106       std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1107       throw INTERP_KERNEL::Exception(oss.str().c_str());
1108     }
1109     return mmuPtr->getGroups(meshDimRelToMax,grps,true);
1110 }
1111
1112 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadField(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1113 {
1114   CheckFileForRead(fileName);
1115   switch(type)
1116     {
1117     case ON_CELLS:
1118       return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1119     case ON_NODES:
1120       return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1121     case ON_GAUSS_PT:
1122       return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1123     case ON_GAUSS_NE:
1124       return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1125     default:
1126       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
1127     } 
1128 }
1129
1130 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsOnSameMesh(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1131                                                                                   const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1132 {
1133   if(its.empty())
1134     return std::vector<ParaMEDMEM::MEDCouplingFieldDouble *>();
1135   CheckFileForRead(fileName);
1136   std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> ret(its.size());
1137   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> > retSafe(its.size());
1138   if(its.empty())
1139     return ret;
1140   //Retrieving mesh of rank 0 and field on rank 0 too.
1141   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1142   MEDFileMesh *mmPtr(mm);
1143   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1144   if(!mmuPtr)
1145     throw INTERP_KERNEL::Exception("MEDLoader::ReadFieldsOnSameMesh : only unstructured mesh is managed !");
1146   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> m=mmuPtr->getMeshAtLevel(meshDimRelToMax);
1147   const DataArrayInt *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax);
1148   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> m2(m->clone(true));
1149   if(o2n)
1150     m2->renumberCells(o2n->begin(),true);
1151   int i=0;
1152   for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1153     {
1154       MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second);
1155       MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> retElt=ff->getFieldOnMeshAtLevel(type,m);
1156       if(o2n)
1157         retElt->renumberCells(o2n->begin(),true);
1158       retElt->setMesh(m2);
1159       retSafe[i]=retElt;
1160     }
1161   i=0;
1162   for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1163     ret[i]=retSafe[i].retn();
1164   return ret;
1165 }
1166
1167 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsCellOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1168                                                                                             const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1169 {
1170   return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its);
1171 }
1172
1173 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsNodeOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1174                                                                                       const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1175 {
1176   return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its);
1177 }
1178
1179 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsGaussOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1180                                                                                        const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1181 {
1182   return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its);
1183 }
1184
1185 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsGaussNEOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1186                                                                                          const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1187 {
1188   return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
1189 }
1190
1191 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldCell(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1192 {
1193   MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,iteration,order);
1194   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1195   MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> m=mm->getGenMeshAtLevel(meshDimRelToMax,false);
1196   MEDFileMesh *mPtr(mm);
1197   MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1198   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret=ff->getFieldOnMeshAtLevel(ON_CELLS,m);
1199   if(muPtr)
1200     {
1201       const DataArrayInt *num=muPtr->getNumberFieldAtLevel(meshDimRelToMax);
1202       if(num)
1203         ret->renumberCells(num->begin());
1204     }
1205   return ret.retn();
1206 }
1207
1208 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldNode(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1209 {
1210   MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,iteration,order);
1211   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1212   MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> m=mm->getGenMeshAtLevel(meshDimRelToMax,false);
1213   MEDFileMesh *mPtr(mm);
1214   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret=ff->getFieldOnMeshAtLevel(ON_NODES,m);
1215   MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1216   if(ff->getPflsReallyUsed().empty())
1217     {
1218       if(muPtr)
1219         {
1220           const DataArrayInt *num=muPtr->getNumberFieldAtLevel(meshDimRelToMax);
1221           if(num)
1222             ret->renumberCells(num->begin());
1223         }
1224     }
1225   else
1226     {
1227       DataArrayInt *pfl=0,*arr2=0;
1228       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> arr=ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl);
1229       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> pflSafe(pfl);
1230       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> mp=m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end());
1231       MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> mzip=static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2));
1232       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr2Safe(arr2);
1233       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr3=arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes());
1234       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> pflSorted(pflSafe->deepCpy()); pflSorted->sort(true);
1235       if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
1236         throw INTERP_KERNEL::Exception("MEDLoader::ReadFieldNode : not implemented yet !");
1237       if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
1238         {
1239           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n2=pflSafe->checkAndPreparePermutation();
1240           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> n2o2=o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples());
1241           mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
1242           arr->setName("");
1243           ret->setArray(arr);
1244         }
1245       ret->setMesh(mzip);
1246     }
1247   return ret.retn();
1248 }
1249
1250 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldGauss(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1251 {
1252   MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,iteration,order);
1253   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1254   MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> m=mm->getGenMeshAtLevel(meshDimRelToMax,false);
1255   MEDFileMesh *mPtr(mm);
1256   MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1257   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret=ff->getFieldOnMeshAtLevel(ON_GAUSS_PT,m);
1258   if(muPtr)
1259     {
1260       const DataArrayInt *num=muPtr->getNumberFieldAtLevel(meshDimRelToMax);
1261       if(num)
1262         ret->renumberCells(num->begin());
1263     }
1264   return ret.retn();
1265 }
1266
1267 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldGaussNE(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1268 {
1269   MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,iteration,order);
1270   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1271   MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> m=mm->getGenMeshAtLevel(meshDimRelToMax,false);
1272   MEDFileMesh *mPtr(mm);
1273   MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1274   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret=ff->getFieldOnMeshAtLevel(ON_GAUSS_NE,m);
1275   if(muPtr)
1276     {
1277       const DataArrayInt *num=muPtr->getNumberFieldAtLevel(meshDimRelToMax);
1278       if(num)
1279         ret->renumberCells(num->begin());
1280     }
1281   return ret.retn();
1282 }
1283
1284 void MEDLoader::WriteUMesh(const char *fileName, const ParaMEDMEM::MEDCouplingUMesh *mesh, bool writeFromScratch)
1285 {
1286   if(!mesh)
1287     throw INTERP_KERNEL::Exception("MEDLoader::WriteUMesh : input mesh is null !");
1288   int mod=writeFromScratch?2:0;
1289   MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> m=MEDFileUMesh::New();
1290   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> mcpy(static_cast<MEDCouplingUMesh *>(mesh->deepCpy()));
1291   m->setMeshAtLevel(0,mcpy,true);
1292   m->write(fileName,mod);
1293 }
1294
1295 void MEDLoader::WriteUMeshDep(const char *fileName, const ParaMEDMEM::MEDCouplingUMesh *mesh, bool writeFromScratch)
1296 {
1297   MEDLoader::WriteUMesh(fileName,mesh,writeFromScratch);
1298 }
1299
1300 void MEDLoader::WriteUMeshesPartition(const char *fileName, const char *meshNameC, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1301 {
1302   std::string meshName(meshNameC);
1303   if(meshName.empty())
1304     throw INTERP_KERNEL::Exception("Trying to write a unstructured mesh with no name ! MED file format needs a not empty mesh name : change 2nd parameter !");
1305   int status=MEDLoaderBase::getStatusOfFile(fileName);
1306   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1307     {
1308       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1309       throw INTERP_KERNEL::Exception(oss.str().c_str());
1310     }
1311   MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> m=MEDFileUMesh::New();
1312   m->setGroupsFromScratch(0,meshes,true);
1313   m->setName(meshNameC);
1314   int mod=writeFromScratch?2:0;
1315   m->write(fileName,mod);
1316 }
1317
1318 void MEDLoader::WriteUMeshesPartitionDep(const char *fileName, const char *meshNameC, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1319 {
1320   WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch);
1321 }
1322
1323 void MEDLoader::WriteUMeshes(const char *fileName, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1324 {
1325   int mod=writeFromScratch?2:0;
1326   MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> m=MEDFileUMesh::New();
1327   m->setMeshes(meshes,true);
1328   m->write(fileName,mod);
1329 }
1330
1331 void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch)
1332 {
1333   MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New();
1334   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f2(f->deepCpy());
1335   const MEDCouplingMesh *m=f2->getMesh();
1336   const MEDCouplingUMesh *um=dynamic_cast<const MEDCouplingUMesh *>(m);
1337   MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm;
1338   int mod=writeFromScratch?2:0;
1339   if(um)
1340     {
1341       MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> mmu=MEDFileUMesh::New();
1342       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n=um->getRenumArrForMEDFileFrmt();
1343       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> n2o=o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples());
1344       f2->renumberCells(o2n->begin(),false);
1345       mmu->setMeshAtLevel(0,const_cast<MEDCouplingUMesh *>(static_cast<const MEDCouplingUMesh *>(f2->getMesh())));
1346       mmu->setRenumFieldArr(0,n2o);
1347       ff->setFieldNoProfileSBT(f2);
1348       mmu->write(fileName,mod);
1349     }
1350   else
1351     throw INTERP_KERNEL::Exception("MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile : only umeshes are dealed in this API for the moment !");
1352   ff->write(fileName,0);
1353 }
1354
1355 void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch)
1356 {
1357   if(!f)
1358     throw INTERP_KERNEL::Exception("MEDLoader::WriteField : input field is NULL !");
1359   f->checkCoherency();
1360   int status=MEDLoaderBase::getStatusOfFile(fileName);
1361   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1362     {
1363       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1364       throw INTERP_KERNEL::Exception(oss.str().c_str());
1365     }
1366   if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
1367     {
1368       MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,true);
1369     }
1370   else
1371     {
1372       std::vector<std::string> meshNames=GetMeshNames(fileName);
1373       if(!f->getMesh())
1374         throw INTERP_KERNEL::Exception("MEDLoader::WriteField : trying to write a field with no mesh !");
1375       std::string fileNameCpp(f->getMesh()->getName());
1376       if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
1377         MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false);
1378       else
1379         {
1380           MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,f->getMesh()->getName().c_str());
1381           const MEDFileMesh *mmPtr(mm);
1382           const MEDFileUMesh *mmuPtr=dynamic_cast<const MEDFileUMesh *>(mmPtr);
1383           if(!mmuPtr)
1384             throw INTERP_KERNEL::Exception("MEDLoader::WriteField : only umeshes are supported now !");
1385           MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f2(f->deepCpy());
1386           MEDCouplingUMesh *m=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1387           if(!m)
1388             throw INTERP_KERNEL::Exception("MEDLoader::WriteField : only umesh in input field supported !");
1389           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n=m->getRenumArrForMEDFileFrmt();
1390           f2->renumberCells(o2n->begin(),false);
1391           m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1392           MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> mread=mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension());
1393           if(f2->getTypeOfField()!=ON_NODES)
1394             {
1395               m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
1396               DataArrayInt *part=0;
1397               bool b=mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part);
1398               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> partSafe(part);
1399               if(!b)
1400                 {
1401                   std::ostringstream oss; oss << "MEDLoader::WriteField : The file \""<< fileName << "\" already contains a mesh named \""<< f->getMesh()->getName() << "\" and this mesh in the file is not compatible (a subpart) with the mesh you intend to write ! This is maybe due to a too strict policy ! Try with to lease it by calling SetCompPolicyForCell !";
1402                   throw INTERP_KERNEL::Exception(oss.str().c_str());
1403                 }
1404               MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> f1ts=MEDFileField1TS::New();
1405               if(part->isIdentity() && part->getNumberOfTuples()==mread->getNumberOfCells())
1406                 f1ts->setFieldNoProfileSBT(f2);
1407               else
1408                 {
1409                   part->setName(f1ts->createNewNameOfPfl().c_str());
1410                   f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1411                 }
1412               f1ts->write(fileName,0);
1413               return ;
1414             }
1415           else
1416             {
1417               DataArrayInt *part=0;
1418               bool b=mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part);
1419               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> partSafe(part);
1420               if(!b)
1421                 {
1422                   std::ostringstream oss; oss << "MEDLoader::WriteField : The file \""<< fileName << "\" already contains a mesh named \""<< f->getMesh()->getName() << "\" and this mesh in the file is not compatible (a subpart regarding nodes) with the mesh you intend to write ! This is maybe due to a too strict epsilon ! Try with to lease it by calling SetEpsilonForNodeComp !";
1423                   throw INTERP_KERNEL::Exception(oss.str().c_str());
1424                 }
1425               MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> f1ts=MEDFileField1TS::New();
1426               if(part->isIdentity() && part->getNumberOfTuples()==mread->getNumberOfNodes())
1427                 f1ts->setFieldNoProfileSBT(f2);
1428               else
1429                 {
1430                   part->setName(f1ts->createNewNameOfPfl().c_str());
1431                   f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1432                 }
1433               f1ts->write(fileName,0);
1434             }
1435         }
1436     }
1437 }
1438
1439 void MEDLoader::WriteFieldDep(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch)
1440 {
1441   WriteField(fileName,f,writeFromScratch);
1442 }
1443
1444 void MEDLoader::WriteFieldUsingAlreadyWrittenMesh(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f)
1445 {
1446   if(!f)
1447     throw INTERP_KERNEL::Exception("MEDLoader::WriteFieldUsingAlreadyWrittenMesh : input field is null !");
1448   f->checkCoherency();
1449   int status=MEDLoaderBase::getStatusOfFile(fileName);
1450   if(status!=MEDLoaderBase::EXIST_RW)
1451     {
1452       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
1453       throw INTERP_KERNEL::Exception(oss.str().c_str());
1454     }
1455   MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> f1ts=MEDFileField1TS::New();
1456   MEDCouplingUMesh *m=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh()));
1457   if(!m)
1458     throw INTERP_KERNEL::Exception("MEDLoader::WriteFieldUsingAlreadyWrittenMesh : only umesh in input field supported !");
1459   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n=m->getRenumArrForMEDFileFrmt();
1460   MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f2(f->deepCpy());
1461   f2->renumberCells(o2n->begin(),false);
1462   f1ts->setFieldNoProfileSBT(f2);
1463   f1ts->write(fileName,0);
1464 }