Commit 0201c32d authored by Javier Quinteros's avatar Javier Quinteros

Data read depending on the internal organization (by channels/time)

parent 15512f1b
......@@ -29,7 +29,6 @@ from .tdms import TDMS
version = '0.2b2'
def str2date(dStr):
"""Transform a string to a datetime.
......
......@@ -333,26 +333,54 @@ class TDMS(object):
else:
raise Exception('Encoding type not supported to export in MiniSEED!')
for ch in range(self.__chstart, self.__chstop + 1, self.__chstep):
self.resetcurrenttime()
# Data is stored with values from one channel in a continuous array
if not self.hasInterleavedData:
for ch in range(self.__chstart, self.__chstop + 1, self.__chstep):
self.resetcurrenttime()
while (self.__twend is None) or (self.__twstart < self.__twend):
# Loop through channels
while self.__samplecur <= self.__sampleend:
data = self.__readdata(channels=[ch])
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
self.__samplecur += len(data[ch])
# No more data in this file. Skip to the next one.
self.__currentfile += 1
try:
self.__select_file()
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(channel=ch)
for chnum, values in data.items():
stats = {'network': 'XX', 'station': '%05d' % chnum, 'location': '',
'channel': 'FH1', 'npts': len(values),
data = self.__readdata(channels=channels)
for ch in 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(values), values[0]))
yield (values, stats)
logs.debug('Data length: %d; First component: %s' % (len(data[ch]), data[ch][0]))
yield (data[ch], stats)
# Update the current sample number
self.__samplecur += len(data[ch])
# 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[channels[0]])
# No more data in this file. Skip to the next one.
self.__currentfile += 1
......@@ -437,35 +465,42 @@ class TDMS(object):
# logs.debug('Date-time %s' % result)
return result
def __readdata(self, channel=0):
def __readdata(self, channels=[0]):
"""Read a chunk of data from the specified channels
Return a dictionary in which channel number is the key and the
value is a numpy array with data
"""
result = dict()
# numSamples = self.__sampleend - self.__samplestart + 1
numSamples = min(self.__sampleend - self.__samplecur + 1, self.__MAXSAMPLES)
if not self.hasInterleavedData:
# Seek where the channel starts and add the offset to the first
# sample to read based in the time window selection
# self.__fi.seek(self.__dataOffset + self.datatypesize*self.samples*channel + self.__samplestart, 0)
self.__fi.seek(self.__dataOffset + self.datatypesize*self.samples*channel + self.__samplecur, 0)
for ch in channels:
# Seek where the channel starts and add the offset to the first
# sample to read based in the time window selection
# self.__fi.seek(self.__dataOffset + self.datatypesize*self.samples*channel + self.__samplestart, 0)
self.__fi.seek(self.__dataOffset + self.datatypesize*self.samples*ch + self.__samplecur, 0)
# Read all selected data from the channel in one step
result = np.fromfile(self.__fi, dtype=self.datatype, count=numSamples)
return {channel: result}
# Read all selected data from the channel in one step
result[ch] = np.fromfile(self.__fi, dtype=self.datatype, count=numSamples)
return result
# Seek where the raw data starts and add the offset to the first
# sample to read based in the time window selection
# self.__fi.seek(self.__dataOffset + self.__samplestart*self.datatypesize*self.numChannels, 0)
self.__fi.seek(self.__dataOffset + self.__samplecur*self.datatypesize*self.numChannels, 0)
# Reserve the data for the result
result = np.zeros((numSamples,), dtype=self.datatype)
for ch in range(numSamples):
for ch in channels:
result[ch] = np.zeros((numSamples,), dtype=self.datatype)
for sample in range(numSamples):
# Read from all channels and select the specific one with an index (channel)
allchannels = np.fromfile(self.__fi, dtype=self.datatype, count=self.numChannels)
result[ch] = allchannels[channel]
for ch in channels:
result[ch][sample] = allchannels[ch]
print(result)
return {channel: result}
# print(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