]> SALOME platform Git repositories - modules/med.git/blob - src/MEDLoader/MEDLoader.cxx
Salome HOME
0d1d57ce0961f03daf8c408d634e03d7b48d47f0
[modules/med.git] / src / MEDLoader / MEDLoader.cxx
1 // Copyright (C) 2007-2012  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
20 #include "MEDLoader.hxx"
21 #include "MEDLoaderBase.hxx"
22 #include "MEDFileUtilities.hxx"
23 #include "CellModel.hxx"
24 #include "MEDCouplingUMesh.hxx"
25 #include "MEDCouplingMemArray.hxx"
26 #include "MEDCouplingFieldDouble.hxx"
27 #include "MEDCouplingGaussLocalization.hxx"
28 #include "MEDCouplingAutoRefCountObjectPtr.hxx"
29
30 #include "InterpKernelAutoPtr.hxx"
31
32 #include "med.h"
33
34 #include <string>
35 #include <limits>
36 #include <cstring>
37 #include <sstream>
38 #include <fstream>
39 #include <numeric>
40 #include <iterator>
41 #include <algorithm>
42
43 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
44                                                    MED_SEG2,
45                                                    MED_SEG3,
46                                                    MED_SEG4,
47                                                    MED_TRIA3,
48                                                    MED_QUAD4,
49                                                    MED_TRIA6,
50                                                    MED_TRIA7,
51                                                    MED_QUAD8,
52                                                    MED_QUAD9,
53                                                    MED_TETRA4,
54                                                    MED_PYRA5,
55                                                    MED_PENTA6,
56                                                    MED_HEXA8,
57                                                    MED_OCTA12,
58                                                    MED_TETRA10,
59                                                    MED_PYRA13,
60                                                    MED_PENTA15,
61                                                    MED_HEXA20,
62                                                    MED_HEXA27,
63                                                    MED_POLYGON,
64                                                    MED_POLYHEDRON };
65
66 med_geometry_type typmainoeud[1] = { MED_NONE };
67
68 INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1,
69                                                                     INTERP_KERNEL::NORM_SEG2,
70                                                                     INTERP_KERNEL::NORM_SEG3,
71                                                                     INTERP_KERNEL::NORM_SEG4,
72                                                                     INTERP_KERNEL::NORM_TRI3,
73                                                                     INTERP_KERNEL::NORM_QUAD4,
74                                                                     INTERP_KERNEL::NORM_TRI6,
75                                                                     INTERP_KERNEL::NORM_TRI7,
76                                                                     INTERP_KERNEL::NORM_QUAD8,
77                                                                     INTERP_KERNEL::NORM_QUAD9,
78                                                                     INTERP_KERNEL::NORM_TETRA4,
79                                                                     INTERP_KERNEL::NORM_PYRA5,
80                                                                     INTERP_KERNEL::NORM_PENTA6,
81                                                                     INTERP_KERNEL::NORM_HEXA8,
82                                                                     INTERP_KERNEL::NORM_HEXGP12,
83                                                                     INTERP_KERNEL::NORM_TETRA10,
84                                                                     INTERP_KERNEL::NORM_PYRA13,
85                                                                     INTERP_KERNEL::NORM_PENTA15,
86                                                                     INTERP_KERNEL::NORM_HEXA20,
87                                                                     INTERP_KERNEL::NORM_HEXA27,
88                                                                     INTERP_KERNEL::NORM_POLYGON,
89                                                                     INTERP_KERNEL::NORM_POLYHED };
90
91 med_geometry_type typmai3[34] = { MED_POINT1,//0
92                                   MED_SEG2,//1
93                                   MED_SEG3,//2
94                                   MED_TRIA3,//3
95                                   MED_QUAD4,//4
96                                   MED_POLYGON,//5
97                                   MED_TRIA6,//6
98                                   MED_TRIA7,//7
99                                   MED_QUAD8,//8
100                                   MED_QUAD9,//9
101                                   MED_SEG4,//10
102                                   MED_NONE,//11
103                                   MED_NONE,//12
104                                   MED_NONE,//13
105                                   MED_TETRA4,//14
106                                   MED_PYRA5,//15
107                                   MED_PENTA6,//16
108                                   MED_NONE,//17
109                                   MED_HEXA8,//18
110                                   MED_NONE,//19
111                                   MED_TETRA10,//20
112                                   MED_NONE,//21
113                                   MED_OCTA12,//22
114                                   MED_PYRA13,//23
115                                   MED_NONE,//24
116                                   MED_PENTA15,//25
117                                   MED_NONE,//26
118                                   MED_HEXA27,//27
119                                   MED_NONE,//28
120                                   MED_NONE,//29
121                                   MED_HEXA20,//30
122                                   MED_POLYHEDRON,//31
123                                   MED_NONE,//32
124                                   MED_NONE//33
125 };
126
127 double MEDLoader::_EPS_FOR_NODE_COMP=1.e-12;
128
129 int MEDLoader::_COMP_FOR_CELL=0;
130
131 int MEDLoader::_TOO_LONG_STR=0;
132
133 using namespace ParaMEDMEM;
134
135 /// @cond INTERNAL
136
137 namespace MEDLoaderNS
138 {
139   class FieldPerTypeCopier
140   {
141   public:
142     FieldPerTypeCopier(double *ptr):_ptr(ptr) { }
143     void operator()(const MEDLoader::MEDFieldDoublePerCellType& elt) { _ptr=std::copy(elt.getArray(),elt.getArray()+elt.getNbOfValues(),_ptr); }
144   private:
145     double *_ptr;
146   };
147  
148   class ConnReaderML
149   {
150   public:
151     ConnReaderML(const int *c, int val):_conn(c),_val(val) { }
152     bool operator() (const int& pos) { return _conn[pos]!=_val; }
153   private:
154     const int *_conn;
155     int _val;
156   };
157   
158   std::vector<std::string> getMeshNamesFid(med_idt fid);
159   void readFieldDoubleDataInMedFile(const char *fileName, const char *meshName, const char *fieldName,
160                                     int iteration, int order, ParaMEDMEM::TypeOfField typeOfOutField,
161                                     std::list<MEDLoader::MEDFieldDoublePerCellType>& field,
162                                     double& time, std::vector<std::string>& infos);
163   std::vector<int> getIdsFromFamilies(const char *fileName, const char *meshName, const std::vector<std::string>& fams);
164   std::vector<int> getIdsFromGroups(const char *fileName, const char *meshName, const std::vector<std::string>& grps);
165   med_int getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception);
166   void dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity);
167   int readUMeshDimFromFile(const char *fileName, const char *meshName, std::vector<int>& possibilities);
168   void readUMeshDataInMedFile(med_idt fid, med_int meshId, DataArrayDouble *&coords, std::list<MEDLoader::MEDConnOfOneElemType>& conn, std::string& desc);
169   int buildMEDSubConnectivityOfOneType(const std::vector<const DataArrayInt *>& conn, const std::vector<const DataArrayInt *>& connIndex, const std::vector<const DataArrayInt *>& families, INTERP_KERNEL::NormalizedCellType type,
170                                        std::vector<int>& conn4MEDFile, std::vector<int>& connIndex4MEDFile, std::vector<int>& connIndexRk24MEDFile,
171                                        std::vector<int>& fam4MEDFile, std::vector<int>& renumber);
172   MEDCouplingUMesh *readUMeshFromFileLev1(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<int>& ids,
173                                           const std::vector<INTERP_KERNEL::NormalizedCellType>& typesToKeep, unsigned& meshDimExtract, int *&cellRenum) throw(INTERP_KERNEL::Exception);
174   void tradMEDFileCoreFrmt2MEDCouplingUMesh(const std::list<MEDLoader::MEDConnOfOneElemType>& medConnFrmt,
175                                             const std::vector<int>& familiesToKeep,
176                                             DataArrayInt* &conn,
177                                             DataArrayInt* &connIndex,
178                                             int *&cellRenum);
179   ParaMEDMEM::DataArrayDouble *buildArrayFromRawData(const std::list<MEDLoader::MEDFieldDoublePerCellType>& fieldPerType,
180                                                      const std::vector<std::string>& infos);
181   int buildMEDSubConnectivityOfOneTypesPolyg(const std::vector<const DataArrayInt *>& conn, const std::vector<const DataArrayInt *>& connIndex, const std::vector<const DataArrayInt *>& families,
182                                              std::vector<int>& conn4MEDFile, std::vector<int>& connIndex4MEDFile, std::vector<int>& fam4MEDFile, std::vector<int>& renumber);
183   int buildMEDSubConnectivityOfOneTypesPolyh(const std::vector<const DataArrayInt *>&conn, const std::vector<const DataArrayInt *>& connIndex, const std::vector<const DataArrayInt *>& families,
184                                              std::vector<int>& conn4MEDFile, std::vector<int>& connIndex4MEDFile, std::vector<int>& connIndexRk24MEDFile,
185                                              std::vector<int>& fam4MEDFile, std::vector<int>& renumber);
186   int buildMEDSubConnectivityOfOneTypeStaticTypes(const std::vector<const DataArrayInt *>& conn, const std::vector<const DataArrayInt *>& connIndex, const std::vector<const DataArrayInt *>& families,
187                                                   INTERP_KERNEL::NormalizedCellType type, std::vector<int>& conn4MEDFile, std::vector<int>& fam4MEDFile, std::vector<int>& renumber);
188   ParaMEDMEM::MEDCouplingFieldDouble *readFieldDoubleLev1(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order,
189                                                           ParaMEDMEM::TypeOfField typeOfOutField) throw(INTERP_KERNEL::Exception);
190   ParaMEDMEM::MEDCouplingFieldDouble *readFieldDoubleLev2(const char *fileName, ParaMEDMEM::TypeOfField typeOfOutField, unsigned meshDim, const int *renumCell, const ParaMEDMEM::MEDCouplingUMesh *mesh,
191                                                           const std::vector<std::string>& infos, const char *fieldName, int iteration, int order, double time,
192                                                           std::list<MEDLoader::MEDFieldDoublePerCellType>& fieldPerCellType) throw(INTERP_KERNEL::Exception);
193   med_idt appendFieldSimpleAtt(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, med_int& numdt, med_int& numo, med_float& dt);
194   void appendFieldDirectly(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f);
195   void appendNodeProfileField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, const int *thisMeshNodeIds);
196   void appendCellProfileField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, const int *thisMeshCellIds);
197   void appendNodeElementProfileField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, const int *thisMeshCellIds);
198   void prepareCellFieldDoubleForWriting(const ParaMEDMEM::MEDCouplingFieldDouble *f, const int *cellIds, std::list<MEDLoader::MEDFieldDoublePerCellType>& split);
199   void fillGaussDataOnField(const char *fileName, const std::list<MEDLoader::MEDFieldDoublePerCellType>& data, MEDCouplingFieldDouble *f);
200   void writeUMeshesDirectly(const char *fileName, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& mesh, const std::vector<const DataArrayInt *>& families, bool forceFromScratch, bool &isRenumbering);
201   void writeUMeshesPartitionDirectly(const char *fileName, const char *meshName, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool forceFromScratch);
202   void writeFieldAndMeshDirectly(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool forceFromScratch);
203   void writeFieldTryingToFitExistingMesh(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f);
204 }
205
206 /// @endcond
207
208 /*!
209  * 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.
210  */
211 void MEDLoader::setEpsilonForNodeComp(double val) throw(INTERP_KERNEL::Exception)
212 {
213   _EPS_FOR_NODE_COMP=val;
214 }
215
216 /*!
217  * 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.
218  */
219 void MEDLoader::setCompPolicyForCell(int val) throw(INTERP_KERNEL::Exception)
220 {
221   _COMP_FOR_CELL=val;
222 }
223
224 /*!
225  * This method set the behaviour of MEDLoader when a too long string is seen in datastructure before copy it in MED file.
226  * By default (0) an exception is thrown. If equal to 1 a warning is emitted in std_err but no exception is thrown.
227  */
228 void MEDLoader::setTooLongStrPolicy(int val) throw(INTERP_KERNEL::Exception)
229 {
230   _TOO_LONG_STR=val;
231 }
232
233 /*!
234  * @param lgth is the size of fam tab. For classical types conn is size of 'lgth'*number_of_nodes_in_type.
235  * @param index is optionnal only for polys. Set it to 0 if it is not the case.
236  * @param connLgth is the size of conn in the case of poly. Unsued if it is not the case.
237  */
238 MEDLoader::MEDConnOfOneElemType::MEDConnOfOneElemType(INTERP_KERNEL::NormalizedCellType type, int *conn, int *index, int *fam, int lgth, int connLgth):_lgth(lgth),_fam(fam),
239                                                                                                                                                        _conn(conn),_index(index),
240                                                                                                                                                        _global(0),_conn_lgth(connLgth),
241                                                                                                                                                        _type(type)
242 {
243 }
244
245 void MEDLoader::MEDConnOfOneElemType::setGlobal(int *global)
246 {
247   if(_global!=global)
248     {
249       if(_global)
250         delete [] _global;
251       _global=global;
252     }
253 }
254
255 void MEDLoader::MEDConnOfOneElemType::releaseArray()
256 {
257   delete [] _fam;
258   delete [] _conn;
259   delete [] _index;
260   delete [] _global;
261 }
262
263 MEDLoader::MEDFieldDoublePerCellType::MEDFieldDoublePerCellType(INTERP_KERNEL::NormalizedCellType type, double *values, int ncomp, int ntuple,
264                                                                 const int *cellIdPerType, const char *locName):_ntuple(ntuple),_ncomp(ncomp),_values(values),_type(type)
265 {
266   if(cellIdPerType)
267     _cell_id_per_type.insert(_cell_id_per_type.end(),cellIdPerType,cellIdPerType+ntuple);
268   if(locName)
269     _loc_name=locName;
270 }
271
272 void MEDLoader::MEDFieldDoublePerCellType::releaseArray()
273 {
274   delete [] _values;
275 }
276
277 /// @cond INTERNAL
278
279 std::vector<std::string> MEDLoaderNS::getMeshNamesFid(med_idt fid)
280 {
281   med_mesh_type type_maillage;
282   char maillage_description[MED_COMMENT_SIZE+1];
283   char dtunit[MED_COMMENT_SIZE+1];
284   med_int space_dim;
285   med_int mesh_dim;
286   char nommaa[MED_NAME_SIZE+1];
287   med_axis_type axistype;
288   med_sorting_type stype;
289   med_int n=MEDnMesh(fid);
290   std::vector<std::string> ret(n);
291   for(int i=0;i<n;i++)
292     {
293       int naxis=MEDmeshnAxis(fid,i+1);
294       INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
295       INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
296       int nstep;
297       MEDmeshInfo(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit);
298       std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
299       ret[i]=cur;
300     }
301   return ret;
302 }
303
304 void MEDLoaderNS::fillGaussDataOnField(const char *fileName, const std::list<MEDLoader::MEDFieldDoublePerCellType>& data, MEDCouplingFieldDouble *f)
305 {
306   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
307   char locName[MED_NAME_SIZE+1];
308   int nloc=MEDnLocalization(fid);
309   med_geometry_type typeGeo;
310   for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=data.begin();iter!=data.end();iter++)
311     {
312       const std::string& loc=(*iter).getLocName();
313       int idLoc=1;
314       int nbOfGaussPt=-1;
315       med_int spaceDim;
316       for(;idLoc<=nloc;idLoc++)
317         {
318           char geointerpname[MED_NAME_SIZE+1]="";
319           char ipointstructmeshname[MED_NAME_SIZE+1]="";
320           med_int nsectionmeshcell;
321           med_geometry_type sectiongeotype;
322           MEDlocalizationInfo(fid,idLoc,locName,&typeGeo,&spaceDim,&nbOfGaussPt, geointerpname, ipointstructmeshname, &nsectionmeshcell,
323                               &sectiongeotype);
324           if(loc==locName)
325             break;
326         }
327       int dim=(int)INTERP_KERNEL::CellModel::GetCellModel((*iter).getType()).getDimension();
328       int nbPtPerCell=(int)INTERP_KERNEL::CellModel::GetCellModel((*iter).getType()).getNumberOfNodes();
329       std::vector<double> refcoo(nbPtPerCell*dim),gscoo(nbOfGaussPt*dim),w(nbOfGaussPt);
330       MEDlocalizationRd(fid,(*iter).getLocName().c_str(),MED_FULL_INTERLACE,&refcoo[0],&gscoo[0],&w[0]);
331       f->setGaussLocalizationOnType((*iter).getType(),refcoo,gscoo,w);
332     }
333   MEDfileClose(fid);
334 }
335
336 /// @endcond
337
338 void MEDLoader::CheckFileForRead(const char *fileName) throw(INTERP_KERNEL::Exception)
339 {
340   MEDFileUtilities::CheckFileForRead(fileName);
341 }
342
343 std::vector<std::string> MEDLoader::GetMeshNames(const char *fileName) throw(INTERP_KERNEL::Exception)
344 {
345   CheckFileForRead(fileName);
346   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
347   std::vector<std::string> ret=MEDLoaderNS::getMeshNamesFid(fid);
348   MEDfileClose(fid);
349   return ret;
350 }
351
352 std::vector< std::pair<std::string,std::string> > MEDLoader::GetComponentsNamesOfField(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception)
353 {
354   CheckFileForRead(fileName);
355   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
356   med_int nbFields=MEDnField(fid);
357   std::vector<std::string> fields(nbFields);
358   med_field_type typcha;
359   for(int i=0;i<nbFields;i++)
360     {
361       med_int ncomp=MEDfieldnComponent(fid,i+1);
362       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
363       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
364       INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
365       med_int nbPdt;
366       med_bool localmesh;
367       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
368       INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
369       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
370       std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
371       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
372       if(curFieldName==fieldName)
373         {
374           std::vector< std::pair<std::string,std::string> > ret(ncomp);
375           for(int j=0;j<ncomp;j++)
376             ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
377                                                       MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
378           return ret;
379         }
380       fields[i]=curFieldName;
381     }
382   std::ostringstream oss; oss << "MEDLoader::GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
383   oss << "Possible field names are : " << std::endl;
384   std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
385   throw INTERP_KERNEL::Exception(oss.str().c_str());
386 }
387
388 /*!
389  * Given a 'fileName' and a 'meshName' this method returns global information concerning this mesh.
390  * It returns, in this order :
391  * - 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...
392  * - the mesh dimension
393  * - the space dimension
394  * - the number of nodes
395  */
396 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)
397 {
398   CheckFileForRead(fileName);
399   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
400   std::set<int> poss;
401   char nommaa[MED_NAME_SIZE+1];
402   char maillage_description[MED_COMMENT_SIZE+1];
403   med_mesh_type type_maillage;
404   std::string trueMeshName;
405   med_int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName);
406   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
407   med_sorting_type sortingType;
408   med_int nstep;
409   med_axis_type axisType;
410   int naxis=MEDmeshnAxis(fid,meshId);
411   INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
412   INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
413   MEDmeshInfo(fid,meshId,nommaa,&spaceDim,&meshDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit); 
414   if(type_maillage!=MED_UNSTRUCTURED_MESH)
415     {
416       std::ostringstream oss; oss << "MEDLoader::GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
417       oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
418       throw INTERP_KERNEL::Exception(oss.str().c_str());
419     }
420   // limitation
421   if(nstep!=1)
422     throw INTERP_KERNEL::Exception("MEDLoader::GetUMeshGlobalInfo : multisteps on mesh not managed !");
423   med_int numdt,numit;
424   med_float dt;
425   MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
426   // endlimitation
427   std::vector<int> dims;
428   std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
429   med_bool changement,transformation;
430   for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
431     {
432       med_geometry_type curMedType=typmai[i];
433       int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
434       if(curNbOfElemM>0)
435         {
436           INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
437           int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
438           dims.push_back(mdimCell);
439           geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
440         }
441     }
442   int maxLev=*std::max_element(dims.begin(),dims.end());
443   int lowLev=*std::min_element(dims.begin(),dims.end());
444   int nbOfLevels=maxLev-lowLev+1;
445   std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
446   for(std::size_t i=0;i<dims.size();i++)
447     {
448       ret[maxLev-dims[i]].push_back(geoTypes[i]);
449     }
450   numberOfNodes=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
451   return ret;
452 }
453
454 std::vector<std::string> MEDLoader::GetMeshNamesOnField(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception)
455 {
456   CheckFileForRead(fileName);
457   std::vector<std::string> ret;
458   //
459   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
460   med_int nbFields=MEDnField(fid);
461   //
462   med_field_type typcha;
463   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
464   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
465   med_bool localmesh;
466   //
467   for(int i=0;i<nbFields;i++)
468     {
469       med_int ncomp=MEDfieldnComponent(fid,i+1);
470       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
471       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
472       med_int nbPdt;
473       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
474       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
475       std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
476       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
477       if(curFieldName==fieldName)
478         ret.push_back(meshName);
479     }
480   MEDfileClose(fid);
481   return ret;
482 }
483
484 std::vector<std::string> MEDLoader::GetMeshFamiliesNames(const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception)
485 {
486   CheckFileForRead(fileName);
487   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
488   med_int nfam=MEDnFamily(fid,meshName);
489   std::vector<std::string> ret(nfam);
490   char nomfam[MED_NAME_SIZE+1];
491   med_int numfam;
492   for(int i=0;i<nfam;i++)
493     {
494       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
495       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
496       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
497       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
498       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
499       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
500       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
501       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
502       ret[i]=cur;
503     }
504   MEDfileClose(fid);
505   return ret;
506 }
507
508
509 std::vector<std::string> MEDLoader::GetMeshFamiliesNamesOnGroup(const char *fileName, const char *meshName, const char *grpName) throw(INTERP_KERNEL::Exception)
510 {
511   CheckFileForRead(fileName);
512   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
513   med_int nfam=MEDnFamily(fid,meshName);
514   std::vector<std::string> ret;
515   char nomfam[MED_NAME_SIZE+1];
516   med_int numfam;
517   for(int i=0;i<nfam;i++)
518     {
519       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
520       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
521       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
522       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
523       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
524       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
525       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
526       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
527       for(int j=0;j<ngro;j++)
528         {
529           std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
530           if(cur2==grpName)
531             ret.push_back(cur);
532         }
533     }
534   MEDfileClose(fid);
535   return ret;
536 }
537
538 std::vector<std::string> MEDLoader::GetMeshGroupsNamesOnFamily(const char *fileName, const char *meshName, const char *famName) throw(INTERP_KERNEL::Exception)
539 {
540   CheckFileForRead(fileName);
541   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
542   med_int nfam=MEDnFamily(fid,meshName);
543   std::vector<std::string> ret;
544   char nomfam[MED_NAME_SIZE+1];
545   med_int numfam;
546   bool found=false;
547   for(int i=0;i<nfam && !found;i++)
548     {
549       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
550       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
551       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
552       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
553       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
554       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
555       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
556       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
557       found=(cur==famName);
558       if(found)
559         for(int j=0;j<ngro;j++)
560           {
561             std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
562             ret.push_back(cur2);
563           }
564     }
565   MEDfileClose(fid);
566   if(!found)
567     {
568       std::ostringstream oss;
569       oss << "MEDLoader::GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
570       throw INTERP_KERNEL::Exception(oss.str().c_str());
571     }
572   return ret;
573 }
574
575   
576 std::vector<std::string> MEDLoader::GetMeshGroupsNames(const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception)
577 {
578   CheckFileForRead(fileName);
579   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
580   med_int nfam=MEDnFamily(fid,meshName);
581   std::vector<std::string> ret;
582   char nomfam[MED_NAME_SIZE+1];
583   med_int numfam;
584   for(int i=0;i<nfam;i++)
585     {
586       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
587       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
588       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
589       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
590       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
591       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
592       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
593       for(int j=0;j<ngro;j++)
594         {
595           std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
596           if(std::find(ret.begin(),ret.end(),cur)==ret.end())
597             ret.push_back(cur);
598         }
599     }
600   MEDfileClose(fid);
601   return ret;
602 }
603 std::vector<ParaMEDMEM::TypeOfField> MEDLoader::GetTypesOfField(const char *fileName, const char *meshName, const char *fieldName) throw(INTERP_KERNEL::Exception)
604 {
605   CheckFileForRead(fileName);
606   std::vector<ParaMEDMEM::TypeOfField> ret;
607   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
608   med_int nbFields=MEDnField(fid);
609   //
610   med_field_type typcha;
611   //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
612   med_int numdt=0,numo=0;
613   med_float dt=0.0;
614   char pflname[MED_NAME_SIZE+1]="";
615   char locname[MED_NAME_SIZE+1]="";
616   char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
617   char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
618   med_bool localmesh;
619   //
620   for(int i=0;i<nbFields;i++)
621     {
622       med_int ncomp=MEDfieldnComponent(fid,i+1);
623       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
624       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
625       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
626       med_int nbPdt;
627       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
628       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
629       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
630       if(curMeshName==meshName)
631         {
632           if(curFieldName==fieldName)
633             {
634               int profilesize,nbi;
635               if(nbPdt>0)
636                 {
637                   bool found=false;
638                   for(int ii=0;ii<nbPdt && !found;ii++)
639                     {
640                       MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
641                       med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
642                                                                 pflname,&profilesize,locname,&nbi);
643                       if(nbOfVal>0)
644                         {
645                           ret.push_back(ON_NODES);
646                           found=true;
647                         }
648                     }
649                 }
650               bool found=false;
651               for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
652                 {
653                   if(nbPdt>0)
654                     {
655                       MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
656                       med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
657                                                                 pflname,&profilesize,locname,&nbi);
658                       if(nbOfVal>0)
659                         {
660                           found=true;
661                           ret.push_back(ON_CELLS);
662                         }
663                     }
664                 }
665             }
666         }
667     }
668   delete [] maa_ass;
669   delete [] nomcha;
670   MEDfileClose(fid);
671   return ret;
672 }
673
674 std::vector<std::string> MEDLoader::GetAllFieldNames(const char *fileName) throw(INTERP_KERNEL::Exception)
675 {
676   CheckFileForRead(fileName);
677   std::vector<std::string> ret;
678   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
679   med_int nbFields=MEDnField(fid);
680   med_field_type typcha;
681   for(int i=0;i<nbFields;i++)
682     {
683       med_int ncomp=MEDfieldnComponent(fid,i+1);
684       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
685       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
686       INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
687       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
688       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
689       med_int nbPdt;
690       med_bool localmesh;
691       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
692       ret.push_back(std::string(nomcha));
693     }
694   MEDfileClose(fid);
695   return ret;
696 }
697
698 std::vector<std::string> MEDLoader::GetAllFieldNamesOnMesh(const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception)
699 {
700   CheckFileForRead(fileName);
701   std::vector<std::string> ret;
702   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
703   med_int nbFields=MEDnField(fid);
704   //
705   med_field_type typcha;
706   char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
707   char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
708   //
709   for(int i=0;i<nbFields;i++)
710     {
711       med_int ncomp=MEDfieldnComponent(fid,i+1);
712       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
713       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
714       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
715       med_int nbPdt;
716       med_bool localmesh;
717       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
718       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
719       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
720       //
721       if(curMeshName==meshName)
722         ret.push_back(curFieldName);
723     }
724   delete [] maa_ass;
725   delete [] nomcha;
726   MEDfileClose(fid);
727   return ret;
728 }
729
730 std::vector<std::string> MEDLoader::GetFieldNamesOnMesh(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception)
731 {
732   CheckFileForRead(fileName);
733   switch(type)
734     {
735     case ON_CELLS:
736       return GetCellFieldNamesOnMesh(fileName,meshName);
737     case ON_NODES:
738       return GetNodeFieldNamesOnMesh(fileName,meshName);
739     default:
740       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
741     } 
742 }
743
744 std::vector<std::string> MEDLoader::GetCellFieldNamesOnMesh(const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception)
745 {
746   CheckFileForRead(fileName);
747   std::vector<std::string> ret;
748   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
749   med_int nbFields=MEDnField(fid);
750   //
751   med_field_type typcha;
752   //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
753   med_int numdt=0,numo=0;
754   med_float dt=0.0;
755   char pflname[MED_NAME_SIZE+1]="";
756   char locname[MED_NAME_SIZE+1]="";
757   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
758   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
759   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
760   med_bool localmesh;
761   med_int nbPdt;
762   //
763   for(int i=0;i<nbFields;i++)
764     {
765       med_int ncomp=MEDfieldnComponent(fid,i+1);
766       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
767       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
768       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
769       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
770       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
771       int profilesize,nbi;
772       if(curMeshName==meshName)
773         {
774           bool found=false;
775           for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
776             {
777               if(nbPdt>0)
778                 {
779                   MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
780                   med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
781                                                             pflname,&profilesize,locname,&nbi);
782                   if(nbOfVal>0)
783                     {
784                       found=true;
785                       ret.push_back(curFieldName);
786                     }
787                 }
788             }
789         }
790     }
791   MEDfileClose(fid);
792   return ret;
793 }
794
795 std::vector<std::string> MEDLoader::GetNodeFieldNamesOnMesh(const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception)
796 {
797   CheckFileForRead(fileName);
798   std::vector<std::string> ret;
799   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
800   med_int nbFields=MEDnField(fid);
801   char pflname[MED_NAME_SIZE+1]="";
802   char locname[MED_NAME_SIZE+1]="";
803   //
804   med_field_type typcha;
805   med_int numdt=0,numo=0;
806   med_float dt=0.0;
807   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
808   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
809   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
810   med_bool localmesh;
811   //
812   for(int i=0;i<nbFields;i++)
813     {
814       med_int ncomp=MEDfieldnComponent(fid,i+1);
815       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
816       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
817       med_int nbPdt;
818       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
819       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
820       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
821       if(nbPdt>0)
822         {
823           int profilesize,nbi;
824           MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
825           med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
826                                                     pflname,&profilesize,locname,&nbi);
827           if(curMeshName==meshName && nbOfVal>0)
828             {
829               ret.push_back(curFieldName);
830             }
831         }
832     }
833   MEDfileClose(fid);
834   return ret;
835 }
836
837 std::vector< std::pair< std::pair<int,int>, double> > MEDLoader::GetAllFieldIterations(const char *fileName, const char *meshName, const char *fieldName) throw(INTERP_KERNEL::Exception)
838 {
839   CheckFileForRead(fileName);
840   std::string meshNameCpp(meshName);
841   std::vector< std::pair< std::pair<int,int>, double > > ret;
842   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
843   med_int nbFields=MEDnField(fid);
844   //
845   med_field_type typcha;
846   med_int numdt=0,numo=0;
847   med_float dt=0.0;
848   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
849   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
850   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
851   med_bool localmesh;
852   //
853   for(int i=0;i<nbFields;i++)
854     {
855       med_int ncomp=MEDfieldnComponent(fid,i+1);
856       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
857       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
858       med_int nbPdt;
859       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
860       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
861       if(curFieldName==fieldName)
862         {
863           for(int k=0;k<nbPdt;k++)
864             {
865               MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
866               ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
867             }
868         }
869     }
870   MEDfileClose(fid);
871   return ret;
872 }
873
874 double MEDLoader::GetTimeAttachedOnFieldIteration(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception)
875 {
876   CheckFileForRead(fileName);
877   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
878   med_int nbFields=MEDnField(fid);
879   //
880   med_field_type typcha;
881   med_int numdt=0,numo=0;
882   med_float dt=0.0;
883   med_bool local;
884   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
885   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
886   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
887   //
888   bool found=false;
889   bool found2=false;
890   double ret=std::numeric_limits<double>::max();
891   for(int i=0;i<nbFields && !found;i++)
892     {
893       med_int ncomp=MEDfieldnComponent(fid,i+1);
894       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
895       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
896       med_int nbPdt;
897       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt);
898       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
899       if(curFieldName==fieldName)
900         {
901           found=true;
902           for(int k=0;k<nbPdt;k++)
903             {
904               MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
905               if(numdt==iteration && numo==order)
906                 {
907                   found2=true;
908                   ret=dt;
909                 }
910             }
911         }
912     }
913   MEDfileClose(fid);
914   if(!found || !found2)
915     {
916       std::ostringstream oss;
917       oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
918       throw INTERP_KERNEL::Exception(oss.str().c_str());
919     }
920   return ret;
921 }
922
923 std::vector< std::pair<int,int> > MEDLoader::GetFieldIterations(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, const char *fieldName) throw(INTERP_KERNEL::Exception)
924 {
925   CheckFileForRead(fileName);
926   switch(type)
927     {
928     case ON_CELLS:
929       return GetCellFieldIterations(fileName,meshName,fieldName);
930     case ON_NODES:
931       return GetNodeFieldIterations(fileName,meshName,fieldName);
932     default:
933       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
934     }
935 }
936
937 std::vector< std::pair<int,int> > MEDLoader::GetCellFieldIterations(const char *fileName, const char *meshName, const char *fieldName) throw(INTERP_KERNEL::Exception)
938 {
939   CheckFileForRead(fileName);
940   std::string meshNameCpp(meshName);
941   std::vector< std::pair<int,int> > ret;
942   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
943   med_int nbFields=MEDnField(fid);
944   //
945   med_field_type typcha;
946   med_int numdt=0,numo=0;
947   med_float dt=0.0;
948   char pflname[MED_NAME_SIZE+1]="";
949   char locname[MED_NAME_SIZE+1]="";
950   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
951   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
952   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
953   med_bool localmesh;
954   //
955   for(int i=0;i<nbFields;i++)
956     {
957       med_int ncomp=MEDfieldnComponent(fid,i+1);
958       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
959       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
960       med_int nbPdt;
961       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
962       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
963       if(curFieldName==fieldName)
964         {
965           bool found=false;
966           for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
967             {
968               for(int k=0;k<nbPdt;k++)
969                 {
970                   int profilesize,nbi;
971                   MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
972                   med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
973                                                             pflname,&profilesize,locname,&nbi);
974                   std::string maa_ass_cpp(maa_ass);
975                   if(meshNameCpp==maa_ass_cpp && nbOfVal>0)
976                     {
977                       found=true;
978                       ret.push_back(std::make_pair(numdt,numo));
979                     }
980                 }
981             }
982         }
983     }
984   MEDfileClose(fid);
985   return ret;
986 }
987
988 std::vector< std::pair<int,int> > MEDLoader::GetNodeFieldIterations(const char *fileName, const char *meshName, const char *fieldName) throw(INTERP_KERNEL::Exception)
989 {
990   CheckFileForRead(fileName);
991   std::string meshNameCpp(meshName);
992   std::vector< std::pair<int,int> > ret;
993   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
994   med_int nbFields=MEDnField(fid);
995   //
996   med_field_type typcha;
997   med_int numdt=0,numo=0;
998   med_float dt=0.0;
999   char pflname[MED_NAME_SIZE+1]="";
1000   char locname[MED_NAME_SIZE+1]="";
1001   INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1002   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
1003   INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1004   med_bool localmesh;
1005   //
1006   for(int i=0;i<nbFields;i++)
1007     {
1008       med_int ncomp=MEDfieldnComponent(fid,i+1);
1009       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1010       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1011       med_int nbPdt;
1012       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
1013       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1014       if(curFieldName==fieldName)
1015         {
1016           for(int k=0;k<nbPdt;k++)
1017             {
1018               int profilesize,nbi;
1019               MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
1020               med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1021                                                         pflname,&profilesize,locname,&nbi);
1022                std::string maa_ass_cpp(maa_ass);
1023                if(meshNameCpp==maa_ass_cpp && nbOfVal>0)
1024                  {
1025                    ret.push_back(std::make_pair(numdt,numo));
1026                  }
1027             }
1028         }
1029     }
1030   MEDfileClose(fid);
1031   return ret;
1032 }
1033
1034 /*!
1035  * This method reads all the content of a field 'fieldName' at a time specified by (iteration,order) lying on a mesh 'meshName' with a specified type 'TypeOfOutField'
1036  * The returned values are strored in 'field' (sorted by type of cell), time corresponding to field, and 'infos' to load properly little strings.
1037  * The principle of this method is to put into 'field' only data that fulfills \b perfectly request.
1038  */
1039 void MEDLoaderNS::readFieldDoubleDataInMedFile(const char *fileName, const char *meshName, const char *fieldName, 
1040                                                int iteration, int order, ParaMEDMEM::TypeOfField typeOfOutField,
1041                                                std::list<MEDLoader::MEDFieldDoublePerCellType>& field,
1042                                                double& time, std::vector<std::string>& infos)
1043 {
1044   time=0.;
1045   MEDFileUtilities::CheckFileForRead(fileName);
1046   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
1047   med_int nbFields=MEDnField(fid);
1048   //
1049   med_field_type typcha;
1050   char nomcha[MED_NAME_SIZE+1]="";
1051   char pflname [MED_NAME_SIZE+1]="";
1052   char locname [MED_NAME_SIZE+1]="";
1053   std::map<ParaMEDMEM::TypeOfField, med_entity_type> tabEnt;
1054   std::map<ParaMEDMEM::TypeOfField, med_geometry_type *> tabType;
1055   std::map<ParaMEDMEM::TypeOfField, int> tabTypeLgth;
1056   med_bool localmesh;
1057   bool found=false;
1058   tabEnt[ON_CELLS]=MED_CELL;
1059   tabType[ON_CELLS]=typmai;
1060   tabTypeLgth[ON_CELLS]=MED_N_CELL_FIXED_GEO;
1061   tabEnt[ON_NODES]=MED_NODE;
1062   tabType[ON_NODES]=typmainoeud;
1063   tabTypeLgth[ON_NODES]=1;
1064   tabEnt[ON_GAUSS_PT]=MED_CELL;
1065   tabType[ON_GAUSS_PT]=typmai;
1066   tabTypeLgth[ON_GAUSS_PT]=MED_N_CELL_FIXED_GEO;
1067   tabEnt[ON_GAUSS_NE]=MED_NODE_ELEMENT;
1068   tabType[ON_GAUSS_NE]=typmai;
1069   tabTypeLgth[ON_GAUSS_NE]=MED_N_CELL_FIXED_GEO;
1070   //
1071   for(int i=0;i<nbFields && !found;i++)
1072     {
1073       med_int ncomp=MEDfieldnComponent(fid,i+1);
1074       INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1075       INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1076       INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
1077       INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1078       med_int nbPdt;
1079       MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
1080       std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
1081       std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1082       found=(curFieldName==fieldName) && (curMeshName==meshName);
1083       if(found)
1084         {
1085           infos.resize(ncomp);
1086           for(int ii=0;ii<ncomp;ii++)
1087             infos[ii]=MEDLoaderBase::buildUnionUnit(comp+ii*MED_SNAME_SIZE,MED_SNAME_SIZE,unit+ii*MED_SNAME_SIZE,MED_SNAME_SIZE);
1088           bool found2=false;
1089           med_int numdt=0,numo=0;
1090           med_float dt=0.0;
1091           for(int k=0;k<nbPdt && !found2;k++)
1092             {
1093               MEDfieldComputingStepInfo(fid,fieldName,k+1,&numdt,&numo,&dt);
1094               found2=(numdt==iteration && numo==order);
1095               if(found2)
1096                 time=dt;
1097             }
1098           if(!found2)
1099             {
1100               std::ostringstream oss; oss << "FieldDouble in file \""<< fileName<< "\" with name \"" << fieldName << "\" on mesh \"" <<  meshName;
1101               oss << "\" does not have such time step : iteration=" << iteration << " order=" << order << std::endl;
1102               throw INTERP_KERNEL::Exception(oss.str().c_str());
1103             }
1104           for(int j=0;j<tabTypeLgth[typeOfOutField];j++)
1105             {
1106               if(nbPdt>0)
1107                 {
1108                   int profilesize,nbi;
1109                   int nval=MEDfieldnValueWithProfile(fid,fieldName,numdt,numo,tabEnt[typeOfOutField],tabType[typeOfOutField][j],1,MED_COMPACT_PFLMODE,pflname,&profilesize,locname,&nbi);
1110                   if(nval>0)
1111                     {
1112                       double *valr=new double[ncomp*nval*nbi];
1113                       MEDfieldValueWithProfileRd(fid,fieldName,iteration,order,tabEnt[typeOfOutField],tabType[typeOfOutField][j],MED_COMPACT_PFLMODE,
1114                                                  pflname,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,(unsigned char*)valr);
1115                       std::string tmp(locname);
1116                       if((locname[0]!='\0' && (typeOfOutField!=ON_GAUSS_PT))
1117                          || (locname[0]=='\0' && typeOfOutField==ON_GAUSS_PT))
1118                         {
1119                           delete [] valr;
1120                           continue;
1121                         }
1122                       INTERP_KERNEL::AutoPtr<int> pfl=0;
1123                       if(pflname[0]!='\0')
1124                         {
1125                           pfl=new int[nval];
1126                           MEDprofileRd(fid,pflname,pfl);
1127                         }
1128                       field.push_back(MEDLoader::MEDFieldDoublePerCellType(typmai2[j],valr,ncomp,nval*nbi,pfl,locname));
1129                     }
1130                 }
1131             }
1132         }
1133     }
1134   if(!found)
1135     {
1136       std::ostringstream oss; oss << "MEDLoaderNS::readFieldDoubleDataInMedFile : no such couple meshName=\"" << meshName << "\", fieldName=\"" << fieldName << "\" in file \"" << fileName << "\" !";
1137       throw INTERP_KERNEL::Exception(oss.str().c_str());
1138     }
1139 }
1140
1141 std::vector<int> MEDLoaderNS::getIdsFromFamilies(const char *fileName, const char *meshName, const std::vector<std::string>& fams)
1142 {
1143   std::vector<int> ret;
1144   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
1145   med_int nfam=MEDnFamily(fid,meshName);
1146   char nomfam[MED_NAME_SIZE+1];
1147   med_int numfam;
1148   for(int i=0;i<nfam;i++)
1149     {
1150       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
1151       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
1152       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
1153       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
1154       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
1155       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
1156       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
1157       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
1158       if(std::find(fams.begin(),fams.end(),cur)!=fams.end())
1159         ret.push_back(numfam);
1160     }
1161   MEDfileClose(fid);
1162   return ret;
1163 }
1164
1165 std::vector<int> MEDLoaderNS::getIdsFromGroups(const char *fileName, const char *meshName, const std::vector<std::string>& grps)
1166 {
1167   std::vector<int> ret;
1168   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
1169   med_int nfam=MEDnFamily(fid,meshName);
1170   char nomfam[MED_NAME_SIZE+1];
1171   med_int numfam;
1172   for(int i=0;i<nfam;i++)
1173     {
1174       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
1175       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
1176       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
1177       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
1178       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
1179       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
1180       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
1181       std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
1182       for(int j=0;j<ngro;j++)
1183         {
1184           std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
1185           if(std::find(grps.begin(),grps.end(),cur2)!=grps.end())
1186             {
1187               ret.push_back(numfam);
1188               break;
1189             }
1190         }
1191     }
1192   MEDfileClose(fid);
1193   return ret;
1194 }
1195
1196 med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception)
1197 {
1198   if(meshName==0)
1199     {
1200       std::vector<std::string> meshes=getMeshNamesFid(fid);
1201       if(meshes.empty())
1202         throw INTERP_KERNEL::Exception("No mesh in file");
1203       trueMeshName=meshes[0];
1204       return 1;
1205     }
1206   std::string meshNameStr(meshName);
1207   std::vector<std::string> meshes=getMeshNamesFid(fid);
1208   if(meshes.empty())
1209     throw INTERP_KERNEL::Exception("No mesh in file");
1210   std::vector<std::string>::iterator iter=std::find(meshes.begin(),meshes.end(),meshNameStr);
1211   if(iter==meshes.end())
1212     {
1213       std::ostringstream os2;
1214       os2 << "MeshName '" << meshName << "' not in file : meshes available : ";
1215       std::copy(meshes.begin(),meshes.end(),std::ostream_iterator<std::string>(os2," "));
1216       throw INTERP_KERNEL::Exception(os2.str().c_str());
1217     }
1218   trueMeshName=meshName;
1219   return iter-meshes.begin()+1;
1220 }
1221
1222 /*!
1223  * This methods allows to merger all entities and to considerate only cell types.
1224  */
1225 void MEDLoaderNS::dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity)
1226 {
1227   if(nbOfElemCell>=nbOfElemFace)
1228     {
1229       whichEntity=MED_CELL;
1230       nbOfElem=nbOfElemCell;
1231     }
1232   else
1233     {
1234       whichEntity=MED_CELL;
1235       nbOfElem=nbOfElemFace;
1236     }
1237 }
1238
1239 /*!
1240  * This method returns a first quick overview of mesh with name 'meshName' into the file 'fileName'.
1241  * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
1242  * @return the maximal mesh dimension of specified mesh. If nothing found -1 is returned.
1243  */
1244 int MEDLoaderNS::readUMeshDimFromFile(const char *fileName, const char *meshName, std::vector<int>& possibilities)
1245 {
1246   possibilities.clear();
1247   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
1248   int ret;
1249   std::set<int> poss;
1250   char nommaa[MED_NAME_SIZE+1];
1251   char maillage_description[MED_COMMENT_SIZE+1];
1252   med_mesh_type type_maillage;
1253   med_int Sdim,Mdim;
1254   std::string trueMeshName;
1255   med_int meshId=getIdFromMeshName(fid,meshName,trueMeshName);
1256   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
1257   med_sorting_type sortingType;
1258   med_int nstep;
1259   med_axis_type axisType;
1260   int naxis=MEDmeshnAxis(fid,meshId);
1261   INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
1262   INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
1263   MEDmeshInfo(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit);
1264   // limitation
1265   if(nstep!=1)
1266     {
1267       throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
1268     } 
1269   med_int numdt,numit;
1270   med_float dt;
1271   MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
1272   // endlimitation
1273   for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
1274     {
1275       med_geometry_type curMedType=typmai[i];
1276       med_bool changement,transformation;
1277       int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
1278       int curNbOfElemF=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);//limitation
1279       int curNbOfElem;
1280       med_entity_type whichEntity;
1281       MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
1282       if(curNbOfElem>0)
1283         {
1284           INTERP_KERNEL::NormalizedCellType type=typmai2[i];
1285           int curDim=(int)INTERP_KERNEL::CellModel::GetCellModel(type).getDimension();
1286           poss.insert(curDim);
1287         }
1288     }
1289   MEDfileClose(fid);
1290   if(!poss.empty())
1291     {
1292       ret=*poss.rbegin();
1293       for(std::set<int>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();it++)
1294         possibilities.push_back(*it-ret);
1295     }
1296   else
1297     ret=-2;
1298   return ret;
1299 }
1300
1301 void MEDLoaderNS::readUMeshDataInMedFile(med_idt fid, med_int meshId, DataArrayDouble *&coords, std::list<MEDLoader::MEDConnOfOneElemType>& conn, std::string& description)
1302 {
1303   char nommaa[MED_NAME_SIZE+1];
1304   char maillage_description[MED_COMMENT_SIZE+1];
1305   med_mesh_type type_maillage;
1306   med_int Mdim;
1307   med_int Sdim;
1308   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
1309   med_sorting_type sortingType;
1310   med_int nstep;
1311   med_axis_type axisType;
1312   med_int numdt,numit;
1313   med_float dt;
1314   med_bool changement,transformation;
1315   // endlimitation
1316   Sdim=MEDmeshnAxis(fid,1);
1317   INTERP_KERNEL::AutoPtr<char> comp=MEDLoaderBase::buildEmptyString(Sdim*MED_SNAME_SIZE);
1318   INTERP_KERNEL::AutoPtr<char> unit=MEDLoaderBase::buildEmptyString(Sdim*MED_SNAME_SIZE);
1319   MEDmeshInfo(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,comp,unit);
1320   description=MEDLoaderBase::buildStringFromFortran(maillage_description,sizeof(maillage_description));
1321   MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
1322   int spaceDim=std::max((int)Mdim,(int)Sdim);
1323   int nCoords=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
1324   // limitation
1325   if(nstep!=1)
1326     {
1327       throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
1328     }
1329   coords=DataArrayDouble::New();
1330   coords->alloc(nCoords,spaceDim);
1331   double *coordsPtr=coords->getPointer();
1332   MEDmeshNodeCoordinateRd(fid,nommaa,numdt,numit,MED_FULL_INTERLACE,coordsPtr);
1333   for(int i=0;i<spaceDim;i++)
1334     {
1335       std::string info=MEDLoaderBase::buildUnionUnit(comp+i*MED_SNAME_SIZE,MED_SNAME_SIZE,unit+i*MED_SNAME_SIZE,MED_SNAME_SIZE);
1336       coords->setInfoOnComponent(i,info.c_str());
1337     }
1338   for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
1339     {
1340       med_geometry_type curMedType=typmai[i];
1341       med_entity_type whichEntity;
1342       int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
1343       int curNbOfElemF=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);//limitation
1344       int curNbOfElem;
1345       MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
1346       if(curNbOfElem>0)
1347         {
1348           int *connTab=new int[(curMedType%100)*curNbOfElem];
1349           int *fam=new int[curNbOfElem];
1350           MEDLoader::MEDConnOfOneElemType elem(typmai2[i],connTab,0,fam,curNbOfElem,-1);
1351           char *noms=new char[MED_SNAME_SIZE*curNbOfElem+1];
1352           med_bool withname=MED_FALSE,withnumber=MED_FALSE,withfam=MED_FALSE;
1353           int *globArr=new int[curNbOfElem];
1354           MEDmeshElementRd(fid,nommaa,numdt,numit,whichEntity,curMedType,MED_NODAL,MED_FULL_INTERLACE,connTab,&withname,noms,&withnumber,globArr,&withfam,fam);
1355           if(!withfam)
1356             std::fill(fam,fam+curNbOfElem,0);
1357           delete [] noms;
1358           //trying to read global numbering
1359           if(withnumber)
1360             elem.setGlobal(globArr);
1361           else
1362             delete [] globArr;
1363           //limitation manage withfam==false
1364           conn.push_back(elem);
1365         }
1366     }
1367   int curNbOfPolyElem;
1368   int curNbOfPolyElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation)-1;
1369   int curNbOfPolyElemF=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation)-1;//limitation
1370   med_entity_type whichPolyEntity;
1371   MEDLoaderNS::dispatchElems(curNbOfPolyElemM,curNbOfPolyElemF,curNbOfPolyElem,whichPolyEntity);
1372   if(curNbOfPolyElem>0)
1373     {
1374       med_int arraySize=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
1375       int *index=new int[curNbOfPolyElem+1];
1376       int *locConn=new int[arraySize];
1377       int *fam=new int[curNbOfPolyElem];
1378       int *globArr=new int[curNbOfPolyElem];
1379       MEDLoader::MEDConnOfOneElemType elem(INTERP_KERNEL::NORM_POLYGON,locConn,index,fam,curNbOfPolyElem,arraySize);
1380       MEDmeshPolygonRd(fid,nommaa,numdt,numit,MED_CELL,MED_NODAL,index,locConn);
1381       if(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
1382         {
1383           if(MEDmeshEntityFamilyNumberRd(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,fam)!=0)
1384             std::fill(fam,fam+curNbOfPolyElem,0);
1385         }
1386       else
1387         std::fill(fam,fam+curNbOfPolyElem,0);
1388       if(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
1389         {
1390           if(MEDmeshEntityNumberRd(fid,nommaa,numdt,numit,whichPolyEntity,MED_POLYGON,globArr)==0)
1391             elem.setGlobal(globArr);
1392           else
1393             delete [] globArr;
1394         }
1395       else
1396         delete [] globArr;
1397       conn.push_back(elem);
1398     }
1399   curNbOfPolyElem=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,&changement,&transformation)-1;
1400   if(curNbOfPolyElem>0)
1401     {
1402       med_int indexFaceLgth,connFaceLgth;
1403       indexFaceLgth=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation);
1404       connFaceLgth=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYHEDRON,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
1405       INTERP_KERNEL::AutoPtr<int> index=new int[curNbOfPolyElem+1];
1406       INTERP_KERNEL::AutoPtr<int> indexFace=new int[indexFaceLgth];
1407       INTERP_KERNEL::AutoPtr<int> locConn=new int[connFaceLgth];
1408       int *fam=new int[curNbOfPolyElem];
1409       int *globArr=new int[curNbOfPolyElem];
1410       MEDmeshPolyhedronRd(fid,nommaa,numdt,numit,MED_CELL,MED_NODAL,index,indexFace,locConn);
1411       if(MEDmeshnEntity(fid,nommaa,numdt,numit,whichPolyEntity,MED_POLYHEDRON,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
1412         {
1413           if(MEDmeshEntityFamilyNumberRd(fid,nommaa,numdt,numit,whichPolyEntity,MED_POLYHEDRON,fam)!=0)
1414             std::fill(fam,fam+curNbOfPolyElem,0);
1415         }
1416       else
1417         std::fill(fam,fam+curNbOfPolyElem,0);
1418       int arraySize=connFaceLgth;
1419       for(int i=0;i<curNbOfPolyElem;i++)
1420         arraySize+=index[i+1]-index[i]-1;
1421       int *finalConn=new int[arraySize];
1422       int *finalIndex=new int[curNbOfPolyElem+1];
1423       finalIndex[0]=1;
1424       int *wFinalConn=finalConn;
1425       for(int i=0;i<curNbOfPolyElem;i++)
1426         {
1427           finalIndex[i+1]=finalIndex[i]+index[i+1]-index[i]-1+indexFace[index[i+1]-1]-indexFace[index[i]-1];
1428           wFinalConn=std::copy(locConn+indexFace[index[i]-1]-1,locConn+indexFace[index[i]]-1,wFinalConn);
1429           for(int j=index[i];j<index[i+1]-1;j++)
1430             {
1431               *wFinalConn++=0;
1432               wFinalConn=std::copy(locConn+indexFace[j]-1,locConn+indexFace[j+1]-1,wFinalConn);
1433             }
1434         }
1435       MEDLoader::MEDConnOfOneElemType elem(INTERP_KERNEL::NORM_POLYHED,finalConn,finalIndex,fam,curNbOfPolyElem,arraySize);
1436       if(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYHEDRON,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
1437         {
1438           if(MEDmeshEntityNumberRd(fid,nommaa,numdt,numit,whichPolyEntity,MED_POLYHEDRON,globArr)==0)
1439             elem.setGlobal(globArr);
1440           else
1441             delete [] globArr;
1442         }
1443       else
1444         delete [] globArr;
1445       conn.push_back(elem);
1446     }
1447 }
1448
1449 /// @cond INTERNAL
1450
1451 namespace MEDLoaderNS
1452 {
1453   template<class T>
1454   unsigned calculateHighestMeshDim(const std::list<T>& conn)
1455   {
1456     unsigned ret=0;
1457     for(typename std::list<T>::const_iterator iter=conn.begin();iter!=conn.end();iter++)
1458       {
1459         unsigned curDim=INTERP_KERNEL::CellModel::GetCellModel((*iter).getType()).getDimension();
1460         if(ret<curDim)
1461           ret=curDim;
1462       }
1463     return ret;
1464   }
1465   
1466   template<class T>
1467   void keepSpecifiedMeshDim(typename std::list<T>& conn, unsigned meshDim)
1468   {
1469     for(typename std::list<T>::iterator iter=conn.begin();iter!=conn.end();)
1470       {
1471         unsigned curDim=INTERP_KERNEL::CellModel::GetCellModel((*iter).getType()).getDimension();
1472         if(curDim!=meshDim)
1473           {
1474             (*iter).releaseArray();
1475             iter=conn.erase(iter);
1476           }
1477         else
1478           iter++;
1479       }
1480   }
1481   
1482   template<class T>
1483   void keepTypes(typename std::list<T>& conn, const std::vector<INTERP_KERNEL::NormalizedCellType>& typesToKeep)
1484   {
1485     if(!typesToKeep.empty())
1486       {
1487         for(typename std::list<T>::iterator iter=conn.begin();iter!=conn.end();)
1488           {
1489             INTERP_KERNEL::NormalizedCellType curType=(*iter).getType();
1490             if(std::find(typesToKeep.begin(),typesToKeep.end(),curType)==typesToKeep.end())
1491               {
1492                 (*iter).releaseArray();
1493                 iter=conn.erase(iter);
1494               }
1495             else
1496               iter++;
1497           }
1498       }
1499   }
1500 }
1501
1502 class FieldPerTypeAccumulator
1503 {
1504 public:
1505   int operator()(int res, const MEDLoader::MEDFieldDoublePerCellType& elt) { return res+elt.getNbOfTuple(); }
1506 };
1507
1508 ParaMEDMEM::DataArrayDouble *MEDLoaderNS::buildArrayFromRawData(const std::list<MEDLoader::MEDFieldDoublePerCellType>& fieldPerType,
1509                                                                 const std::vector<std::string>& infos)
1510 {
1511   ParaMEDMEM::DataArrayDouble *ret=ParaMEDMEM::DataArrayDouble::New();
1512   int totalNbOfTuple=std::accumulate(fieldPerType.begin(),fieldPerType.end(),0,FieldPerTypeAccumulator());
1513   int nbOfComp=(*fieldPerType.begin()).getNbComp();
1514   double *ptr=new double[nbOfComp*totalNbOfTuple];
1515   ret->useArray(ptr,true,ParaMEDMEM::CPP_DEALLOC,totalNbOfTuple,nbOfComp);
1516   std::for_each(fieldPerType.begin(),fieldPerType.end(),FieldPerTypeCopier(ptr));
1517   for(int i=0;i<nbOfComp;i++)
1518     ret->setInfoOnComponent(i,infos[i].c_str());
1519   return ret;
1520 }
1521
1522 class PolyCounterForFams
1523 {
1524 public:
1525   PolyCounterForFams(int id, const int *index):_id(id),_index(index),_count(0),_sigma(0) { }
1526   void operator()(int val) { if(val==_id) _sigma+=_index[_count+1]-_index[_count]; _count++; }
1527   int getSigma() const { return _sigma; }
1528 private:
1529   int _id;
1530   const int *_index;
1531   int _count;
1532   int _sigma;
1533 };
1534
1535 /*!
1536  * This method fills unstructured connectivity using basic MED file format 'medConnFrmt'.
1537  * If in each elements of 'medConnFrmt' a renumbering cell array is found the aggregate array 'cellRenum' is returned.
1538  */
1539 void MEDLoaderNS::tradMEDFileCoreFrmt2MEDCouplingUMesh(const std::list<MEDLoader::MEDConnOfOneElemType>& medConnFrmt,
1540                                                        const std::vector<int>& familiesToKeep,
1541                                                        DataArrayInt* &conn,
1542                                                        DataArrayInt* &connIndex,
1543                                                        int *&cellRenum)
1544 {
1545   bool keepAll=familiesToKeep.empty();
1546   if(medConnFrmt.empty())
1547     {
1548       conn=0;
1549       connIndex=0;
1550       cellRenum=0;
1551       return ;
1552     }
1553   std::list<MEDLoader::MEDConnOfOneElemType>::const_iterator iter=medConnFrmt.begin();
1554   int totalNbOfCells=0;
1555   int totalNbOfMedConn=0;
1556   bool renumber=true;
1557   cellRenum=0;
1558   for(;iter!=medConnFrmt.end();iter++)
1559     {
1560       if((*iter).getGlobal()==0)
1561         renumber=false;
1562       const INTERP_KERNEL::CellModel& cellMod=INTERP_KERNEL::CellModel::GetCellModel((*iter).getType());
1563       if(keepAll)
1564         totalNbOfCells+=(*iter).getLength();
1565       else
1566         for(std::vector<int>::const_iterator iter2=familiesToKeep.begin();iter2!=familiesToKeep.end();iter2++)
1567           totalNbOfCells+=std::count((*iter).getFam(),(*iter).getFam()+(*iter).getLength(),*iter2);
1568       if(!cellMod.isDynamic())
1569         if(keepAll)
1570           totalNbOfMedConn+=(*iter).getLength()*cellMod.getNumberOfNodes();
1571         else
1572           for(std::vector<int>::const_iterator iter2=familiesToKeep.begin();iter2!=familiesToKeep.end();iter2++)
1573             totalNbOfMedConn+=std::count((*iter).getFam(),(*iter).getFam()+(*iter).getLength(),*iter2)*cellMod.getNumberOfNodes();
1574       else
1575         if(keepAll)
1576           totalNbOfMedConn+=(*iter).getConnLength();
1577         else
1578           for(std::vector<int>::const_iterator iter2=familiesToKeep.begin();iter2!=familiesToKeep.end();iter2++)
1579             {
1580               PolyCounterForFams res=std::for_each((*iter).getFam(),(*iter).getFam()+(*iter).getLength(),PolyCounterForFams(*iter2,(*iter).getIndex()));
1581               totalNbOfMedConn+=res.getSigma();
1582             }
1583     }
1584   connIndex=DataArrayInt::New();
1585   conn=DataArrayInt::New();
1586   connIndex->alloc(totalNbOfCells+1,1);
1587   int *connIdxPtr=connIndex->getPointer();
1588   int connFillId=0;
1589   conn->alloc(totalNbOfMedConn+totalNbOfCells,1);
1590   int *connPtr=conn->getPointer();
1591   if(renumber)
1592     cellRenum=new int[totalNbOfCells];
1593   int *renumW=cellRenum;
1594   for(iter=medConnFrmt.begin();iter!=medConnFrmt.end();iter++)
1595     {
1596       INTERP_KERNEL::NormalizedCellType type=(*iter).getType();
1597       const int *sourceConn=(*iter).getArray();
1598       const int *sourceIndex=(*iter).getIndex();
1599       const int *globalNum=(*iter).getGlobal();
1600       const INTERP_KERNEL::CellModel& cellMod=INTERP_KERNEL::CellModel::GetCellModel(type);
1601       int nbOfCellsInCurType;
1602       int nbOfNodesIn1Cell=cellMod.getNumberOfNodes();
1603       nbOfCellsInCurType=(*iter).getLength();
1604       bool isDyn=cellMod.isDynamic();
1605       int *tmpConnPtr;
1606       for(int i=0;i<nbOfCellsInCurType;i++)
1607         {
1608           if(keepAll)
1609             {//duplication of next 3 lines needed.
1610               *connIdxPtr=connFillId;
1611               *connPtr++=type;
1612               if(renumber)
1613                 *renumW++=globalNum[i];
1614               if(!isDyn)
1615                 tmpConnPtr=std::transform(sourceConn,sourceConn+nbOfNodesIn1Cell,connPtr,std::bind2nd(std::minus<int>(),1));
1616               else
1617                 tmpConnPtr=std::transform(sourceConn,sourceConn+sourceIndex[i+1]-sourceIndex[i],connPtr,std::bind2nd(std::minus<int>(),1));
1618               connIdxPtr++;
1619               nbOfNodesIn1Cell=tmpConnPtr-connPtr;
1620               connFillId+=nbOfNodesIn1Cell+1;
1621               connPtr=tmpConnPtr;
1622             }
1623           else if(std::find(familiesToKeep.begin(),familiesToKeep.end(),(*iter).getFam()[i])!=familiesToKeep.end())
1624             {//duplication of next 3 lines needed.
1625               *connIdxPtr=connFillId;
1626               *connPtr++=type;
1627               if(renumber)
1628                 *renumW++=globalNum[i];
1629               if(!isDyn)
1630                 tmpConnPtr=std::transform(sourceConn,sourceConn+nbOfNodesIn1Cell,connPtr,std::bind2nd(std::minus<int>(),1));
1631               else//The duplication of code is motivated by the line underneath.
1632                 tmpConnPtr=std::transform((*iter).getArray()+sourceIndex[i]-1,(*iter).getArray()+sourceIndex[i+1]-1,connPtr,std::bind2nd(std::minus<int>(),1));
1633               connIdxPtr++;
1634               nbOfNodesIn1Cell=tmpConnPtr-connPtr;
1635               connFillId+=nbOfNodesIn1Cell+1;
1636               connPtr=tmpConnPtr;
1637             }
1638           sourceConn+=nbOfNodesIn1Cell;
1639         }
1640       *connIdxPtr=connFillId;
1641     }
1642 }
1643
1644 namespace MEDLoaderNS
1645 {
1646   template<class T>
1647   void releaseMEDFileCoreFrmt(typename std::list<T>& medConnFrmt)
1648   {
1649     for(typename std::list<T>::iterator iter=medConnFrmt.begin();iter!=medConnFrmt.end();iter++)
1650       (*iter).releaseArray();
1651     medConnFrmt.clear();
1652   }
1653 }
1654
1655 /*!
1656  * This method builds a sub set of connectivity for a given type 'type'. \b WARNING connV,connVIndex and familiesV must have same size !
1657  * @param connV input containing connectivity with MEDCoupling format.
1658  * @param connVIndex input containing connectivity index in MEDCoupling format.
1659  * @param familiesV input that may be equal to 0. This specifies an array specifying cell family foreach cell.
1660  * @param type input specifying which cell types will be extracted in conn4MEDFile. 
1661  * @param conn4MEDFile output containing the connectivity directly understandable by MEDFile; conn4MEDFile has to be empty before this method called.
1662  * @param connIndex4MEDFile output containing index connectivity understandable by MEDFile; only used by polygons and polyhedrons (it is face nodal connec).
1663  * @param connIndexRk24MEDFile output containing index of rank 2 understandable by MEDFile; only used by polyhedrons.
1664  * @param fam4MEDFile output containing family number of cells whose type is 'type'. This output is updated only if 'families' is different than 0.
1665  * @return nb of elements extracted.
1666  */
1667 int MEDLoaderNS::buildMEDSubConnectivityOfOneTypeStaticTypes(const std::vector<const DataArrayInt *>& connV, const std::vector<const DataArrayInt *>& connVIndex, const std::vector<const DataArrayInt *>& familiesV,
1668                                                              INTERP_KERNEL::NormalizedCellType type, std::vector<int>& conn4MEDFile, std::vector<int>& fam4MEDFile, std::vector<int>& renumber)
1669 {
1670   int ret=0;
1671   int nbOfMeshes=connV.size();
1672   int renumOffset=0;
1673   for(int i=0;i<nbOfMeshes;i++)
1674     {
1675       const DataArrayInt *conn=connV[i];
1676       const DataArrayInt *connIndex=connVIndex[i];
1677       const DataArrayInt *families=familiesV[i];
1678       int nbOfElem=connIndex->getNbOfElems()-1;
1679       const int *connPtr=conn->getConstPointer();
1680       const int *connIdxPtr=connIndex->getConstPointer();
1681       const int *famPtr=0;
1682       if(families)
1683         famPtr=families->getConstPointer();
1684       for(int ii=0;ii<nbOfElem;ii++)
1685         {
1686           int delta=connIdxPtr[1]-connIdxPtr[0];
1687           if(*connPtr==type)
1688             {
1689               conn4MEDFile.insert(conn4MEDFile.end(),connPtr+1,connPtr+delta);
1690               if(families)
1691                 fam4MEDFile.push_back(famPtr[ii]);
1692               renumber.push_back(ii+1+renumOffset);
1693               ret++;
1694             }
1695           connIdxPtr++;
1696           connPtr+=delta;
1697         }
1698       renumOffset+=nbOfElem;
1699     }
1700   std::transform(conn4MEDFile.begin(),conn4MEDFile.end(),conn4MEDFile.begin(),std::bind2nd(std::plus<int>(),1));
1701   return ret;
1702 }
1703
1704 int MEDLoaderNS::buildMEDSubConnectivityOfOneTypesPolyg(const std::vector<const DataArrayInt *>&connV, const std::vector<const DataArrayInt *>& connVIndex, const std::vector<const DataArrayInt *>& familiesV,
1705                                                         std::vector<int>& conn4MEDFile, std::vector<int>& connIndex4MEDFile, std::vector<int>& fam4MEDFile, std::vector<int>& renumber)
1706 {
1707   int ret=0;
1708   int nbOfMeshes=connV.size();
1709   connIndex4MEDFile.push_back(1);
1710   int renumOffset=0;
1711   for(int i=0;i<nbOfMeshes;i++)
1712     {
1713       const DataArrayInt *conn=connV[i];
1714       const DataArrayInt *connIndex=connVIndex[i];
1715       const DataArrayInt *families=familiesV[i];
1716       int nbOfElem=connIndex->getNbOfElems()-1;
1717       const int *connPtr=conn->getConstPointer();
1718       const int *connIdxPtr=connIndex->getConstPointer();
1719       const int *famPtr=0;
1720       if(families)
1721         famPtr=families->getConstPointer();
1722       for(int ii=0;ii<nbOfElem;ii++)
1723         {
1724           int delta=connIdxPtr[1]-connIdxPtr[0];
1725           if(*connPtr==INTERP_KERNEL::NORM_POLYGON)
1726             {
1727               conn4MEDFile.insert(conn4MEDFile.end(),connPtr+1,connPtr+delta);
1728               connIndex4MEDFile.push_back(connIndex4MEDFile.back()+delta-1);
1729               if(families)
1730                 fam4MEDFile.push_back(famPtr[ii]);
1731               renumber.push_back(ii+1+renumOffset);
1732               ret++;
1733             }
1734           connIdxPtr++;
1735           connPtr+=delta;
1736         }
1737       renumOffset+=nbOfElem;
1738     }
1739   std::transform(conn4MEDFile.begin(),conn4MEDFile.end(),conn4MEDFile.begin(),std::bind2nd(std::plus<int>(),1));
1740   return ret;
1741 }
1742   
1743 int MEDLoaderNS::buildMEDSubConnectivityOfOneTypesPolyh(const std::vector<const DataArrayInt *>& connV, const std::vector<const DataArrayInt *>& connVIndex, const std::vector<const DataArrayInt *>& familiesV,
1744                                                         std::vector<int>& conn4MEDFile, std::vector<int>& connIndex4MEDFile, std::vector<int>& connIndexRk24MEDFile,
1745                                                         std::vector<int>& fam4MEDFile, std::vector<int>& renumber)
1746 {
1747   int ret=0;
1748   int nbOfMeshes=connV.size();
1749   connIndexRk24MEDFile.push_back(1);
1750   connIndex4MEDFile.push_back(1);
1751   int renumOffset=0;
1752   for(int i=0;i<nbOfMeshes;i++)
1753     {
1754       const DataArrayInt *conn=connV[i];
1755       const DataArrayInt *connIndex=connVIndex[i];
1756       const DataArrayInt *families=familiesV[i];
1757       int nbOfElem=connIndex->getNbOfElems()-1;
1758       const int *connPtr=conn->getConstPointer();
1759       const int *connIdxPtr=connIndex->getConstPointer();
1760       const int *famPtr=0;
1761       if(families)
1762         famPtr=families->getConstPointer();
1763       for(int ii=0;ii<nbOfElem;ii++)
1764         {
1765           int delta=connIdxPtr[1]-connIdxPtr[0];
1766           if(*connPtr==INTERP_KERNEL::NORM_POLYHED)
1767             {
1768               int nbOfFacesOfPolyh=std::count(connPtr+1,connPtr+delta,-1)+1;
1769               const int *work=connPtr+1;
1770               while(work!=connPtr+delta)
1771                 {
1772                   const int *end=std::find(work,connPtr+delta,-1);
1773                   conn4MEDFile.insert(conn4MEDFile.end(),work,end);
1774                   connIndex4MEDFile.push_back(connIndex4MEDFile.back()+std::distance(work,end));
1775                   if(end==connPtr+delta)
1776                     work=connPtr+delta;
1777                   else
1778                     work=end+1;
1779                 }
1780               connIndexRk24MEDFile.push_back(connIndexRk24MEDFile.back()+nbOfFacesOfPolyh);
1781               if(families)
1782                 fam4MEDFile.push_back(famPtr[ii]);
1783               renumber.push_back(ii+1+renumOffset);
1784               ret++;
1785             }
1786           connIdxPtr++;
1787           connPtr+=delta;
1788         }
1789       renumOffset+=nbOfElem;
1790     }
1791   std::transform(conn4MEDFile.begin(),conn4MEDFile.end(),conn4MEDFile.begin(),std::bind2nd(std::plus<int>(),1));
1792   return ret;
1793 }
1794   
1795 /*!
1796  * This method builds a sub set of connectivity for a given type 'type'.
1797  * @param conn input containing connectivity with MEDCoupling format.
1798  * @param connIndex input containing connectivity index in MEDCoupling format.
1799  * @param families input containing, if any, the family number of each cells
1800  * @param type input specifying which cell types will be extracted in conn4MEDFile. 
1801  * @param conn4MEDFile output containing the connectivity directly understandable by MEDFile; conn4MEDFile has to be empty before this method called.
1802  * @param connIndex4MEDFile output containing index connectivity understandable by MEDFile; only used by polygons and polyhedrons (it is face nodal connec).
1803  * @param connIndexRk24MEDFile output containing index of rank 2 understandable by MEDFile; only used by polyhedrons.
1804  * @param fam4MEDFile output containing families id of cells whose type is 'type'.
1805  * @return nb of elements extracted.
1806  */
1807 int MEDLoaderNS::buildMEDSubConnectivityOfOneType(const std::vector<const DataArrayInt *>& conn, const std::vector<const DataArrayInt *>& connIndex, const std::vector<const DataArrayInt *>& families,
1808                                                   INTERP_KERNEL::NormalizedCellType type, std::vector<int>& conn4MEDFile,
1809                                                   std::vector<int>& connIndex4MEDFile, std::vector<int>& connIndexRk24MEDFile, std::vector<int>& fam4MEDFile, std::vector<int>& renumber)
1810 {
1811     
1812   const INTERP_KERNEL::CellModel& cellMod=INTERP_KERNEL::CellModel::GetCellModel(type);
1813   if(!cellMod.isDynamic())
1814     return buildMEDSubConnectivityOfOneTypeStaticTypes(conn,connIndex,families,type,conn4MEDFile,fam4MEDFile,renumber);
1815   else
1816     {
1817       if(type==INTERP_KERNEL::NORM_POLYGON)
1818         return buildMEDSubConnectivityOfOneTypesPolyg(conn,connIndex,families,conn4MEDFile,connIndex4MEDFile,fam4MEDFile,renumber);
1819       else
1820         return buildMEDSubConnectivityOfOneTypesPolyh(conn,connIndex,families,conn4MEDFile,connIndex4MEDFile,connIndexRk24MEDFile,fam4MEDFile,renumber);
1821     }
1822 }
1823   
1824 /*!
1825  * @param ids is a in vector containing families ids whose cells have to be kept. If empty all cells are kept.
1826  * @param typesToKeep is a in vector that indicates which types to keep after dimension filtering.
1827  * @param meshDimExtract out parameter that gives the mesh dimension.
1828  * @param cellRenum out parameter that specifies the renumbering (if !=0) of cells in file.
1829  */
1830 MEDCouplingUMesh *MEDLoaderNS::readUMeshFromFileLev1(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<int>& ids,
1831                                                      const std::vector<INTERP_KERNEL::NormalizedCellType>& typesToKeep, unsigned& meshDimExtract, int *&cellRenum) throw(INTERP_KERNEL::Exception)
1832 {
1833   if(meshDimRelToMax>0)
1834     throw INTERP_KERNEL::Exception("meshDimRelToMax must be <=0 !");
1835   //Extraction data from MED file.
1836   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
1837   std::string trueMeshName;
1838   med_int mid=getIdFromMeshName(fid,meshName,trueMeshName);
1839   DataArrayDouble *coords=0;
1840   std::list<MEDLoader::MEDConnOfOneElemType> conn;
1841   std::string descr;
1842   readUMeshDataInMedFile(fid,mid,coords,conn,descr);
1843   meshDimExtract=MEDLoaderNS::calculateHighestMeshDim<MEDLoader::MEDConnOfOneElemType>(conn);
1844   meshDimExtract=meshDimExtract+meshDimRelToMax;
1845   MEDLoaderNS::keepSpecifiedMeshDim<MEDLoader::MEDConnOfOneElemType>(conn,meshDimExtract);
1846   MEDLoaderNS::keepTypes<MEDLoader::MEDConnOfOneElemType>(conn,typesToKeep);
1847   MEDfileClose(fid);
1848   //Put data in returned data structure.
1849   MEDCouplingUMesh *ret=MEDCouplingUMesh::New();
1850   ret->setName(trueMeshName.c_str());
1851   ret->setDescription(descr.c_str());
1852   ret->setMeshDimension(meshDimExtract);
1853   //
1854   ret->setCoords(coords);
1855   coords->decrRef();
1856   //
1857   DataArrayInt *connArr,*connIndexArr;
1858   tradMEDFileCoreFrmt2MEDCouplingUMesh(conn,ids,connArr,connIndexArr,cellRenum);
1859   ret->setConnectivity(connArr,connIndexArr);
1860   //clean-up
1861   if(connArr)
1862     connArr->decrRef();
1863   if(connIndexArr)
1864     connIndexArr->decrRef();
1865   releaseMEDFileCoreFrmt<MEDLoader::MEDConnOfOneElemType>(conn);
1866   return ret;
1867 }
1868
1869 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoaderNS::readFieldDoubleLev2(const char *fileName, ParaMEDMEM::TypeOfField typeOfOutField, unsigned meshDim, const int *cellRenum, const ParaMEDMEM::MEDCouplingUMesh *mesh,
1870                                                                      const std::vector<std::string>& infos, const char *fieldName, int iteration, int order, double time,
1871                                                                      std::list<MEDLoader::MEDFieldDoublePerCellType>& fieldPerCellType) throw(INTERP_KERNEL::Exception)
1872 {
1873   if(typeOfOutField==ON_CELLS || typeOfOutField==ON_GAUSS_PT || typeOfOutField==ON_GAUSS_NE)
1874     MEDLoaderNS::keepSpecifiedMeshDim<MEDLoader::MEDFieldDoublePerCellType>(fieldPerCellType,meshDim);
1875   if(fieldPerCellType.empty())
1876     {
1877       std::ostringstream oss; oss << "Error on reading file \"" << fileName << "\" meshName=\"" << mesh->getName();
1878       oss << std::endl << "FieldName=\"" << fieldName << "\" (iteration=" << iteration << ",order=" << order << ")" << std::endl;
1879       if(typeOfOutField==ON_CELLS || typeOfOutField==ON_GAUSS_PT || typeOfOutField==ON_GAUSS_NE)
1880         oss << "Request for cell field, maybe it is an ON_NODES field ?";
1881       else
1882         oss << "Request for a node field, maybe it is an ON_CELLS field ?";
1883       throw INTERP_KERNEL::Exception(oss.str().c_str());
1884     }
1885   //for profiles
1886   ParaMEDMEM::MEDCouplingUMesh *newMesh=0;
1887   std::string mName(mesh->getName());
1888   for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=fieldPerCellType.begin();iter!=fieldPerCellType.end();iter++)
1889     {
1890       const std::vector<int>& cellIds=(*iter).getCellIdPerType();
1891       if(!cellIds.empty())
1892         {
1893           std::vector<int> ci(cellIds.size());
1894           std::transform(cellIds.begin(),cellIds.end(),ci.begin(),std::bind2nd(std::plus<int>(),-1));
1895           ParaMEDMEM::MEDCouplingUMesh *mesh2=0;
1896           if(typeOfOutField==ON_CELLS)
1897             {
1898               if(newMesh)
1899                 mesh2=newMesh->keepSpecifiedCells((*iter).getType(),&ci[0],&ci[0]+ci.size());
1900               else
1901                 mesh2=mesh->keepSpecifiedCells((*iter).getType(),&ci[0],&ci[0]+ci.size());
1902             }
1903           else if(typeOfOutField==ON_NODES)
1904             {
1905               DataArrayInt *da=0,*da2=0;
1906               if(newMesh)
1907                 {
1908                   if((int)ci.size()!=newMesh->getNumberOfNodes())
1909                     {
1910                       da=newMesh->getCellIdsFullyIncludedInNodeIds(&ci[0],&ci[ci.size()]);
1911                       mesh2=dynamic_cast<MEDCouplingUMesh *>(newMesh->buildPartAndReduceNodes(da->getConstPointer(),da->getConstPointer()+da->getNbOfElems(),da2));
1912                     }
1913                 }
1914               else
1915                 {
1916                   if((int)ci.size()!=mesh->getNumberOfNodes())
1917                     {
1918                       da=mesh->getCellIdsFullyIncludedInNodeIds(&ci[0],&ci[ci.size()]);
1919                       mesh2=dynamic_cast<MEDCouplingUMesh *>(mesh->buildPartAndReduceNodes(da->getConstPointer(),da->getConstPointer()+da->getNbOfElems(),da2));
1920                       //
1921                       int nnodes=mesh2->getNumberOfNodes();
1922                       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da3=DataArrayInt::New();
1923                       const int *da2Ptr=da2->getConstPointer();
1924                       da3->alloc(nnodes,1);
1925                       int *da3Ptr=da3->getPointer();
1926                       for(int i=0;i<(int)ci.size();i++)
1927                         {
1928                           int val=da2Ptr[ci[i]];
1929                           if(val!=-1)
1930                             da3Ptr[val]=i;
1931                         }
1932                       mesh2->renumberNodes(da3->getConstPointer(),nnodes);
1933                     }
1934                   else
1935                     {
1936                       mesh2=mesh->clone(true);
1937                       da=DataArrayInt::New();
1938                       da->alloc((int)ci.size(),1);
1939                       std::copy(ci.begin(),ci.end(),da->getPointer());
1940                       da2=da->invertArrayO2N2N2O(ci.size());
1941                       mesh2->renumberNodes(da2->getConstPointer(),(int)ci.size());
1942                     }
1943                 }
1944               if(da)
1945                 da->decrRef();
1946               if(da2)
1947                 da2->decrRef();
1948             }
1949           if(newMesh)
1950             newMesh->decrRef();
1951           newMesh=mesh2;
1952         }
1953     }
1954   //
1955   ParaMEDMEM::MEDCouplingFieldDouble *ret=ParaMEDMEM::MEDCouplingFieldDouble::New(typeOfOutField,ONE_TIME);
1956   ret->setName(fieldName);
1957   ret->setTime(time,iteration,order);
1958   if(newMesh)
1959     {
1960       newMesh->setName(mName.c_str());//retrieving mesh name to avoid renaming due to mesh restriction in case of profile.
1961       ret->setMesh(newMesh);
1962       newMesh->decrRef();
1963     }
1964   else
1965     ret->setMesh(mesh);
1966   ParaMEDMEM::DataArrayDouble *arr=buildArrayFromRawData(fieldPerCellType,infos);
1967   ret->setArray(arr);
1968   arr->decrRef();
1969   //
1970   if(typeOfOutField==ON_GAUSS_PT)
1971     fillGaussDataOnField(fileName,fieldPerCellType,ret);
1972   if(cellRenum)
1973     ret->renumberCellsWithoutMesh(cellRenum,true);
1974   return ret;
1975 }
1976
1977 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoaderNS::readFieldDoubleLev1(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order,
1978                                                                      ParaMEDMEM::TypeOfField typeOfOutField) throw(INTERP_KERNEL::Exception)
1979 {
1980   std::list<MEDLoader::MEDFieldDoublePerCellType> fieldPerCellType;
1981   double time;
1982   std::vector<std::string> infos;
1983   readFieldDoubleDataInMedFile(fileName,meshName,fieldName,iteration,order,typeOfOutField,fieldPerCellType,time,infos);
1984   std::vector<int> familiesToKeep;
1985   std::vector<INTERP_KERNEL::NormalizedCellType> typesToKeep;
1986   if(typeOfOutField==ON_CELLS || typeOfOutField==ON_GAUSS_PT || typeOfOutField==ON_GAUSS_NE)
1987     for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=fieldPerCellType.begin();iter!=fieldPerCellType.end();iter++)
1988       typesToKeep.push_back((*iter).getType());
1989   unsigned meshDim;
1990   int *cellRenum;
1991   if(fieldPerCellType.empty())
1992     {
1993       std::ostringstream oss; oss << "Error on reading file \"" << fileName << "\" meshName=\"" << meshName << "\" meshDimRelToMax=" << meshDimRelToMax;
1994       oss << std::endl << "FieldName=\"" << fieldName << "\" (iteration=" << iteration << ",order=" << order << ")" << std::endl;
1995       if(typeOfOutField==ON_CELLS || typeOfOutField==ON_GAUSS_PT || typeOfOutField==ON_GAUSS_NE)
1996         oss << "Request for cell field, maybe it is a node instead or by changing meshDimRelToMax ?";
1997       else
1998         oss << "Request for a node field, maybe it is a cell field instead ?";
1999       throw INTERP_KERNEL::Exception(oss.str().c_str());
2000       }
2001   MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingUMesh> mesh=readUMeshFromFileLev1(fileName,meshName,meshDimRelToMax,familiesToKeep,typesToKeep,meshDim,cellRenum);
2002   ParaMEDMEM::MEDCouplingFieldDouble *ret=readFieldDoubleLev2(fileName,typeOfOutField,meshDim,cellRenum,mesh,infos,fieldName,iteration,order,time,fieldPerCellType);
2003   if(cellRenum)
2004     mesh->renumberCells(cellRenum,true);
2005   //clean-up
2006   delete [] cellRenum;
2007   releaseMEDFileCoreFrmt<MEDLoader::MEDFieldDoublePerCellType>(fieldPerCellType);
2008   return ret;
2009 }
2010
2011 /// @endcond
2012
2013 MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, const char *meshName, int meshDimRelToMax) throw(INTERP_KERNEL::Exception)
2014 {
2015   CheckFileForRead(fileName);
2016   std::vector<int> familiesToKeep;
2017   std::vector<INTERP_KERNEL::NormalizedCellType> typesToKeep;
2018   unsigned meshDim;
2019   int *cellRenum;
2020   ParaMEDMEM::MEDCouplingUMesh *ret=MEDLoaderNS::readUMeshFromFileLev1(fileName,meshName,meshDimRelToMax,familiesToKeep,typesToKeep,meshDim,cellRenum);
2021   if(cellRenum)
2022     {
2023       ret->renumberCells(cellRenum,true);
2024       delete [] cellRenum;
2025     }
2026   return ret;
2027 }
2028
2029 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, int meshDimRelToMax) throw(INTERP_KERNEL::Exception)
2030 {
2031   CheckFileForRead(fileName);
2032   std::vector<int> familiesToKeep;
2033   std::vector<INTERP_KERNEL::NormalizedCellType> typesToKeep;
2034   unsigned meshDim;
2035   int *cellRenum;
2036   ParaMEDMEM::MEDCouplingUMesh *ret=MEDLoaderNS::readUMeshFromFileLev1(fileName,0,meshDimRelToMax,familiesToKeep,typesToKeep,meshDim,cellRenum);
2037   if(cellRenum)
2038     {
2039       ret->renumberCells(cellRenum,true);
2040       delete [] cellRenum;
2041     }
2042   return ret;
2043 }
2044
2045 int MEDLoader::ReadUMeshDimFromFile(const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception)
2046 {
2047   CheckFileForRead(fileName);
2048   std::vector<int> poss;
2049   return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
2050 }
2051
2052 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFamilies(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<std::string>& fams) throw(INTERP_KERNEL::Exception)
2053 {
2054   CheckFileForRead(fileName);
2055   std::vector<int> familiesToKeep=MEDLoaderNS::getIdsFromFamilies(fileName,meshName,fams);
2056   std::vector<INTERP_KERNEL::NormalizedCellType> typesToKeep;
2057   unsigned meshDim;
2058   int *cellRenum;
2059   ParaMEDMEM::MEDCouplingUMesh *ret=MEDLoaderNS::readUMeshFromFileLev1(fileName,meshName,meshDimRelToMax,familiesToKeep,typesToKeep,meshDim,cellRenum);
2060   if(fams.size()==1)
2061     ret->setName(fams.back().c_str());
2062   if(cellRenum)
2063     {
2064       ret->renumberCells(cellRenum,true);
2065       delete [] cellRenum;
2066     }
2067   return ret;
2068 }
2069
2070 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromGroups(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<std::string>& grps) throw(INTERP_KERNEL::Exception)
2071 {
2072   CheckFileForRead(fileName);
2073   std::vector<int> familiesToKeep=MEDLoaderNS::getIdsFromGroups(fileName,meshName,grps);
2074   std::vector<INTERP_KERNEL::NormalizedCellType> typesToKeep;
2075   unsigned meshDim;
2076   int *cellRenum;
2077   ParaMEDMEM::MEDCouplingUMesh *ret=MEDLoaderNS::readUMeshFromFileLev1(fileName,meshName,meshDimRelToMax,familiesToKeep,typesToKeep,meshDim,cellRenum);
2078   if(grps.size()==1)
2079     ret->setName(grps.back().c_str());
2080   if(cellRenum)
2081     {
2082       ret->renumberCells(cellRenum,true);
2083       delete [] cellRenum;
2084     }
2085   return ret;
2086 }
2087
2088 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)
2089 {
2090   CheckFileForRead(fileName);
2091   switch(type)
2092     {
2093     case ON_CELLS:
2094       return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
2095     case ON_NODES:
2096       return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
2097     case ON_GAUSS_PT:
2098       return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
2099     case ON_GAUSS_NE:
2100       return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
2101     default:
2102       throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
2103     } 
2104 }
2105
2106 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsOnSameMesh(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
2107                                                                                   const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
2108 {
2109   if(its.empty())
2110     return std::vector<ParaMEDMEM::MEDCouplingFieldDouble *>();
2111   CheckFileForRead(fileName);
2112   std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> ret(its.size());
2113   if(its.empty())
2114     return ret;
2115   //Retrieving mesh of rank 0 and field on rank 0 too.
2116   std::list<MEDLoader::MEDFieldDoublePerCellType> fieldPerCellType;
2117   double time;
2118   std::vector<std::string> infos;
2119   MEDLoaderNS::readFieldDoubleDataInMedFile(fileName,meshName,fieldName,its[0].first,its[0].second,type,fieldPerCellType,time,infos);
2120   std::vector<int> familiesToKeep;
2121   std::vector<INTERP_KERNEL::NormalizedCellType> typesToKeep;
2122   if(type==ON_CELLS || type==ON_GAUSS_PT || type==ON_GAUSS_NE)
2123     for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=fieldPerCellType.begin();iter!=fieldPerCellType.end();iter++)
2124       typesToKeep.push_back((*iter).getType());
2125   unsigned meshDim;
2126   int *cellRenum;
2127   MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingUMesh> m1=MEDLoaderNS::readUMeshFromFileLev1(fileName,meshName,meshDimRelToMax,familiesToKeep,typesToKeep,meshDim,cellRenum);
2128   ret[0]=MEDLoaderNS::readFieldDoubleLev2(fileName,type,meshDim,cellRenum,m1,infos,fieldName,its[0].first,its[0].second,time,fieldPerCellType);
2129   if(cellRenum)
2130     m1->renumberCells(cellRenum,true);
2131   MEDLoaderNS::releaseMEDFileCoreFrmt<MEDLoader::MEDFieldDoublePerCellType>(fieldPerCellType);
2132   //
2133   for(int itId=1;itId<(int)its.size();itId++)
2134     {
2135       std::list<MEDLoader::MEDFieldDoublePerCellType> fieldPerCellType2;
2136       double timmee;
2137       std::vector<std::string> infoss;
2138       MEDLoaderNS::readFieldDoubleDataInMedFile(fileName,meshName,fieldName,its[itId].first,its[itId].second,type,fieldPerCellType2,timmee,infoss);
2139       ret[itId]=MEDLoaderNS::readFieldDoubleLev2(fileName,type,meshDim,cellRenum,m1,infoss,fieldName,its[itId].first,its[itId].second,timmee,fieldPerCellType2);
2140       //clean-up
2141       MEDLoaderNS::releaseMEDFileCoreFrmt<MEDLoader::MEDFieldDoublePerCellType>(fieldPerCellType2);
2142     }
2143   delete [] cellRenum;
2144   return ret;
2145 }
2146
2147 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsCellOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
2148                                                                                             const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
2149 {
2150   return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its);
2151 }
2152
2153 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsNodeOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
2154                                                                                       const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
2155 {
2156   return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its);
2157 }
2158
2159 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsGaussOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
2160                                                                                        const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
2161 {
2162   return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its);
2163 }
2164
2165 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsGaussNEOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
2166                                                                                          const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
2167 {
2168   return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
2169 }
2170
2171 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldCell(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception)
2172 {
2173   return MEDLoaderNS::readFieldDoubleLev1(fileName,meshName,meshDimRelToMax,fieldName,iteration,order,ON_CELLS);
2174 }
2175
2176 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldNode(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception)
2177 {
2178   return MEDLoaderNS::readFieldDoubleLev1(fileName,meshName,meshDimRelToMax,fieldName,iteration,order,ON_NODES);
2179 }
2180
2181 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldGauss(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception)
2182 {
2183   return MEDLoaderNS::readFieldDoubleLev1(fileName,meshName,meshDimRelToMax,fieldName,iteration,order,ON_GAUSS_PT);
2184 }
2185
2186 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldGaussNE(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception)
2187 {
2188   return MEDLoaderNS::readFieldDoubleLev1(fileName,meshName,meshDimRelToMax,fieldName,iteration,order,ON_GAUSS_NE);
2189 }
2190
2191 /*!
2192  * @param families input parameter that specifies the field on int on each cells of 'mesh'.
2193  * @param isRenumbering output parameter that specifies if a renumbering of mesh has been needed.
2194  */
2195 void MEDLoaderNS::writeUMeshesDirectly(const char *fileName, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& mesh, const std::vector<const DataArrayInt *>& families, bool forceFromScratch, bool &isRenumbering)
2196 {
2197   med_idt fid=MEDfileOpen(fileName,forceFromScratch?MED_ACC_CREAT:MED_ACC_RDWR);
2198   std::string meshName(mesh[0]->getName());
2199   if(meshName=="")
2200     {
2201       MEDfileClose(fid);
2202       throw INTERP_KERNEL::Exception("MEDCouplingMesh must have a not null name !");
2203     }
2204   isRenumbering=false;
2205   bool isFamilies=true;
2206   std::vector<const DataArrayInt *> conn;
2207   std::vector<const DataArrayInt *> connIndex;
2208   std::set<INTERP_KERNEL::NormalizedCellType> allTypes;
2209   for(std::vector<const ParaMEDMEM::MEDCouplingUMesh *>::const_iterator iter=mesh.begin();iter!=mesh.end();iter++)
2210     {
2211       isRenumbering|=!(*iter)->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
2212       isFamilies&=(families[std::distance(mesh.begin(),iter)]!=0);
2213       conn.push_back((*iter)->getNodalConnectivity());
2214       connIndex.push_back((*iter)->getNodalConnectivityIndex());
2215       const std::set<INTERP_KERNEL::NormalizedCellType>& curTypes=(*iter)->getAllTypes();
2216       allTypes.insert(curTypes.begin(),curTypes.end());
2217     }
2218   INTERP_KERNEL::AutoPtr<char> maa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2219   INTERP_KERNEL::AutoPtr<char> desc=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
2220   MEDLoaderBase::safeStrCpy(meshName.c_str(),MED_NAME_SIZE,maa,MEDLoader::_TOO_LONG_STR);
2221   MEDLoaderBase::safeStrCpy(mesh[0]->getDescription(),MED_COMMENT_SIZE,desc,MEDLoader::_TOO_LONG_STR);
2222   const int spaceDim=mesh[0]->getSpaceDimension();
2223   const int meshDim=mesh[0]->getMeshDimension();
2224   const DataArrayDouble *arr=mesh[0]->getCoords();
2225   INTERP_KERNEL::AutoPtr<char> comp=MEDLoaderBase::buildEmptyString(spaceDim*MED_SNAME_SIZE);
2226   INTERP_KERNEL::AutoPtr<char> unit=MEDLoaderBase::buildEmptyString(spaceDim*MED_SNAME_SIZE);
2227   for(int i=0;i<spaceDim;i++)
2228     {
2229       std::string info=arr->getInfoOnComponent(i);
2230       std::string c,u;
2231       MEDLoaderBase::splitIntoNameAndUnit(info,c,u);
2232       MEDLoaderBase::safeStrCpy2(c.c_str(),MED_SNAME_SIZE-1,comp+i*MED_SNAME_SIZE,MEDLoader::_TOO_LONG_STR);//MED_TAILLE_PNOM-1 to avoid to write '\0' on next compo
2233       MEDLoaderBase::safeStrCpy2(u.c_str(),MED_SNAME_SIZE-1,unit+i*MED_SNAME_SIZE,MEDLoader::_TOO_LONG_STR);//MED_TAILLE_PNOM-1 to avoid to write '\0' on next compo
2234     }
2235   MEDmeshCr(fid,maa,spaceDim,meshDim,MED_UNSTRUCTURED_MESH,desc,"",MED_SORT_DTIT,MED_CARTESIAN,comp,unit);
2236   for(std::vector<const ParaMEDMEM::MEDCouplingUMesh *>::const_iterator iter=mesh.begin();iter!=mesh.end();iter++)
2237     {
2238       for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
2239         {
2240           med_geometry_type curMedType=typmai[i];
2241           INTERP_KERNEL::NormalizedCellType curType=typmai2[i];
2242           if(allTypes.find(curType)!=allTypes.end())
2243             {
2244               std::vector<int> medConn;
2245               std::vector<int> medConnIndex;
2246               std::vector<int> medConnIndex2;
2247               std::vector<int> fam;
2248               std::vector<int> renumber;
2249               int nbOfElt=MEDLoaderNS::buildMEDSubConnectivityOfOneType(conn,connIndex,families,curType,medConn,medConnIndex,medConnIndex2,fam,renumber);
2250               if(curMedType!=MED_POLYGON && curMedType!=MED_POLYHEDRON)
2251                 MEDmeshElementConnectivityWr(fid,maa,-1,-1,0.,MED_CELL,curMedType,MED_NODAL,MED_FULL_INTERLACE,nbOfElt,&medConn[0]);
2252               else
2253                 {
2254                   if(curMedType==MED_POLYGON)
2255                     MEDmeshPolygonWr(fid,maa,-1,-1,0.,MED_CELL,MED_NODAL,medConnIndex.size(),&medConnIndex[0],&medConn[0]);
2256                   if(curMedType==MED_POLYHEDRON)
2257                     {
2258                       MEDmeshPolyhedronWr(fid,maa,-1,-1,0.,MED_CELL,MED_NODAL,medConnIndex2.size(),&medConnIndex2[0],medConnIndex.size(),&medConnIndex[0],
2259                                          &medConn[0]);
2260                     }
2261                 }
2262               if(isFamilies)
2263                 MEDmeshEntityFamilyNumberWr(fid,maa,-1,-1,MED_CELL,curMedType,nbOfElt,&fam[0]);
2264               if(isRenumbering)
2265                 MEDmeshEntityNumberWr(fid,maa,-1,-1,MED_CELL,curMedType,nbOfElt,&renumber[0]);
2266             }
2267         }
2268     }
2269   char familyName[MED_NAME_SIZE+1];
2270   std::fill(familyName,familyName+MED_NAME_SIZE+1,'\0');
2271   const char DftFamilyName[]="DftFamily";
2272   std::copy(DftFamilyName,DftFamilyName+sizeof(DftFamilyName),familyName);
2273   MEDfamilyCr(fid,maa,familyName,0,0,0);
2274   
2275   MEDmeshNodeCoordinateWr(fid,maa,-1,-1,0.,MED_FULL_INTERLACE,mesh[0]->getNumberOfNodes(),arr->getConstPointer());
2276   MEDfileClose(fid);
2277 }
2278
2279 /*!
2280  * In this method meshes are assumed to shared the same coords.
2281  * This method makes the assumption that 'meshes' is not empty, no check on that is done (responsability of the caller)
2282  */
2283 void MEDLoaderNS::writeUMeshesPartitionDirectly(const char *fileName, const char *meshName, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool forceFromScratch)
2284 {
2285   std::string meshNameCpp(meshName);
2286   INTERP_KERNEL::AutoPtr<char> maa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2287   MEDLoaderBase::safeStrCpy(meshName,MED_NAME_SIZE,maa,MEDLoader::_TOO_LONG_STR);
2288   if(meshNameCpp=="")
2289     throw INTERP_KERNEL::Exception("writeUMeshesPartitionDirectly : Invalid meshName : Must be different from \"\" !");
2290   std::vector< DataArrayInt * > corr;
2291   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> m=ParaMEDMEM::MEDCouplingUMesh::FuseUMeshesOnSameCoords(meshes,0,corr);
2292   m->setName(meshName);
2293   std::vector< std::vector<int> > fidsOfGroups;
2294   std::vector< const DataArrayInt * > corr2(corr.begin(),corr.end());
2295   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr2=DataArrayInt::MakePartition(corr2,m->getNumberOfCells(),fidsOfGroups);
2296   for(std::vector< DataArrayInt * >::iterator it=corr.begin();it!=corr.end();it++)
2297     (*it)->decrRef();
2298   bool isRenumbering;
2299   std::vector<const MEDCouplingUMesh *> mv(1); mv[0]=m;
2300   std::vector<const DataArrayInt *> famv(1); famv[0]=arr2;
2301   writeUMeshesDirectly(fileName,mv,famv,forceFromScratch,isRenumbering);
2302   // families creation
2303   std::set<int> familyIds;
2304   for(std::vector< std::vector<int> >::const_iterator it1=fidsOfGroups.begin();it1!=fidsOfGroups.end();it1++)
2305     for(std::vector<int>::const_iterator it2=(*it1).begin();it2!=(*it1).end();it2++)
2306       familyIds.insert(*it2);
2307   std::vector< std::vector<int> > gidsOfFamilies(familyIds.size());
2308   int fid=0;
2309   for(std::set<int>::const_iterator it=familyIds.begin();it!=familyIds.end();it++,fid++)
2310     {
2311       int gid=0;
2312       for(std::vector< std::vector<int> >::const_iterator it1=fidsOfGroups.begin();it1!=fidsOfGroups.end();it1++,gid++)
2313         for(std::vector<int>::const_iterator it2=(*it1).begin();it2!=(*it1).end();it2++)
2314           if(*it2==*it)
2315             gidsOfFamilies[fid].push_back(gid);
2316     }
2317   fid=0;
2318   med_idt fid2=MEDfileOpen(fileName,MED_ACC_RDWR);
2319   for(std::set<int>::const_iterator it=familyIds.begin();it!=familyIds.end();it++,fid++)
2320     {
2321       int ngro=gidsOfFamilies[fid].size();
2322       INTERP_KERNEL::AutoPtr<char> groName=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE*ngro);
2323       for(int i=0;i<ngro;i++)
2324         MEDLoaderBase::safeStrCpy2(meshes[gidsOfFamilies[fid][i]]->getName(),MED_LNAME_SIZE-1,groName+i*MED_LNAME_SIZE,MEDLoader::_TOO_LONG_STR);//MED_LNAME_SIZE-1 to avoid to write '\0' on next compo
2325       std::ostringstream oss; oss << "Family_" << *it;
2326       INTERP_KERNEL::AutoPtr<char> famName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2327       MEDLoaderBase::safeStrCpy(oss.str().c_str(),MED_NAME_SIZE,famName,MEDLoader::_TOO_LONG_STR);
2328       MEDfamilyCr(fid2,maa,famName,*it,ngro,groName);
2329     }
2330   MEDfileClose(fid2);
2331 }
2332
2333 /*!
2334  * This method makes the assumption that f->getMesh() nodes are fully included in already written mesh in 'fileName'.
2335  * @param thisMeshNodeIds points to a tab of size f->getMesh()->getNumberOfNodes() that says for a node i in f->getMesh() that its id is thisMeshNodeIds[i] is already written mesh.
2336  */
2337 void MEDLoaderNS::appendNodeProfileField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, const int *thisMeshNodeIds)
2338 {
2339   med_int numdt,numo;
2340   med_float dt;
2341   INTERP_KERNEL::AutoPtr<char> nommaa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2342   MEDLoaderBase::safeStrCpy(f->getMesh()->getName(),MED_NAME_SIZE,nommaa,MEDLoader::_TOO_LONG_STR);
2343   med_idt fid=appendFieldSimpleAtt(fileName,f,numdt,numo,dt);
2344   int nbOfNodes=f->getMesh()->getNumberOfNodes();
2345   const double *pt=f->getArray()->getConstPointer();
2346   INTERP_KERNEL::AutoPtr<int> profile=new int[nbOfNodes];
2347   std::ostringstream oss; oss << "Pfln" << f->getName();
2348   INTERP_KERNEL::AutoPtr<char> profileName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2349   MEDLoaderBase::safeStrCpy(oss.str().c_str(),MED_NAME_SIZE,profileName,MEDLoader::_TOO_LONG_STR);
2350   std::transform(thisMeshNodeIds,thisMeshNodeIds+nbOfNodes,(int *)profile,std::bind2nd(std::plus<int>(),1));
2351   MEDprofileWr(fid,profileName,nbOfNodes,profile);
2352   MEDfieldValueWithProfileWr(fid,f->getName(),numdt,numo,dt,MED_NODE,MED_NONE,MED_COMPACT_PFLMODE,profileName,MED_NO_LOCALIZATION,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nbOfNodes,(const unsigned char*)pt);
2353   MEDfileClose(fid);
2354 }
2355
2356 /*!
2357  * This method makes the assumption that f->getMesh() cells are fully included in already written mesh in 'fileName'.
2358  * @param thisMeshCellIdsPerType points to a tab of size f->getMesh()->getNumberOfCells() that says for a cell i in f->getMesh() that its id is thisMeshCellIds[i] of corresponding type is already written mesh.
2359  */
2360 void MEDLoaderNS::appendCellProfileField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, const int *thisMeshCellIdsPerType)
2361 {
2362   med_int numdt,numo;
2363   med_float dt;
2364   int nbComp=f->getNumberOfComponents();
2365   med_idt fid=appendFieldSimpleAtt(fileName,f,numdt,numo,dt);
2366   std::list<MEDLoader::MEDFieldDoublePerCellType> split;
2367   prepareCellFieldDoubleForWriting(f,thisMeshCellIdsPerType,split);
2368   const double *pt=f->getArray()->getConstPointer();
2369   int number=0;
2370   for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=split.begin();iter!=split.end();iter++)
2371     {
2372       INTERP_KERNEL::AutoPtr<char> nommaa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2373       MEDLoaderBase::safeStrCpy(f->getMesh()->getName(),MED_NAME_SIZE,nommaa,MEDLoader::_TOO_LONG_STR);
2374       INTERP_KERNEL::AutoPtr<char> profileName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2375       std::ostringstream oss; oss << "Pfl" << f->getName() << "_" << number++;
2376       MEDLoaderBase::safeStrCpy(oss.str().c_str(),MED_NAME_SIZE,profileName,MEDLoader::_TOO_LONG_STR);
2377       const std::vector<int>& ids=(*iter).getCellIdPerType();
2378       int *profile=new int [ids.size()];
2379       std::transform(ids.begin(),ids.end(),profile,std::bind2nd(std::plus<int>(),1));
2380       MEDprofileWr(fid,profileName,ids.size(),profile);
2381       delete [] profile;
2382       MEDfieldValueWithProfileWr(fid,f->getName(),numdt,numo,dt,MED_CELL,typmai3[(int)(*iter).getType()],MED_COMPACT_PFLMODE,profileName,
2383                                  MED_NO_LOCALIZATION,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,(*iter).getNbOfTuple(),(const unsigned char*)pt);
2384       pt+=(*iter).getNbOfTuple()*nbComp;
2385     }
2386   MEDfileClose(fid);
2387 }
2388
2389 void MEDLoaderNS::appendNodeElementProfileField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, const int *thisMeshCellIdsPerType)
2390 {
2391   med_int numdt,numo;
2392   med_float dt;
2393   int nbComp=f->getNumberOfComponents();
2394   med_idt fid=appendFieldSimpleAtt(fileName,f,numdt,numo,dt);
2395   std::list<MEDLoader::MEDFieldDoublePerCellType> split;
2396   prepareCellFieldDoubleForWriting(f,thisMeshCellIdsPerType,split);
2397   const double *pt=f->getArray()->getConstPointer();
2398   int number=0;
2399   for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=split.begin();iter!=split.end();iter++)
2400     {
2401       INTERP_KERNEL::AutoPtr<char> nommaa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2402       MEDLoaderBase::safeStrCpy(f->getMesh()->getName(),MED_NAME_SIZE,nommaa,MEDLoader::_TOO_LONG_STR);
2403       INTERP_KERNEL::AutoPtr<char> profileName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2404       std::ostringstream oss; oss << "Pfl" << f->getName() << "_" << number++;
2405       MEDLoaderBase::safeStrCpy(oss.str().c_str(),MED_NAME_SIZE,profileName,MEDLoader::_TOO_LONG_STR);
2406       const std::vector<int>& ids=(*iter).getCellIdPerType();
2407       int *profile=new int [ids.size()];
2408       std::transform(ids.begin(),ids.end(),profile,std::bind2nd(std::plus<int>(),1));
2409       MEDprofileWr(fid,profileName,ids.size(),profile);
2410       delete [] profile;
2411       int nbPtPerCell=(int)INTERP_KERNEL::CellModel::GetCellModel((*iter).getType()).getNumberOfNodes();
2412       int nbOfEntity=f->getMesh()->getNumberOfCellsWithType((*iter).getType());
2413       int nbOfValues=nbPtPerCell*nbOfEntity;
2414       MEDfieldValueWithProfileWr(fid,f->getName(),numdt,numo,dt,MED_NODE_ELEMENT,typmai3[(int)(*iter).getType()],
2415                                  MED_COMPACT_PFLMODE,profileName,
2416                                  MED_NO_LOCALIZATION,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,
2417                                  nbOfEntity,(const unsigned char*)pt);
2418       pt+=nbOfValues*nbComp;
2419     }
2420   MEDfileClose(fid);
2421 }
2422
2423 /*!
2424  * This method performs the classical job for fields before any values setting.
2425  */
2426 med_idt MEDLoaderNS::appendFieldSimpleAtt(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, med_int& numdt, med_int& numo, med_float& dt)
2427 {
2428   std::string fieldName(f->getName());
2429   if(fieldName.empty())
2430     throw INTERP_KERNEL::Exception("MEDLoaderNS::appendFieldSimpleAtt : Trying to store a field with no name ! MED file format requires a NON EMPTY field name !");
2431   med_idt fid=MEDfileOpen(fileName,MED_ACC_RDWR);
2432   int nbComp=f->getNumberOfComponents();
2433   INTERP_KERNEL::AutoPtr<char> comp=MEDLoaderBase::buildEmptyString(nbComp*MED_SNAME_SIZE);
2434   INTERP_KERNEL::AutoPtr<char> unit=MEDLoaderBase::buildEmptyString(nbComp*MED_SNAME_SIZE);
2435   for(int i=0;i<nbComp;i++)
2436     {
2437       std::string info=f->getArray()->getInfoOnComponent(i);
2438       std::string c,u;
2439       MEDLoaderBase::splitIntoNameAndUnit(info,c,u);
2440       MEDLoaderBase::safeStrCpy2(c.c_str(),MED_SNAME_SIZE-1,comp+i*MED_SNAME_SIZE,MEDLoader::_TOO_LONG_STR);
2441       MEDLoaderBase::safeStrCpy2(u.c_str(),MED_SNAME_SIZE-1,unit+i*MED_SNAME_SIZE,MEDLoader::_TOO_LONG_STR);
2442     }
2443   INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
2444   INTERP_KERNEL::AutoPtr<char> maaname=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2445   INTERP_KERNEL::AutoPtr<char> fname=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2446   MEDLoaderBase::safeStrCpy(f->getName(),MED_NAME_SIZE,fname,MEDLoader::_TOO_LONG_STR);
2447   MEDLoaderBase::safeStrCpy(f->getMesh()->getName(),MED_NAME_SIZE,maaname,MEDLoader::_TOO_LONG_STR);
2448   MEDLoaderBase::safeStrCpy(f->getTimeUnit(),MED_SNAME_SIZE,dt_unit,MEDLoader::_TOO_LONG_STR);
2449   MEDfieldCr(fid,fname,MED_FLOAT64,nbComp,comp,unit,dt_unit,maaname);
2450   ParaMEDMEM::TypeOfTimeDiscretization td=f->getTimeDiscretization();
2451   if(td==ParaMEDMEM::NO_TIME)
2452     {
2453       numdt=MED_NO_DT; numo=MED_NO_IT; dt=0.0;
2454     }
2455   else if(td==ParaMEDMEM::ONE_TIME)
2456     {
2457       int tmp1,tmp2;
2458       double tmp0=f->getTime(tmp1,tmp2);
2459       numdt=(med_int)tmp1; numo=(med_int)tmp2;
2460       dt=(med_float)tmp0;
2461     }
2462   return fid;
2463 }
2464
2465 void MEDLoaderNS::appendFieldDirectly(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f2)
2466 {
2467   med_int numdt,numo;
2468   med_float dt;
2469   //renumbering
2470   const ParaMEDMEM::MEDCouplingFieldDouble *f=f2;
2471   const MEDCouplingMesh *mesh=f->getMesh();
2472   const MEDCouplingUMesh *meshC=dynamic_cast<const MEDCouplingUMesh *>(mesh);
2473   if(!meshC)
2474     throw INTERP_KERNEL::Exception("Not implemented yet for not unstructured mesh !");
2475   bool renum=!meshC->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
2476   if(renum)
2477     {
2478       ParaMEDMEM::MEDCouplingFieldDouble *f3=f2->clone(true);
2479       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=meshC->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
2480       f3->renumberCells(da->getConstPointer(),false);
2481       f=f3;
2482     }
2483   //end renumbering
2484   int nbComp=f->getNumberOfComponents();
2485   med_idt fid=appendFieldSimpleAtt(fileName,f,numdt,numo,dt);
2486   const double *pt=f->getArray()->getConstPointer();
2487   INTERP_KERNEL::AutoPtr<char> nommaa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2488   MEDLoaderBase::safeStrCpy(f->getMesh()->getName(),MED_NAME_SIZE,nommaa,MEDLoader::_TOO_LONG_STR);
2489   switch(f->getTypeOfField())
2490     {
2491     case ParaMEDMEM::ON_CELLS:
2492       {
2493         std::list<MEDLoader::MEDFieldDoublePerCellType> split;
2494         prepareCellFieldDoubleForWriting(f,0,split);
2495         for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=split.begin();iter!=split.end();iter++)
2496           {
2497             MEDfieldValueWithProfileWr(fid,f->getName(),numdt,numo,dt,MED_CELL,typmai3[(int)(*iter).getType()],MED_COMPACT_PFLMODE,
2498                                        MED_ALLENTITIES_PROFILE,MED_NO_LOCALIZATION,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,(*iter).getNbOfTuple(),(const unsigned char*)pt);
2499             pt+=(*iter).getNbOfTuple()*nbComp;
2500           }
2501         break;
2502       }
2503     case ParaMEDMEM::ON_NODES:
2504       {
2505         int nbOfTuples=f->getArray()->getNumberOfTuples();
2506         MEDfieldValueWithProfileWr(fid,f->getName(),numdt,numo,dt,MED_NODE,MED_NONE,MED_COMPACT_PFLMODE,
2507                                    MED_ALLENTITIES_PROFILE,MED_NO_LOCALIZATION,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nbOfTuples,(const unsigned char*)pt);
2508         break;
2509       }
2510     case ParaMEDMEM::ON_GAUSS_PT:
2511       {
2512         std::list<MEDLoader::MEDFieldDoublePerCellType> split;
2513         prepareCellFieldDoubleForWriting(f,0,split);
2514         int idGp=0;
2515         for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=split.begin();iter!=split.end();iter++)
2516           {
2517             INTERP_KERNEL::AutoPtr<char> nomGauss=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2518             std::ostringstream oss; oss << "GP_" << f->getName() << idGp++;
2519             MEDLoaderBase::safeStrCpy(oss.str().c_str(),MED_NAME_SIZE,nomGauss,MEDLoader::_TOO_LONG_STR);
2520             int id=f->getGaussLocalizationIdOfOneType((*iter).getType());
2521             const MEDCouplingGaussLocalization& gl=f->getGaussLocalization(id);
2522             MEDlocalizationWr(fid,nomGauss,typmai3[(int)(*iter).getType()],mesh->getMeshDimension(),&gl.getRefCoords()[0],MED_FULL_INTERLACE,
2523                               gl.getNumberOfGaussPt(),&gl.getGaussCoords()[0],&gl.getWeights()[0],MED_NO_INTERPOLATION, MED_NO_MESH_SUPPORT);
2524             int nbOfEntity=f->getMesh()->getNumberOfCellsWithType((*iter).getType());
2525             int nbOfValues=gl.getNumberOfGaussPt()*nbOfEntity;
2526             INTERP_KERNEL::AutoPtr<char> fieldname=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
2527             MEDLoaderBase::safeStrCpy(f->getName(),MED_NAME_SIZE,fieldname,MEDLoader::_TOO_LONG_STR);
2528             MEDfieldValueWithProfileWr(fid,fieldname,numdt,numo,dt,MED_CELL,typmai3[(int)(*iter).getType()],MED_COMPACT_PFLMODE,
2529                                        MED_ALLENTITIES_PROFILE,nomGauss,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nbOfEntity,(const unsigned char*)pt);
2530             pt+=nbOfValues*nbComp;
2531           }
2532         break;
2533       }
2534     case ParaMEDMEM::ON_GAUSS_NE:
2535       {
2536         std::list<MEDLoader::MEDFieldDoublePerCellType> split;
2537         prepareCellFieldDoubleForWriting(f,0,split);
2538         for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=split.begin();iter!=split.end();iter++)
2539           {
2540             int nbPtPerCell=(int)INTERP_KERNEL::CellModel::GetCellModel((*iter).getType()).getNumberOfNodes();
2541             int nbOfEntity=f->getMesh()->getNumberOfCellsWithType((*iter).getType());
2542             int nbOfValues=nbPtPerCell*nbOfEntity;
2543             MEDfieldValueWithProfileWr(fid,f->getName(),numdt,numo,dt,MED_NODE_ELEMENT,typmai3[(int)(*iter).getType()],MED_COMPACT_PFLMODE,
2544                                        MED_ALLENTITIES_PROFILE,MED_NO_LOCALIZATION,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,nbOfEntity,(const unsigned char*)pt);
2545             pt+=nbOfValues*nbComp;
2546           }
2547         break;
2548       }
2549     default:
2550       throw INTERP_KERNEL::Exception("Not managed this type of FIELD !");
2551     }
2552   MEDfileClose(fid);
2553   if(renum)
2554     f->decrRef();
2555 }
2556
2557 /*!
2558  * This method splits field 'f' into types to be ready for writing.
2559  * @param cellIdsPerType this parameter can be 0 if not in profile mode. If it is != 0 this array is of size f->getMesh()->getNumberOfCells().
2560  */
2561 void MEDLoaderNS::prepareCellFieldDoubleForWriting(const ParaMEDMEM::MEDCouplingFieldDouble *f, const int *cellIdsPerType, std::list<MEDLoader::MEDFieldDoublePerCellType>& split)
2562 {
2563   int nbComp=f->getNumberOfComponents();
2564   const MEDCouplingMesh *mesh=f->getMesh();
2565   const MEDCouplingUMesh *meshC=dynamic_cast<const MEDCouplingUMesh *>(mesh);
2566   if(!meshC)
2567     throw INTERP_KERNEL::Exception("Not implemented yet for not unstructured mesh !");
2568   if(!meshC->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO))
2569     throw INTERP_KERNEL::Exception("Unstructuded mesh has not consecutive cell types !");
2570   const int *connI=meshC->getNodalConnectivityIndex()->getConstPointer();
2571   const int *conn=meshC->getNodalConnectivity()->getConstPointer();
2572   int nbOfCells=meshC->getNumberOfCells();
2573   INTERP_KERNEL::NormalizedCellType curType;
2574   const int *wCellIdsPT=cellIdsPerType;
2575   for(const int *pt=connI;pt!=connI+nbOfCells;)
2576     {
2577       curType=(INTERP_KERNEL::NormalizedCellType)conn[*pt];
2578       const int *pt2=std::find_if(pt+1,connI+nbOfCells,ConnReaderML(conn,(int)curType));
2579       if(!cellIdsPerType)
2580         split.push_back(MEDLoader::MEDFieldDoublePerCellType(curType,0,nbComp,pt2-pt,0,0));
2581       else
2582         {
2583           split.push_back(MEDLoader::MEDFieldDoublePerCellType(curType,0,nbComp,pt2-pt,wCellIdsPT,0));
2584           wCellIdsPT+=std::distance(pt,pt2);
2585         }
2586       pt=pt2;
2587     }
2588 }
2589
2590 void MEDLoaderNS::writeFieldAndMeshDirectly(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool forceFromScratch)
2591 {
2592   f->checkCoherency();
2593   std::string meshName(f->getMesh()->getName());
2594   if(meshName.empty())
2595     throw INTERP_KERNEL::Exception("Trying to write a mesh (f->getMesh()) with no name ! MED file format needs a not empty mesh name !");
2596   std::string fieldName(f->getName());
2597   if(fieldName.empty())
2598     throw INTERP_KERNEL::Exception("Trying to write a field with no name ! MED file format needs a not empty field name !");
2599   MEDCouplingUMesh *mesh=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh()));
2600   if(mesh)
2601     {
2602       bool isRenumbering;
2603       std::vector<const MEDCouplingUMesh *> meshV(1); meshV[0]=mesh;
2604       std::vector<const DataArrayInt *> famV(1); famV[0]=0;
2605       writeUMeshesDirectly(fileName,meshV,famV,forceFromScratch,isRenumbering);
2606       if(isRenumbering)
2607         {
2608           MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> f2=f->clone(true);
2609           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=mesh->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
2610           f2->renumberCells(da->getConstPointer(),false);
2611           appendFieldDirectly(fileName,f2);
2612         }
2613       else
2614         appendFieldDirectly(fileName,f);
2615       return ;
2616     }
2617   throw INTERP_KERNEL::Exception("The mesh underlying field is not unstructured ! Only unstructured mesh supported for writting now !");
2618 }
2619
2620 /*!
2621  * When called this method expectes that file 'fileName' is already existing and has a mesh with name equal to
2622  * f->getMesh()->getName(). If not the behaviour of this method is not warranted.
2623  * This method reads the corresponding mesh into the file and try to fit it with f->getMesh().
2624  * If it appears that f->getMesh() equals exactly mesh into the file 
2625  */
2626 void MEDLoaderNS::writeFieldTryingToFitExistingMesh(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f)
2627 {
2628   std::vector<int> poss;
2629   int mDimInFile=MEDLoaderNS::readUMeshDimFromFile(fileName,f->getMesh()->getName(),poss);
2630   int mdim=f->getMesh()->getMeshDimension();
2631   int f2=mdim-mDimInFile;
2632   if(std::find(poss.begin(),poss.end(),f2)==poss.end())
2633     {
2634       std::ostringstream oss; oss << "Trying to fit with the existing \"" << f->getMesh()->getName() << "mesh in file \"" << fileName;
2635       oss << "\" but meshdimension does not match !";
2636       throw INTERP_KERNEL::Exception(oss.str().c_str());
2637     }
2638   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> m=MEDLoader::ReadUMeshFromFile(fileName,f->getMesh()->getName(),f2);
2639   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> m2=MEDCouplingUMesh::MergeUMeshes(m,(MEDCouplingUMesh *)f->getMesh());
2640   bool areNodesMerged;
2641   int newNbOfNodes;
2642   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=m2->mergeNodes(MEDLoader::_EPS_FOR_NODE_COMP,areNodesMerged,newNbOfNodes);
2643   if(!areNodesMerged || newNbOfNodes!=m->getNumberOfNodes())
2644     {
2645       std::ostringstream oss; oss << "Nodes in already written mesh \"" << f->getMesh()->getName() << "\" in file \"" << fileName << "\" does not fit coordinates of unstructured grid f->getMesh() !";
2646       throw INTERP_KERNEL::Exception(oss.str().c_str());
2647     }
2648   switch(f->getTypeOfField())
2649     {
2650     case ParaMEDMEM::ON_CELLS:
2651       {
2652         MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da2=m2->zipConnectivityTraducer(MEDLoader::_COMP_FOR_CELL);
2653         if(m2->getNumberOfCells()!=m->getNumberOfCells())
2654           {
2655             std::ostringstream oss1; oss1 << "Cells in already written mesh \"" << f->getMesh()->getName() << "\" in file \"" << fileName << "\" does not fit connectivity of unstructured grid f->getMesh() !";
2656             throw INTERP_KERNEL::Exception(oss1.str().c_str());
2657           }
2658         da=m2->convertCellArrayPerGeoType(da2);
2659         MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da3=da->substr(m2->getNumberOfCells());
2660         da2=m2->convertCellArrayPerGeoType(da3);
2661         appendCellProfileField(fileName,f,da2->getConstPointer());
2662         break;
2663       }
2664     case ParaMEDMEM::ON_GAUSS_NE:
2665       {
2666         MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da2=m2->zipConnectivityTraducer(MEDLoader::_COMP_FOR_CELL);
2667         if(m2->getNumberOfCells()!=m->getNumberOfCells())
2668           {
2669             std::ostringstream oss1; oss1 << "Cells in already written mesh \"" << f->getMesh()->getName() << "\" in file \"" << fileName << "\" does not fit connectivity of unstructured grid f->getMesh() !";
2670             throw INTERP_KERNEL::Exception(oss1.str().c_str());
2671           }
2672         da=m2->convertCellArrayPerGeoType(da2);
2673         MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da3=da->substr(m2->getNumberOfCells());
2674         da2=m2->convertCellArrayPerGeoType(da3);
2675         appendNodeElementProfileField(fileName,f,da2->getConstPointer());
2676         break;
2677       }
2678     case ParaMEDMEM::ON_NODES:
2679       {
2680         appendNodeProfileField(fileName,f,da->getConstPointer()+m->getNumberOfNodes());
2681         break;
2682       }
2683     default:
2684       {
2685         throw INTERP_KERNEL::Exception("Not implemented other profile fitting from already written mesh for fields than on NODES and on CELLS.");
2686       }
2687     }
2688 }
2689
2690 void MEDLoader::WriteUMesh(const char *fileName, const ParaMEDMEM::MEDCouplingUMesh *mesh, bool writeFromScratch) throw(INTERP_KERNEL::Exception)
2691 {
2692   std::string meshName(mesh->getName());
2693   if(meshName.empty())
2694     throw INTERP_KERNEL::Exception("Trying to write a unstructured mesh with no name ! MED file format needs a not empty mesh name !");
2695   int status=MEDLoaderBase::getStatusOfFile(fileName);
2696   bool isRenumbering;
2697   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
2698     {
2699       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
2700       throw INTERP_KERNEL::Exception(oss.str().c_str());
2701     }
2702   std::vector<const ParaMEDMEM::MEDCouplingUMesh *> meshV(1); meshV[0]=mesh;
2703   std::vector<const ParaMEDMEM::DataArrayInt *> famV(1); famV[0]=0;
2704   if(writeFromScratch)
2705     {
2706       MEDLoaderNS::writeUMeshesDirectly(fileName,meshV,famV,true,isRenumbering);
2707       return ;
2708     }
2709   if(status==MEDLoaderBase::NOT_EXIST)
2710     {
2711       MEDLoaderNS::writeUMeshesDirectly(fileName,meshV,famV,true,isRenumbering);
2712       return;
2713     }
2714   else
2715     {
2716       std::vector<std::string> meshNames=GetMeshNames(fileName);
2717       if(std::find(meshNames.begin(),meshNames.end(),meshName)==meshNames.end())
2718         MEDLoaderNS::writeUMeshesDirectly(fileName,meshV,famV,false,isRenumbering);
2719       else
2720         {
2721           std::ostringstream oss; oss << "File \'" << fileName << "\' already exists and has already a mesh called \"";
2722           oss << meshName << "\" !";
2723           throw INTERP_KERNEL::Exception(oss.str().c_str());
2724         }
2725     }
2726 }
2727
2728 void MEDLoader::WriteUMeshDep(const char *fileName, const ParaMEDMEM::MEDCouplingUMesh *mesh, bool writeFromScratch) throw(INTERP_KERNEL::Exception)
2729 {
2730   std::string meshName(mesh->getName());
2731   if(meshName.empty())
2732     throw INTERP_KERNEL::Exception("Trying to write a unstructured mesh with no name ! MED file format needs a not empty mesh name !");
2733   int status=MEDLoaderBase::getStatusOfFile(fileName);
2734   bool isRenumbering;
2735   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
2736     {
2737       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
2738       throw INTERP_KERNEL::Exception(oss.str().c_str());
2739     }
2740   std::vector<const ParaMEDMEM::MEDCouplingUMesh *> meshV(1); meshV[0]=mesh;
2741   std::vector<const ParaMEDMEM::DataArrayInt *> famV(1); famV[0]=0;
2742   if(writeFromScratch)
2743     {
2744       MEDLoaderNS::writeUMeshesDirectly(fileName,meshV,famV,true,isRenumbering);
2745       return ;
2746     }
2747   if(status==MEDLoaderBase::NOT_EXIST)
2748     {
2749       MEDLoaderNS::writeUMeshesDirectly(fileName,meshV,famV,true,isRenumbering);
2750       return;
2751     }
2752   else
2753     MEDLoaderNS::writeUMeshesDirectly(fileName,meshV,famV,false,isRenumbering);
2754 }
2755
2756 void MEDLoader::WriteUMeshesPartition(const char *fileName, const char *meshNameC, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch) throw(INTERP_KERNEL::Exception)
2757 {
2758   std::string meshName(meshNameC);
2759   if(meshName.empty())
2760     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 !");
2761   int status=MEDLoaderBase::getStatusOfFile(fileName);
2762   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
2763     {
2764       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
2765       throw INTERP_KERNEL::Exception(oss.str().c_str());
2766     }
2767   if(meshes.empty())
2768     throw INTERP_KERNEL::Exception("List of meshes must be not empty !");
2769   const DataArrayDouble *coords=meshes.front()->getCoords();
2770   for(std::vector<const ParaMEDMEM::MEDCouplingUMesh *>::const_iterator iter=meshes.begin();iter!=meshes.end();iter++)
2771     if(coords!=(*iter)->getCoords())
2772       throw INTERP_KERNEL::Exception("Meshes does not not share the same coordinates : try method MEDCouplingPointSet::tryToShareSameCoords !");
2773   std::set<std::string> tmp;
2774   for(std::vector<const ParaMEDMEM::MEDCouplingUMesh *>::const_iterator iter=meshes.begin();iter!=meshes.end();iter++)
2775     {
2776       if(tmp.find((*iter)->getName())==tmp.end())
2777         tmp.insert((*iter)->getName());
2778       else
2779         throw INTERP_KERNEL::Exception("The names of meshes must be different each other !");
2780     }
2781   tmp.clear();
2782   if(writeFromScratch)
2783     {
2784       MEDLoaderNS::writeUMeshesPartitionDirectly(fileName,meshNameC,meshes,true);
2785       return ;
2786     }
2787   if(status==MEDLoaderBase::NOT_EXIST)
2788     {
2789       MEDLoaderNS::writeUMeshesPartitionDirectly(fileName,meshNameC,meshes,true);
2790       return;
2791     }
2792   else
2793     {
2794       std::vector<std::string> meshNames=GetMeshNames(fileName);
2795       if(std::find(meshNames.begin(),meshNames.end(),meshName)==meshNames.end())
2796         MEDLoaderNS::writeUMeshesPartitionDirectly(fileName,meshNameC,meshes,false);
2797       else
2798         {
2799           std::ostringstream oss; oss << "File \'" << fileName << "\' already exists and has already a mesh called \"";
2800           oss << meshName << "\" !";
2801           throw INTERP_KERNEL::Exception(oss.str().c_str());
2802         }
2803     }
2804 }
2805
2806 void MEDLoader::WriteUMeshesPartitionDep(const char *fileName, const char *meshNameC, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch) throw(INTERP_KERNEL::Exception)
2807 {
2808   std::string meshName(meshNameC);
2809   if(meshName.empty())
2810     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 !");
2811   int status=MEDLoaderBase::getStatusOfFile(fileName);
2812   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
2813     {
2814       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
2815       throw INTERP_KERNEL::Exception(oss.str().c_str());
2816     }
2817   if(meshes.empty())
2818     throw INTERP_KERNEL::Exception("List of meshes must be not empty !");
2819   const DataArrayDouble *coords=meshes.front()->getCoords();
2820   for(std::vector<const ParaMEDMEM::MEDCouplingUMesh *>::const_iterator iter=meshes.begin();iter!=meshes.end();iter++)
2821     if(coords!=(*iter)->getCoords())
2822       throw INTERP_KERNEL::Exception("Meshes does not not share the same coordinates : try method MEDCouplingPointSet::tryToShareSameCoords !");
2823   std::set<std::string> tmp;
2824   for(std::vector<const ParaMEDMEM::MEDCouplingUMesh *>::const_iterator iter=meshes.begin();iter!=meshes.end();iter++)
2825     {
2826       if(tmp.find((*iter)->getName())==tmp.end())
2827         tmp.insert((*iter)->getName());
2828       else
2829         throw INTERP_KERNEL::Exception("The names of meshes must be different each other !");
2830     }
2831   tmp.clear();
2832   if(writeFromScratch)
2833     {
2834       MEDLoaderNS::writeUMeshesPartitionDirectly(fileName,meshNameC,meshes,true);
2835       return ;
2836     }
2837   if(status==MEDLoaderBase::NOT_EXIST)
2838     {
2839       MEDLoaderNS::writeUMeshesPartitionDirectly(fileName,meshNameC,meshes,true);
2840       return;
2841     }
2842   else
2843     {
2844       MEDLoaderNS::writeUMeshesPartitionDirectly(fileName,meshNameC,meshes,false);
2845     }
2846 }
2847
2848 void MEDLoader::WriteUMeshes(const char *fileName, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch) throw(INTERP_KERNEL::Exception)
2849 {
2850   int status=MEDLoaderBase::getStatusOfFile(fileName);
2851   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
2852     {
2853       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
2854       throw INTERP_KERNEL::Exception(oss.str().c_str());
2855     }
2856   if(meshes.empty())
2857     throw INTERP_KERNEL::Exception("List of meshes must be not empty !");
2858   std::string meshName(meshes[0]->getName());
2859   if(meshName.empty())
2860     throw INTERP_KERNEL::Exception("Trying to write a unstructured mesh with no name ! MED file format needs a not empty mesh name : change name of first element of 2nd parameter !");
2861   const DataArrayDouble *coords=meshes.front()->getCoords();
2862   for(std::vector<const ParaMEDMEM::MEDCouplingUMesh *>::const_iterator iter=meshes.begin();iter!=meshes.end();iter++)
2863     if(coords!=(*iter)->getCoords())
2864       throw INTERP_KERNEL::Exception("Meshes does not not share the same coordinates : try method MEDCouplingPointSet::tryToShareSameCoords !");
2865   std::set<int> tmp;
2866   for(std::vector<const ParaMEDMEM::MEDCouplingUMesh *>::const_iterator iter=meshes.begin();iter!=meshes.end();iter++)
2867     {
2868       if(tmp.find((*iter)->getMeshDimension())==tmp.end())
2869         tmp.insert((*iter)->getMeshDimension());
2870       else
2871         throw INTERP_KERNEL::Exception("The mesh dimension of meshes must be different each other !");
2872     }
2873   tmp.clear();
2874   bool isRenumbering;
2875   std::vector<const DataArrayInt *> families(meshes.size());
2876   if(writeFromScratch)
2877     {
2878       MEDLoaderNS::writeUMeshesDirectly(fileName,meshes,families,true,isRenumbering);
2879       return ;
2880     }
2881   if(status==MEDLoaderBase::NOT_EXIST)
2882     {
2883       MEDLoaderNS::writeUMeshesDirectly(fileName,meshes,families,true,isRenumbering);
2884       return;
2885     }
2886   else
2887     {
2888       MEDLoaderNS::writeUMeshesDirectly(fileName,meshes,families,false,isRenumbering);
2889       return;
2890     }
2891 }
2892
2893 void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch) throw(INTERP_KERNEL::Exception)
2894 {
2895   int status=MEDLoaderBase::getStatusOfFile(fileName);
2896   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
2897     {
2898       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
2899       throw INTERP_KERNEL::Exception(oss.str().c_str());
2900     }
2901   if(writeFromScratch)
2902     {
2903       MEDLoaderNS::writeFieldAndMeshDirectly(fileName,f,true);
2904      return ;
2905     }
2906   if(status==MEDLoaderBase::NOT_EXIST)
2907     {
2908      MEDLoaderNS::writeFieldAndMeshDirectly(fileName,f,true);
2909      return ;
2910     }
2911   else
2912     {
2913       std::vector<std::string> meshNames=GetMeshNames(fileName);
2914       std::string fileNameCpp(f->getMesh()->getName());
2915       if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
2916         MEDLoaderNS::writeFieldAndMeshDirectly(fileName,f,false);
2917       else
2918         MEDLoaderNS::writeFieldTryingToFitExistingMesh(fileName,f);
2919     }
2920 }
2921
2922 void MEDLoader::WriteFieldDep(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch) throw(INTERP_KERNEL::Exception)
2923 {
2924   int status=MEDLoaderBase::getStatusOfFile(fileName);
2925   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
2926     {
2927       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
2928       throw INTERP_KERNEL::Exception(oss.str().c_str());
2929     }
2930   if(writeFromScratch)
2931     {
2932       MEDLoaderNS::writeFieldAndMeshDirectly(fileName,f,true);
2933      return ;
2934     }
2935   if(status==MEDLoaderBase::NOT_EXIST)
2936     {
2937      MEDLoaderNS::writeFieldAndMeshDirectly(fileName,f,true);
2938      return ;
2939     }
2940   else
2941     MEDLoaderNS::writeFieldAndMeshDirectly(fileName,f,false);
2942 }
2943
2944 void MEDLoader::WriteFieldUsingAlreadyWrittenMesh(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f) throw(INTERP_KERNEL::Exception)
2945 {
2946   f->checkCoherency();
2947   int status=MEDLoaderBase::getStatusOfFile(fileName);
2948   if(status!=MEDLoaderBase::EXIST_RW)
2949     {
2950       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
2951       throw INTERP_KERNEL::Exception(oss.str().c_str());
2952     }
2953   MEDLoaderNS::appendFieldDirectly(fileName,f);
2954 }