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