Skip to content

Commit 624cbbe

Browse files
Merge pull request #262 from mhearne-usgs/allmagfix
Modified toDict method of detail event to get magnitudes from all pha…
2 parents 445dbe4 + 3471049 commit 624cbbe

4 files changed

Lines changed: 67683 additions & 4907 deletions

File tree

libcomcat/classes.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
# stdlib imports
2+
import logging
3+
import re
4+
import time
5+
from collections import OrderedDict
26
from datetime import datetime, timedelta
37
from urllib.error import HTTPError
48
from urllib.parse import urlparse
5-
from collections import OrderedDict
6-
import re
7-
import time
8-
import logging
99

10-
# third party imports
11-
from obspy.core.event import read_events
12-
import pandas as pd
13-
import numpy as np
1410
import dateutil
11+
import numpy as np
12+
import pandas as pd
1513
import requests
1614

15+
# third party imports
16+
from obspy.core.event import read_events
17+
1718
# local imports
1819
from libcomcat.exceptions import (
20+
ArgumentConflictError,
1921
ConnectionError,
22+
ContentNotFoundError,
2023
ProductNotFoundError,
21-
ArgumentConflictError,
2224
UndefinedVersionError,
23-
ContentNotFoundError,
2425
)
2526
from libcomcat.utils import HEADERS, TIMEOUT
2627

@@ -674,12 +675,15 @@ def toDict(
674675
edict.update(_get_focal_mechanism_info(focal))
675676

676677
if get_all_magnitudes:
678+
phases = []
677679
if self.hasProduct("phase-data"):
678-
product = "phase-data"
679-
else:
680-
product = "origin"
681-
phases = self.getProducts(product, source="all")
680+
phase_products = self.getProducts("phase-data", source="all")
681+
phases += phase_products
682+
if self.hasProduct("origin"):
683+
origin_products = self.getProducts("origin", source="all")
684+
phases += origin_products
682685
imag = 0
686+
unique_magnitudes = []
683687
for phase_data in phases:
684688
# we don't want duplicates of phase data information
685689
# from us origin product
@@ -691,14 +695,22 @@ def toDict(
691695
# ######################################
692696
phase_url = phase_data.getContentURL("quakeml.xml")
693697
try:
694-
catalog = read_events(phase_url)
698+
catalog = read_events(phase_url, format="QUAKEML")
695699
except Exception as e:
700+
catalog = read_events(phase_url, format="QUAKEML")
696701
fmt = "Could not parse quakeml file from %s. " "Error: %s"
697702
tpl = (phase_url, str(e))
698703
logging.warning(fmt % tpl)
699704
continue
700705
event = catalog.events[0]
701706
for magnitude in event.magnitudes:
707+
# since resource IDs for magnitudes are unique, we can use this
708+
# to track whether we're getting duplicate magnitudes
709+
# from phase-data and origin products.
710+
magid = magnitude.resource_id.id
711+
if magid in unique_magnitudes:
712+
continue
713+
unique_magnitudes.append(magid)
702714
edict["magnitude%i" % imag] = magnitude.mag
703715
edict["magtype%i" % imag] = magnitude.magnitude_type
704716
cname = "magsource%i" % imag

0 commit comments

Comments
 (0)