Skip to content

Commit

Permalink
Figure.meca: Fix beachball offsetting with dict/pandas inputs (Generi…
Browse files Browse the repository at this point in the history
…cMappingTools#2202)

Co-authored-by: Wei Ji <[email protected]>
  • Loading branch information
2 people authored and Josh Sixsmith committed Dec 21, 2022
1 parent 14caffd commit 7ee5a14
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pygmt/src/meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ def meca(
Depth(s) of event location in kilometers. Must be the same length as
the number of events. Will override the ``depth`` values in ``spec``
if ``spec`` is a dict or pd.DataFrame.
plot_longitude: int, float, list, or 1d numpy array
plot_longitude: int, float, str, list, or 1d numpy array
Longitude(s) at which to place beachball. Must be the same length as
the number of events. Will override the ``plot_longitude`` values in
``spec`` if ``spec`` is a dict or pd.DataFrame.
plot_latitude: int, float, list, or 1d numpy array
plot_latitude: int, float, str, list, or 1d numpy array
Latitude(s) at which to place beachball. List must be the same length
as the number of events. Will override the ``plot_latitude`` values in
``spec`` if ``spec`` is a dict or pd.DataFrame.
Expand Down Expand Up @@ -272,10 +272,10 @@ def meca(
spec["latitude"] = np.atleast_1d(latitude)
if depth is not None:
spec["depth"] = np.atleast_1d(depth)
if plot_longitude is not None: # must be in string type
spec["plot_longitude"] = np.atleast_1d(plot_longitude).astype(str)
if plot_latitude is not None: # must be in string type
spec["plot_latitude"] = np.atleast_1d(plot_latitude).astype(str)
if plot_longitude is not None:
spec["plot_longitude"] = np.atleast_1d(plot_longitude)
if plot_latitude is not None:
spec["plot_latitude"] = np.atleast_1d(plot_latitude)
if event_name is not None:
spec["event_name"] = np.atleast_1d(event_name).astype(str)

Expand All @@ -293,9 +293,13 @@ def meca(
newcols = ["longitude", "latitude", "depth"] + param_conventions[convention]
if "plot_longitude" in spec.columns and "plot_latitude" in spec.columns:
newcols += ["plot_longitude", "plot_latitude"]
spec[["plot_longitude", "plot_latitude"]] = spec[
["plot_longitude", "plot_latitude"]
].astype(str)
kwargs["A"] = True
if "event_name" in spec.columns:
newcols += ["event_name"]
spec["event_name"] = spec["event_name"].astype(str)
# reorder columns in DataFrame
spec = spec.reindex(newcols, axis=1)
elif isinstance(spec, np.ndarray) and spec.ndim == 1:
Expand Down
28 changes: 28 additions & 0 deletions pygmt/tests/test_meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,34 @@ def test_meca_dict_offset():
return fig


@pytest.mark.mpl_image_compare(filename="test_meca_dict_offset.png")
def test_meca_dict_offset_in_dict():
"""
Test offsetting beachballs for a dict input with offset parameters in the
dict.
See https://github.com/GenericMappingTools/pygmt/issues/2016.
"""
fig = Figure()
focal_mechanism = dict(
strike=330,
dip=30,
rake=90,
magnitude=3,
plot_longitude=-124.5,
plot_latitude=47.5,
)
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
fig.meca(
spec=focal_mechanism,
scale="1c",
longitude=-124,
latitude=48,
depth=12.0,
)
return fig


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

0 comments on commit 7ee5a14

Please sign in to comment.