OrocosComponentLibrary  2.8.3
NetcdfReporting.cpp
1 #include "NetcdfReporting.hpp"
2 #include <rtt/RTT.hpp>
3 #include <rtt/Logger.hpp>
4 #include <rtt/types/Types.hpp>
5 #include <rtt/types/TemplateTypeInfo.hpp>
6 #include "NetcdfMarshaller.hpp"
7 #include "NetcdfHeaderMarshaller.hpp"
8 
9 #include "ocl/Component.hpp"
10 ORO_CREATE_COMPONENT(OCL::NetcdfReporting)
11 
12 #include <netcdf.h>
13 
14 namespace OCL
15 {
16  using namespace RTT;
17  using namespace std;
18 
19  NetcdfReporting::NetcdfReporting(const std::string& fr_name)
20  : ReportingComponent( fr_name ),
21  repfile("ReportFile","Location on disc to store the reports.", "reports.nc")
22  {
23  this->properties()->addProperty( repfile );
24 
25  if(types::TypeInfoRepository::Instance()->getTypeInfo<short>() == 0 )
26  {
27  types::TypeInfoRepository::Instance()->addType(new types::TemplateTypeInfo<short, true>("short"));
28  }
29  }
30 
31  bool NetcdfReporting::startHook()
32  {
33  int retval;
34 
39  retval = nc_create(repfile.get().c_str(), NC_CLOBBER | NC_SHARE, &ncid);
40  if ( retval ) {
41  log(Error) << "Could not create "+repfile.get()+" for reporting."<<endlog();
42  return false;
43  }
44 
49  retval = nc_def_dim(ncid, "time", NC_UNLIMITED, &dimsid);
50  if ( retval ) {
51  log(Error) << "Could not create time dimension "+repfile.get() <<endlog();
52  return false;
53  }
54 
58  retval = nc_enddef( ncid );
59  if ( retval ) {
60  log(Error) << "Could not leave define mode in "+repfile.get() <<endlog();
61  return false;
62  }
63 
64  fheader = new RTT::NetcdfHeaderMarshaller( ncid , dimsid);
65  fbody = new RTT::NetcdfMarshaller( ncid );
66 
67  this->addMarshaller( fheader, fbody );
68 
69  return ReportingComponent::startHook();
70  }
71 
72  void NetcdfReporting::stopHook()
73  {
74  int retval;
75 
76  ReportingComponent::stopHook();
77 
78  this->removeMarshallers();
79 
83  if ( ncid )
84  retval = nc_close (ncid);
85  if ( retval )
86  log(Error) << "Could not close file "+repfile.get()+" for reporting."<<endlog();
87  }
88 
89 }
A component which writes data reports to a netCDF file.
A marsh::MarshallInterface for writing data logs into the variables of a netcdf file.
A marsh::MarshallInterface for generating variables in a netcdf dataset.
STL namespace.
This file contains the macros and definitions to create dynamically loadable components.
The Orocos Component Library.
Definition: Component.hpp:43
Definition: Category.hpp:10