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