OrocosComponentLibrary  2.9.0
NetcdfMarshaller.hpp
1 #ifndef PI_PROPERTIES_NETCDFTABLESERIALIZER
2 #define PI_PROPERTIES_NETCDFTABLESERIALIZER
3 
4 #include <rtt/Property.hpp>
5 #include <rtt/base/PropertyIntrospection.hpp>
6 #include <boost/lexical_cast.hpp>
7 
8 #include <netcdf.h>
9 #include <iostream>
10 using namespace std;
11 
12 namespace RTT
13 {
14 
21  : public marsh::MarshallInterface
22  {
23  int ncid;
24  size_t index;
25  int nameless_counter;
26  std::string prefix;
27 
28  public:
33  NetcdfMarshaller(int ncid) :
34  ncid ( ncid ) {index=0;}
35 
36  virtual ~NetcdfMarshaller() {}
37 
38  virtual void serialize(base::PropertyBase* v)
39  {
40  Property<PropertyBag>* bag = dynamic_cast< Property<PropertyBag>* >( v );
41  if ( bag )
42  this->serialize( *bag );
43  else {
44  Property<char>* Pc = dynamic_cast< Property<char>* >( v );
45  if ( Pc )
46  {
47  store(Pc);
48  return;
49  }
50  Property<short>* Ps = dynamic_cast< Property<short>* >( v );
51  if ( Ps )
52  {
53  store(Ps);
54  return;
55  }
56  Property<int>* Pi = dynamic_cast< Property<int>* >( v );
57  if (Pi)
58  {
59  store(Pi);
60  return;
61  }
62  Property<float>* Pf = dynamic_cast< Property<float>* >( v );
63  if (Pf)
64  {
65  store(Pf);
66  return;
67  }
68  Property<double>* Pd = dynamic_cast< Property<double>* >( v );
69  if (Pd)
70  {
71  store(Pd);
72  return;
73  }
74  Property<std::vector<double> >* Pv = dynamic_cast< Property<std::vector<double> >* >( v );
75  if (Pv)
76  {
77  store(Pv);
78  return;
79  }
80  }
81  }
82 
83  virtual void serialize(const PropertyBag &v)
84  {
85  for (
86  PropertyBag::const_iterator i = v.getProperties().begin();
87  i != v.getProperties().end();
88  i++ )
89  {
90  this->serialize( *i );
91  }
92  }
93 
94  virtual void serialize(const Property<PropertyBag> &v)
95  {
96  std::string oldpref = prefix;
97 
98  // Avoid a "." in the beginning of a variable name
99  if(prefix.empty())
100  prefix = v.getName();
101  else
102  prefix += "." + v.getName();
103 
104  serialize(v.rvalue());
105 
106  prefix = oldpref;
107  nameless_counter = 0;
108  }
109 
113  void store(Property<char> *v)
114  {
115  int retval;
116  int varid;
117  signed char value = v->rvalue();
118  std::string sname = composeName(v->getName());
119 
123  retval = nc_inq_varid(ncid, sname.c_str(), &varid);
124  if (retval)
125  log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
126 
130  retval = nc_put_var1_schar(ncid, varid, &index, &value);
131  if(retval)
132  log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
133  }
134 
138  void store(Property<short> *v)
139  {
140 
141  int retval;
142  int varid;
143  short value = v->rvalue();
144  std::string sname = composeName(v->getName());
145 
149  retval = nc_inq_varid(ncid, sname.c_str(), &varid);
150  if (retval)
151  log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
152 
156  retval = nc_put_var1_short(ncid, varid, &index, &value);
157  if(retval)
158  log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
159  }
160 
164  void store(Property<int> *v)
165  {
166  int retval;
167  int varid;
168  int value = v->rvalue();
169  std::string sname = composeName(v->getName());
170 
174  retval = nc_inq_varid(ncid, sname.c_str(), &varid);
175  if (retval)
176  log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
177 
181  retval = nc_put_var1_int(ncid, varid, &index, &value);
182  if(retval)
183  log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
184  }
185 
189  void store(Property<float> *v)
190  {
191  int retval;
192  int varid;
193  float value = v->rvalue();
194  std::string sname = composeName(v->getName());
195 
199  retval = nc_inq_varid(ncid, sname.c_str(), &varid);
200  if (retval)
201  log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
202 
206  retval = nc_put_var1_float(ncid, varid, &index, &value);
207  if(retval)
208  log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
209 
210  }
211 
215  void store(Property<double> *v)
216  {
217  int retval;
218  int varid;
219  double value = v->rvalue();
220  std::string sname = composeName(v->getName());
221 
225  retval = nc_inq_varid(ncid, sname.c_str(), &varid);
226  if (retval)
227  log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
228 
232  retval = nc_put_var1_double(ncid, varid, &index, &value);
233  if(retval)
234  log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
235  }
236 
240  void store(Property<std::vector<double> > *v)
241  {
242  int retval;
243  int varid;
244  const char *name = v->getName().c_str();
245  size_t start[2], count[2];
246 
250  start[0] = index; start[1] = 0;
254  count[0] = 1; count[1] = v->rvalue().size();
255 
256  retval = nc_inq_varid(ncid, name, &varid);
257  if (retval)
258  log(Error) << "Could not get variable id of " << name << ", error " << retval <<endlog();
259 
260  retval = nc_put_vara_double(ncid, varid, start, count, &(v->rvalue().front()));
261  if(retval)
262  log(Error) << "Could not write variable " << name << ", error " << retval <<endlog();
263 
264  }
265 
266  std::string composeName(std::string propertyName)
267  {
268  std::string last_name;
269 
270  if( propertyName.empty() ) {
271  nameless_counter++;
272  last_name = boost::lexical_cast<std::string>( nameless_counter );
273  }
274  else {
275  nameless_counter = 0;
276  last_name = propertyName;
277  }
278  if ( prefix.empty() )
279  return last_name;
280  else
281  return prefix + "." + last_name;
282  }
283 
287  virtual void flush()
288  {
289  index++;
290  }
291 
292  };
293 }
294 #endif
void store(Property< float > *v)
Write float data to corresponding variable name.
A marsh::MarshallInterface for writing data logs into the variables of a netcdf file.
void store(Property< int > *v)
Write int data to corresponding variable name.
void store(Property< short > *v)
Write short data to corresponding variable name.
void store(Property< std::vector< double > > *v)
Write double array data into corresponding variable name.
STL namespace.
virtual void flush()
Increase unlimited time dimension.
void store(Property< char > *v)
Write char data to corresponding variable name.
Definition: Category.hpp:10
NetcdfMarshaller(int ncid)
Create a new NetcdfMarshaller.
void store(Property< double > *v)
Write double data to corresponding variable name.