Commit 5179cacf authored by Javier Quinteros's avatar Javier Quinteros
Browse files

Iterate data including channel selection.

parent a7de5f2c
......@@ -27,13 +27,17 @@ class TDMS(object):
if self.__fi is not None:
self.__fi.close()
def __init__(self, filename, iterate='D', loglevel='INFO'):
def __init__(self, filename, chfrom=0, chto=None, iterate='D', loglevel='INFO'):
logs = logging.getLogger('Init TDMS')
logs.setLevel(loglevel)
# Log level
self.__loglevel = loglevel
# Channel from and to
self.__chfrom = chfrom
self.__chto = chto
# Name of file
self.__filename = filename
......@@ -179,16 +183,33 @@ class TDMS(object):
# channelSize = self.__data2mask[datatype][1]*numValues
self.__readproperties(self.metadata[obj])
self.samples = int((self.__segmentOffset - self.__dataOffset) / numChannels / self.__data2mask[datatype][1])
self.datatype = self.__data2mask[datatype][0]
self.datatypesize = self.__data2mask[datatype][1]
self.numChannels = numChannels
self.samples = int((self.__segmentOffset - self.__dataOffset) / numChannels / self.datatypesize)
logs.info('Samples: %s' % self.samples)
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))
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
# logs.debug('channelSize: %s bytes' % channelSize)
# chunkSize = chunkSize + channelSize
# logs.info('chunkSize: %s bytes' % chunkSize)
# print(self.__segmentOffset, self.__dataOffset, numChannels, self.__data2mask[datatype][1])
# New or changed objects
newObjects = struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
def __iter__(self):
if self.iterate:
if self.iterate == 'M':
return self.__iter_metadata__()
else:
return self.__iter_data__()
......@@ -197,15 +218,9 @@ class TDMS(object):
# Metadata
logs = logging.getLogger('Iterate Data')
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]))
# New or changed objects
newObjects = struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
# Loop through channels
for ch in range(1):
result = self.__readdata(self.__data2mask[datatype][0], self.__data2mask[datatype][1], samples,
numChannels, channel=ch)
for ch in range(self.__chfrom, self.__chto+1):
yield self.__readdata(channel=ch)
def __iter_metadata__(self):
# Metadata
......@@ -275,19 +290,21 @@ class TDMS(object):
logs.debug('Date-time %s' % result)
return result
def __readdata(self, datatype, datasize, samples, numChannels, channel=0):
if not self.interleavedData:
def __readdata(self, channel=0):
if not self.hasInterleavedData:
# Seek where the channel starts
self.__fi.seek(self.__dataOffset + datasize*samples*channel, 0)
result = struct.unpack('%c%s' % (self.__endian, datatype*samples), self.__fi.read(datasize*samples))
self.__fi.seek(self.__dataOffset + self.datatypesize*self.samples*channel, 0)
result = struct.unpack('%c%s' % (self.__endian, self.datatypesize*self.samples),
self.__fi.read(self.datatypesize*self.samples))
# result = list(result)
else:
# Seek where the raw data starts
self.__fi.seek(self.__dataOffset, 0)
result = list()
for ch in range(samples):
for ch in range(self.samples):
# Read from all channels and select the specific one with an index (channel)
result.append(struct.unpack('%c%s' % (self.__endian, datatype*numChannels), self.__fi.read(datasize*numChannels))[channel])
result.append(struct.unpack('%c%s' % (self.__endian, self.datatype*self.numChannels),
self.__fi.read(self.datatypesize*self.numChannels))[channel])
return result
......@@ -309,7 +326,7 @@ def main():
logs = logging.getLogger('OpenFile')
logs.setLevel(args.loglevel)
td = TDMS(args.filename, iterate='M')
td = TDMS(args.filename, chfrom=800, chto=831)
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