Salome HOME
Revert "Synchronize adm files"
[modules/med.git] / src / MEDLoader / MEDFileParameter.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDFileParameter.hxx"
22 #include "MEDFileUtilities.hxx"
23 #include "MEDLoaderBase.hxx"
24
25 #include "InterpKernelAutoPtr.hxx"
26
27 #include <set>
28
29 using namespace ParaMEDMEM;
30
31 MEDFileParameter1TS::MEDFileParameter1TS(int iteration, int order, double time):_iteration(iteration),_order(order),_time(time)
32 {
33 }
34
35 MEDFileParameter1TS::MEDFileParameter1TS():_iteration(-1),_order(-1),_time(0.)
36 {
37 }
38
39 bool MEDFileParameter1TS::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
40 {
41   std::ostringstream oss;
42   if(!other)
43     { what="Other is null"; return false; }
44   if(_iteration!=other->_iteration)
45     { oss << "iteration number mismatches " << _iteration << " != " << other->_iteration; what=oss.str(); return false; }
46   if(_order!=other->_order)
47     { oss << "order number mismatches " << _iteration << " != " << other->_iteration; what=oss.str(); return false; }
48   if(fabs(_time-other->_time)>eps)
49     { oss << "time mismatches " << _time << " != " << other->_time << " eps=" << eps; what=oss.str(); return false; }
50   return true;
51 }
52
53 MEDFileParameter1TS *MEDFileParameterDouble1TSWTI::deepCpy() const
54 {
55   return new MEDFileParameterDouble1TSWTI(*this);
56 }
57
58 bool MEDFileParameterDouble1TSWTI::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
59 {
60   if(!MEDFileParameter1TS::isEqual(other,eps,what))
61     return false;
62   const MEDFileParameterDouble1TSWTI *otherC=dynamic_cast<const MEDFileParameterDouble1TSWTI *>(other);
63   if(!otherC)
64     { what="IsEqual fails because this is double parameter other no !"; return false; }
65   if(fabs(_arr-otherC->_arr)>eps)
66     { std::ostringstream oss; oss << "value differ " << _arr << " != " << otherC->_arr << " (eps=" << eps << ")"; return false; }
67   return true;
68 }
69
70 std::size_t MEDFileParameterDouble1TSWTI::getHeapMemorySizeWithoutChildren() const
71 {
72   return sizeof(MEDFileParameterDouble1TSWTI);
73 }
74
75 std::vector<const BigMemoryObject *> MEDFileParameterDouble1TSWTI::getDirectChildrenWithNull() const
76 {
77   return std::vector<const BigMemoryObject *>();
78 }
79
80 std::string MEDFileParameterDouble1TSWTI::simpleRepr() const
81 {
82   std::ostringstream oss;
83   simpleRepr2(0,oss);
84   return oss.str();
85 }
86
87 void MEDFileParameterDouble1TSWTI::simpleRepr2(int bkOffset, std::ostream& oss) const
88 {
89   std::string startOfLine(bkOffset,' ');
90   oss << startOfLine << "ParameterDoubleItem with (iteration,order) = (" << _iteration << "," << _order << ")" << std::endl;
91   oss << startOfLine << "Time associacited = " << _time << std::endl;
92   oss << startOfLine << "The value is ***** " << _arr <<  " *****" << std::endl;
93 }
94
95 MEDFileParameterDouble1TSWTI *MEDFileParameterDouble1TSWTI::New(int iteration, int order, double time)
96 {
97   return new MEDFileParameterDouble1TSWTI(iteration,order,time);
98 }
99
100 MEDFileParameterDouble1TSWTI::MEDFileParameterDouble1TSWTI():_arr(std::numeric_limits<double>::max())
101 {
102 }
103
104 MEDFileParameterDouble1TSWTI::MEDFileParameterDouble1TSWTI(int iteration, int order, double time):MEDFileParameter1TS(iteration,order,time)
105 {
106 }
107
108 void MEDFileParameterDouble1TSWTI::finishLoading(med_idt fid, const std::string& name, int dt, int it, int nbOfSteps)
109 {
110   std::ostringstream oss; oss << "MEDFileParameterDouble1TS::finishLoading : no specified time step (" << dt << "," << it << ") ! Time steps available : ";
111   for(int i=0;i<nbOfSteps;i++)
112     {
113       int locDt,locIt;
114       double tim;
115       MEDparameterComputationStepInfo(fid,name.c_str(),i+1,&locDt,&locIt,&tim);
116       if(dt==locDt && it==locIt)
117         {
118           _iteration=locDt; _order=locIt; _time=tim;
119           MEDparameterValueRd(fid,name.c_str(),_iteration,_order,reinterpret_cast<unsigned char *const>(&_arr));
120           return ;
121         }
122       else
123         {
124           oss << "(" << locDt << "," << locIt << ")";
125           if(i!=nbOfSteps-1)
126             oss << ", ";
127         }
128     }
129   throw INTERP_KERNEL::Exception(oss.str().c_str());
130 }
131
132 void MEDFileParameterDouble1TSWTI::readValue(med_idt fid, const std::string& name)
133 {
134   MEDparameterValueRd(fid,name.c_str(),_iteration,_order,reinterpret_cast<unsigned char *const>(&_arr));
135 }
136
137 void MEDFileParameterDouble1TSWTI::finishLoading(med_idt fid, const std::string& name, int timeStepId)
138 {
139   int locDt,locIt;
140   double dt;
141   MEDparameterComputationStepInfo(fid,name.c_str(),timeStepId+1,&locDt,&locIt,&dt);
142   _iteration=locDt; _order=locIt; _time=dt;
143   MEDparameterValueRd(fid,name.c_str(),_iteration,_order,reinterpret_cast<unsigned char *const>(&_arr));
144 }
145
146 void MEDFileParameterDouble1TSWTI::writeLL(med_idt fid, const std::string& name, const MEDFileWritable& mw) const
147 {
148   char nameW[MED_NAME_SIZE+1];
149   MEDLoaderBase::safeStrCpy(name.c_str(),MED_NAME_SIZE,nameW,mw.getTooLongStrPolicy());
150   MEDparameterValueWr(fid,nameW,_iteration,_order,_time,reinterpret_cast<const unsigned char *>(&_arr));
151 }
152
153 std::size_t MEDFileParameterTinyInfo::getHeapMemSizeOfStrings() const
154 {
155   return _dt_unit.capacity()+_name.capacity()+_desc_name.capacity();
156 }
157
158 bool MEDFileParameterTinyInfo::isEqualStrings(const MEDFileParameterTinyInfo& other, std::string& what) const
159 {
160   std::ostringstream oss;
161   if(_name!=other._name)
162     { oss << "name differ ! this=" << _name << " and other=" << other._name; what=oss.str(); return false; }
163   if(_desc_name!=other._desc_name)
164     { oss << "name differ ! this=" << _desc_name << " and other=" << other._desc_name; what=oss.str(); return false; }
165   if(_dt_unit!=other._dt_unit)
166     { oss << "unit of time differ ! this=" << _dt_unit << " and other=" << other._dt_unit; what=oss.str(); return false; }
167   return true;
168 }
169
170 void MEDFileParameterTinyInfo::writeLLHeader(med_idt fid, med_parameter_type typ) const
171 {
172   char nameW[MED_NAME_SIZE+1],descW[MED_COMMENT_SIZE+1],dtunitW[MED_SNAME_SIZE+1];
173   MEDLoaderBase::safeStrCpy(_name.c_str(),MED_NAME_SIZE,nameW,getTooLongStrPolicy());
174   MEDLoaderBase::safeStrCpy(_desc_name.c_str(),MED_COMMENT_SIZE,descW,getTooLongStrPolicy());
175   MEDLoaderBase::safeStrCpy(_dt_unit.c_str(),MED_SNAME_SIZE,dtunitW,getTooLongStrPolicy());
176   MEDparameterCr(fid,nameW,typ,descW,dtunitW);
177 }
178
179 void MEDFileParameterTinyInfo::mainRepr(int bkOffset, std::ostream& oss) const
180 {
181   std::string startOfLine(bkOffset,' ');
182   oss << startOfLine << "Parameter with name \"" << _name << "\"" << std::endl;
183   oss << startOfLine << "Parameter with description \"" << _desc_name << "\"" << std::endl;
184   oss << startOfLine << "Parameter with unit name \"" << _dt_unit << "\"" << std::endl;
185 }
186
187 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New()
188 {
189   return new MEDFileParameterDouble1TS;
190 }
191
192 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const std::string& fileName)
193 {
194   return new MEDFileParameterDouble1TS(fileName);
195 }
196
197 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const std::string& fileName, const std::string& paramName)
198 {
199   return new MEDFileParameterDouble1TS(fileName,paramName);
200 }
201
202 MEDFileParameterDouble1TS *MEDFileParameterDouble1TS::New(const std::string& fileName, const std::string& paramName, int dt, int it)
203 {
204   return new MEDFileParameterDouble1TS(fileName,paramName,dt,it);
205 }
206
207 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS()
208 {
209 }
210
211 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const std::string& fileName, const std::string& paramName, int dt, int it)
212 {
213   MEDFileUtilities::CheckFileForRead(fileName);
214   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
215   int nbPar=MEDnParameter(fid);
216   std::ostringstream oss; oss << "MEDFileParameterDouble1TS : no double param name \"" << paramName << "\" ! Double Parameters available are : ";
217   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
218   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
219   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
220   med_parameter_type paramType;
221   for(int i=0;i<nbPar;i++)
222     {
223       int nbOfSteps;
224       MEDparameterInfo(fid,i+1,pName,&paramType,descName,unitName,&nbOfSteps);
225       std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
226       if(paramNameCpp==paramName && paramType==MED_FLOAT64)
227         {
228           _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
229           _name=paramNameCpp;
230           _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
231           finishLoading(fid,_name,dt,it,nbOfSteps);
232           return ;
233         }
234       else
235         {
236           oss << paramNameCpp;
237           if(i!=nbPar-1) oss << ", ";
238         }
239     }
240   throw INTERP_KERNEL::Exception(oss.str().c_str());
241 }
242
243 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const std::string& fileName, const std::string& paramName)
244 {
245   MEDFileUtilities::CheckFileForRead(fileName);
246   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
247   int nbPar=MEDnParameter(fid);
248   std::ostringstream oss; oss << "MEDFileParameterDouble1TS : no double param name \"" << paramName << "\" ! Double Parameters available are : ";
249   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
250   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
251   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
252   med_parameter_type paramType;
253   for(int i=0;i<nbPar;i++)
254     {
255       int nbOfSteps;
256       MEDparameterInfo(fid,i+1,pName,&paramType,descName,unitName,&nbOfSteps);
257       std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
258       if(paramNameCpp==paramName && paramType==MED_FLOAT64)
259         {
260           if(nbOfSteps>0)
261             {
262               _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
263               _name=paramNameCpp;
264               _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
265               finishLoading(fid,_name,0);
266               return ;
267             }
268           else
269             {
270               std::ostringstream oss2; oss2 << "Param name \"" << paramName << "\" exists but no time steps on it !";
271               throw INTERP_KERNEL::Exception(oss2.str().c_str());
272             }
273         }
274       else
275         {
276           oss << paramNameCpp;
277           if(i!=nbPar-1) oss << ", ";
278         }
279     }
280   throw INTERP_KERNEL::Exception(oss.str().c_str());
281 }
282
283 MEDFileParameterDouble1TS::MEDFileParameterDouble1TS(const std::string& fileName)
284 {
285   MEDFileUtilities::CheckFileForRead(fileName);
286   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
287   int nbPar=MEDnParameter(fid);
288   if(nbPar<1)
289     {
290       std::ostringstream oss2; oss2 << "No parameter in file \"" << fileName << "\" !";  
291       throw INTERP_KERNEL::Exception(oss2.str().c_str());
292     }
293   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
294   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
295   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
296   med_parameter_type paramType;
297   int nbOfSteps;
298   MEDparameterInfo(fid,1,pName,&paramType,descName,unitName,&nbOfSteps);
299   std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
300   if(paramType==MED_FLOAT64)
301     {
302       if(nbOfSteps>0)
303         {
304           _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
305           _name=paramNameCpp;
306           _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
307           finishLoading(fid,_name,0);
308           return ;
309         }
310       else
311         {
312           std::ostringstream oss2; oss2 << "Double param name \"" << paramNameCpp << "\" exists in file \""<< fileName << "\"but no time steps on it !";
313           throw INTERP_KERNEL::Exception(oss2.str().c_str());
314         }
315     }
316   else
317     {
318       std::ostringstream oss2; oss2 << "First parameter in file \"" << fileName << "\" is not double !";  
319       throw INTERP_KERNEL::Exception(oss2.str().c_str());
320     }
321 }
322
323 bool MEDFileParameterDouble1TS::isEqual(const MEDFileParameter1TS *other, double eps, std::string& what) const
324 {
325   if(!MEDFileParameterDouble1TSWTI::isEqual(other,eps,what))
326     return false;
327   const MEDFileParameterDouble1TS *otherC=dynamic_cast<const MEDFileParameterDouble1TS *>(other);
328   if(!otherC)
329     { what="Other is not of type MEDFileParameterDouble1TS as this"; return false; }
330   if(!isEqualStrings(*otherC,what))
331     return false;
332   return true;
333 }
334
335 MEDFileParameter1TS *MEDFileParameterDouble1TS::deepCpy() const
336 {
337   return new MEDFileParameterDouble1TS(*this);
338 }
339
340 std::string MEDFileParameterDouble1TS::simpleRepr() const
341 {
342   std::ostringstream oss;
343   MEDFileParameterTinyInfo::mainRepr(0,oss);
344   MEDFileParameterDouble1TSWTI::simpleRepr2(0,oss);
345   return oss.str();
346 }
347
348 std::size_t MEDFileParameterDouble1TS::getHeapMemorySizeWithoutChildren() const
349 {
350   return getHeapMemSizeOfStrings()+sizeof(MEDFileParameterDouble1TS);
351 }
352
353 std::vector<const BigMemoryObject *> MEDFileParameterDouble1TS::getDirectChildrenWithNull() const
354 {
355   return std::vector<const BigMemoryObject *>();
356 }
357
358 void MEDFileParameterDouble1TS::write(const std::string& fileName, int mode) const
359 {
360   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
361   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
362   MEDFileParameterTinyInfo::writeLLHeader(fid,MED_FLOAT64);
363   MEDFileParameterDouble1TSWTI::writeLL(fid,_name,*this);
364 }
365
366 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New()
367 {
368   return new MEDFileParameterMultiTS;
369 }
370
371 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New(const std::string& fileName)
372 {
373   return new MEDFileParameterMultiTS(fileName);
374 }
375
376 MEDFileParameterMultiTS *MEDFileParameterMultiTS::New(const std::string& fileName, const std::string& paramName)
377 {
378   return new MEDFileParameterMultiTS(fileName,paramName);
379 }
380
381 MEDFileParameterMultiTS::MEDFileParameterMultiTS()
382 {
383 }
384
385 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const MEDFileParameterMultiTS& other, bool deepCopy):MEDFileParameterTinyInfo(other),_param_per_ts(other._param_per_ts)
386 {
387   if(deepCopy)
388     for(std::size_t i=0;i<_param_per_ts.size();i++)
389       {
390         const MEDFileParameter1TS *elt=_param_per_ts[i];
391         if(elt)
392           _param_per_ts[i]=elt->deepCpy();
393       }
394 }
395
396 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const std::string& fileName)
397 {
398   MEDFileUtilities::CheckFileForRead(fileName);
399   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
400   int nbPar=MEDnParameter(fid);
401   if(nbPar<1)
402     {
403       std::ostringstream oss; oss << "MEDFileParameterMultiTS : no parameter in file \"" << fileName << "\" !" ;
404       throw INTERP_KERNEL::Exception(oss.str().c_str());
405     }
406   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
407   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
408   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
409   med_parameter_type paramType;
410   int nbOfSteps;
411   MEDparameterInfo(fid,1,pName,&paramType,descName,unitName,&nbOfSteps);
412   std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
413   _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
414   _name=paramNameCpp;
415   _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
416   finishLoading(fid,paramType,nbOfSteps);
417 }
418
419 MEDFileParameterMultiTS::MEDFileParameterMultiTS(const std::string& fileName, const std::string& paramName)
420 {
421   MEDFileUtilities::CheckFileForRead(fileName);
422   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
423   int nbPar=MEDnParameter(fid);
424   std::ostringstream oss; oss << "MEDFileParameterDouble1TS : no double param name \"" << paramName << "\" ! Double Parameters available are : ";
425   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
426   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
427   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
428   med_parameter_type paramType;
429   for(int i=0;i<nbPar;i++)
430     {
431       int nbOfSteps;
432       MEDparameterInfo(fid,i+1,pName,&paramType,descName,unitName,&nbOfSteps);
433       std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
434       if(paramNameCpp==paramName)
435         {
436           if(nbOfSteps>0)
437             {
438               _dt_unit=MEDLoaderBase::buildStringFromFortran(unitName,MED_SNAME_SIZE);
439               _name=paramNameCpp;
440               _desc_name=MEDLoaderBase::buildStringFromFortran(descName,MED_COMMENT_SIZE);
441               finishLoading(fid,paramType,nbOfSteps);
442               return ;
443             }
444           else
445             {
446               std::ostringstream oss2; oss2 << "Param name \"" << paramName << "\" exists but no time steps on it !";
447               throw INTERP_KERNEL::Exception(oss2.str().c_str());
448             }
449         }
450       else
451         {
452           oss << paramNameCpp;
453           if(i!=nbPar-1) oss << ", ";
454         }
455     }
456   throw INTERP_KERNEL::Exception(oss.str().c_str());
457 }
458
459 void MEDFileParameterMultiTS::finishLoading(med_idt fid, med_parameter_type typ, int nbOfSteps)
460 {
461   _param_per_ts.resize(nbOfSteps);
462   for(int i=0;i<nbOfSteps;i++)
463     {
464       int dt,it;
465       double tim;
466       MEDparameterComputationStepInfo(fid,_name.c_str(),i+1,&dt,&it,&tim);
467       switch(typ)
468       {
469         case MED_FLOAT64:
470           _param_per_ts[i]=MEDFileParameterDouble1TSWTI::New(dt,it,tim);
471           _param_per_ts[i]->readValue(fid,_name.c_str());
472           break;
473           /*case MED_INT32;
474          _param_per_ts[i]=;
475          break;*/
476         default:
477           throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::finishLoading : supporting only FLOAT64 !");
478       }
479     }
480 }
481
482 std::size_t MEDFileParameterMultiTS::getHeapMemorySizeWithoutChildren() const
483 {
484   std::size_t ret(sizeof(MEDFileParameterMultiTS));
485   ret+=sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS>)*_param_per_ts.capacity();
486   return ret;
487 }
488
489 std::vector<const BigMemoryObject *> MEDFileParameterMultiTS::getDirectChildrenWithNull() const
490 {
491   std::vector<const BigMemoryObject *> ret;
492   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
493     ret.push_back((const MEDFileParameter1TS *)*it);
494   return ret;
495 }
496
497 MEDFileParameterMultiTS *MEDFileParameterMultiTS::deepCpy() const
498 {
499   return new MEDFileParameterMultiTS(*this,true);
500 }
501
502 bool MEDFileParameterMultiTS::isEqual(const MEDFileParameterMultiTS *other, double eps, std::string& what) const
503 {
504   if(!other)
505     { what="other is null !"; return false; }
506   if(_param_per_ts.size()!=other->_param_per_ts.size())
507     { what="number of time steps differs !"; return false; }
508   std::ostringstream oss;
509   for(std::size_t i=0;i<_param_per_ts.size();i++)
510     {
511       const MEDFileParameter1TS *a(_param_per_ts[i]),*b(other->_param_per_ts[i]);
512       if((a && !b) || (!a && b))
513         { oss << "At time step id #" << i << " pointer is defined on one side not in the other !"; what=oss.str(); return false; }
514       if(a)
515         if(!a->isEqual(b,eps,what))
516           { oss << " At time step id #" << i << " non equality !"; what+=oss.str(); return false; }
517     }
518   return true;
519 }
520
521 void MEDFileParameterMultiTS::write(const std::string& fileName, int mode) const
522 {
523   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
524   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
525   writeLL(fid,*this);
526 }
527
528 void MEDFileParameterMultiTS::writeLL(med_idt fid, const MEDFileWritable& mw) const
529 {
530   std::set<med_parameter_type> diffType;
531   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
532     {
533       const MEDFileParameter1TS *elt(*it);
534       if(dynamic_cast<const MEDFileParameterDouble1TSWTI *>(elt))
535         diffType.insert(MED_FLOAT64);
536     }
537   if(diffType.size()>1)
538     throw INTERP_KERNEL::Exception("MEDFileParameterMultiTS::writeLL : impossible to mix type of data in parameters in MED file ! Only float64 or only int32 ...");
539   if(diffType.empty())
540     return;
541   med_parameter_type typ=*diffType.begin();
542   MEDFileParameterTinyInfo::writeLLHeader(fid,typ);
543   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
544     {
545       const MEDFileParameter1TS *elt(*it);
546       if(elt)
547         elt->writeLL(fid,_name,mw);
548     }
549 }
550
551 std::string MEDFileParameterMultiTS::simpleRepr() const
552 {
553   std::ostringstream oss;
554   simpleRepr2(0,oss);
555   return oss.str();
556 }
557
558 void MEDFileParameterMultiTS::simpleRepr2(int bkOffset, std::ostream& oss) const
559 {
560   MEDFileParameterTinyInfo::mainRepr(bkOffset,oss);
561   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
562     {
563       const MEDFileParameter1TS *elt(*it);
564       if(elt)
565         elt->simpleRepr2(bkOffset+2,oss);
566     }
567 }
568
569 void MEDFileParameterMultiTS::appendValue(int dt, int it, double time, double val)
570 {
571   MEDCouplingAutoRefCountObjectPtr<MEDFileParameterDouble1TSWTI> elt=MEDFileParameterDouble1TSWTI::New(dt,it,time);
572   elt->setValue(val);
573   MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> elt2((MEDFileParameterDouble1TSWTI*)elt); elt2->incrRef();
574   _param_per_ts.push_back(elt2);
575 }
576
577 double MEDFileParameterMultiTS::getDoubleValue(int iteration, int order) const
578 {
579   int pos=getPosOfTimeStep(iteration,order);
580   const MEDFileParameter1TS *elt=_param_per_ts[pos];
581   if(!elt)
582     {
583       std::ostringstream oss; oss << "MEDFileParameterMultiTS::getDoubleValue : time iteration it=" << iteration << " order=" << order;
584       oss << " exists but elt is empty !"; 
585       throw INTERP_KERNEL::Exception(oss.str().c_str());
586     }
587   const MEDFileParameterDouble1TSWTI *eltC=dynamic_cast<const MEDFileParameterDouble1TSWTI *>(elt);
588   if(!eltC)
589     {
590       std::ostringstream oss; oss << "MEDFileParameterMultiTS::getDoubleValue : time iteration it=" << iteration << " order=" << order;
591       oss << " exists but not double !"; 
592     }
593   return eltC->getValue();
594 }
595
596 int MEDFileParameterMultiTS::getPosOfTimeStep(int iteration, int order) const
597 {
598   int ret=0;
599   std::ostringstream oss; oss << "MEDFileParameterMultiTS::getPosOfTimeStep : no such iteration=" << iteration << " order=" << order << " ! Possibilities are :";
600   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++,ret++)
601     {
602       const MEDFileParameter1TS *elt(*it);
603       if(elt)
604         {
605           if(elt->getIteration()==iteration && elt->getOrder()==order)
606             return ret;
607           else
608             oss << "(" << elt->getIteration() << "," << elt->getOrder() << "), ";
609         }
610     }
611   throw INTERP_KERNEL::Exception(oss.str().c_str());
612 }
613
614 int MEDFileParameterMultiTS::getPosGivenTime(double time, double eps) const
615 {
616   int ret=0;
617   std::ostringstream oss; oss << "MEDFileParameterMultiTS::getPosGivenTime : no such time=" << time << " ! Possibilities are :";
618   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++,ret++)
619     {
620       const MEDFileParameter1TS *elt(*it);
621       if(elt)
622         {
623           if(fabs(elt->getTimeValue()-time)<=eps)
624             return ret;
625           else
626             oss << elt->getTimeValue() << ", ";
627         }
628     }
629   throw INTERP_KERNEL::Exception(oss.str().c_str());
630 }
631
632 /*!
633  * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
634  */
635 MEDFileParameter1TS *MEDFileParameterMultiTS::getTimeStepAtPos(int posId) const
636 {
637   if(posId<0 || posId>=(int)_param_per_ts.size())
638     {
639       std::ostringstream oss; oss << "MEDFileParameterMultiTS::getTimeStepAtPos : invalid pos ! Should be in [0," << _param_per_ts.size() << ") !";
640       throw INTERP_KERNEL::Exception(oss.str().c_str());
641     }
642   return const_cast<MEDFileParameter1TS *>(static_cast<const MEDFileParameter1TS *>(_param_per_ts[posId]));
643 }
644
645 void MEDFileParameterMultiTS::eraseTimeStepIds(const int *startIds, const int *endIds)
646 {
647   std::vector<bool> b(_param_per_ts.size(),true);
648   int len=(int)_param_per_ts.size();
649   for(const int *w=startIds;w!=endIds;w++)
650     if(*w>=0 && *w<len)
651       b[*w]=false;
652     else
653       {
654         std::ostringstream oss; oss << "MEDFileParameterMultiTS::eraseTimeStepIds : At pos #" << std::distance(startIds,w) << " value is " << *w << " should be in [0," << len << ") !"; throw INTERP_KERNEL::Exception(oss.str().c_str());
655       }
656   std::size_t newNb=std::count(b.begin(),b.end(),true);
657   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> > paramPerTs(newNb);
658   std::size_t j=0;
659   for(std::size_t i=0;i<_param_per_ts.size();i++)
660     if(b[i])
661       paramPerTs[j++]=_param_per_ts[i];
662   _param_per_ts=paramPerTs;
663 }
664
665 int MEDFileParameterMultiTS::getNumberOfTS() const
666 {
667   return (int) getIterations().size();
668 }
669
670 std::vector< std::pair<int,int> > MEDFileParameterMultiTS::getIterations() const
671 {
672   std::vector< std::pair<int,int> > ret;
673   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
674     {
675       const MEDFileParameter1TS *elt(*it);
676       if(elt)
677         ret.push_back(std::pair<int,int>(elt->getIteration(),elt->getOrder()));
678     }
679   return ret;
680 }
681
682 /*!
683  * \param [out] ret1
684  */
685 std::vector< std::pair<int,int> > MEDFileParameterMultiTS::getTimeSteps(std::vector<double>& ret1) const
686 {
687   std::vector< std::pair<int,int> > ret0;
688   ret1.clear();
689   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameter1TS> >::const_iterator it=_param_per_ts.begin();it!=_param_per_ts.end();it++)
690     {
691       const MEDFileParameter1TS *elt(*it);
692       if(elt)
693         {
694           ret0.push_back(std::pair<int,int>(elt->getIteration(),elt->getOrder()));
695           ret1.push_back(elt->getTimeValue());
696         }
697     }
698   return ret0;
699 }
700
701 MEDFileParameters *MEDFileParameters::New()
702 {
703   return new MEDFileParameters;
704 }
705
706 MEDFileParameters *MEDFileParameters::New(const std::string& fileName)
707 {
708   return new MEDFileParameters(fileName);
709 }
710
711 MEDFileParameters::MEDFileParameters(const std::string& fileName)
712 {
713   MEDFileUtilities::CheckFileForRead(fileName);
714   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
715   int nbPar=MEDnParameter(fid);
716   _params.resize(nbPar);
717   INTERP_KERNEL::AutoPtr<char> pName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
718   INTERP_KERNEL::AutoPtr<char> descName=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
719   INTERP_KERNEL::AutoPtr<char> unitName=MEDLoaderBase::buildEmptyString(MED_SNAME_SIZE);
720   med_parameter_type paramType;
721   for(int i=0;i<nbPar;i++)
722     {
723       int nbOfSteps;
724       MEDparameterInfo(fid,i+1,pName,&paramType,descName,unitName,&nbOfSteps);
725       std::string paramNameCpp=MEDLoaderBase::buildStringFromFortran(pName,MED_NAME_SIZE);
726       _params[i]=MEDFileParameterMultiTS::New(fileName,paramNameCpp);
727     }
728 }
729
730 MEDFileParameters::MEDFileParameters()
731 {
732 }
733
734 std::size_t MEDFileParameters::getHeapMemorySizeWithoutChildren() const
735 {
736   std::size_t ret(sizeof(MEDFileParameters));
737   ret+=sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS>)*_params.capacity();
738   return ret;
739 }
740
741 std::vector<const BigMemoryObject *> MEDFileParameters::getDirectChildrenWithNull() const
742 {
743   std::vector<const BigMemoryObject *> ret;
744   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
745     ret.push_back((const MEDFileParameterMultiTS *)*it);
746   return ret;
747 }
748
749 MEDFileParameters *MEDFileParameters::deepCpy() const
750 {
751   return new MEDFileParameters(*this,true);
752 }
753
754 bool MEDFileParameters::isEqual(const MEDFileParameters *other, double eps, std::string& what) const
755 {
756   if(!other)
757     { what="other is null !"; return false; }
758   if(_params.size()!=other->_params.size())
759     { what="number of parameters differs !"; return false; }
760   std::ostringstream oss;
761   for(std::size_t i=0;i<_params.size();i++)
762     {
763       const MEDFileParameterMultiTS *a(_params[i]),*b(other->_params[i]);
764       if((a && !b) || (!a && b))
765         { oss << "At param with id #" << i << " pointer is defined on one side not in the other !"; what=oss.str(); return false; }
766       if(a)
767         if(!a->isEqual(b,eps,what))
768           { oss << " At param with id #" << i << " non equality !"; what+=oss.str(); return false; }
769     }
770   return true;
771 }
772
773 MEDFileParameters::MEDFileParameters(const MEDFileParameters& other, bool deepCopy):MEDFileWritable(other),_params(other._params)
774 {
775   if(deepCopy)
776     for(std::size_t i=0;i<_params.size();i++)
777       {
778         const MEDFileParameterMultiTS *elt=_params[i];
779         if(elt)
780           _params[i]=elt->deepCpy();
781       }
782 }
783
784 void MEDFileParameters::write(const std::string& fileName, int mode) const
785 {
786   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
787   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
788   writeLL(fid);
789 }
790
791 void MEDFileParameters::writeLL(med_idt fid) const
792 {
793   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
794     {
795       const MEDFileParameterMultiTS *elt(*it);
796       if(elt)
797         elt->writeLL(fid,*this);
798     }
799 }
800
801 std::vector<std::string> MEDFileParameters::getParamsNames() const
802 {
803   std::vector<std::string> ret(_params.size());
804   int i=0;
805   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++,i++)
806     {
807       const MEDFileParameterMultiTS *p=(*it);
808       if(p)
809         {
810           ret[i]=p->getName();
811         }
812       else
813         {
814           std::ostringstream oss; oss << "MEDFileParameters::getParamsNames : At rank #" << i << " param is not defined !";
815           throw INTERP_KERNEL::Exception(oss.str().c_str());
816         }
817     }
818   return ret;
819 }
820
821 std::string MEDFileParameters::simpleRepr() const
822 {
823   std::ostringstream oss;
824   simpleReprWithoutHeader(oss);
825   return oss.str();
826 }
827
828 void MEDFileParameters::simpleReprWithoutHeader(std::ostream& oss) const
829 {
830   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++)
831     {
832       const MEDFileParameterMultiTS *elt(*it);
833       if(elt)
834         elt->simpleRepr2(2,oss);
835     }
836 }
837
838 void MEDFileParameters::resize(int newSize)
839 {
840   if(newSize<0)
841     throw INTERP_KERNEL::Exception("MEDFileParameters::resize : should be positive !");
842   _params.resize(newSize);
843 }
844
845 void MEDFileParameters::pushParam(MEDFileParameterMultiTS *param)
846 {
847   if(param)
848     param->incrRef();
849   MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> elt(param);
850   _params.push_back(elt);
851 }
852
853 void MEDFileParameters::setParamAtPos(int i, MEDFileParameterMultiTS *param)
854 {
855   if(i<0)
856     throw INTERP_KERNEL::Exception("MEDFileParameters::setParamAtPos : should be positive !");
857   if(i>=(int)_params.size())
858     _params.resize(i+1);
859   if(param)
860     param->incrRef();
861   MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> elt(param);
862   _params[i]=elt;
863 }
864
865 /*!
866  * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
867  */
868 MEDFileParameterMultiTS *MEDFileParameters::getParamAtPos(int i) const
869 {
870   if(i<0 || i>=(int)_params.size())
871     {
872       std::ostringstream oss; oss << "MEDFileParameters::getParamAtPos : should be in [0," << _params.size() << ") !";
873       throw INTERP_KERNEL::Exception(oss.str().c_str());
874     }
875   const MEDFileParameterMultiTS *elt=_params[i];
876   return const_cast<MEDFileParameterMultiTS *>(elt);
877 }
878
879 /*!
880  * \return an internal pointer that can be null. Warning the caller is \b not responsible of the returned pointer.
881  */
882 MEDFileParameterMultiTS *MEDFileParameters::getParamWithName(const std::string& paramName) const
883 {
884   int pos=getPosFromParamName(paramName);
885   return getParamAtPos(pos);
886 }
887
888 void MEDFileParameters::destroyParamAtPos(int i)
889 {
890   if(i<0 || i>=(int)_params.size())
891     {
892       std::ostringstream oss; oss << "MEDFileParameters::destroyParamAtPos : should be in [0," << _params.size() << ") !";
893       throw INTERP_KERNEL::Exception(oss.str().c_str());
894     }
895   _params[i]=MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS>(0);
896 }
897
898 int MEDFileParameters::getPosFromParamName(const std::string& paramName) const
899 {
900   std::ostringstream oss; oss << "MEDFileParameters::getPosFromParamName : no such name=" << paramName << " ! Possibilities are :";
901   int ret=0;
902   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileParameterMultiTS> >::const_iterator it=_params.begin();it!=_params.end();it++,ret++)
903     {
904       const MEDFileParameterMultiTS *elt(*it);
905       if(elt)
906         {
907           if(std::string(elt->getName())==paramName)
908             return ret;
909           else
910             oss << elt->getName() << ", ";
911         }
912     }
913   throw INTERP_KERNEL::Exception(oss.str().c_str());
914 }
915
916 int MEDFileParameters::getNumberOfParams() const
917 {
918   return (int)_params.size();
919 }