complete cython interface, python example usage.py

This commit is contained in:
2021-01-26 13:21:54 +01:00
parent 52115139bb
commit 00528e9460
7 changed files with 145 additions and 6 deletions

View File

@@ -160,6 +160,17 @@ struct tdm_channelgroup {
return formatter.get_info();
}
const std::string get_json()
{
std::stringstream ss;
ss<<"{"<<"\"group-id\":\""<<id_
<<"\",\"name\":\""<<name_
<<"\",\"description\":\""<<description_
<<"\",\"root\":\""<<root_
<<"\",\"channels\":\""<<join_strings(channels_)<<"\"}";
return ss.str();
}
};
// -------------------------------------------------------------------------- //
@@ -216,6 +227,20 @@ struct tdm_channel {
return formatter.get_info();
}
const std::string get_json()
{
std::stringstream ss;
ss<<"{"<<"\"channel-id\":\""<<id_
<<"\",\"name\":\""<<name_
<<"\",\"description\":\""<<description_
<<"\",\"unit_string\":\""<<unit_string_
<<"\",\"datatype\":\""<<datatype_
<<"\",\"minimum\":\""<<minimum_
<<"\",\"maximum\":\""<<maximum_
<<"\",\"group\":\""<<group_<<"\"}";
return ss.str();
}
};
// -------------------------------------------------------------------------- //

View File

@@ -104,6 +104,20 @@ public:
return *this;
}
// obtain number as double
double as_double()
{
double num;
if ( dtidx_ == 0 ) num = (double)sint16_;
else if ( dtidx_ == 1 ) num = (double)sint32_;
else if ( dtidx_ == 2 ) num = (double)uint8_;
else if ( dtidx_ == 3 ) num = (double)uint16_;
else if ( dtidx_ == 4 ) num = (double)uint32_;
else if ( dtidx_ == 5 ) num = (double)float32_;
else if ( dtidx_ == 6 ) num = (double)float64_;
return num;
}
// define custom stream operator to print the correct type
friend std::ostream& operator<<(std::ostream& out, const tdmdatatype& num)
{

View File

@@ -177,6 +177,34 @@ public:
// extract channel by id
std::vector<tdmdatatype> get_channel(std::string& id);
// additional methods for Cython/Python compatibiliy
//
// extract and return any channel as datatype double
std::vector<double> get_channel_as_double(std::string id)
{
std::vector<tdmdatatype> tdmchn = this->get_channel(id);
std::vector<double> chn;
for ( tdmdatatype el: tdmchn ) chn.push_back(el.as_double());
return chn;
}
// get channel(-group) meta-data
std::string get_channelgroup_info(std::string id)
{
if ( tdmchannelgroups_.count(id) == 1 ) {
return tdmchannelgroups_.at(id).get_json();
} else {
throw std::runtime_error(std::string("channelgroup does not exist: ") + id);
}
}
std::string get_channel_info(std::string id)
{
if ( tdmchannels_.count(id) == 1 ) {
return tdmchannels_.at(id).get_json();
} else {
throw std::runtime_error(std::string("channel does not exist: ") + id);
}
}
// dump a single channel/entire group (identified by id) to file
void print_channel(std::string &id, const char* filename, bool include_meta = true);
void print_group(std::string &id, const char* filename, bool include_meta = true, char sep = ' ');