Commit eec00ff1 authored by Javier Quinteros's avatar Javier Quinteros
Browse files

Timewindow selection through parameters.

And minor bugfixes.
parent cdef13d0
......@@ -65,6 +65,11 @@ class TDMS(object):
dt = datetime.datetime.strptime(file[len(filename):-len('.tdms')], '_%Z_%Y%m%d_%H%M%S.%f')
self.__available.append({'dt': dt, 'name': file, 'samples': None})
if self.__twstart is None:
self.__twstart = dt
if self.__twstart < self.__available[0]['dt']:
self.__twstart = self.__available[0]['dt']
logs.info('Data detected from %s to %s' % (self.__available[0]['dt'], self.__available[-1]['dt']))
# What should we iterate? D: Data; M: Metadata
......@@ -248,13 +253,14 @@ class TDMS(object):
# Calculate endtime based on the number of samples declared and the sampling rate
self.endtime = self.starttime + datetime.timedelta(seconds=(self.samples-1)/self.sampling_rate)
self.__samplestart = floor((self.__twstart - self.starttime).total_seconds() * self.sampling_rate)
if self.__twend >= self.endtime:
self.__samplestart = max(floor((self.__twstart - self.starttime).total_seconds() * self.sampling_rate), 0)
if (self.__twend is None) or (self.__twend >= self.endtime):
self.__sampleend = self.samples-1
else:
self.__sampleend = ceil((self.__twend - self.starttime).total_seconds() * self.sampling_rate)
logs.info('Samples: %s' % self.samples)
logs.info('Samples selected: %s-%s' % (self.__samplestart, self.__sampleend))
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]))
......@@ -383,6 +389,24 @@ class TDMS(object):
return result
def str2date(dStr):
"""Transform a string to a datetime.
:param dStr: A datetime in ISO format.
:type dStr: string
:return: A datetime represented the converted input.
:rtype: datetime
"""
# In case of empty string
if (dStr is None) or (not len(dStr)):
return None
dateParts = dStr.replace('-', ' ').replace('T', ' ')
dateParts = dateParts.replace(':', ' ').replace('.', ' ')
dateParts = dateParts.replace('Z', '').split()
return datetime.datetime(*map(int, dateParts))
def main():
# Check verbosity in the output
msg = 'Read and convert waveforms generated by a DAS system.'
......@@ -395,6 +419,12 @@ def main():
parser.add_argument('-d', '--directory',
help='Directory where files are located.',
default='.')
parser.add_argument('--start', '--starttime',
help='Start of the selected time window.\nFormat: 2019-02-01T00:01:02.123456Z',
default=None)
parser.add_argument('--end', '--endtime',
help='End of the selected time window.\nFormat: 2019-02-01T00:01:02.123456Z',
default=None)
parser.add_argument('filename', help='File name to read and process.')
args = parser.parse_args()
......@@ -403,9 +433,15 @@ def main():
logs = logging.getLogger('OpenFile')
logs.setLevel(args.loglevel)
dt = datetime.datetime(2019, 5, 8, 9, 37, 50)
td = TDMS(args.filename, directory=args.directory, iterate='D', chstart=800, chstop=801, chstep=5,
starttime=dt, endtime=dt+datetime.timedelta(seconds=0.01))
dtstart = str2date(args.start)
dtend = str2date(args.end)
if dtend is not None and dtstart is not None and dtstart >= dtend:
logs.error('End time is smaller than Start time.')
return
td = TDMS(args.filename, directory=args.directory, iterate='D', chstart=0, chstop=1, chstep=5,
starttime=dtstart, endtime=dtend)
with td:
print(td.starttime, td.endtime)
......
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