Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure.meca: Refactor tests for plotting a single focal mechanism #2533

Merged
merged 11 commits into from
May 30, 2023
Merged
4 changes: 0 additions & 4 deletions pygmt/tests/baseline/test_meca_gcmt_convention.png.dvc

This file was deleted.

4 changes: 0 additions & 4 deletions pygmt/tests/baseline/test_meca_spec_1d_array.png.dvc

This file was deleted.

4 changes: 0 additions & 4 deletions pygmt/tests/baseline/test_meca_spec_dictionary.png.dvc

This file was deleted.

4 changes: 0 additions & 4 deletions pygmt/tests/baseline/test_meca_spec_file.png.dvc

This file was deleted.

4 changes: 4 additions & 0 deletions pygmt/tests/baseline/test_meca_spec_single_focalmecha.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: fc7c271049fc8583914b508c4bee08fe
size: 9830
path: test_meca_spec_single_focalmecha.png
170 changes: 63 additions & 107 deletions pygmt/tests/test_meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,73 @@
from pygmt.helpers import GMTTempFile


@pytest.mark.mpl_image_compare
def test_meca_spec_dictionary():
@pytest.mark.mpl_image_compare(filename="test_meca_spec_single_focalmecha.png")
@pytest.mark.parametrize("inputtype", ["dict_mecha", "dict_full", "array1d", "pandas"])
def test_meca_spec_single_focalmecha(inputtype):
"""
Test passing a single focal mechanism to the spec parameter.
"""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different inputtype are different ways to passing a single focal mechanism to the spec parameter.

if inputtype == "dict_mecha":
args = {
"spec": {"strike": 0, "dip": 90, "rake": 0, "magnitude": 5},
"longitude": 0,
"latitude": 5,
"depth": 0,
}
elif inputtype == "dict_full":
args = {
"spec": {
"longitude": 0,
"latitude": 5,
"depth": 0,
"strike": 0,
"dip": 90,
"rake": 0,
"magnitude": 5,
}
}
elif inputtype == "array1d":
args = {
"spec": np.array([0, 5, 0, 0, 90, 0, 5]),
"convention": "a",
}
elif inputtype == "pandas":
args = {
"spec": pd.DataFrame(
{
"longitude": 0,
"latitude": 5,
"depth": 0,
"strike": 0,
"dip": 90,
"rake": 0,
"magnitude": 5,
},
index=[0],
)
}
fig = Figure()
fig.basemap(region=[-1, 1, 4, 6], projection="M8c", frame=2)
fig.meca(scale="2.5c", **args)
return fig


@pytest.mark.mpl_image_compare(filename="test_meca_spec_single_focalmecha.png")
def test_meca_spec_single_focalmecha_file():
"""
Test supplying a dictionary containing a single focal mechanism to the spec
Test supplying a file containing focal mechanisms and locations to the spec
parameter.
"""
fig = Figure()
# Right lateral strike slip focal mechanism
fig.meca(
spec={"strike": 0, "dip": 90, "rake": 0, "magnitude": 5},
longitude=0,
latitude=5,
depth=0,
scale="2.5c",
region=[-1, 1, 4, 6],
projection="M14c",
frame=2,
)
fig.basemap(region=[-1, 1, 4, 6], projection="M8c", frame=2)
with GMTTempFile() as temp:
with open(temp.name, mode="w", encoding="utf8") as temp_file:
temp_file.write("0 5 0 0 90 0 5")
fig.meca(
spec=temp.name,
convention="aki",
scale="2.5c",
)
return fig


Expand Down Expand Up @@ -83,44 +132,6 @@ def test_meca_spec_dataframe():
return fig


@pytest.mark.mpl_image_compare
def test_meca_spec_1d_array():
"""
Test supplying a 1-D numpy array containing focal mechanisms and locations
to the spec parameter.
"""
fig = Figure()
# supply focal mechanisms to meca as a 1-D numpy array, here we are using
# the Harvard CMT zero trace convention but the focal mechanism
# parameters may be specified any of the available conventions. Since we
# are not using a dict or dataframe the convention and component should
# be specified.
focal_mechanism = [
-127.40, # longitude
40.87, # latitude
12, # depth
-3.19, # mrr
0.16, # mtt
3.03, # mff
-1.02, # mrt
-3.93, # mrf
-0.02, # mtf
23, # exponent
0, # plot_lon, 0 to plot at event location
0, # plot_lat, 0 to plot at event location
]
focal_mech_array = np.asarray(focal_mechanism)
fig.meca(
spec=focal_mech_array,
convention="mt",
component="full",
region=[-128, -127, 40, 41],
scale="2c",
projection="M14c",
)
return fig


@pytest.mark.mpl_image_compare
def test_meca_spec_2d_array():
"""
Expand Down Expand Up @@ -151,30 +162,6 @@ def test_meca_spec_2d_array():
return fig


@pytest.mark.mpl_image_compare
def test_meca_spec_file():
"""
Test supplying a file containing focal mechanisms and locations to the spec
parameter.
"""
fig = Figure()
focal_mechanism = [-127.43, 40.81, 12, -3.19, 1.16, 3.93, -1.02, -3.93, -1.02, 23]
# writes temp file to pass to gmt
with GMTTempFile() as temp:
with open(temp.name, mode="w", encoding="utf8") as temp_file:
temp_file.write(" ".join([str(x) for x in focal_mechanism]))
# supply focal mechanisms to meca as a file
fig.meca(
spec=temp.name,
convention="mt",
component="full",
region=[-128, -127, 40, 41],
scale="2c",
projection="M14c",
)
return fig


@pytest.mark.mpl_image_compare
def test_meca_loc_array():
"""
Expand Down Expand Up @@ -207,37 +194,6 @@ def test_meca_loc_array():
return fig


@pytest.mark.mpl_image_compare
def test_meca_gcmt_convention():
"""
Test plotting beachballs using the global CMT convention.
"""
fig = Figure()
# specify focal mechanisms
focal_mechanisms = {
"strike1": 180,
"dip1": 18,
"rake1": -88,
"strike2": 0,
"dip2": 72,
"rake2": -90,
"mantissa": 5.5,
"exponent": 0,
}
fig.meca(
spec=focal_mechanisms,
scale="1c",
longitude=239.384,
latitude=34.556,
depth=12,
convention="gcmt",
region=[239, 240, 34, 35.2],
projection="m2.5c",
frame=True,
)
return fig


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