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