Commit 7d4604b7 authored by Jerome Touvier's avatar Jerome Touvier
Browse files

better nodata handling

parent e8156e52
......@@ -4,6 +4,7 @@ from tempfile import NamedTemporaryFile
from flask import make_response
from obspy.clients.fdsn import Client
from obspy.clients.fdsn.client import FDSNNoDataException
from obspy.core import UTCDateTime
from obspy.signal.filter import envelope
......@@ -14,6 +15,7 @@ from apps.globals import MAX_DATA_POINTS
from apps.globals import MAX_DATA_POINTS_PROCESSING
from apps.globals import MAX_PLOTS
from apps.globals import USER_AGENT_TIMESERIES_INVENTORY
from apps.utils import error_nodata
from apps.utils import error_request
from apps.utils import get_bounds
from apps.utils import get_signal
......@@ -196,7 +198,11 @@ def get_output(params):
tic1 = time.time()
if FROM_CLIENT:
st = get_signal_from_client(params)
try:
st = get_signal_from_client(params)
except FDSNNoDataException as nodata:
logging.debug(repr(nodata))
return error_nodata(params)
else:
st = get_signal(params)
logging.info(f"Get data in {tictac(tic1)} seconds.")
......@@ -218,15 +224,11 @@ def get_output(params):
st.attach_response(get_response(params))
except Exception as ex:
logging.debug(str(ex))
code = params["nodata"]
return error_request(
msg=f"HTTP._{code}_", details=Error.RESPONSE, code=code
)
return error_nodata(params, Error.RESPONSE)
st = get_processed_signal(st, params)
if not st:
code = params["nodata"]
return error_request(msg=f"HTTP._{code}_", details=Error.NODATA, code=code)
return error_nodata(params)
if params["format"] == "plot":
response = static_plots(params, st)
......
......@@ -8,12 +8,14 @@ from bokeh.models import DatetimeTickFormatter, HoverTool
from bokeh.plotting import figure
from bokeh.resources import CDN
from flask import make_response
from obspy.clients.fdsn.client import FDSNNoDataException
from apps.globals import Error
from apps.globals import FROM_CLIENT
from apps.globals import MAX_DATA_POINTS
from apps.globals import MAX_DATA_POINTS_PROCESSING
from apps.globals import MAX_PLOTS
from apps.utils import error_nodata
from apps.utils import error_request
from apps.utils import get_bounds
from apps.utils import get_signal
......@@ -129,7 +131,11 @@ def get_output(params):
tic1 = time.time()
if FROM_CLIENT:
st = get_signal_from_client(params)
try:
st = get_signal_from_client(params)
except FDSNNoDataException as nodata:
logging.debug(repr(nodata))
return error_nodata(params)
else:
st = get_signal(params)
logging.info(f"Get data in {tictac(tic1)} seconds.")
......@@ -150,15 +156,11 @@ def get_output(params):
st.attach_response(get_response(params))
except Exception as ex:
logging.debug(str(ex))
code = params["nodata"]
return error_request(
msg=f"HTTP._{code}_", details=Error.RESPONSE, code=code
)
return error_nodata(params, Error.RESPONSE)
st = get_processed_signal(st, params)
if not st:
code = params["nodata"]
return error_request(msg=f"HTTP._{code}_", details=Error.NODATA, code=code)
return error_nodata(params)
if params["iplot"]:
response = dynamic_plots(params, st)
......
......@@ -15,7 +15,6 @@ from matplotlib.ticker import FuncFormatter, MaxNLocator
import numpy as np
from obspy.clients.fdsn import Client
from obspy.clients.fdsn.client import FDSNNoDataException
from obspy.core import read, UTCDateTime
from obspy.core.stream import Stream
......@@ -150,11 +149,6 @@ def tictac(tic):
return round(time.time() - tic, 2)
# Result HTTP code 400 shortcut function
def error_param(params, dmesg):
return (params, {"msg": HTTP._400_, "details": dmesg, "code": 400})
# Error request function
def error_request(msg="", details="", code=500):
request_date = datetime.utcnow().strftime("%Y-%b-%d %H:%M:%S UTC")
......@@ -168,6 +162,16 @@ Service version:
version:{VERSION}"""
return Response(message_error, status=code, mimetype="text/plain")
# No data response alias
def error_nodata(params, dmesg=Error.NODATA):
code = int(params["nodata"])
return error_request(msg=f"HTTP._{code}_", details=dmesg, code=code)
# Result HTTP code 400 shortcut function
def error_param(params, dmesg):
return (params, {"msg": HTTP._400_, "details": dmesg, "code": 400})
# Error 413 response alias
def overflow_error(dmesg):
......@@ -349,32 +353,25 @@ def get_signal_from_client(params):
:param params: parameter dictionary with url parameters (network, station, ...)
:returns: a tuple (trace.time, trace.data, trace.stats)
:raises FDSNNoDataException: raises value exception
"""
logging.debug("Enter in get_signal_from_client function.")
start = UTCDateTime(params["start"])
end = UTCDateTime(params["end"])
try:
logging.debug("Enter in get_signal_from_client function.")
client = Client(FDSN_CLIENT, user_agent=USER_AGENT_TIMESERIES)
st = client.get_waveforms(
params["network"],
params["station"],
params["location"],
params["channel"],
start,
end,
)
logging.debug("Transfer terminated.")
st.trim(start, end)
logging.debug("Trimming terminated.")
return st
except FDSNNoDataException as nodata:
logging.debug(repr(nodata))
except Exception as ex:
logging.exception(str(ex))
client = Client(FDSN_CLIENT, user_agent=USER_AGENT_TIMESERIES)
st = client.get_waveforms(
params["network"],
params["station"],
params["location"],
params["channel"],
start,
end,
)
logging.debug("Transfer terminated.")
st.trim(start, end)
logging.debug("Trimming terminated.")
return st
def get_response(params):
......@@ -417,7 +414,6 @@ def get_signal(params):
:param params: parameter dictionary with url parameters (network, station, ...)
:returns: a tuple (trace.time, trace.data, trace.stats)
:raises FDSNNoDataException: raises value exception
"""
logging.debug("Enter in get_signal function.")
......@@ -540,7 +536,7 @@ def get_bounds(st):
def date_format_hours(x, pos=None):
"""See date_format_days function
Only the first tick contains the full date.
Only the first tick contains the full date.
"""
fmt = "%D %H:%M:%S" if pos == 1 else "%H:%M:%S"
return datetime.utcfromtimestamp(x).strftime(fmt)
......
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