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

Refactor meca to use virtualfile_from_data #1613

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions pygmt/src/meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
import pandas as pd
from pygmt.clib import Session
from pygmt.exceptions import GMTError, GMTInvalidInput
from pygmt.helpers import (
build_arg_string,
data_kind,
dummy_context,
fmt_docstring,
kwargs_to_strings,
use_alias,
)
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias


def data_format_code(convention, component="full"):
Expand Down Expand Up @@ -136,7 +129,7 @@ def meca(

Parameters
----------
spec: dict, 1D array, 2D array, pd.DataFrame, or str
spec : str or dict or numpy.ndarray or pandas.DataFrame
Either a filename containing focal mechanism parameters as columns, a
1- or 2-D array with the same, or a dictionary. If a filename or array,
`convention` is required so we know how to interpret the
Expand Down Expand Up @@ -442,20 +435,19 @@ def update_pointers(data_pointers):
else:
raise GMTError("Parameter 'spec' contains values of an unsupported type.")

# Ensure non-file types are a 2d array
if isinstance(spec, (list, np.ndarray)):
spec = np.atleast_2d(spec)
weiji14 marked this conversation as resolved.
Show resolved Hide resolved

# determine data_foramt from convection and component
data_format = data_format_code(convention=convention, component=component)

# Assemble -S flag
kwargs["S"] = data_format + scale

kind = data_kind(spec)
with Session() as lib:
if kind == "matrix":
file_context = lib.virtualfile_from_matrix(np.atleast_2d(spec))
elif kind == "file":
file_context = dummy_context(spec)
else:
raise GMTInvalidInput(f"Unrecognized data type: {type(spec)}")
# Choose how data will be passed into the module
file_context = lib.virtualfile_from_data(check_kind="vector", data=spec)
with file_context as fname:
arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("meca", arg_str)
4 changes: 2 additions & 2 deletions pygmt/tests/test_meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_meca_spec_dictionary():
fig = Figure()
# Right lateral strike slip focal mechanism
fig.meca(
dict(strike=0, dip=90, rake=0, magnitude=5),
spec=dict(strike=0, dip=90, rake=0, magnitude=5),
longitude=0,
latitude=5,
depth=0,
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_meca_spec_1d_array():
]
focal_mech_array = np.asarray(focal_mechanism)
fig.meca(
focal_mech_array,
spec=focal_mech_array,
convention="mt",
component="full",
region=[-128, -127, 40, 41],
Expand Down