diff --git a/README.md b/README.md index 7b4db00..24331ab 100644 --- a/README.md +++ b/README.md @@ -267,7 +267,16 @@ for grp in grpids : print("failed to print channelgroup: " + str(grp) + " : " + str(e)) ``` -For a full example including more details see `python/usage.py`. +For a full example including more details see [python/usage.py](python/usage.py) +and the absolute minimal example [minimal usage](python/minimal.py). In order +to simply extract all data of the TDM datatset and dump it to files in a given +(existing!) directory, do + +```Python +import tdm_reaper +jack = tdm_reaper.tdmreaper(b'samples/SineData.tdm',b'samples/SineData.tdx') +jack.write_all(b"./my_tdm_data/") +``` ## References diff --git a/cython/py_tdm_reaper.pyx b/cython/py_tdm_reaper.pyx index f8df4c6..89badb4 100644 --- a/cython/py_tdm_reaper.pyx +++ b/cython/py_tdm_reaper.pyx @@ -42,3 +42,10 @@ cdef class tdmreaper: def print_channel(self, string id, const char* filename, bool include_meta): self.cpp_tdm.print_channel(id,filename,include_meta) + + # print all data grouped by channelgroups + def write_all(self, string outputdir) : + grpids = self.cpp_tdm.get_channelgroup_ids() + for id in grpids : + grpfile = outputdir.decode() + "/channelgroup_" + id.decode() + ".csv" + self.cpp_tdm.print_group(id,grpfile.encode(),True,ord(',')) diff --git a/cython/tdm_reaper.pxd b/cython/tdm_reaper.pxd index c2b0705..6288235 100644 --- a/cython/tdm_reaper.pxd +++ b/cython/tdm_reaper.pxd @@ -24,5 +24,5 @@ cdef extern from "tdm_reaper.hpp": string get_channelgroup_info(string id) except+ string get_channel_info(string id) except+ # print a channel(-group) - void print_group(string id, const char* filename, bool include_meta, char delimiter) - void print_channel(string id, const char* filename, bool include_meta) + void print_group(string id, const char* filename, bool include_meta, char delimiter) except+ + void print_channel(string id, const char* filename, bool include_meta) except+ diff --git a/lib/tdm_reaper.cpp b/lib/tdm_reaper.cpp index c667792..9240fab 100644 --- a/lib/tdm_reaper.cpp +++ b/lib/tdm_reaper.cpp @@ -610,6 +610,9 @@ std::vector tdm_reaper::get_channel(std::string& id) void tdm_reaper::print_channel(std::string &id, const char* filename, bool include_meta) { + // check required path + this->check_filename_path(filename); + // check for channel id if ( this->tdmchannels_.count(id) != 1 ) { @@ -652,6 +655,9 @@ void tdm_reaper::print_channel(std::string &id, const char* filename, bool inclu void tdm_reaper::print_group(std::string &id, const char* filename, bool include_meta, char sep) { + // check required path + this->check_filename_path(filename); + // check for group id if ( this->tdmchannelgroups_.count(id) != 1 ) { @@ -761,6 +767,20 @@ void tdm_reaper::print_group(std::string &id, const char* filename, bool include } } +void tdm_reaper::check_filename_path(const char* filename) +{ + // declare filesystem path instance from filename + std::filesystem::path pt(filename); + + // get pure directory path + pt.remove_filename(); + + if ( !std::filesystem::is_directory(pt) ) + { + throw std::runtime_error(std::string("directory does not exist: ") + pt.c_str() ); + } +} + // -------------------------------------------------------------------------- // void tdm_reaper::check_local_datatypes() diff --git a/lib/tdm_reaper.hpp b/lib/tdm_reaper.hpp index 6893c1e..83fc4d9 100644 --- a/lib/tdm_reaper.hpp +++ b/lib/tdm_reaper.hpp @@ -208,6 +208,7 @@ public: // 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 = ' '); + void check_filename_path(const char* filename); // check machine's datatypes // https://en.cppreference.com/w/cpp/language/types diff --git a/python/minimal.py b/python/minimal.py new file mode 100644 index 0000000..cf949a5 --- /dev/null +++ b/python/minimal.py @@ -0,0 +1,15 @@ + +import tdm_reaper + +# create 'tdm_reaper' instance object +try : + jack = tdm_reaper.tdmreaper(b'samples/SineData.tdm',b'samples/SineData.tdx') + # list ids of channelgroups + grpids = jack.get_channelgroup_ids() + # iterate through groups + for grp in grpids : + # write channelgroup to file (prepend './' for local directory!!) + grpfile = "./channelgroup_" + str(grp.decode()) + ".csv" + jack.print_channelgroup(grp,grpfile.encode(),True,ord(' ')) +except RuntimeError as e : + print("failed to load/decode/write TDM files: " + str(e)) diff --git a/src/main.cpp b/src/main.cpp index f013d77..17cbc9c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -391,7 +391,7 @@ int main(int argc, char* argv[]) } } - //std::this_thread::sleep_for(std::chrono::milliseconds(10000)); + // std::this_thread::sleep_for(std::chrono::milliseconds(10000)); return 0; }