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