From 52b8dbb8f437032af67c8f98d92ab8ea61ea39f9 Mon Sep 17 00:00:00 2001 From: chad-iris Date: Sun, 2 Oct 2016 18:05:39 -0700 Subject: [PATCH 1/3] Fix irisws-distaz client and return enhanced results --- obspy/clients/iris/client.py | 39 +++++++++++++++---------- obspy/clients/iris/tests/test_client.py | 16 ++++++---- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/obspy/clients/iris/client.py b/obspy/clients/iris/client.py index bfce863b237..16d2879b804 100644 --- a/obspy/clients/iris/client.py +++ b/obspy/clients/iris/client.py @@ -15,8 +15,8 @@ from future.utils import native_str import io -import json import platform +from lxml import objectify with standard_library.hooks(): import urllib.parse @@ -73,11 +73,11 @@ class Client(object): >>> result = client.distaz(stalat=1.1, stalon=1.2, evtlat=3.2, ... evtlon=1.4) >>> print(result['distance']) - 2.09554 + 2.10256 >>> print(result['backazimuth']) - 5.46946 + 5.46944 >>> print(result['azimuth']) - 185.47692 + 185.47695 """ def __init__(self, base_url="http://service.iris.edu/irisws", user="", password="", timeout=20, debug=False, @@ -558,7 +558,7 @@ def sacpz(self, network, station, location="*", channel="*", def distaz(self, stalat, stalon, evtlat, evtlon): """ Low-level interface for `distaz` Web service of IRIS - (http://service.iris.edu/irisws/distaz/) - release 1.0.1 (2010). + (http://service.iris.edu/irisws/distaz/) - release 1.0.3 (2016). This method will calculate the great-circle angular distance, azimuth, and backazimuth between two geographic coordinate pairs. All results @@ -583,6 +583,9 @@ def distaz(self, stalat, stalon, evtlat, evtlon): Latitudes are converted to geocentric latitudes using the WGS84 spheroid to correct for ellipticity. + The distance (in degrees) and distancemeters are the distance + between the two points on the spheroid. + .. rubric:: Example >>> from obspy.clients.iris import Client @@ -590,27 +593,31 @@ def distaz(self, stalat, stalon, evtlat, evtlon): >>> result = client.distaz(stalat=1.1, stalon=1.2, evtlat=3.2, ... evtlon=1.4) >>> print(result['distance']) - 2.09554 + 2.10256 + >>> print(result['distancemeters']) + 233272.79028 >>> print(result['backazimuth']) - 5.46946 + 5.46944 >>> print(result['azimuth']) - 185.47692 + 185.47695 + >>> print(result['ellipsoidname']) + WGS84 """ - # set JSON as expected content type - headers = {'Accept': 'application/json'} # build up query try: - data = self._fetch("distaz", headers=headers, stalat=stalat, - stalon=stalon, evtlat=evtlat, evtlon=evtlon) + data = self._fetch("distaz", stalat=stalat, stalon=stalon, + evtlat=evtlat, evtlon=evtlon) except urllib.request.HTTPError as e: msg = "No response data available (%s: %s)" msg = msg % (e.__class__.__name__, e) raise Exception(msg) - data = json.loads(data.decode()) + data = objectify.fromstring(data.decode()) results = {} - results['distance'] = data['distance'] - results['backazimuth'] = data['backAzimuth'] - results['azimuth'] = data['azimuth'] + results['ellipsoidname'] = data.ellipsoid.attrib['name'] + results['distance'] = data.distance + results['distancemeters'] = data.distanceMeters + results['backazimuth'] = data.backAzimuth + results['azimuth'] = data.azimuth return results def flinnengdahl(self, lat, lon, rtype="both"): diff --git a/obspy/clients/iris/tests/test_client.py b/obspy/clients/iris/tests/test_client.py index 828822b2417..acf752d2f10 100644 --- a/obspy/clients/iris/tests/test_client.py +++ b/obspy/clients/iris/tests/test_client.py @@ -58,14 +58,18 @@ def test_distaz(self): client = Client() # normal request result = client.distaz(stalat=1.1, stalon=1.2, evtlat=3.2, evtlon=1.4) - self.assertAlmostEqual(result['distance'], 2.09554) - self.assertAlmostEqual(result['backazimuth'], 5.46946) - self.assertAlmostEqual(result['azimuth'], 185.47692) + self.assertAlmostEqual(result['distance'], 2.10256) + self.assertAlmostEqual(result['distancemeters'], 233272.79028) + self.assertAlmostEqual(result['backazimuth'], 5.46944) + self.assertAlmostEqual(result['azimuth'], 185.47695) + self.assertEqual(result['ellipsoidname'], 'WGS84') # w/o kwargs result = client.distaz(1.1, 1.2, 3.2, 1.4) - self.assertAlmostEqual(result['distance'], 2.09554) - self.assertAlmostEqual(result['backazimuth'], 5.46946) - self.assertAlmostEqual(result['azimuth'], 185.47692) + self.assertAlmostEqual(result['distance'], 2.10256) + self.assertAlmostEqual(result['distancemeters'], 233272.79028) + self.assertAlmostEqual(result['backazimuth'], 5.46944) + self.assertAlmostEqual(result['azimuth'], 185.47695) + self.assertEqual(result['ellipsoidname'], 'WGS84') # missing parameters self.assertRaises(Exception, client.distaz, stalat=1.1) self.assertRaises(Exception, client.distaz, 1.1) From b36ff9ed9e0b29ed27c6815d144862d2bbd8bcb6 Mon Sep 17 00:00:00 2001 From: chad-iris Date: Sun, 2 Oct 2016 19:42:44 -0700 Subject: [PATCH 2/3] Add backticks when referring to keys in the results for clarity --- obspy/clients/iris/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/obspy/clients/iris/client.py b/obspy/clients/iris/client.py index 16d2879b804..1bce2511810 100644 --- a/obspy/clients/iris/client.py +++ b/obspy/clients/iris/client.py @@ -577,13 +577,13 @@ def distaz(self, stalat, stalon, evtlat, evtlon): :return: Dictionary containing values for azimuth, backazimuth and distance. - The azimuth is the angle from the station to the event, while the - backazimuth is the angle from the event to the station. + The ``azimuth`` is the angle from the station to the event, while the + ``backazimuth`` is the angle from the event to the station. Latitudes are converted to geocentric latitudes using the WGS84 spheroid to correct for ellipticity. - The distance (in degrees) and distancemeters are the distance + The ``distance`` (in degrees) and ``distancemeters`` are the distance between the two points on the spheroid. .. rubric:: Example From 05fa18e49c8e4b1aea78d2e4de79a444bdea435a Mon Sep 17 00:00:00 2001 From: Lion Krischer Date: Mon, 3 Oct 2016 08:27:03 +0200 Subject: [PATCH 3/3] Fixing traveltime doc test --- obspy/clients/iris/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/obspy/clients/iris/client.py b/obspy/clients/iris/client.py index 1bce2511810..57964d3d2ca 100644 --- a/obspy/clients/iris/client.py +++ b/obspy/clients/iris/client.py @@ -776,7 +776,7 @@ def traveltime(self, model='iasp91', phases=DEFAULT_PHASES, evdepth=0.0, Distance Depth Phase Travel Ray Param Takeoff Incident ... (deg) (km) Name Time (s) p (s/deg) (deg) (deg) ... ------------------------------------------------------------------... - 3.24 22.9 P 49.39 13.749 53.77 45.82 ... + 3.24 22.9 P 49.39 13.750 53.77 45.82 ... 3.24 22.9 Pn 49.40 13.754 53.80 45.84 ... """ kwargs = {}