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