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