Skip to content

Commit b0b63d8

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

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

pygmt/src/text.py

+19-18
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,21 @@ 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:
165+
raise GMTInvalidInput(
166+
"Provide either position only, or x/y pairs, or textfiles."
167+
)
168+
if text is None or is_nonstr_iter(text):
162169
raise GMTInvalidInput(
163170
"Provide either position only, or x/y pairs, not both"
164171
)
165-
kind = "vectors"
166-
167-
if kind == "vectors" and text is None:
168-
raise GMTInvalidInput("Must provide text with x/y pairs or position")
172+
kind = "file"
173+
textfiles = ""
169174

170175
# Build the -F option in gmt text.
171176
if "F" not in kwargs.keys() and (
@@ -193,19 +198,15 @@ def text_(
193198
extra_arrays.append(kwargs["t"])
194199
kwargs["t"] = ""
195200

201+
# Append text as the last column
202+
# text must be in str type, see issue #706
203+
if kind == "vectors":
204+
extra_arrays.append(np.atleast_1d(text).astype(str))
205+
196206
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-
)
207+
file_context = lib.virtualfile_from_data(
208+
check_kind="vector", data=textfiles, x=x, y=y, extra_arrays=extra_arrays
209+
)
209210
with file_context as fname:
210211
arg_str = " ".join([fname, build_arg_string(kwargs)])
211212
lib.call_module("text", arg_str)

0 commit comments

Comments
 (0)