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