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