Skip to content

Commit

Permalink
Refactor meca to use virtualfile_from_data
Browse files Browse the repository at this point in the history
Need to ensure that the spec numpy.array is a 2D
matrix for now, so that test_meca_spec_dictionary
and test_meca_spec_1d_array works.
  • Loading branch information
weiji14 committed Nov 7, 2021
1 parent 4b80f3c commit 3f981e1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
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)

# 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

0 comments on commit 3f981e1

Please sign in to comment.