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