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