Commit 76d79d81 authored by Javier Quinteros's avatar Javier Quinteros
Browse files

Extract data from more than one file based on time window selection.

parent d37e2cd5
......@@ -58,10 +58,14 @@ class TDMS(object):
# Sampling Rate
self.sampling_rate = None
# File currently being processed
self.__currentfile = None
# Name of file
self.__filename = filename
self.__directory = directory
self.__available = list()
for file in sorted(os.listdir(directory)):
if not file.startswith(filename):
continue
......@@ -74,7 +78,6 @@ class TDMS(object):
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
self.iterate = iterate
......@@ -110,20 +113,30 @@ class TDMS(object):
# tdsTypeComplexDoubleFloat = 0x10000d,
# tdsTypeDAQmxRawData = 0xFFFFFFFF
def __enter__(self):
logs = logging.getLogger('Open file')
def __select_file(self):
logs = logging.getLogger('Select file')
logs.setLevel(self.__loglevel)
for idx, fi in enumerate(self.__available):
if fi['dt'] > self.__twstart:
if not idx:
raise Exception('Data not available in the specified time window')
filename = os.path.join(self.__directory, self.__available[idx-1]['name'])
break
if self.__currentfile is None:
for idx, fi in enumerate(self.__available):
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
break
else:
raise Exception('Data not available in the specified time window')
else:
raise Exception('Data not available in the specified time window')
filename = os.path.join(self.__directory, self.__available[self.__currentfile]['name'])
self.__twstart = self.__available[self.__currentfile]['dt']
logs.info('Opening %s; Startime: %s' % (self.__available[self.__currentfile]['name'], self.__twstart))
# Reset some properties before opening the new file
self.starttime = None
self.endtime = None
self.metadata = dict()
# filename = os.path.join(self.__directory, self.__available[0]['name'])
self.__fi = open(filename, 'rb')
leadin = self.__fi.read(self.__HEADERLEN)
......@@ -169,14 +182,17 @@ class TDMS(object):
self.__segmentOffset += self.__HEADERLEN
self.__dataOffset += self.__HEADERLEN
logs.info('Metadata: ' + ('yes' if self.hasmetadata else 'no'))
logs.info('Object list: ' + ('yes' if self.hasnewObjects else 'no'))
logs.info('Raw data: ' + ('yes' if self.hasrawData else 'no'))
logs.info('Interleaved data: ' + ('yes' if self.hasInterleavedData else 'no'))
logs.info('BigEndian: ' + ('yes' if self.__endian == '<' else 'no'))
logs.info('DAQmx raw data: ' + ('yes' if self.hasDAQmxRawData else 'no'))
logs.debug('Metadata: ' + ('yes' if self.hasmetadata else 'no'))
logs.debug('Object list: ' + ('yes' if self.hasnewObjects else 'no'))
logs.debug('Raw data: ' + ('yes' if self.hasrawData else 'no'))
logs.debug('Interleaved data: ' + ('yes' if self.hasInterleavedData else 'no'))
logs.debug('BigEndian: ' + ('yes' if self.__endian == '<' else 'no'))
logs.debug('DAQmx raw data: ' + ('yes' if self.hasDAQmxRawData else 'no'))
self.readMetadata()
def __enter__(self):
self.__select_file()
return self
def readMetadata(self):
......@@ -302,16 +318,22 @@ class TDMS(object):
else:
raise Exception('Encoding type not supported to export in MiniSEED!')
# Loop through channels
for ch in range(self.__chstart, self.__chstop+1, self.__chstep):
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, 'encoding': enc,
'reclen': 512}}
yield (data, stats)
while self.__twstart < self.__twend:
# Loop through channels
for ch in range(self.__chstart, self.__chstop+1, self.__chstep):
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, 'encoding': enc,
'reclen': 512}}
yield (data, stats)
# No more data in this file. Skip to the next one.
self.__currentfile += 1
self.__select_file()
def __iter_metadata__(self):
# Metadata
......@@ -467,14 +489,27 @@ def main():
chstart=args.chstart, chstop=args.chstop, chstep=args.chstep,
starttime=dtstart, endtime=dtend)
openedfiles = dict()
with td:
for data, stats in td:
st = Stream([Trace(data=data, header=stats)])
print(data)
print(st)
st.write('%s.%s.%s.%s.mseed' % (stats['network'], stats['station'],
stats['location'], stats['channel']),
format='MSEED')
try:
fout = openedfiles[(stats['network'], stats['station'],
stats['location'], stats['channel'])]
except KeyError:
fout = open('%s.%s.%s.%s.mseed' % (stats['network'], stats['station'],
stats['location'], stats['channel']), 'wb')
openedfiles[(stats['network'], stats['station'],
stats['location'], stats['channel'])] = fout
st.write(fout, format='MSEED')
# Close all opened files
for fout in openedfiles.values():
fout.close()
if __name__ == '__main__':
......
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