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