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