Commit ffb87982 authored by Javier Quinteros's avatar Javier Quinteros

Simplify code of __iter_data

parent 0f2a23fb
...@@ -612,65 +612,26 @@ class TDMS(object): ...@@ -612,65 +612,26 @@ class TDMS(object):
# Data # Data
logs = logging.getLogger('Iterate Data') logs = logging.getLogger('Iterate Data')
# Data is stored with values from one channel in a continuous array while (self.__twend is None) or (self.__twstart < self.__twend):
if not self.__hasInterleavedData: data = self.__readdata(channels=self.__channels)
# for ch in range(self.__chstart, self.__chstop + 1, self.__chstep): # Loop through channels
for ch in self.__channels: for ch in self.__channels:
self.__resetcurrenttime() stats = {'network': 'XX', 'station': '%05d' % ch, 'location': '',
'channel': 'FH1', 'npts': len(data[ch]),
while (self.__twend is None) or (self.__twstart < self.__twend): 'sampling_rate': self.sampling_rate,
# Loop through channels 'starttime': UTCDateTime(self.__twstart),
while self.__samplecur <= self.__sampleend: 'mseed': {'byteorder': self.__endian,
data = self.__readdata(channels=[ch]) 'reclen': 512}}
stats = {'network': 'XX', 'station': '%05d' % ch, 'location': '', logs.debug('Data length: %d; First component: %s' % (len(data[ch]), data[ch][0]))
'channel': 'FH1', 'npts': len(data[ch]), yield data[ch], stats
'sampling_rate': self.sampling_rate,
'starttime': UTCDateTime(self.__twstart), # No more data in this file. Skip to the next one.
'mseed': {'byteorder': self.__endian, self.__currentfile += 1
'reclen': 512}} try:
# logs.debug('Stats: %s' % stats) logs.debug('Moving to next file...')
logs.debug('Data length: %d; First component: %s' % (len(data[ch]), data[ch][0])) self.__search_data()
yield data[ch], stats except IndexError:
break
# Update the current sample number
self.__samplecur += len(data[ch])
# No more data in this file. Skip to the next one.
self.__currentfile += 1
try:
self.__search_data()
except IndexError:
break
else:
# channels = list(range(self.__chstart, self.__chstop + 1, self.__chstep))
while (self.__twend is None) or (self.__twstart < self.__twend):
# Loop through channels
while self.__samplecur <= self.__sampleend:
data = self.__readdata(channels=self.__channels)
# for ch in channels:
for ch in self.__channels:
stats = {'network': 'XX', 'station': '%05d' % ch, 'location': '',
'channel': 'FH1', 'npts': len(data[ch]),
'sampling_rate': self.sampling_rate,
'starttime': UTCDateTime(self.__twstart),
'mseed': {'byteorder': self.__endian,
'reclen': 512}}
# logs.debug('Stats: %s' % stats)
logs.debug('Data length: %d; First component: %s' % (len(data[ch]), data[ch][0]))
yield data[ch], stats
# Update the current sample number based on first channel
# We assume that all chunks of different channels have the same length
self.__samplecur += len(data[self.__channels[0]])
# No more data in this file. Skip to the next one.
self.__currentfile += 1
try:
logs.debug('Moving to next file...')
self.__search_data()
except IndexError:
break
def __iter_metadata__(self): def __iter_metadata__(self):
# Metadata # Metadata
...@@ -758,7 +719,8 @@ class TDMS(object): ...@@ -758,7 +719,8 @@ class TDMS(object):
return struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0] return struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
def __readdata(self, channels=None): def __readdata(self, channels=None):
"""Read a chunk of data from the specified channels """Read a chunk of data from the specified channels.
Update the attribute __samplecur
:param channels: List of channel numbers to read data from :param channels: List of channel numbers to read data from
:type channels: list :type channels: list
...@@ -792,6 +754,10 @@ class TDMS(object): ...@@ -792,6 +754,10 @@ class TDMS(object):
# Read all selected data from the channel in one step # Read all selected data from the channel in one step
result[ch] = np.fromfile(self.__fi, dtype=self.__datatype, count=numsamples) result[ch] = np.fromfile(self.__fi, dtype=self.__datatype, count=numsamples)
# Update the current sample number based on the number of components read
self.__samplecur += numsamples
return result return result
# Seek where the raw data starts and add the offset to the first # Seek where the raw data starts and add the offset to the first
...@@ -809,5 +775,7 @@ class TDMS(object): ...@@ -809,5 +775,7 @@ class TDMS(object):
for ch in channels: for ch in channels:
result[ch][sample] = allchannels[ch] result[ch][sample] = allchannels[ch]
# print(result) # Update the current sample number based on the number of components read
self.__samplecur += numsamples
return result return result
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