Commit 15512f1b authored by Javier Quinteros's avatar Javier Quinteros

__readdata returns a dict with channel number and data as key-value pairs

parent 9a5693ae
......@@ -15,10 +15,18 @@ class TDMS(object):
self.__fi.close()
def __init__(self, filename, directory='.', chstart=0, chstop=None, chstep=1,
starttime=None, endtime=None, iterate='D', loglevel='INFO'):
starttime=None, endtime=None, iterate='D', decimate=1,
loglevel='INFO'):
logs = logging.getLogger('Init TDMS')
logs.setLevel(loglevel)
self.__decimate = decimate
# Define a buffer to store the window over the signal to be
# convoluted with the FIR filter wile decimating
# Keys will be channel number and the values will be np.arrays
self.__buffer = dict()
# Log level
self.__loglevel = loglevel
......@@ -299,6 +307,11 @@ class TDMS(object):
logs.warning('Resetting chstart to %s' % (numChannels-1))
self.__chstop = numChannels-1
# Create a buffer space to store the signal coefficients to be
# convoluted during the decimation
for channel in range(self.__chstart, self.__chstop + 1, self.__chstep):
self.__buffer[channel] = np.zeros((235,), dtype=self.datatype)
# New or changed objects
newObjects = struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
......@@ -327,18 +340,19 @@ class TDMS(object):
# Loop through channels
while self.__samplecur <= self.__sampleend:
data = self.__readdata(channel=ch)
stats = {'network': 'XX', 'station': '%05d' % ch, 'location': '',
'channel': 'FH1', 'npts': len(data),
'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), data[0]))
yield (data, stats)
# Read data in blocks
self.__samplecur += len(data)
for chnum, values in data.items():
stats = {'network': 'XX', 'station': '%05d' % chnum, 'location': '',
'channel': 'FH1', 'npts': len(values),
'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)
# Update the current sample number
self.__samplecur += len(data[ch])
# No more data in this file. Skip to the next one.
self.__currentfile += 1
......@@ -424,6 +438,12 @@ class TDMS(object):
return result
def __readdata(self, channel=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
"""
# numSamples = self.__sampleend - self.__samplestart + 1
numSamples = min(self.__sampleend - self.__samplecur + 1, self.__MAXSAMPLES)
if not self.hasInterleavedData:
......@@ -434,16 +454,18 @@ class TDMS(object):
# Read all selected data from the channel in one step
result = np.fromfile(self.__fi, dtype=self.datatype, count=numSamples)
else:
# 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):
# Read from all channels and select the specific one with an index (channel)
result[ch] = np.fromfile(self.__fi, dtype=self.datatype, count=self.numChannels)[channel]
return result
return {channel: 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):
# 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]
print(result)
return {channel: 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