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