Skip to content

Commit b299ad0

Browse files
committed
Refactor text to use virtual_from_data
1 parent 955a55e commit b299ad0

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

pygmt/src/text.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pygmt.helpers import (
88
build_arg_string,
99
data_kind,
10-
dummy_context,
1110
fmt_docstring,
1211
is_nonstr_iter,
1312
kwargs_to_strings,
@@ -157,15 +156,19 @@ def text_(
157156
# Ensure inputs are either textfiles, x/y/text, or position/text
158157
if position is None:
159158
kind = data_kind(textfiles, x, y, text)
159+
if kind == "vectors" and text is None:
160+
raise GMTInvalidInput("Must provide text with x/y pairs")
161+
if kind == "file" and text is not None:
162+
raise GMTInvalidInput("Can't provide text if textfiles is specified.")
160163
else:
161-
if x is not None or y is not None:
164+
if x is not None or y is not None or textfiles is not None:
162165
raise GMTInvalidInput(
163-
"Provide either position only, or x/y pairs, not both"
166+
"Provide either position only, or x/y pairs, or textfiles."
164167
)
165-
kind = "vectors"
166-
167-
if kind == "vectors" and text is None:
168-
raise GMTInvalidInput("Must provide text with x/y pairs or position")
168+
if text is None or is_nonstr_iter(text):
169+
raise GMTInvalidInput("Text can't be None or array.")
170+
kind = "file"
171+
textfiles = ""
169172

170173
# Build the -F option in gmt text.
171174
if "F" not in kwargs.keys() and (
@@ -193,19 +196,15 @@ def text_(
193196
extra_arrays.append(kwargs["t"])
194197
kwargs["t"] = ""
195198

199+
# Append text as the last column
200+
# text must be in str type, see issue #706
201+
if kind == "vectors":
202+
extra_arrays.append(np.atleast_1d(text).astype(str))
203+
196204
with Session() as lib:
197-
file_context = dummy_context(textfiles) if kind == "file" else ""
198-
if kind == "vectors":
199-
if position is not None:
200-
file_context = dummy_context("")
201-
else:
202-
file_context = lib.virtualfile_from_vectors(
203-
np.atleast_1d(x),
204-
np.atleast_1d(y),
205-
*extra_arrays,
206-
# text must be in str type, see issue #706
207-
np.atleast_1d(text).astype(str),
208-
)
205+
file_context = lib.virtualfile_from_data(
206+
check_kind="vector", data=textfiles, x=x, y=y, extra_arrays=extra_arrays
207+
)
209208
with file_context as fname:
210209
arg_str = " ".join([fname, build_arg_string(kwargs)])
211210
lib.call_module("text", arg_str)

0 commit comments

Comments
 (0)