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