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