Skip to content

Commit

Permalink
Put checks for unequal sized lists near top and add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weiji14 committed Nov 8, 2021
1 parent 3f981e1 commit 9945684
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pygmt/src/meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,29 @@ def update_pointers(data_pointers):
):
raise GMTError("Location not fully specified.")

# check the inputs for longitude, latitude, and depth
# just in case the user entered different length lists
if (
isinstance(longitude, (list, np.ndarray))
or isinstance(latitude, (list, np.ndarray))
or isinstance(depth, (list, np.ndarray))
):
if (len(longitude) != len(latitude)) or (len(longitude) != len(depth)):
raise GMTError("Unequal number of focal mechanism locations supplied.")

if isinstance(spec, dict) and any(
isinstance(s, (list, np.ndarray)) for s in spec.values()
):
# before constructing the 2D array lets check that each key
# of the dict has the same quantity of values to avoid bugs
list_length = len(list(spec.values())[0])
for value in list(spec.values()):
if len(value) != list_length:
raise GMTError(
"Unequal number of focal mechanism "
"parameters supplied in 'spec'."
)

param_conventions = {
"AKI": ["strike", "dip", "rake", "magnitude"],
"GCMT": ["strike1", "dip1", "dip2", "rake2", "mantissa", "exponent"],
Expand Down
34 changes: 34 additions & 0 deletions pygmt/tests/test_meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pandas as pd
import pytest
from pygmt import Figure
from pygmt.exceptions import GMTError
from pygmt.helpers import GMTTempFile

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
Expand Down Expand Up @@ -56,6 +57,39 @@ def test_meca_spec_dict_list():
return fig


def test_meca_spec_unequal_sized_lists_fails():
"""
Test that supplying a dictionary containing unequal sized lists of
coordinates (longitude/latitude/depth) or focal mechanisms
(strike/dip/rake/magnitude) to the spec parameter fails.
"""
fig = Figure()

# Unequal sized coordinates (longitude/latitude/depth)
focal_mechanisms = dict(
strike=[330, 350], dip=[30, 50], rake=[90, 90], magnitude=[3, 2]
)
with pytest.raises(GMTError):
fig.meca(
spec=focal_mechanisms,
longitude=[-124.3],
latitude=[48.1, 48.2],
depth=[12.0],
scale="2c",
)

# Unequal sized focal mechanisms (strike/dip/rake/magnitude)
focal_mechanisms = dict(strike=[330], dip=[30, 50], rake=[90], magnitude=[3, 2])
with pytest.raises(GMTError):
fig.meca(
spec=focal_mechanisms,
longitude=[-124.3, -124.4],
latitude=[48.1, 48.2],
depth=[12.0, 11.0],
scale="2c",
)


@pytest.mark.mpl_image_compare
def test_meca_spec_dataframe():
"""
Expand Down

0 comments on commit 9945684

Please sign in to comment.