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