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

Small fixes and cleanup to fdsnws importer

parents 09551a18 5c5db22a
...@@ -17,10 +17,9 @@ Can run standalone: ...@@ -17,10 +17,9 @@ Can run standalone:
""" """
import datetime
import os import os
import sys import sys
from typing import List, Optional, Union from typing import List, Union
import urllib.request as ul import urllib.request as ul
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
import yaml import yaml
...@@ -60,17 +59,15 @@ def fetch_fm(root: ET.Element, ns: dict, preferredfmID: str) -> list: ...@@ -60,17 +59,15 @@ def fetch_fm(root: ET.Element, ns: dict, preferredfmID: str) -> list:
_perror("Oops: no <nodalPlanes> object seen") _perror("Oops: no <nodalPlanes> object seen")
break break
d: List[dict] = [{} for i in range(2)] d: List[dict] = [dict(), dict()]
for k in range(2): for k in range(2):
plane = np.find("ns:nodalPlane" + str(k + 1), ns) plane = np.find("ns:nodalPlane" + str(k + 1), ns)
#d[k] = dict() if plane is None:
if not plane:
continue continue
for child in plane: for child in plane:
found = child.find("ns:value", ns) found = child.find("ns:value", ns)
print(found) if found is None:
#if not found: continue
# continue
v = found.text v = found.text
tag = child.tag tag = child.tag
tag = tag[tag.index("}") + 1 :] tag = tag[tag.index("}") + 1 :]
...@@ -89,10 +86,10 @@ def fetch_magnitude( ...@@ -89,10 +86,10 @@ def fetch_magnitude(
continue continue
child = m.find("ns:mag", ns) child = m.find("ns:mag", ns)
if not child: if child is None:
return None return None
value = child.find("ns:value", ns) value = child.find("ns:value", ns)
if not value: if value is None:
return None return None
v = value.text v = value.text
try: try:
...@@ -154,14 +151,11 @@ def fetch_quakeml_ws(evid: str) -> str: ...@@ -154,14 +151,11 @@ def fetch_quakeml_ws(evid: str) -> str:
) )
) )
# print(url)
req = ul.Request(url) req = ul.Request(url)
u = ul.urlopen(req) u = ul.urlopen(req)
buf = u.read().decode("utf8") buf = u.read().decode("utf8")
print("Got", len(buf), "char(s).") print("Got", len(buf), "char(s).")
with open("tmp.xml", "w") as fid:
fid.write(str(buf))
return buf return buf
...@@ -188,7 +182,6 @@ def fetch_quakeml(path: str) -> Union[dict, None]: ...@@ -188,7 +182,6 @@ def fetch_quakeml(path: str) -> Union[dict, None]:
ns = QUAKEML_NS ns = QUAKEML_NS
event = root[0][0] event = root[0][0]
# .find("ns:event", ns)
if not event: if not event:
print("Couldn't get an event!") print("Couldn't get an event!")
return None return None
...@@ -202,14 +195,14 @@ def fetch_quakeml(path: str) -> Union[dict, None]: ...@@ -202,14 +195,14 @@ def fetch_quakeml(path: str) -> Union[dict, None]:
_perror("Oops, couldn't get event id from " + event.attrib["publicID"]) _perror("Oops, couldn't get event id from " + event.attrib["publicID"])
preferredoriginIDElem = root[0][0].find("ns:preferredOriginID", ns) preferredoriginIDElem = root[0][0].find("ns:preferredOriginID", ns)
if preferredoriginIDElem is None:
if not preferredoriginIDElem: _perror("Oops, couldn't find the preferred origin ID")
return None return None
preferredoriginID = preferredoriginIDElem.text preferredoriginID = preferredoriginIDElem.text
preferredmagID = None preferredmagID = None
preferredMagnitudeIDElem = root[0][0].find("ns:preferredMagnitudeID", ns) preferredMagnitudeIDElem = root[0][0].find("ns:preferredMagnitudeID", ns)
if preferredMagnitudeIDElem: if preferredMagnitudeIDElem is not None:
try: try:
preferredmagID = preferredMagnitudeIDElem.text preferredmagID = preferredMagnitudeIDElem.text
except AttributeError: except AttributeError:
...@@ -217,7 +210,7 @@ def fetch_quakeml(path: str) -> Union[dict, None]: ...@@ -217,7 +210,7 @@ def fetch_quakeml(path: str) -> Union[dict, None]:
preferredfmID = None preferredfmID = None
try: try:
preferredfmIDElem = root[0][0].find("ns:preferredFocalMechanismID", ns) preferredfmIDElem = root[0][0].find("ns:preferredFocalMechanismID", ns)
if preferredfmIDElem: if preferredfmIDElem is not None:
preferredfmID = preferredfmIDElem.text preferredfmID = preferredfmIDElem.text
except AttributeError: except AttributeError:
pass pass
...@@ -277,6 +270,8 @@ http://geofon.gfz-potsdam.de/eqinfo/list.php ...@@ -277,6 +270,8 @@ http://geofon.gfz-potsdam.de/eqinfo/list.php
sys.exit(1) sys.exit(1)
ev_info = fetch_quakeml(evid) ev_info = fetch_quakeml(evid)
if ev_info is None:
print("Got no dictionary from QuakeML")
outfile = evid + ".yaml" outfile = evid + ".yaml"
with open(outfile, "w") as fid: with open(outfile, "w") as fid:
fid.write(yaml.safe_dump(ev_info, default_flow_style=False)) fid.write(yaml.safe_dump(ev_info, default_flow_style=False))
......
...@@ -44,8 +44,9 @@ class QuakeMLReadTestCase(unittest.TestCase): ...@@ -44,8 +44,9 @@ class QuakeMLReadTestCase(unittest.TestCase):
""" """
evid = "gfz2021ekhv" evid = "gfz2021ekhv"
result = fetch_quakeml(evid) result = fetch_quakeml(evid)
print(result)
self.assertTrue(isinstance(result, dict)) self.assertTrue(isinstance(result, dict))
self.assertGreater(len(result), 3)
self.assertEqual(result["id"], evid)
def test_read_no_eventid(self): def test_read_no_eventid(self):
""" """
...@@ -54,7 +55,23 @@ class QuakeMLReadTestCase(unittest.TestCase): ...@@ -54,7 +55,23 @@ class QuakeMLReadTestCase(unittest.TestCase):
""" """
infile = "/no/such/file" infile = "/no/such/file"
with self.assertRaises(IOError): with self.assertRaises(IOError):
result = fetch_quakeml(infile) fetch_quakeml(infile)
def test_read_good_eventid2(self):
"""
Might fail if the GEOFON fdsnws-event webservice is unavailable.
Does the dictionary have the values expected for this event?
"""
evid = "gfz2021htpl"
result = fetch_quakeml(evid)
self.assertTrue(isinstance(result, dict))
self.assertGreater(len(result), 3)
self.assertEqual(result["date"], "2021-04-21")
self.assertEqual(len(result["focalmechanism"]), 2)
self.assertEqual(result["id"], "gfz2021htpl")
self.assertLess(abs(float(result["magnitude"]) - 4.465), 0.01)
self.assertEqual(len(result["origin"]), 3)
self.assertTrue(result["time"].startswith("08:08:23"))
def tearDown(self): def tearDown(self):
try: try:
......
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