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