Commit aff0599e authored by Javier Quinteros's avatar Javier Quinteros
Browse files

Numpy arrays for the data extracted.

parent 8401701b
......@@ -21,6 +21,7 @@ import os
import logging
import struct
import datetime
import numpy as np
class TDMS(object):
......@@ -200,7 +201,14 @@ class TDMS(object):
# channelSize = self.__data2mask[datatype][1]*numValues
self.__readproperties(self.metadata[obj])
self.datatype = self.__data2mask[datatype][0]
# Set the data type as numpy expects it
if self.__data2mask[datatype][0] == 'h':
self.datatype = 'int16'
elif self.__data2mask[datatype][0] == 'f':
self.datatype = 'float32'
else:
raise Exception('Data type not supported! (%s)' % self.__data2mask[datatype][0])
self.datatypesize = self.__data2mask[datatype][1]
self.numChannels = numChannels
self.samples = int((self.__segmentOffset - self.__dataOffset) / numChannels / self.datatypesize)
......@@ -311,17 +319,20 @@ class TDMS(object):
if not self.hasInterleavedData:
# Seek where the channel starts
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)
# Read all channel in one step
# FIXME Big/Little Endian not set!
result = np.fromfile(self.__fi, dtype=self.datatype, count=self.samples)
else:
# Seek where the raw data starts
self.__fi.seek(self.__dataOffset, 0)
result = list()
# Reserve the data for the result
result = np.zeros((self.samples,), dtype=self.datatype)
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, self.datatype*self.numChannels),
self.__fi.read(self.datatypesize*self.numChannels))[channel])
# FIXME Big/Little Endian not set!
result[ch] = np.fromfile(self.__fi, dtype=self.datatype, count=self.numChannels)[channel]
return result
......@@ -347,7 +358,7 @@ def main():
logs.setLevel(args.loglevel)
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,
td = TDMS(args.filename, directory=args.directory, iterate='D', chstart=800, chstop=801, chstep=5,
starttime=dt, endtime=dt+datetime.timedelta(seconds=30))
with td:
......
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