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