Commit 8401701b authored by Javier Quinteros's avatar Javier Quinteros
Browse files

Input for TDSM is a directory and experiment name.

Filename is internally created based on dir, experiment and timestamps.
First additions to support timewindow selection.
parent 5179cacf
......@@ -17,6 +17,7 @@
###############################################################################
import argparse
import os
import logging
import struct
import datetime
......@@ -27,7 +28,8 @@ class TDMS(object):
if self.__fi is not None:
self.__fi.close()
def __init__(self, filename, chfrom=0, chto=None, iterate='D', loglevel='INFO'):
def __init__(self, filename, directory='.', chstart=0, chstop=None, chstep=1,
starttime=None, endtime=None, iterate='D', loglevel='INFO'):
logs = logging.getLogger('Init TDMS')
logs.setLevel(loglevel)
......@@ -35,12 +37,26 @@ class TDMS(object):
self.__loglevel = loglevel
# Channel from and to
self.__chfrom = chfrom
self.__chto = chto
self.__chstart = chstart
self.__chstop = chstop
self.__chstep = chstep
# Timewindow selection
self.__starttime = starttime
self.__endtime = endtime
# Name of file
self.__filename = filename
self.__directory = directory
self.__available = list()
for file in sorted(os.listdir(directory)):
if not file.startswith(filename):
continue
dt = datetime.datetime.strptime(file[len(filename):-len('.tdms')], '_%Z_%Y%m%d_%H%M%S.%f')
self.__available.append({'dt': dt, 'name': file, 'samples': None})
logs.info('Data detected from %s to %s' % (self.__available[0]['dt'], self.__available[-1]['dt']))
# What should we iterate? D: Data; M: Metadata
self.iterate = iterate
......@@ -80,7 +96,8 @@ class TDMS(object):
logs = logging.getLogger('Open file')
logs.setLevel(self.__loglevel)
self.__fi = open(self.__filename, 'rb')
filename = os.path.join(self.__directory, self.__available[0]['name'])
self.__fi = open(filename, 'rb')
leadin = self.__fi.read(self.__HEADERLEN)
(tag, ToCmask) = struct.unpack('<4si', leadin[:8])
......@@ -191,15 +208,15 @@ class TDMS(object):
logs.info('Total chunks size: %s' % (self.__segmentOffset - self.__dataOffset))
logs.info('Length of channel: %d' % ((self.__segmentOffset - self.__dataOffset)/numChannels/self.__data2mask[datatype][1]))
if self.__chfrom >= numChannels:
logs.error('Cannot export from channel %s. Only %s channels present.' % (self.__chfrom, numChannels))
if self.__chstart >= numChannels:
logs.error('Cannot export from channel %s. Only %s channels present.' % (self.__chstart, numChannels))
raise Exception
if self.__chto is None:
self.__chto = numChannels-1
elif self.__chto >= numChannels:
logs.warning('Resetting chfrom to %s' % (numChannels-1))
self.__chto = numChannels-1
if self.__chstop is None:
self.__chstop = numChannels-1
elif self.__chstop >= numChannels:
logs.warning('Resetting chstart to %s' % (numChannels-1))
self.__chstop = numChannels-1
# logs.debug('channelSize: %s bytes' % channelSize)
# chunkSize = chunkSize + channelSize
......@@ -219,7 +236,7 @@ class TDMS(object):
logs = logging.getLogger('Iterate Data')
# Loop through channels
for ch in range(self.__chfrom, self.__chto+1):
for ch in range(self.__chstart, self.__chstop+1, self.__chstep):
yield self.__readdata(channel=ch)
def __iter_metadata__(self):
......@@ -318,6 +335,9 @@ def main():
choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO',
'DEBUG'],
default='WARNING')
parser.add_argument('-d', '--directory',
help='Directory where files are located.',
default='.')
parser.add_argument('filename', help='File name to read and process.')
args = parser.parse_args()
......@@ -326,7 +346,10 @@ def main():
logs = logging.getLogger('OpenFile')
logs.setLevel(args.loglevel)
td = TDMS(args.filename, chfrom=800, chto=831)
dt = datetime.datetime(2019, 5, 8, 9, 38, 5, 409000)
td = TDMS(args.filename, directory=args.directory, iterate='M', chstart=800, chstop=801, chstep=5,
starttime=dt, endtime=dt+datetime.timedelta(seconds=30))
with td:
for ch in td:
print(ch)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment