Skip to content

Commit

Permalink
Make classes in SSPEvent more dict-like
Browse files Browse the repository at this point in the history
So that they can be serialized to YAML more easily.
  • Loading branch information
claudiodsf committed Mar 25, 2024
1 parent 5ef0f02 commit 99c202b
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions sourcespec/ssp_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,34 @@ def _dyne_cm_to_N_m(value):
return None if value is None else value * 1e-7


class SSPCoordinate():
class _SSPEventBaseClass():
"""
SourceSpec event base class.
"""
# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def keys(self):
"""
Return the event attribute names as a list.
"""
return self.__dict__.keys()

def values(self):
"""
Return the event attributes as a list.
"""
return self.__dict__.values()

def items(self):
"""
Return the event attributes as a list of tuples.
"""
return self.__dict__.items()


class SSPCoordinate(_SSPEventBaseClass):
"""
SourceSpec coordinate class.
Expand All @@ -63,18 +90,14 @@ def __init__(self, value=None, units=None, **kwargs):
else:
raise ValueError('coordinate units must be deg')

# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def __str__(self):
try:
return f'{self.value_in_deg:.4f}°'
except TypeError as e:
raise TypeError('Incomplete coordinate data') from e


class SSPDepth():
class SSPDepth(_SSPEventBaseClass):
"""
SourceSpec depth class.
"""
Expand All @@ -84,10 +107,6 @@ def __init__(self, value=None, units=None, **kwargs):
self.value = _float(value)
self.units = units

# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def __str__(self):
try:
return f'{self.value:.1f} {self.units}'
Expand Down Expand Up @@ -117,7 +136,7 @@ def value_in_km(self):
raise ValueError('depth units must be m or km')


class SSPHypocenter():
class SSPHypocenter(_SSPEventBaseClass):
"""
SourceSpec hypocenter class.
"""
Expand All @@ -139,10 +158,6 @@ def __init__(self, longitude=None, latitude=None, depth=None,
self.depth = SSPDepth(**depth)
self.origin_time = _time(origin_time)

# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def __str__(self):
try:
return (
Expand All @@ -155,7 +170,7 @@ def __str__(self):
raise TypeError('Incomplete hypocenter data') from e


class SSPMagnitude():
class SSPMagnitude(_SSPEventBaseClass):
"""
SourceSpec magnitude class.
"""
Expand All @@ -166,10 +181,6 @@ def __init__(self, value=None, mag_type=None, **kwargs):
self.mag_type = mag_type
self.computed = False

# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def __str__(self):
try:
mag_type = self.mag_type or 'M'
Expand Down Expand Up @@ -198,7 +209,7 @@ def from_scalar_moment(self, scalar_moment):
self.computed = True


class SSPScalarMoment():
class SSPScalarMoment(_SSPEventBaseClass):
"""
SourceSpec scalar moment class.
"""
Expand All @@ -209,10 +220,6 @@ def __init__(self, value=None, units=None, **kwargs):
self.units = units
self.to_N_m()

# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def __str__(self):
try:
return (
Expand Down Expand Up @@ -247,7 +254,7 @@ def from_moment_tensor(self, moment_tensor):
self.to_N_m()


class SSPFocalMechanism():
class SSPFocalMechanism(_SSPEventBaseClass):
"""
SourceSpec focal mechanism class.
Expand All @@ -261,10 +268,6 @@ def __init__(self, strike=None, dip=None, rake=None, units=None, **kwargs):
self.rake = _float(rake)
self.units = units # currently not used

# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def __str__(self):
try:
return (
Expand All @@ -290,7 +293,7 @@ def from_moment_tensor(self, moment_tensor):
self.strike, self.dip, self.rake = _fps1


class SSPMomentTensor():
class SSPMomentTensor(_SSPEventBaseClass):
"""
SourceSpec moment tensor class.
"""
Expand All @@ -307,10 +310,6 @@ def __init__(self, units=None, m_rr=None, m_tt=None, m_pp=None, m_rt=None,
self.m_tp = _float(m_tp)
self.to_N_m()

# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def __str__(self):
try:
return (
Expand Down Expand Up @@ -338,7 +337,7 @@ def to_N_m(self):
self.units = 'N-m'


class SSPEvent():
class SSPEvent(_SSPEventBaseClass):
"""
SourceSpec event class.
"""
Expand All @@ -359,10 +358,6 @@ def __init__(self, event_dict=None):
if event_dict is not None:
self.from_event_dict(event_dict)

# make the class subscriptable
def __getitem__(self, key):
return getattr(self, key)

def __str__(self):
outstr = ''
if self.event_id is not None:
Expand Down

0 comments on commit 99c202b

Please sign in to comment.