Commit 4a0301a6 authored by Javier Quinteros's avatar Javier Quinteros

Take into account the case in which channels are defined by chstart, chstop and chstep

parent 12fe6342
......@@ -6,6 +6,7 @@ from obspy import UTCDateTime
import numpy as np
from math import floor
from math import ceil
from numbers import Number
def tup2time(fraction, seconds):
......@@ -91,20 +92,26 @@ class TDMS(object):
self.__decimate = decimate
# Selection of channels
if not isinstance(chstart, Number) or not isinstance(chstop, Number) or not isinstance(chstep, Number):
logs.error('chstart, chstop and chstep must be numbers and preferably ints')
raise TypeError('chstart, chstop and chstep must be numbers and preferably ints')
self.__chstart = chstart
self.__chstop = chstop
self.__chstep = chstep
# channels has priority. Otherwise, chstart, chstop and chstep are used
if channels is not None:
if isinstance(channels, list):
self.__channels = channels
else:
raise TypeError('"channels" must be a list')
else:
# FIXME If chstop is None because we don't know the total number of channels
# we need to keep the three values (start, stop, step) until we read it in readmetadata
self.__channels = list(range(chstart, chstop+1, chstep))
if channels is not None and isinstance(channels, list):
self.__channels = channels
# List of channels cannot be empty
if not len(self.__channels):
raise Exception('Channel list is empty!')
# List of channels cannot be empty
if not len(self.__channels):
raise Exception('Channel list is empty!')
else:
# If chstart, chstop and chstep will define the selected channels we need to keep the three
# values (start, stop, step) and define the channels later in readmetadata
self.__channels = None
# self.__channels = list(range(chstart, chstop+1, chstep))
# Time window selection
self.__twstart = starttime
......@@ -212,7 +219,6 @@ class TDMS(object):
except IndexError:
raise NoData()
def __select_file(self):
logs = logging.getLogger('Select file')
logs.setLevel(self.__loglevel)
......@@ -418,6 +424,17 @@ class TDMS(object):
self.__datatypesize = self.__data2mask[datatype][1]
self.numchannels = numchannels
# If channels was not defined at creation time
if self.__channels is None:
if self.__chstart >= numchannels:
raise Exception('No valid channel IDs selected!')
if self.__chstop >= numchannels:
logs.warning('Resetting chstop from %s to %s' % (self.__chstop, numchannels-1))
self.__chstop = numchannels - 1
# Define list of selected channels
self.__channels = list(range(self.__chstart, self.__chstop + 1, self.__chstep))
self.__samples = int((self.__segmentOffset - self.__dataOffset) / numchannels / self.__datatypesize)
# Calculate endtime based on the number of samples declared and the sampling rate
......@@ -450,22 +467,6 @@ class TDMS(object):
channellength = ((self.__segmentOffset - self.__dataOffset)/numchannels/self.__data2mask[datatype][1])
logs.debug('Length of channel: %d' % channellength)
# Check that the list of channels include only valid IDs
for idx, chnum in reversed(list(enumerate(self.__channels))):
if chnum >= numchannels:
logs.warning('Cannot export from channel %s. Only %s channels present.' % (chnum, numchannels))
del self.__channels[idx]
# Raise an exception if there are no channels selected
if not len(self.__channels):
raise Exception('No valid channel IDs selected!')
# if self.__chstop is None:
# self.__chstop = numchannels-1
# elif self.__chstop >= numchannels:
# logs.warning('Resetting chstop to %s' % (numchannels-1))
# self.__chstop = numchannels-1
# New or changed objects
newObjects = struct.unpack('%cI' % self.__endian, self.__fi.read(4))[0]
......
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