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