From 99456848c71c554c7cebcfa682d9221ed6d6d7d3 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 8 Nov 2021 13:47:41 +1300 Subject: [PATCH] Put checks for unequal sized lists near top and add some unit tests --- pygmt/src/meca.py | 23 +++++++++++++++++++++++ pygmt/tests/test_meca.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/pygmt/src/meca.py b/pygmt/src/meca.py index babf767e6c7..6ad8973266c 100644 --- a/pygmt/src/meca.py +++ b/pygmt/src/meca.py @@ -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"], diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index dcaf4804850..3a1c90b1488 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -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") @@ -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(): """