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