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