Skip to content

Commit

Permalink
Merge pull request #1543 from chad-iris/fix-irisws-distaz
Browse files Browse the repository at this point in the history
Fix irisws-distaz client following service upgrade to 1.0.3
  • Loading branch information
krischer committed Oct 3, 2016
2 parents 2de7e33 + 05fa18e commit 255a2c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
45 changes: 26 additions & 19 deletions obspy/clients/iris/client.py
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -577,40 +577,47 @@ 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
between the two points on the spheroid.
.. rubric:: Example
>>> from obspy.clients.iris import Client
>>> client = Client()
>>> 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"):
Expand Down Expand Up @@ -769,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 = {}
Expand Down
16 changes: 10 additions & 6 deletions obspy/clients/iris/tests/test_client.py
Expand Up @@ -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)
Expand Down

0 comments on commit 255a2c1

Please sign in to comment.