00001 #ifndef PI_PROPERTIES_NETCDFTABLESERIALIZER
00002 #define PI_PROPERTIES_NETCDFTABLESERIALIZER
00003
00004 #include <rtt/Property.hpp>
00005 #include <rtt/base/PropertyIntrospection.hpp>
00006 #include <boost/lexical_cast.hpp>
00007
00008 #include <netcdf.h>
00009
00010 namespace RTT
00011 {
00012
00018 class NetcdfMarshaller
00019 : public marsh::MarshallInterface
00020 {
00021 int ncid;
00022 size_t index;
00023 int nameless_counter;
00024 std::string prefix;
00025
00026 public:
00031 NetcdfMarshaller(int ncid) :
00032 ncid ( ncid ) {index=0;}
00033
00034 virtual ~NetcdfMarshaller() {}
00035
00036 virtual void serialize(base::PropertyBase* v)
00037 {
00038 Property<PropertyBag>* bag = dynamic_cast< Property<PropertyBag>* >( v );
00039 if ( bag )
00040 this->serialize( *bag );
00041 else {
00042 Property<char>* Pc = dynamic_cast< Property<char>* >( v );
00043 if ( Pc )
00044 {
00045 store(Pc);
00046 return;
00047 }
00048 Property<short>* Ps = dynamic_cast< Property<short>* >( v );
00049 if ( Ps )
00050 {
00051 store(Ps);
00052 return;
00053 }
00054 Property<int>* Pi = dynamic_cast< Property<int>* >( v );
00055 if (Pi)
00056 {
00057 store(Pi);
00058 return;
00059 }
00060 Property<float>* Pf = dynamic_cast< Property<float>* >( v );
00061 if (Pf)
00062 {
00063 store(Pf);
00064 return;
00065 }
00066 Property<double>* Pd = dynamic_cast< Property<double>* >( v );
00067 if (Pd)
00068 {
00069 store(Pd);
00070 return;
00071 }
00072 Property<std::vector<double> >* Pv = dynamic_cast< Property<std::vector<double> >* >( v );
00073 if (Pv)
00074 {
00075 store(Pv);
00076 return;
00077 }
00078 }
00079 }
00080
00081 virtual void serialize(const PropertyBag &v)
00082 {
00083 for (
00084 PropertyBag::const_iterator i = v.getProperties().begin();
00085 i != v.getProperties().end();
00086 i++ )
00087 {
00088 this->serialize( *i );
00089 }
00090 }
00091
00092 virtual void serialize(const Property<PropertyBag> &v)
00093 {
00094 std::string oldpref = prefix;
00095
00096
00097 if(prefix.empty())
00098 prefix = v.getName();
00099 else
00100 prefix += "." + v.getName();
00101
00102 serialize(v.get());
00103
00104 prefix = oldpref;
00105 nameless_counter = 0;
00106 }
00107
00111 void store(Property<char> *v)
00112 {
00113 int retval;
00114 int varid;
00115 signed char value = v->get();
00116 std::string sname = composeName(v->getName());
00117
00121 retval = nc_inq_varid(ncid, sname.c_str(), &varid);
00122 if (retval)
00123 log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
00124
00128 retval = nc_put_var1_schar(ncid, varid, &index, &value);
00129 if(retval)
00130 log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
00131 }
00132
00136 void store(Property<short> *v)
00137 {
00138
00139 int retval;
00140 int varid;
00141 short value = v->get();
00142 std::string sname = composeName(v->getName());
00143
00147 retval = nc_inq_varid(ncid, sname.c_str(), &varid);
00148 if (retval)
00149 log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
00150
00154 retval = nc_put_var1_short(ncid, varid, &index, &value);
00155 if(retval)
00156 log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
00157 }
00158
00162 void store(Property<int> *v)
00163 {
00164 int retval;
00165 int varid;
00166 int value = v->get();
00167 std::string sname = composeName(v->getName());
00168
00172 retval = nc_inq_varid(ncid, sname.c_str(), &varid);
00173 if (retval)
00174 log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
00175
00179 retval = nc_put_var1_int(ncid, varid, &index, &value);
00180 if(retval)
00181 log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
00182 }
00183
00187 void store(Property<float> *v)
00188 {
00189 int retval;
00190 int varid;
00191 float value = v->get();
00192 std::string sname = composeName(v->getName());
00193
00197 retval = nc_inq_varid(ncid, sname.c_str(), &varid);
00198 if (retval)
00199 log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
00200
00204 retval = nc_put_var1_float(ncid, varid, &index, &value);
00205 if(retval)
00206 log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
00207
00208 }
00209
00213 void store(Property<double> *v)
00214 {
00215 int retval;
00216 int varid;
00217 double value = v->get();
00218 std::string sname = composeName(v->getName());
00219
00223 retval = nc_inq_varid(ncid, sname.c_str(), &varid);
00224 if (retval)
00225 log(Error) << "Could not get variable id of " << sname << ", error " << retval <<endlog();
00226
00230 retval = nc_put_var1_double(ncid, varid, &index, &value);
00231 if(retval)
00232 log(Error) << "Could not write variable " << sname << ", error " << retval <<endlog();
00233 }
00234
00238 void store(Property<std::vector<double> > *v)
00239 {
00240 int retval;
00241 int varid;
00242 const char *name = v->getName().c_str();
00243 size_t start[2], count[2];
00244
00248 start[0] = index; start[1] = 0;
00252 count[0] = 1; count[1] = v->get().size();
00253
00254 retval = nc_inq_varid(ncid, name, &varid);
00255 if (retval)
00256 log(Error) << "Could not get variable id of " << name << ", error " << retval <<endlog();
00257
00258 retval = nc_put_vara_double(ncid, varid, start, count, &(v->get().front()));
00259 if(retval)
00260 log(Error) << "Could not write variable " << name << ", error " << retval <<endlog();
00261
00262 }
00263
00264 std::string composeName(std::string propertyName)
00265 {
00266 std::string prefix_name;
00267
00268 if( propertyName.empty() ) {
00269 nameless_counter++;
00270 prefix_name = prefix + boost::lexical_cast<std::string>( nameless_counter );
00271 }
00272 else {
00273 nameless_counter = 0;
00274 prefix_name = propertyName;
00275 }
00276 return prefix_name;
00277 }
00278
00282 virtual void flush()
00283 {
00284 index++;
00285 }
00286
00287 };
00288 }
00289 #endif