Skip to content

Commit

Permalink
feat: only add additional magnitudes if includeallmagnitudes is reque…
Browse files Browse the repository at this point in the history
…sted
  • Loading branch information
schmidni committed Aug 16, 2023
1 parent 55b8a12 commit b4869b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 10 additions & 6 deletions catalog_tools/io/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import requests

from catalog_tools.catalog import Catalog
from catalog_tools.io.parser import CustomContentHandler
from catalog_tools.io.parser import QuakeMLHandler


class FDSNWSEventClient():
Expand All @@ -28,7 +28,8 @@ def get_events(self, start_time: Optional[datetime] = None,
min_magnitude: Optional[float] = None,
include_all_magnitudes: Optional[bool] = None,
event_type: Optional[str] = None,
delta_m: float = 0.1) -> pd.DataFrame:
delta_m: float = 0.1,
include_uncertainty: bool = False) -> pd.DataFrame:
"""Downloads an earthquake catalog based on a URL.
Args:
Expand Down Expand Up @@ -73,17 +74,20 @@ def get_events(self, start_time: Optional[datetime] = None,
if event_type:
request_url += f'&eventtype={event_type}'

print(request_url)

catalog = []
parser = make_parser()
parser.setFeature(handler.feature_namespaces, False)
parser.setContentHandler(CustomContentHandler(catalog))
parser.setContentHandler(QuakeMLHandler(
catalog, includeallmagnitudes=include_all_magnitudes))

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)

# if not include_uncertainty:
# wrong way round
# rgx = "(_uncertainty|_lowerUncertainty|" \
# "_upperUncertainty|_confidenceLevel)$"
# df = df.filter(regex=rgx)
return df
16 changes: 10 additions & 6 deletions catalog_tools/io/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def extract_secondary_magnitudes(magnitudes: list) -> dict:
return magnitude_dict


def parse_to_dict(event: dict, origins: list, magnitudes: list) -> dict:
def parse_to_dict(event: dict, origins: list, magnitudes: list,
includeallmagnitudes: bool = True) -> dict:
preferred_origin = \
get_preferred_origin(origins,
event.get('preferredOriginID', None))
Expand All @@ -126,8 +127,10 @@ def parse_to_dict(event: dict, origins: list, magnitudes: list) -> dict:
get_preferred_magnitude(magnitudes,
event.get('preferredMagnitudeID', None))

if magnitudes:
if magnitudes and includeallmagnitudes:
magnitudes = select_secondary_magnitudes(magnitudes)
else:
magnitudes = []

event_params = \
{value: event.get(key, None) for key, value in EVENT_MAPPINGS.items()}
Expand All @@ -138,15 +141,15 @@ def parse_to_dict(event: dict, origins: list, magnitudes: list) -> dict:
extract_secondary_magnitudes(magnitudes)


class CustomContentHandler(xml.sax.ContentHandler):
class QuakeMLHandler(xml.sax.ContentHandler):
"""
Custom ContentHandler class that extends ContenHandler to
stream parse QuakeML files.
"""

def __init__(self, catalog):
def __init__(self, catalog, includeallmagnitudes=True):
self.catalog = catalog

self.includeallmagnitudes = includeallmagnitudes
self.event = []
self.origin = []
self.magnitude = []
Expand Down Expand Up @@ -176,7 +179,8 @@ def startElement(self, tagName, attrs):
def endElement(self, tagName):
if tagName == 'event':
self.catalog.append(parse_to_dict(
self.event[-1], self.origin, self.magnitude))
self.event[-1], self.origin, self.magnitude,
includeallmagnitudes=self.includeallmagnitudes))
self.parent = ''
self.location = ''
self.event = []
Expand Down

0 comments on commit b4869b3

Please sign in to comment.