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