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