...
 
Commits (4)
......@@ -65,7 +65,7 @@ class TDMS(object):
:param chstep: Step between channels in the selection
:type chstep: int
:param channels: Selection of channels to work with (list of integers)
:type channels: list
:type channels: list or NoneType
:param starttime: Start of the selected time window
:type starttime: datetime.datetime or NoneType
:param endtime: End of the selected time window
......@@ -99,6 +99,10 @@ class TDMS(object):
logs.error('chstop must be a number or None')
raise TypeError('chstop must be a number or None')
if not isinstance(channels, list) and channels is not None:
logs.error('channels must be a list of numbers or None')
raise TypeError('channels must be a list of numbers or None')
self.__chstart = chstart
self.__chstop = chstop
self.__chstep = chstep
......
......@@ -394,39 +394,59 @@ class StationAPI(object):
channels = streams if len(streams) else None
t = TDMS(self.__experiment, directory=self.__directory, starttime=starttime, endtime=endtime, iterate='M',
channels=channels)
with t:
lat = None
lon = None
elev = None
samprate = None
# stt = t.starttime
# ent = t.endtime
for data in t:
if lat is None:
lat = data[latkey]
if lon is None:
lon = data[lonkey]
if elev is None:
elev = data[elevkey]
if samprate is None:
samprate = data[srkey]
# if not data['data']:
# yield json.dumps(data, default=datetime.isoformat)
if data['data']:
stt = data['starttime']
ent = data['endtime']
if level == 'station':
text = 'XX|%s||FH1|%s|%s|%s|SiteName|%s|%s\n' %\
(data['id'], lat, lon, elev, stt.isoformat(), ent.isoformat())
else:
text = 'XX|%s||FH1|%s|%s|%s|depth|azimuth|dip|DAS|Scale|ScaleFrequency|ScaleUnits|%s|%s|%s\n' %\
(data['id'], lat, lon, elev, samprate, stt.isoformat(), ent.isoformat())
# yield json.dumps(data, default=datetime.isoformat)
yield text
# It is not optimal to cycle through channels instead of reading them all at once
# However it is the best to merge time windows from files int one epoch
# This allows to construct the generator returning one line at each iteration
# FIXME If there is no stream selected via the "station" parameter this will raise an Exception
for ch in channels:
t = TDMS(self.__experiment, directory=self.__directory, starttime=starttime, endtime=endtime, iterate='M',
channels=[ch])
result = dict()
with t:
lat = None
lon = None
elev = None
samprate = None
# stt = t.starttime
# ent = t.endtime
for data in t:
if lat is None:
lat = data[latkey]
if lon is None:
lon = data[lonkey]
if elev is None:
elev = data[elevkey]
if samprate is None:
samprate = data[srkey]
# if not data['data']:
# yield json.dumps(data, default=datetime.isoformat)
if data['data']:
stt = data['starttime']
ent = data['endtime']
if level == 'station':
key = 'XX|%s||FH1|%s|%s|%s|SiteName' % (data['id'], lat, lon, elev)
else:
key = 'XX|%s||FH1|%s|%s|%s|depth|azimuth|dip|DAS|Scale|ScaleFrequency|ScaleUnits|%s' % \
(data['id'], lat, lon, elev, samprate)
if key in result:
# Get the minimum starttime
stt = stt if stt < result[key][0] else result[key][0]
# Get the maximum endtime
ent = ent if ent > result[key][1] else result[key][1]
result[key] = (stt, ent)
# Send the epoch(s)
for key in result:
# Key is in the proper format for station or channel level. We only need to add the start and end time
text = '%s|%s|%s\n' % (key, stt.isoformat(), ent.isoformat())
yield text
return
......@@ -605,7 +625,6 @@ class DataselectAPI(object):
except Exception:
# Send Error 400
message = 'Wrong formatted list of stations (%s).' % sta
# self.log.error(message)
cherrypy.response.headers['Content-Type'] = 'text/plain'
raise cherrypy.HTTPError(400, errormessage(400, message))
......@@ -615,7 +634,6 @@ class DataselectAPI(object):
except Exception:
# Send Error 400
message = 'Error converting the "starttime" parameter (%s).' % starttime
# self.log.error(message)
cherrypy.response.headers['Content-Type'] = 'text/plain'
raise cherrypy.HTTPError(400, errormessage(400, message))
else:
......@@ -627,7 +645,6 @@ class DataselectAPI(object):
except Exception:
# Send Error 400
message = 'Error converting the "endtime" parameter (%s).' % endtime
# self.log.error(message)
cherrypy.response.headers['Content-Type'] = 'text/plain'
raise cherrypy.HTTPError(400, errormessage(400, message))
else:
......@@ -694,7 +711,6 @@ def main():
# Read general parameters
experiment = config.get('General', 'experiment')
# logging.basicConfig(level=loglevel)
server_config = {
'global': {
......