Skip to content

Commit

Permalink
feat: add testing for the client
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidni committed Aug 17, 2023
1 parent d25b1e4 commit 2db3d3a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 13 deletions.
34 changes: 21 additions & 13 deletions catalog_tools/io/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class FDSNWSEventClient():
"""
Client for downloading earthquake catalogs from the FDSNWS event service.
Args:
url: base url of the FDSNWS event service
(eg. 'https://earthquake.usgs.gov/fdsnws/event/1/query')
Expand All @@ -25,26 +27,29 @@ def get_events(self, start_time: datetime | None = None,
min_longitude: float | None = None,
max_longitude: float | None = None,
min_magnitude: float | None = None,
max_magnitude: float | None = None,
include_all_magnitudes: bool | None = None,
event_type: str | None = None,
delta_m: float = 0.1,
delta_m: float | None = 0.1,
include_uncertainty: bool = False) -> pd.DataFrame:
"""Downloads an earthquake catalog based on a URL.
Args:
base_query: base query url ()
start_time: start time of the catalog.
end_time: end time of the catalog. defaults to current time.
min_latitude: minimum latitude of catalog.
max_latitude: maximum latitude of catalog.
min_longitude: minimum longitude of catalog.
max_longitude: maximum longitude of catalog.
min_magnitude: minimum magnitude of catalog.
start_time: start time of the catalog.
end_time: end time of the catalog. defaults to
current time.
min_latitude: minimum latitude of catalog.
max_latitude: maximum latitude of catalog.
min_longitude: minimum longitude of catalog.
max_longitude: maximum longitude of catalog.
min_magnitude: minimum magnitude of catalog.
max_magnitude: maximum magnitude of catalog.
include_all_magnitudes: whether to include all magnitudes.
event_type: type of event to download.
delta_m: magnitude bin size. if >0, then events of
magnitude >= (min_magnitude - delta_m/2) will be downloaded.
include_uncertainty: whether to include uncertainty columns.
event_type: type of event to download.
delta_m: magnitude bin size. if >0, then events of
magnitude >= (min_magnitude - delta_m/2)
will be downloaded.
include_uncertainty: whether to include uncertainty columns.
Returns:
The catalog as a Catalog Object.
Expand All @@ -69,6 +74,8 @@ def get_events(self, start_time: datetime | None = None,
request_url += f'&minmagnitude={min_magnitude - (delta_m / 2)}'
elif min_magnitude:
request_url += f'&minmagnitude={min_magnitude}'
if max_magnitude:
request_url += f'&maxmagnitude={max_magnitude}'
if include_all_magnitudes:
request_url += f'&includeallmagnitudes={include_all_magnitudes}'
if event_type:
Expand All @@ -83,6 +90,7 @@ def get_events(self, start_time: datetime | None = None,

r = requests.get(request_url, stream=True)
r.raw.decode_content = True # if content-encoding is used decode

parser.parse(r.raw)

df = Catalog.from_dict(catalog)
Expand Down
79 changes: 79 additions & 0 deletions catalog_tools/io/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os
from datetime import datetime

import responses
from responses import matchers

from catalog_tools.catalog import Catalog
from catalog_tools.io.client import FDSNWSEventClient

PATH_RESOURCES = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'query.xml')

date_format = "%Y-%m-%dT%H:%M:%S"


@responses.activate
def test_download_catalog_1():
url = 'http://mocked_url'
start_time = datetime(1900, 1, 1)
end_time = datetime(2022, 1, 1)
delta_m = 0.1
min_mag = 3.0
max_mag = 5.0
min_lat = 45.0
max_lat = 50.0
min_lon = 5.0
max_lon = 10.0
event_type = 'earthquake'

responses.add(responses.GET, url,
body=open(PATH_RESOURCES, 'rb'), status=200,
match=[
matchers.query_param_matcher(
{'starttime': start_time.strftime(date_format),
'endtime': end_time.strftime(date_format),
'minmagnitude': min_mag - (delta_m / 2),
'maxmagnitude': max_mag,
'minlatitude': min_lat,
'maxlatitude': max_lat,
'minlongitude': min_lon,
'maxlongitude': max_lon,
'eventtype': event_type})],)

responses.add(responses.GET, url,
body=open(PATH_RESOURCES, 'rb'), status=200,
match=[
matchers.query_param_matcher(
{'includeallmagnitudes': True,
'minmagnitude': min_mag})],)

client = FDSNWSEventClient(url)

cat = client.get_events(
start_time=start_time,
end_time=end_time,
min_magnitude=min_mag,
max_magnitude=max_mag,
min_latitude=min_lat,
max_latitude=max_lat,
min_longitude=min_lon,
max_longitude=max_lon,
event_type=event_type
)

assert len(cat) == 4
assert isinstance(cat, Catalog)

assert cat.columns.tolist().sort() == \
['evaluationMode', 'eventid', 'event_type', 'time', 'latitude',
'longitude', 'depth', 'magnitude', 'magnitude_type'].sort()

cat = client.get_events(
include_all_magnitudes=True,
include_uncertainty=True,
delta_m=None,
min_magnitude=min_mag,
)

assert len(cat.columns) == 22

0 comments on commit 2db3d3a

Please sign in to comment.