Commit ce87885f authored by g-weatherill's avatar g-weatherill
Browse files

Fixes datetime formatting and adds new error for FDSN failure

parent a265e042
Pipeline #23444 passed with stage
in 12 minutes and 19 seconds
...@@ -213,9 +213,9 @@ class Earthquake(object): ...@@ -213,9 +213,9 @@ class Earthquake(object):
"object found in %s" % path "object found in %s" % path
) )
# Time is not necessarily in ISO format, so fix this for use with datetime object # Time is not necessarily in ISO format, so fix this for use with datetime object
hms, micsec = event["time"].split(".") hh, mm, ss = event["time"].split(":")
micsec = micsec.replace("Z", "0")[:6] ss = "{:2.6f}".format(float(ss.replace("Z", "")))
event_time = ".".join([hms, micsec]) event_time = ":".join([hh, mm, ss])
d_t = datetime.datetime.fromisoformat(" ".join([event["date"], event_time])) d_t = datetime.datetime.fromisoformat(" ".join([event["date"], event_time]))
# If the event has a focal mechanism then parse this into the correct format # If the event has a focal mechanism then parse this into the correct format
if event["focalmechanism"]: if event["focalmechanism"]:
......
...@@ -21,6 +21,7 @@ import os ...@@ -21,6 +21,7 @@ import os
import sys import sys
from typing import List, Union from typing import List, Union
import urllib.request as ul import urllib.request as ul
from http.client import HTTPException
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
import yaml import yaml
...@@ -152,8 +153,11 @@ def fetch_quakeml_ws(evid: str) -> str: ...@@ -152,8 +153,11 @@ def fetch_quakeml_ws(evid: str) -> str:
) )
) )
) )
req = ul.Request(url) req = ul.Request(url)
u = ul.urlopen(req) u = ul.urlopen(req)
if u.code != 200:
raise HTTPException("FDSN result not returned for event %s with url:\n%s" % (evid, url))
buf = u.read().decode("utf8") buf = u.read().decode("utf8")
print("Got", len(buf), "char(s).") print("Got", len(buf), "char(s).")
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
import os import os
import unittest import unittest
from http.client import HTTPException
from shakyground2.io.import_fdsnws_eq import fetch_quakeml from shakyground2.io.import_fdsnws_eq import fetch_quakeml
...@@ -73,6 +74,22 @@ class QuakeMLReadTestCase(unittest.TestCase): ...@@ -73,6 +74,22 @@ class QuakeMLReadTestCase(unittest.TestCase):
self.assertEqual(len(result["origin"]), 3) self.assertEqual(len(result["origin"]), 3)
self.assertTrue(result["time"].startswith("08:08:23")) self.assertTrue(result["time"].startswith("08:08:23"))
def test_bad_html_connection(self):
"""
If a bad url is given, check that it raises an HTTPException
"""
evid = "gfzabadname"
with self.assertRaises(HTTPException) as he:
fetch_quakeml(evid)
badurl = (
"http://geofon.gfz-potsdam.de/fdsnws/event/1/query?"
"includeallmagnitudes=true&includefocalmechanism=true&eventid={:s}".format(evid)
)
self.assertEqual(
str(he.exception),
"FDSN result not returned for event %s with url:\n%s" % (evid, badurl),
)
def tearDown(self): def tearDown(self):
try: try:
os.unlink(self.outfile) os.unlink(self.outfile)
......
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