Commit 615b034e authored by Javier Quinteros's avatar Javier Quinteros
Browse files

Add iteration for the metadata.

parent 9a310e45
......@@ -23,7 +23,7 @@ import datetime
class TDMS(object):
def __init__(self, filename, loglevel='INFO'):
def __init__(self, filename, iterate='D', loglevel='INFO'):
logs = logging.getLogger('OpenFile')
logs.setLevel(loglevel)
......@@ -59,9 +59,6 @@ class TDMS(object):
# tdsTypeComplexDoubleFloat = 0x10000d,
# tdsTypeDAQmxRawData = 0xFFFFFFFF
if input is None:
logs.error('No input file was specified!')
self.__fi = open(filename, 'rb')
leadin = self.__fi.read(HEADERLEN)
......@@ -73,6 +70,8 @@ class TDMS(object):
self.interleavedData = bool(ToCmask & kTocInterleavedData)
self.DAQmxRawData = bool(ToCmask & kTocDAQmxRawData)
self.iterate = iterate
# All input from now on will be formatted by this
self.__endian = '>' if ToCmask & kTocBigEndian else '<'
......@@ -82,7 +81,7 @@ class TDMS(object):
(version, self.__segmentOffset, self.__dataOffset) = \
struct.unpack('%ciQQ' % self.__endian, leadin[8:])
logs.info((tag, ToCmask, version, self.__segmentOffset, self.__dataOffset))
logs.debug((tag, ToCmask, version, self.__segmentOffset, self.__dataOffset))
if version != 4713:
logs.warning('Version number is not 4713!')
......@@ -108,14 +107,32 @@ class TDMS(object):
logs.info('DAQmx raw data: ' + ('yes' if self.DAQmxRawData else 'no'))
def __iter__(self):
return self
if self.iterate:
return self.__iter_metadata__()
else:
return self.__iter_data__()
def __next__(self):
return self.__next__metadata__()
def __iter_data__(self):
# Metadata
logs = logging.getLogger('Iterate Data')
def __next__metadata__(self):
logs = logging.getLogger('Iterate Metadata')
# samples = int((self.__segmentOffset - self.__dataOffset)/numChannels/self.__data2mask[datatype][1])
# logs.info('chunkSize: %s bytes' % chunkSize)
# 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)
def __iter_metadata__(self):
# Metadata
logs = logging.getLogger('Iterate Metadata')
# Number of objects (unsigned int - 32b)
numObjects = struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
logs.info('Number of objects in metadata: %s' % numObjects)
......@@ -125,51 +142,47 @@ class TDMS(object):
for obj in range(numObjects):
channelSize = 0
objPath = self.__readstring()
logs.info('Object %s: %s' % (obj, objPath))
logs.debug('Object %s: %s' % (obj, objPath))
result = {'path': objPath}
rawDataIdx = struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
if rawDataIdx == self.__FF32b:
logs.info('No raw data assigned to this segment')
self.__readproperties()
logs.debug('No raw data assigned to this segment')
result['data'] = False
yield self.__readproperties(result)
continue
elif not rawDataIdx:
logs.info('Raw data index in this segment matches the index the same object had in the previous segment')
logs.debug('Raw data index in this segment matches the index the same object had in the previous segment')
else:
result['data'] = True
numChannels += 1
# There is raw data!
sizeBytes = None
datatype, arraylen, numValues = struct.unpack('%cIIQ' % self.__endian, self.__fi.read(16))
datatype, arraylen, result['numValues'] = \
struct.unpack('%cIIQ' % self.__endian, self.__fi.read(16))
if datatype == 0x20:
sizeBytes = struct.unpack('%cQ' % self.__endian, self.__fi.read(8))[0]
result['sizeBytes'] = struct.unpack('%cQ' % self.__endian, self.__fi.read(8))[0]
if arraylen != 1:
logs.error('Array length MUST be 1! Actual value: %s' % arraylen)
logs.debug('obj %s; datatype: %s; numValues: %s; size: %s' % (obj, datatype, numValues,
self.__data2mask[datatype][1]*numValues))
channelSize = self.__data2mask[datatype][1]*numValues
result['datatype'] = self.__data2mask[datatype][0]
# logs.debug('obj %s; datatype: %s; numValues: %s; size: %s' % (obj, datatype, result['numValues'],
# self.__data2mask[datatype][1]*result['numValues']))
channelSize = self.__data2mask[datatype][1]*result['numValues']
self.__readproperties()
yield result
# logs.debug('channelSize: %s bytes' % channelSize)
chunkSize = chunkSize + channelSize
# print(self.__segmentOffset, self.__dataOffset, numChannels, self.__data2mask[datatype][1])
# samples = int((self.__segmentOffset - self.__dataOffset)/numChannels/self.__data2mask[datatype][1])
# logs.info('chunkSize: %s bytes' % chunkSize)
# 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)
def __readstring(self):
# logs = logging.getLogger('readstring')
strlen = struct.unpack('%cI' % self.__endian, self.__fi.read(4))
......@@ -202,17 +215,22 @@ class TDMS(object):
logs.debug('result: %s' % result)
return result
def __readproperties(self):
def __readproperties(self, result=dict()):
logs = logging.getLogger('readproperties')
numProps = struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
logs.debug('%s properties' % numProps)
if numProps:
logs.debug('%s properties' % numProps)
for prop in range(numProps):
propStr = self.__readstring()
logs.debug('Prop: %s' % propStr)
value = self.__readvalue()
result[propStr] = value
logs.debug(value)
return result
def __readdatatype(self):
return struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
......@@ -260,7 +278,9 @@ def main():
logs = logging.getLogger('OpenFile')
logs.setLevel(args.loglevel)
TDMS(args.filename)
for ch in TDMS(args.filename, iterate='M'):
print(ch)
if __name__ == '__main__':
......
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