From 16945c3c7af2c5c2a7a7e5cd35d23b340f92b1f0 Mon Sep 17 00:00:00 2001 From: Mario Fink Date: Fri, 12 Jul 2019 10:41:04 +0200 Subject: [PATCH] added function extract_all(...) for use in existing python script --- README.md | 15 ++++++++++++++ pytdm_ripper.pyx | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/README.md b/README.md index 6d15f1d..26f61e0 100644 --- a/README.md +++ b/README.md @@ -78,3 +78,18 @@ wrapper. ```Shell python extract_all.py --help ``` +- The same functionality may be obtained from an existing python script by + importing the tdm_ripper module and calling the extract_all function. For + instance + + ```Python + import tdm_ripper as td + + td.extract_all(b"samples/SineData.tdm",b"samples/SineData.tdx",b"data/",b"my_tdm") + ``` + + where the arguments "data/" and "my_tdm" are optional. "data/" specifies the + directory where all .csv outout is dumped while "my_tdm" represents a name + prefix for all csv. files. + Note, that all string arguments must be converted to bytes before passing to + the argument list by prepending "b". diff --git a/pytdm_ripper.pyx b/pytdm_ripper.pyx index a658c4a..16b775c 100644 --- a/pytdm_ripper.pyx +++ b/pytdm_ripper.pyx @@ -1,6 +1,7 @@ from tdm_ripper cimport tdm_ripper import numpy as np +import re cdef class pytdmripper: @@ -76,3 +77,56 @@ cdef class pytdmripper: def close(self): dummy = "" + +#-----------------------------------------------------------------------------# + +def extract_all(string tdmfile, string tdxfile, string outdirx = b"./", string prfxnam = b""): + """ + Python function extracting all available data from .tdm and .tdx to .csv dump + + Args: + tdmfile: path and filename of .tdm file + tdxfile: path and filename of associated .tdx file + outdirx: directory where all .csv output is dumped + prfxnam: optionally specify a name prefix for all .csv files + + Return: + None + """ + + # TODO preliminary: assume utf-8 + encoding = 'utf-8' + + # set up instance of Cython ripper + td = pytdmripper(tdmfile,tdxfile) + + # if no name prefix is given, .tdm filename will be used + prfx = "" + if prfxnam.decode(encoding) == prfx : + prfx = tdmfile.decode(encoding).rstrip('.tdm').split('/')[-1] + else: + prfx = prfxnam.decode(encoding) + + # obtain number of available groups and channels + numgr = td.num_groups() + numch = td.num_channels() + + # dump all meta information + td.print_meta((outdirx.decode(encoding)+prfx+'.csv').encode(encoding)) + + # dump all available groups and channels + for g in range(0,numgr): + numgrch = td.no_channels(g) + for c in range(0,numgrch): + # obtained overall channel id + chid = td.obtain_channel_id(g,c) + # get group's and channel's name + gname = td.group_name(g) + cname = td.channel_name(g,c) + # use regular expression replacement to sanitize group and channel names + gname = re.sub('[!@#$%^&*()-+= ,]','',gname) + cname = re.sub('[!@#$%^&*()-+= ,]','',cname) + # generate filename + fichan = prfx + '_' + str(g+1) + '_' + str(c+1) + '_' + gname + '_' + cname + '.csv' + # print channel + td.print_channel(chid,(outdirx.decode(encoding)+fichan).encode(encoding))