Salome HOME
79bc806824f9ed256b744f68cdc8d4209a9f95e0
[tools/medcoupling.git] / src / MEDLoader / MEDLoader.cxx
1 // Copyright (C) 2007-2016  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 "MCAuto.hxx"
36
37 #include "InterpKernelAutoPtr.hxx"
38
39 #include "med.h"
40
41 #include <string>
42 #include <limits>
43 #include <cstring>
44 #include <sstream>
45 #include <fstream>
46 #include <numeric>
47 #include <iterator>
48 #include <algorithm>
49
50
51 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
52   MED_SEG2,
53   MED_SEG3,
54   MED_SEG4,
55   MED_TRIA3,
56   MED_QUAD4,
57   MED_TRIA6,
58   MED_TRIA7,
59   MED_QUAD8,
60   MED_QUAD9,
61   MED_TETRA4,
62   MED_PYRA5,
63   MED_PENTA6,
64   MED_HEXA8,
65   MED_OCTA12,
66   MED_TETRA10,
67   MED_PYRA13,
68   MED_PENTA15,
69   MED_PENTA18,
70   MED_HEXA20,
71   MED_HEXA27,
72   MED_POLYGON,
73   MED_POLYGON2,
74   MED_POLYHEDRON };
75
76 med_geometry_type typmainoeud[1] = { MED_NONE };
77
78 INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1,
79   INTERP_KERNEL::NORM_SEG2,
80   INTERP_KERNEL::NORM_SEG3,
81   INTERP_KERNEL::NORM_SEG4,
82   INTERP_KERNEL::NORM_TRI3,
83   INTERP_KERNEL::NORM_QUAD4,
84   INTERP_KERNEL::NORM_TRI6,
85   INTERP_KERNEL::NORM_TRI7,
86   INTERP_KERNEL::NORM_QUAD8,
87   INTERP_KERNEL::NORM_QUAD9,
88   INTERP_KERNEL::NORM_TETRA4,
89   INTERP_KERNEL::NORM_PYRA5,
90   INTERP_KERNEL::NORM_PENTA6,
91   INTERP_KERNEL::NORM_HEXA8,
92   INTERP_KERNEL::NORM_HEXGP12,
93   INTERP_KERNEL::NORM_TETRA10,
94   INTERP_KERNEL::NORM_PYRA13,
95   INTERP_KERNEL::NORM_PENTA15,
96   INTERP_KERNEL::NORM_PENTA18,
97   INTERP_KERNEL::NORM_HEXA20,
98   INTERP_KERNEL::NORM_HEXA27,
99   INTERP_KERNEL::NORM_POLYGON,
100   INTERP_KERNEL::NORM_QPOLYG,
101   INTERP_KERNEL::NORM_POLYHED };
102
103 med_geometry_type typmai3[34] = { MED_POINT1,//0
104   MED_SEG2,//1
105   MED_SEG3,//2
106   MED_TRIA3,//3
107   MED_QUAD4,//4
108   MED_POLYGON,//5
109   MED_TRIA6,//6
110   MED_TRIA7,//7
111   MED_QUAD8,//8
112   MED_QUAD9,//9
113   MED_SEG4,//10
114   MED_NONE,//11
115   MED_NONE,//12
116   MED_NONE,//13
117   MED_TETRA4,//14
118   MED_PYRA5,//15
119   MED_PENTA6,//16
120   MED_NONE,//17
121   MED_HEXA8,//18
122   MED_NONE,//19
123   MED_TETRA10,//20
124   MED_NONE,//21
125   MED_OCTA12,//22
126   MED_PYRA13,//23
127   MED_NONE,//24
128   MED_PENTA15,//25
129   MED_NONE,//26
130   MED_HEXA27,//27
131   MED_PENTA18,//28
132   MED_NONE,//29
133   MED_HEXA20,//30
134   MED_POLYHEDRON,//31
135   MED_POLYGON2,//32
136   MED_NONE//33
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   med_int meshId=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   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       int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
188       int curNbOfElemF(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));//limitation
189       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 med_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 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       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       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(int nbOfElemCell, int nbOfElemFace, 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=majj;
343   minor=minn;
344   release=rell;
345 }
346
347 /*!
348  * This method sets the epsilon value used for node comparison when trying to buid 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, int& 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   med_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;
392   med_axis_type axisType;
393   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,&spaceDim,&meshDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
397   if(type_maillage!=MED_UNSTRUCTURED_MESH)
398     {
399       std::ostringstream oss; oss << "GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
400       oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
401       throw INTERP_KERNEL::Exception(oss.str().c_str());
402     }
403   // limitation
404   if(nstep!=1)
405     throw INTERP_KERNEL::Exception("GetUMeshGlobalInfo : multisteps on mesh not managed !");
406   med_int numdt,numit;
407   med_float dt;
408   MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
409   // endlimitation
410   std::vector<int> dims;
411   std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
412   med_bool changement,transformation;
413   for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
414     {
415       med_geometry_type curMedType=typmai[i];
416       int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
417       if(curNbOfElemM>0)
418         {
419           INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
420           int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
421           dims.push_back(mdimCell);
422           geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
423         }
424     }
425   int maxLev=*std::max_element(dims.begin(),dims.end());
426   int lowLev=*std::min_element(dims.begin(),dims.end());
427   int nbOfLevels=maxLev-lowLev+1;
428   std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
429   for(std::size_t i=0;i<dims.size();i++)
430     {
431       ret[maxLev-dims[i]].push_back(geoTypes[i]);
432     }
433   numberOfNodes=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
434   return ret;
435 }
436
437 void MEDCoupling::CheckFileForRead(const std::string& fileName)
438 {
439   MEDFileUtilities::CheckFileForRead(fileName);
440 }
441
442 std::vector<std::string> MEDCoupling::GetMeshNames(const std::string& fileName)
443 {
444   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
445   return MEDLoaderNS::getMeshNamesFid(fid);
446 }
447
448 std::vector< std::pair<std::string,std::string> > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName)
449 {
450   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
451   med_int nbFields(MEDnField(fid));
452   std::vector<std::string> fields(nbFields);
453   med_field_type typcha;
454   for(int i=0;i<nbFields;i++)
455     {
456       med_int ncomp(MEDfieldnComponent(fid,i+1));
457       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
458       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
459       INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
460       med_int nbPdt;
461       med_bool localmesh;
462       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
463       INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
464       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
465       std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
466       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
467       if(curFieldName==fieldName)
468         {
469           std::vector< std::pair<std::string,std::string> > ret(ncomp);
470           for(int j=0;j<ncomp;j++)
471             ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
472                 MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
473           return ret;
474         }
475       fields[i]=curFieldName;
476     }
477   std::ostringstream oss; oss << "GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
478   oss << "Possible field names are : " << std::endl;
479   std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
480   throw INTERP_KERNEL::Exception(oss.str().c_str());
481 }
482
483 std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName)
484 {
485   std::vector<std::string> ret;
486   //
487   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
488   med_int nbFields=MEDnField(fid);
489   //
490   med_field_type typcha;
491   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
492   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
493   med_bool localmesh;
494   //
495   for(int i=0;i<nbFields;i++)
496     {
497       med_int ncomp(MEDfieldnComponent(fid,i+1));
498       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
499       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
500       med_int nbPdt;
501       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
502       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
503       std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
504       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
505       if(curFieldName==fieldName)
506         ret.push_back(meshName);
507     }
508   return ret;
509 }
510
511 std::vector<std::string> MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName)
512 {
513   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
514   med_int nfam=MEDnFamily(fid,meshName.c_str());
515   std::vector<std::string> ret(nfam);
516   char nomfam[MED_NAME_SIZE+1];
517   med_int numfam;
518   for(int i=0;i<nfam;i++)
519     {
520       int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
521       med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
522       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
523       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
524       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
525       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
526       MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
527       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
528       ret[i]=cur;
529     }
530   return ret;
531 }
532
533
534 std::vector<std::string> MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName)
535 {
536   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
537   med_int nfam=MEDnFamily(fid,meshName.c_str());
538   std::vector<std::string> ret;
539   char nomfam[MED_NAME_SIZE+1];
540   med_int numfam;
541   for(int i=0;i<nfam;i++)
542     {
543       int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
544       med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
545       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
546       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
547       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
548       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
549       MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
550       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
551       for(int j=0;j<ngro;j++)
552         {
553           std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
554           if(cur2==grpName)
555             ret.push_back(cur);
556         }
557     }
558   return ret;
559 }
560
561 std::vector<std::string> MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName)
562 {
563   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
564   med_int nfam(MEDnFamily(fid,meshName.c_str()));
565   std::vector<std::string> ret;
566   char nomfam[MED_NAME_SIZE+1];
567   med_int numfam;
568   bool found=false;
569   for(int i=0;i<nfam && !found;i++)
570     {
571       int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
572       med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
573       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
574       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
575       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
576       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
577       MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
578       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
579       found=(cur==famName);
580       if(found)
581         for(int j=0;j<ngro;j++)
582           {
583             std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
584             ret.push_back(cur2);
585           }
586     }
587   if(!found)
588     {
589       std::ostringstream oss;
590       oss << "GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
591       throw INTERP_KERNEL::Exception(oss.str().c_str());
592     }
593   return ret;
594 }
595
596
597 std::vector<std::string> MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName)
598 {
599   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
600   med_int nfam=MEDnFamily(fid,meshName.c_str());
601   std::vector<std::string> ret;
602   char nomfam[MED_NAME_SIZE+1];
603   med_int numfam;
604   for(int i=0;i<nfam;i++)
605     {
606       int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
607       med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
608       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
609       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
610       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
611       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
612       MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
613       for(int j=0;j<ngro;j++)
614         {
615           std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
616           if(std::find(ret.begin(),ret.end(),cur)==ret.end())
617             ret.push_back(cur);
618         }
619     }
620   return ret;
621 }
622
623 std::vector<MEDCoupling::TypeOfField> MEDCoupling::GetTypesOfField(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
624 {
625   std::vector<MEDCoupling::TypeOfField> ret;
626   MCAuto<MEDFileAnyTypeFieldMultiTS> fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false));
627   if(fs->getMeshName()!=meshName)
628     {
629       std::ostringstream oss; oss << "GetTypesOfField : The field \"" << fieldName << "\" in file \"" << fileName << "\" is not lying on mesh \"" << meshName << "\"";
630       oss << " The name of the mesh in file is \"" << fs->getMeshName() << "\"!";
631       throw INTERP_KERNEL::Exception(oss.str().c_str());
632     }
633   int nbTS(fs->getNumberOfTS());
634   if(nbTS==0)
635     return ret;
636   for(int i=0;i<nbTS;i++)
637     {
638       MCAuto<MEDFileAnyTypeField1TS> f1ts(fs->getTimeStepAtPos(i));
639       std::vector<MEDCoupling::TypeOfField> tof(f1ts->getTypesOfFieldAvailable());
640       for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=tof.begin();it!=tof.end();it++)
641         if(std::find(ret.begin(),ret.end(),*it)==ret.end())
642           ret.push_back(*it);
643      }
644   // sort ret to put before ON_NODES then ON_CELLS then the remaining.
645   std::vector<MEDCoupling::TypeOfField> ret2;
646   if(std::find(ret.begin(),ret.end(),ON_NODES)!=ret.end())
647     ret2.push_back(ON_NODES);
648   if(std::find(ret.begin(),ret.end(),ON_CELLS)!=ret.end())
649     ret2.push_back(ON_CELLS);
650   for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=ret.begin();it!=ret.end();it++)
651     if(*it!=ON_NODES && *it!=ON_CELLS)
652       ret2.push_back(*it);
653   return ret2;
654 }
655
656 std::vector<std::string> MEDCoupling::GetAllFieldNames(const std::string& fileName)
657 {
658   std::vector<std::string> ret;
659   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
660   med_int nbFields=MEDnField(fid);
661   med_field_type typcha;
662   for(int i=0;i<nbFields;i++)
663     {
664       med_int ncomp(MEDfieldnComponent(fid,i+1));
665       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
666       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
667       INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
668       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
669       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
670       med_int nbPdt;
671       med_bool localmesh;
672       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
673       ret.push_back(std::string(nomcha));
674     }
675   return ret;
676 }
677
678 std::vector<std::string> MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
679 {
680   std::vector<std::string> ret;
681   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
682   med_int nbFields=MEDnField(fid);
683   //
684   med_field_type typcha;
685   char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
686   char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
687   //
688   for(int i=0;i<nbFields;i++)
689     {
690       med_int ncomp(MEDfieldnComponent(fid,i+1));
691       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
692       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
693       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
694       med_int nbPdt;
695       med_bool localmesh;
696       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
697       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
698       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
699       //
700       if(curMeshName==meshName)
701         ret.push_back(curFieldName);
702     }
703   delete [] maa_ass;
704   delete [] nomcha;
705   return ret;
706 }
707
708 std::vector<std::string> MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName)
709 {
710   MEDCoupling::CheckFileForRead(fileName);
711   switch(type)
712   {
713     case ON_CELLS:
714       return GetCellFieldNamesOnMesh(fileName,meshName);
715     case ON_NODES:
716       return GetNodeFieldNamesOnMesh(fileName,meshName);
717     default:
718       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
719   }
720 }
721
722 std::vector<std::string> MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
723 {
724   std::vector<std::string> ret;
725   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
726   med_int nbFields=MEDnField(fid);
727   //
728   med_field_type typcha;
729   //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
730   med_int numdt=0,numo=0;
731   med_float dt=0.0;
732   char pflname[MED_NAME_SIZE+1]="";
733   char locname[MED_NAME_SIZE+1]="";
734   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
735   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
736   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
737   med_bool localmesh;
738   med_int nbPdt;
739   //
740   for(int i=0;i<nbFields;i++)
741     {
742       med_int ncomp(MEDfieldnComponent(fid,i+1));
743       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
744       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
745       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
746       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
747       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
748       int profilesize,nbi;
749       if(curMeshName==meshName)
750         {
751           bool found=false;
752           for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
753             {
754               if(nbPdt>0)
755                 {
756                   MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
757                   med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
758                                                             pflname,&profilesize,locname,&nbi));
759                   if(nbOfVal>0)
760                     {
761                       found=true;
762                       ret.push_back(curFieldName);
763                     }
764                 }
765             }
766         }
767     }
768   return ret;
769 }
770
771 std::vector<std::string> MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
772 {
773   std::vector<std::string> ret;
774   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
775   med_int nbFields=MEDnField(fid);
776   char pflname[MED_NAME_SIZE+1]="";
777   char locname[MED_NAME_SIZE+1]="";
778   //
779   med_field_type typcha;
780   med_int numdt=0,numo=0;
781   med_float dt=0.0;
782   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
783   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
784   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
785   med_bool localmesh;
786   //
787   for(int i=0;i<nbFields;i++)
788     {
789       med_int ncomp(MEDfieldnComponent(fid,i+1));
790       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
791       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
792       med_int nbPdt;
793       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
794       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
795       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
796       if(nbPdt>0)
797         {
798           int profilesize,nbi;
799           MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
800           med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
801                                                     pflname,&profilesize,locname,&nbi));
802           if(curMeshName==meshName && nbOfVal>0)
803             {
804               ret.push_back(curFieldName);
805             }
806         }
807     }
808   return ret;
809 }
810
811 std::vector< std::pair< std::pair<int,int>, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName)
812 {
813   std::vector< std::pair< std::pair<int,int>, double > > ret;
814   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
815   med_int nbFields=MEDnField(fid);
816   //
817   med_field_type typcha;
818   med_int numdt=0,numo=0;
819   med_float dt=0.0;
820   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
821   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
822   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
823   med_bool localmesh;
824   //
825   std::ostringstream oss; oss << "GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
826   for(int i=0;i<nbFields;i++)
827     {
828       med_int ncomp(MEDfieldnComponent(fid,i+1));
829       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
830       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
831       med_int nbPdt;
832       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
833       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
834       if(curFieldName==fieldName)
835         {
836           for(int k=0;k<nbPdt;k++)
837             {
838               MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
839               ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
840             }
841           return ret;
842         }
843       else
844         {
845           oss << "\"" << curFieldName << "\"";
846           if(i!=nbFields-1) oss << ", ";
847         }
848     }
849   oss << " !";
850   throw INTERP_KERNEL::Exception(oss.str().c_str());
851 }
852
853 double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order)
854 {
855   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
856   med_int nbFields=MEDnField(fid);
857   //
858   med_field_type typcha;
859   med_int numdt=0,numo=0;
860   med_float dt=0.0;
861   med_bool local;
862   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
863   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
864   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
865   //
866   bool found=false;
867   bool found2=false;
868   double ret=std::numeric_limits<double>::max();
869   for(int i=0;i<nbFields && !found;i++)
870     {
871       med_int ncomp(MEDfieldnComponent(fid,i+1));
872       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
873       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
874       med_int nbPdt;
875       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt));
876       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
877       if(curFieldName==fieldName)
878         {
879           found=true;
880           for(int k=0;k<nbPdt;k++)
881             {
882               MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
883               if(numdt==iteration && numo==order)
884                 {
885                   found2=true;
886                   ret=dt;
887                 }
888             }
889         }
890     }
891   if(!found || !found2)
892     {
893       std::ostringstream oss;
894       oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
895       throw INTERP_KERNEL::Exception(oss.str().c_str());
896     }
897   return ret;
898 }
899
900 std::vector< std::pair<int,int> > MEDCoupling::GetFieldIterations(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName)
901 {
902   MEDCoupling::CheckFileForRead(fileName);
903   switch(type)
904   {
905     case ON_CELLS:
906       return GetCellFieldIterations(fileName,meshName,fieldName);
907     case ON_NODES:
908       return GetNodeFieldIterations(fileName,meshName,fieldName);
909     default:
910       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
911   }
912 }
913
914 std::vector< std::pair<int,int> > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
915 {
916   std::string meshNameCpp(meshName);
917   std::vector< std::pair<int,int> > ret;
918   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
919   med_int nbFields=MEDnField(fid);
920   //
921   med_field_type typcha;
922   med_int numdt=0,numo=0;
923   med_float dt=0.0;
924   char pflname[MED_NAME_SIZE+1]="";
925   char locname[MED_NAME_SIZE+1]="";
926   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
927   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
928   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
929   med_bool localmesh;
930   //
931   std::ostringstream oss; oss << "GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
932   std::set<std::string> s2;
933   for(int i=0;i<nbFields;i++)
934     {
935       med_int ncomp(MEDfieldnComponent(fid,i+1));
936       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
937       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
938       med_int nbPdt;
939       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
940       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
941       if(curFieldName==fieldName)
942         {
943           bool found=false;
944           for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
945             {
946               for(int k=0;k<nbPdt;k++)
947                 {
948                   int profilesize,nbi;
949                   MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
950                   med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
951                                                             pflname,&profilesize,locname,&nbi));
952                   std::string maa_ass_cpp(maa_ass);
953                   if(nbOfVal>0)
954                     {
955                       if(meshNameCpp==maa_ass_cpp)
956                         {
957                           found=true;
958                           ret.push_back(std::make_pair(numdt,numo));
959                         }
960                       else
961                         s2.insert(maa_ass_cpp);
962                     }
963                 }
964             }
965         }
966       else
967         {
968           oss << "\"" << curFieldName << "\"";
969           if(i!=nbFields-1) oss << ", ";
970         }
971     }
972   if(ret.empty())
973     {
974       if(!s2.empty())
975         {
976           oss << ". Cell Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
977           std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
978         }
979       oss << " !";
980       throw INTERP_KERNEL::Exception(oss.str().c_str());
981     }
982   return ret;
983 }
984
985 std::vector< std::pair<int,int> > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
986 {
987   std::string meshNameCpp(meshName);
988   std::vector< std::pair<int,int> > ret;
989   MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
990   med_int nbFields=MEDnField(fid);
991   //
992   med_field_type typcha;
993   med_int numdt=0,numo=0;
994   med_float dt=0.0;
995   char pflname[MED_NAME_SIZE+1]="";
996   char locname[MED_NAME_SIZE+1]="";
997   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
998   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
999   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1000   med_bool localmesh;
1001   //
1002   std::ostringstream oss; oss << "GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
1003   std::set<std::string> s2;
1004   for(int i=0;i<nbFields;i++)
1005     {
1006       med_int ncomp(MEDfieldnComponent(fid,i+1));
1007       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1008       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1009       med_int nbPdt;
1010       MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
1011       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1012       if(curFieldName==fieldName)
1013         {
1014           for(int k=0;k<nbPdt;k++)
1015             {
1016               int profilesize,nbi;
1017               MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
1018               med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1019                                                         pflname,&profilesize,locname,&nbi));
1020               std::string maa_ass_cpp(maa_ass);
1021               if(nbOfVal>0)
1022                 {
1023                   if(meshNameCpp==maa_ass_cpp)
1024                     { ret.push_back(std::make_pair(numdt,numo)); }
1025                   else
1026                     s2.insert(maa_ass_cpp);
1027                 }
1028             }
1029         }
1030       else
1031         {
1032           oss << "\"" << curFieldName << "\"";
1033           if(i!=nbFields-1) oss << ", ";
1034         }
1035     }
1036   if(ret.empty())
1037     {
1038       if(!s2.empty())
1039         {
1040           oss << ". Node Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1041           std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1042         }
1043       oss << " !";
1044       throw INTERP_KERNEL::Exception(oss.str().c_str());
1045     }
1046   return ret;
1047 }
1048
1049 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1050 {
1051   MEDCoupling::CheckFileForRead(fileName);
1052   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1053   MEDFileMesh *mmPtr(mm);
1054   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1055   if(mmuPtr)
1056     return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1057   MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1058   if(mmcPtr)
1059     {
1060       const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1061       return const_cast<MEDCouplingCMesh *>(ret);
1062     }
1063   MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1064   if(mmc2Ptr)
1065     {
1066       const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1067       return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1068     }
1069   std::ostringstream oss; oss << "ReadMeshFromFile : The mesh \"" << meshName << "\" in file \"" << fileName << "\" has not a recognized type !";
1070   throw INTERP_KERNEL::Exception(oss.str().c_str());
1071 }
1072
1073 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1074 {
1075   MEDCoupling::CheckFileForRead(fileName);
1076   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1077   MEDFileMesh *mmPtr(mm);
1078   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1079   if(mmuPtr)
1080     return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1081   MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1082   if(mmcPtr)
1083     {
1084       const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1085       return const_cast<MEDCouplingCMesh *>(ret);
1086     }
1087   MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1088   if(mmc2Ptr)
1089     {
1090       const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1091       return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1092     }
1093   std::ostringstream oss; oss << "ReadMeshFromFile (2) : The first mesh \"" << mm->getName() << "\" in file \"" << fileName << "\" has not a recognized type !";
1094   throw INTERP_KERNEL::Exception(oss.str().c_str());
1095 }
1096
1097 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1098 {
1099   MEDCoupling::CheckFileForRead(fileName);
1100   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1101   MEDFileMesh *mmPtr(mm);
1102   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1103   if(!mmuPtr)
1104     {
1105       std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !";
1106       throw INTERP_KERNEL::Exception(oss.str().c_str());
1107     }
1108   return  mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1109 }
1110
1111 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1112 {
1113   MEDCoupling::CheckFileForRead(fileName);
1114   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1115   MEDFileMesh *mmPtr(mm);
1116   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1117   if(!mmuPtr)
1118     {
1119       std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1120       throw INTERP_KERNEL::Exception(oss.str().c_str());
1121     }
1122   return  mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1123 }
1124
1125 int MEDCoupling::ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName)
1126 {
1127   MEDCoupling::CheckFileForRead(fileName);
1128   std::vector<int> poss;
1129   return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
1130 }
1131
1132 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& fams)
1133 {
1134   MEDCoupling::CheckFileForRead(fileName);
1135   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1136   MEDFileMesh *mmPtr(mm);
1137   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1138   if(!mmuPtr)
1139     {
1140       std::ostringstream oss; oss << "ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1141       throw INTERP_KERNEL::Exception(oss.str().c_str());
1142     }
1143   return mmuPtr->getFamilies(meshDimRelToMax,fams,true);
1144 }
1145
1146 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& grps)
1147 {
1148   MEDCoupling::CheckFileForRead(fileName);
1149   MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1150   MEDFileMesh *mmPtr(mm);
1151   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1152   if(!mmuPtr)
1153     {
1154       std::ostringstream oss; oss << "ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1155       throw INTERP_KERNEL::Exception(oss.str().c_str());
1156     }
1157   return mmuPtr->getGroups(meshDimRelToMax,grps,true);
1158 }
1159
1160 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName)
1161 {
1162   std::vector<std::string> fieldNames(GetAllFieldNames(fileName));
1163   std::size_t sz(fieldNames.size());
1164   if(sz==0)
1165     {
1166       std::ostringstream oss;
1167       oss << "The file \"" << fileName << "\" contains no field !";
1168       throw INTERP_KERNEL::Exception(oss.str());
1169     }
1170   if(sz>1)
1171     {
1172       std::ostringstream oss;
1173       oss << "In file \"" << fileName << "\" there are more than one field !" << std::endl;
1174       oss << "You are invited to use ReadField(fileName, fieldName) instead to avoid misleading concerning field you want to read !" << std::endl;
1175       oss << "For information, fields available are :" << std::endl;
1176       for(std::vector<std::string>::const_iterator it=fieldNames.begin();it!=fieldNames.end();it++)
1177         oss << " - \"" << *it << "\"" << std::endl;
1178       throw INTERP_KERNEL::Exception(oss.str());
1179     }
1180   return ReadField(fileName,fieldNames[0]);
1181 }
1182
1183 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName)
1184 {
1185   std::vector< std::pair< std::pair<int,int>, double> > iterations(GetAllFieldIterations(fileName,fieldName));
1186   std::size_t sz(iterations.size());
1187   if(sz==0)
1188     {
1189       std::ostringstream oss;
1190       oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with no time steps !";
1191       throw INTERP_KERNEL::Exception(oss.str());
1192     }
1193   if(sz>1)
1194     {
1195       std::ostringstream oss;
1196       oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with more than one time steps !" << std::endl;
1197       oss << "You are invited to use ReadField(fileName, fieldName, iteration, order) instead to avoid misleading concerning time steps." << std::endl;
1198       oss << "For information, time steps available for field \"" << fieldName << "\" are :" << std::endl;
1199       for(std::vector< std::pair< std::pair<int,int>, double> >::const_iterator it=iterations.begin();it!=iterations.end();it++)
1200         oss << " - " << (*it).first.first << ", " << (*it).first.second << " (" << (*it).second << ")" << std::endl;
1201       throw INTERP_KERNEL::Exception(oss.str());
1202     }
1203   return ReadField(fileName,fieldName,iterations[0].first.first,iterations[0].first.second);
1204 }
1205
1206 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order)
1207 {
1208   MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1209   MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
1210   {
1211     MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1212     if(f1.isNotNull())
1213       {
1214         MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f1->field(mesh));
1215         return MEDCoupling::DynamicCast<MEDCouplingFieldDouble,MEDCouplingField>(ret);
1216       }
1217   }
1218   {
1219     MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
1220     if(f1.isNotNull())
1221       {
1222         MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(f1->field(mesh));
1223         return MEDCoupling::DynamicCast<MEDCouplingFieldInt,MEDCouplingField>(ret);
1224       }
1225   }
1226   {
1227     MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1228     if(f1.isNotNull())
1229       {
1230         MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(f1->field(mesh));
1231         return MEDCoupling::DynamicCast<MEDCouplingFieldFloat,MEDCouplingField>(ret);
1232       }
1233   }
1234   throw INTERP_KERNEL::Exception("MEDCoupling::ReadField : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1235   //MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
1236   //MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f->field(mesh));
1237   //return ret;
1238 }
1239
1240 MCAuto<MEDCoupling::MEDCouplingFieldDouble> MEDCoupling::ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1241 {
1242   MEDCoupling::CheckFileForRead(fileName);
1243   switch(type)
1244   {
1245     case ON_CELLS:
1246       return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1247     case ON_NODES:
1248       return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1249     case ON_GAUSS_PT:
1250       return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1251     case ON_GAUSS_NE:
1252       return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1253     default:
1254       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
1255   }
1256 }
1257
1258 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsOnSameMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1259                                                                                      const std::vector<std::pair<int,int> >& its)
1260 {
1261   if(its.empty())
1262     return std::vector<MEDCoupling::MEDCouplingFieldDouble *>();
1263   MEDCoupling::CheckFileForRead(fileName);
1264   std::vector<MEDCoupling::MEDCouplingFieldDouble *> ret(its.size());
1265   std::vector< MCAuto<MEDCouplingFieldDouble> > retSafe(its.size());
1266   if(its.empty())
1267     return ret;
1268   //Retrieving mesh of rank 0 and field on rank 0 too.
1269   MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1270   MEDFileMesh *mmPtr(mm);
1271   MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1272   if(!mmuPtr)
1273     throw INTERP_KERNEL::Exception("ReadFieldsOnSameMesh : only unstructured mesh is managed !");
1274   MCAuto<MEDCouplingUMesh> m=mmuPtr->getMeshAtLevel(meshDimRelToMax);
1275   const DataArrayInt *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax);
1276   MCAuto<MEDCouplingUMesh> m2(m->clone(true));
1277   if(o2n)
1278     m2->renumberCells(o2n->begin(),true);
1279   int i=0;
1280   for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1281     {
1282       MCAuto<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second);
1283       MCAuto<MEDCouplingFieldDouble> retElt=ff->getFieldOnMeshAtLevel(type,m);
1284       if(o2n)
1285         retElt->renumberCells(o2n->begin(),true);
1286       retElt->setMesh(m2);
1287       retSafe[i]=retElt;
1288     }
1289   i=0;
1290   for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1291     ret[i]=retSafe[i].retn();
1292   return ret;
1293 }
1294
1295 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsCellOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1296                                                                                       const std::vector<std::pair<int,int> >& its)
1297 {
1298   return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its);
1299 }
1300
1301 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsNodeOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1302                                                                                       const std::vector<std::pair<int,int> >& its)
1303 {
1304   return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its);
1305 }
1306
1307 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1308                                                                                        const std::vector<std::pair<int,int> >& its)
1309 {
1310   return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its);
1311 }
1312
1313 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussNEOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1314                                                                                          const std::vector<std::pair<int,int> >& its)
1315 {
1316   return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
1317 }
1318
1319 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1320 {
1321   MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1322   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1323   MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1324   MEDFileMesh *mPtr(mm);
1325   MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1326   MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_CELLS,m));
1327   if(muPtr)
1328     {
1329       const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1330       if(num)
1331         ret->renumberCells(num->begin());
1332     }
1333   return ret.retn();
1334 }
1335
1336 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1337 {
1338   MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1339   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1340   MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1341   MEDFileMesh *mPtr(mm);
1342   MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m));
1343   MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1344   if(ff->getPflsReallyUsed().empty())
1345     {
1346       if(muPtr)
1347         {
1348           const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1349           if(num)
1350             ret->renumberCells(num->begin());
1351         }
1352     }
1353   else
1354     {
1355       DataArrayInt *pfl=0,*arr2=0;
1356       MCAuto<DataArrayDouble> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl));
1357       MCAuto<DataArrayInt> pflSafe(pfl);
1358       MCAuto<DataArrayInt> mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end()));
1359       MCAuto<MEDCouplingUMesh> mzip(static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2)));
1360       MCAuto<DataArrayInt> arr2Safe(arr2);
1361       MCAuto<DataArrayInt> arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes()));
1362       MCAuto<DataArrayInt> pflSorted(pflSafe->deepCopy()); pflSorted->sort(true);
1363       if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
1364         throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !");
1365       if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
1366         {
1367           MCAuto<DataArrayInt> o2n2(pflSafe->checkAndPreparePermutation());
1368           MCAuto<DataArrayInt> n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples()));
1369           mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
1370           arr->setName("");
1371           ret->setArray(arr);
1372         }
1373       ret->setMesh(mzip);
1374     }
1375   return ret.retn();
1376 }
1377
1378 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1379 {
1380   MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1381   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1382   MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1383   MEDFileMesh *mPtr(mm);
1384   MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1385   MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_PT,m));
1386   if(muPtr)
1387     {
1388       const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1389       if(num)
1390         ret->renumberCells(num->begin());
1391     }
1392   return ret.retn();
1393 }
1394
1395 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1396 {
1397   MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1398   MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1399   MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1400   MEDFileMesh *mPtr(mm);
1401   MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1402   MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_NE,m));
1403   if(muPtr)
1404     {
1405       const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1406       if(num)
1407         ret->renumberCells(num->begin());
1408     }
1409   return ret.retn();
1410 }
1411
1412 void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch)
1413 {
1414   if(!mesh)
1415     throw INTERP_KERNEL::Exception("WriteMesh : input mesh is null !");
1416   const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(mesh));
1417   if(um)
1418     {
1419       WriteUMesh(fileName,um,writeFromScratch);
1420       return ;
1421     }
1422   int mod=writeFromScratch?2:0;
1423   const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(mesh));
1424   if(um2)
1425     {
1426       MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1427       AssignStaticWritePropertiesTo(*mmu);
1428       mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1429       mmu->write(fileName,mod);
1430       return ;
1431     }
1432   const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(mesh));
1433   if(um3)
1434     {
1435       MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1436       AssignStaticWritePropertiesTo(*mmc);
1437       mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1438       mmc->write(fileName,mod);
1439       return ;
1440     }
1441   const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(mesh));
1442   if(um4)
1443     {
1444       MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1445       AssignStaticWritePropertiesTo(*mmc);
1446       mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1447       mmc->write(fileName,mod);
1448       return ;
1449     }
1450   throw INTERP_KERNEL::Exception("WriteMesh : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1451 }
1452
1453 void MEDCoupling::WriteUMesh(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1454 {
1455   if(!mesh)
1456     throw INTERP_KERNEL::Exception("WriteUMesh : input mesh is null !");
1457   int mod=writeFromScratch?2:0;
1458   MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1459   AssignStaticWritePropertiesTo(*m);
1460   MCAuto<MEDCouplingUMesh> mcpy(static_cast<MEDCouplingUMesh *>(mesh->deepCopy()));
1461   m->setMeshAtLevel(0,mcpy,true);
1462   m->write(fileName,mod);
1463 }
1464
1465 void MEDCoupling::WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1466 {
1467   WriteUMesh(fileName,mesh,writeFromScratch);
1468 }
1469
1470 void MEDCoupling::WriteUMeshesPartition(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1471 {
1472   std::string meshName(meshNameC);
1473   if(meshName.empty())
1474     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 !");
1475   int status=MEDLoaderBase::getStatusOfFile(fileName);
1476   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1477     {
1478       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1479       throw INTERP_KERNEL::Exception(oss.str().c_str());
1480     }
1481   MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1482   AssignStaticWritePropertiesTo(*m);
1483   m->setGroupsFromScratch(0,meshes,true);
1484   m->setName(meshNameC);
1485   int mod=writeFromScratch?2:0;
1486   m->write(fileName,mod);
1487 }
1488
1489 void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1490 {
1491   WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch);
1492 }
1493
1494 void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1495 {
1496   int mod=writeFromScratch?2:0;
1497   MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1498   AssignStaticWritePropertiesTo(*m);
1499   m->setMeshes(meshes,true);
1500   m->write(fileName,mod);
1501 }
1502
1503 void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1504 {
1505   MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New());
1506   AssignStaticWritePropertiesTo(*ff);
1507   MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1508   const MEDCouplingMesh *m(f2->getMesh());
1509   const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(m));
1510   const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(m));
1511   const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(m));
1512   const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(m));
1513   MCAuto<MEDFileMesh> mm;
1514   int mod=writeFromScratch?2:0;
1515   if(um)
1516     {
1517       MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1518       AssignStaticWritePropertiesTo(*mmu);
1519       MCAuto<DataArrayInt> o2n(um->getRenumArrForMEDFileFrmt());
1520       MCAuto<DataArrayInt> n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples()));
1521       f2->renumberCells(o2n->begin(),false);
1522       mmu->setMeshAtLevel(0,const_cast<MEDCouplingUMesh *>(static_cast<const MEDCouplingUMesh *>(f2->getMesh())));
1523       mmu->setRenumFieldArr(0,n2o);
1524       ff->setFieldNoProfileSBT(f2);
1525       mmu->write(fileName,mod);
1526     }
1527   else if(um2)
1528     {
1529       MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1530       AssignStaticWritePropertiesTo(*mmu);
1531       mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1532       ff->setFieldNoProfileSBT(f2);
1533       mmu->write(fileName,mod);
1534     }
1535   else if(um3)
1536     {
1537       MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1538       AssignStaticWritePropertiesTo(*mmc);
1539       mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1540       ff->setFieldNoProfileSBT(f2);
1541       mmc->write(fileName,mod);
1542     }
1543   else if(um4)
1544     {
1545       MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1546       AssignStaticWritePropertiesTo(*mmc);
1547       mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1548       ff->setFieldNoProfileSBT(f2);
1549       mmc->write(fileName,mod);
1550     }
1551   else
1552     throw INTERP_KERNEL::Exception("MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1553   ff->write(fileName,0);
1554 }
1555
1556 void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1557 {
1558   if(!f)
1559     throw INTERP_KERNEL::Exception("WriteField : input field is NULL !");
1560   f->checkConsistencyLight();
1561   int status=MEDLoaderBase::getStatusOfFile(fileName);
1562   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1563     {
1564       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1565       throw INTERP_KERNEL::Exception(oss.str().c_str());
1566     }
1567   if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
1568     {
1569       MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,true);
1570     }
1571   else
1572     {
1573       std::vector<std::string> meshNames=GetMeshNames(fileName);
1574       if(!f->getMesh())
1575         throw INTERP_KERNEL::Exception("WriteField : trying to write a field with no mesh !");
1576       std::string fileNameCpp(f->getMesh()->getName());
1577       if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
1578         MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false);
1579       else
1580         {
1581           MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str()));
1582           AssignStaticWritePropertiesTo(*mm);
1583           const MEDFileMesh *mmPtr(mm);
1584           const MEDFileUMesh *mmuPtr=dynamic_cast<const MEDFileUMesh *>(mmPtr);
1585           if(!mmuPtr)
1586             throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !");
1587           MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1588           MEDCouplingUMesh *m=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1589           if(!m)
1590             throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !");
1591           MCAuto<DataArrayInt> o2n=m->getRenumArrForMEDFileFrmt();
1592           f2->renumberCells(o2n->begin(),false);
1593           m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1594           MCAuto<MEDCouplingUMesh> mread=mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension());
1595           if(f2->getTypeOfField()!=ON_NODES)
1596             {
1597               m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
1598               DataArrayInt *part=0;
1599               bool b=mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part);
1600               MCAuto<DataArrayInt> partSafe(part);
1601               if(!b)
1602                 {
1603                   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 !";
1604                   throw INTERP_KERNEL::Exception(oss.str().c_str());
1605                 }
1606               MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1607               AssignStaticWritePropertiesTo(*f1ts);
1608               if(part->isIota(mread->getNumberOfCells()))
1609                 f1ts->setFieldNoProfileSBT(f2);
1610               else
1611                 {
1612                   part->setName(f1ts->createNewNameOfPfl().c_str());
1613                   f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1614                 }
1615               f1ts->write(fileName,0);
1616               return ;
1617             }
1618           else
1619             {
1620               DataArrayInt *part=0;
1621               bool b=mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part);
1622               MCAuto<DataArrayInt> partSafe(part);
1623               if(!b)
1624                 {
1625                   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 !";
1626                   throw INTERP_KERNEL::Exception(oss.str().c_str());
1627                 }
1628               MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1629               AssignStaticWritePropertiesTo(*f1ts);
1630               if(part->isIota(mread->getNumberOfNodes()))
1631                 f1ts->setFieldNoProfileSBT(f2);
1632               else
1633                 {
1634                   part->setName(f1ts->createNewNameOfPfl().c_str());
1635                   f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1636                 }
1637               f1ts->write(fileName,0);
1638             }
1639         }
1640     }
1641 }
1642
1643 void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1644 {
1645   WriteField(fileName,f,writeFromScratch);
1646 }
1647
1648 void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f)
1649 {
1650   if(!f)
1651     throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !");
1652   f->checkConsistencyLight();
1653   int status=MEDLoaderBase::getStatusOfFile(fileName);
1654   if(status!=MEDLoaderBase::EXIST_RW)
1655     {
1656       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
1657       throw INTERP_KERNEL::Exception(oss.str().c_str());
1658     }
1659   MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1660   AssignStaticWritePropertiesTo(*f1ts);
1661   MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh())));
1662   if(m)
1663     {
1664       MCAuto<DataArrayInt> o2n(m->getRenumArrForMEDFileFrmt());
1665       MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1666       f2->renumberCells(o2n->begin(),false);
1667       f1ts->setFieldNoProfileSBT(f2);
1668     }
1669   else
1670     f1ts->setFieldNoProfileSBT(f);
1671   f1ts->write(fileName,0);
1672 }