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 #23446 passed with stage
in 11 minutes and 18 seconds
......@@ -213,9 +213,9 @@ class Earthquake(object):
"object found in %s" % path
)
# Time is not necessarily in ISO format, so fix this for use with datetime object
hms, micsec = event["time"].split(".")
micsec = micsec.replace("Z", "0")[:6]
event_time = ".".join([hms, micsec])
hh, mm, ss = event["time"].split(":")
ss = "{:2.6f}".format(float(ss.replace("Z", "")))
event_time = ":".join([hh, mm, ss])
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 event["focalmechanism"]:
......
......@@ -21,6 +21,7 @@ import os
import sys
from typing import List, Union
import urllib.request as ul
from http.client import HTTPException
from xml.etree import ElementTree as ET
import yaml
......@@ -152,8 +153,11 @@ def fetch_quakeml_ws(evid: str) -> str:
)
)
)
req = ul.Request(url)
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")
print("Got", len(buf), "char(s).")
......
......@@ -8,6 +8,7 @@
# ----------------------------------------------------------------------
import os
import unittest
from http.client import HTTPException
from shakyground2.io.import_fdsnws_eq import fetch_quakeml
......@@ -73,6 +74,22 @@ class QuakeMLReadTestCase(unittest.TestCase):
self.assertEqual(len(result["origin"]), 3)
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):
try:
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