Skip to content
Snippets Groups Projects
Commit fa6d251c authored by Javier Quinteros's avatar Javier Quinteros
Browse files

Refactor and create __search_data to pay attention to gaps

parent 97a4b07a
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,14 @@ def tup2time(fraction, seconds):
return result
class PotentialGap(Exception):
pass
class NoData(Exception):
pass
class TDMS(object):
"""Class to read and export seismic waveforms in TDMS format"""
......@@ -30,7 +38,6 @@ class TDMS(object):
def __enter__(self):
"""Method which allows to use the syntax 'with object:' and use it inside"""
# self.__select_file()
# Create a buffer space to store the signal coefficients to be
# convoluted during the decimation
......@@ -190,19 +197,29 @@ class TDMS(object):
self.__sampleend = None
self.__samplecur = None
self.__select_file()
try:
self.__search_data()
except IndexError:
raise NoData()
def __select_file(self):
logs = logging.getLogger('Select file')
logs.setLevel(self.__loglevel)
# print('select', self.__twstart, self.starttime, self.__currentfile)
if self.__currentfile is None:
for idx, fi in enumerate(self.__available):
# print(self.__twstart, fi['dt'])
if self.__twstart < fi['dt']:
if not idx:
raise Exception('Data not available in the specified time window')
filename = os.path.join(self.__directory, self.__available[idx-1]['name'])
self.__currentfile = idx-1
# print(self.__currentfile, filename)
logs.debug(
'Opening %s; Starttime: %s' % (self.__available[self.__currentfile]['name'], self.__twstart))
break
else:
raise Exception('Data not available in the specified time window')
......@@ -218,8 +235,10 @@ class TDMS(object):
raise IndexError
logs.debug('Opening %s; Starttime: %s' % (self.__available[self.__currentfile]['name'], self.__twstart))
# print(self.__twstart)
# Reset some properties before opening the new file
self.starttime = self.__available[self.__currentfile]['dt']
# print(self.starttime)
self.endtime = None
self.metadata = dict()
......@@ -281,13 +300,33 @@ class TDMS(object):
logs.debug('BigEndian: ' + ('yes' if self.__endian == '<' else 'no'))
logs.debug('DAQmx raw data: ' + ('yes' if hasDAQmxRawData else 'no'))
# self.__readmetadata()
def __search_data(self):
"""
:raise: IndexError
"""
while True:
# Loop through files until there is nothing else (IndexError)
self.__select_file()
# Read the metadata and calculate samples to read
# Skip to the next file if there is a gap
try:
self.__readmetadata()
return
except PotentialGap:
logging.debug('Potential gap detected!')
self.__currentfile += 1
def __resetcurrenttime(self):
self.__twstart = self.__origstarttime
self.__twend = self.__origendtime
self.__currentfile = None
self.__select_file()
self.__search_data()
# print('reset', self.__twstart, self.starttime)
def __readmetadata(self):
# Metadata
......@@ -375,10 +414,15 @@ class TDMS(object):
self.endtime = self.starttime + datetime.timedelta(seconds=(self.__samples-1)/self.sampling_rate)
# Sample to start extraction from based on the initial datetime of the file (__twstart)
# print(self.__twstart, self.starttime)
self.__samplestart = max(floor((self.__twstart - self.starttime).total_seconds() * self.sampling_rate), 0)
if self.__samplestart >= self.__samples:
raise PotentialGap('Start reading at %s, but only %s samples' % (self.__samplestart, self.__samples))
# Should I readjust __twstart to align it exactly with the time of the samples?
# print(self.__twstart, self.starttime + datetime.timedelta(seconds=self.__samplestart/self.sampling_rate))
self.__twstart = self.starttime + datetime.timedelta(seconds=self.__samplestart/self.sampling_rate)
# print(self.__twstart, self.starttime, self.__samplestart)
self.__samplecur = self.__samplestart
......@@ -546,6 +590,7 @@ class TDMS(object):
# Data
logs = logging.getLogger('Iterate Data')
# print(self.__currentfile, self.__twstart, self.starttime, self.__samplestart, self.__samplecur, self.__sampleend)
# 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):
......@@ -571,7 +616,7 @@ class TDMS(object):
# No more data in this file. Skip to the next one.
self.__currentfile += 1
try:
self.__select_file()
self.__search_data()
except IndexError:
break
else:
......@@ -600,7 +645,7 @@ class TDMS(object):
self.__currentfile += 1
try:
logs.debug('Moving to next file...')
self.__select_file()
self.__search_data()
except IndexError:
break
......@@ -619,7 +664,7 @@ class TDMS(object):
# No more data in this file. Skip to the next one.
self.__currentfile += 1
try:
self.__select_file()
self.__search_data()
except IndexError:
break
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment