Skip to content

Commit 416cf8f

Browse files
committed
Initial support for passing strings to GMT via GMT_Put_Vectors
1 parent 30728fd commit 416cf8f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

pygmt/clib/session.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
np.uint64: "GMT_ULONG",
6060
np.uint32: "GMT_UINT",
6161
np.datetime64: "GMT_DATETIME",
62+
np.str_: "GMT_TEXT",
6263
}
6364

6465

@@ -718,9 +719,7 @@ def _check_dtype_and_dim(self, array, ndim):
718719
"""
719720
# check the array has the given dimension
720721
if array.ndim != ndim:
721-
raise GMTInvalidInput(
722-
"Expected a numpy 1d array, got {}d.".format(array.ndim)
723-
)
722+
raise GMTInvalidInput(f"Expected a numpy 1d array, got {array.ndim}d.")
724723

725724
# check the array has a valid/known data type
726725
if array.dtype.type not in DTYPES:
@@ -744,7 +743,7 @@ def put_vector(self, dataset, column, vector):
744743
first. Use ``family='GMT_IS_DATASET|GMT_VIA_VECTOR'``.
745744
746745
Not at all numpy dtypes are supported, only: float64, float32, int64,
747-
int32, uint64, and uint32.
746+
int32, uint64, uint32, datetime64 and str_.
748747
749748
.. warning::
750749
The numpy array must be C contiguous in memory. If it comes from a
@@ -776,23 +775,24 @@ def put_vector(self, dataset, column, vector):
776775
)
777776

778777
gmt_type = self._check_dtype_and_dim(vector, ndim=1)
779-
if gmt_type == self["GMT_DATETIME"]:
778+
if gmt_type in (self["GMT_TEXT"], self["GMT_DATETIME"]):
780779
vector_pointer = (ctp.c_char_p * len(vector))()
781-
vector_pointer[:] = np.char.encode(
782-
np.datetime_as_string(array_to_datetime(vector))
783-
)
780+
if gmt_type == self["GMT_DATETIME"]:
781+
vector_pointer[:] = np.char.encode(
782+
np.datetime_as_string(array_to_datetime(vector))
783+
)
784+
else:
785+
vector_pointer[:] = np.char.encode(vector)
784786
else:
785787
vector_pointer = vector.ctypes.data_as(ctp.c_void_p)
786788
status = c_put_vector(
787789
self.session_pointer, dataset, column, gmt_type, vector_pointer
788790
)
789791
if status != 0:
790792
raise GMTCLibError(
791-
" ".join(
792-
[
793-
"Failed to put vector of type {}".format(vector.dtype),
794-
"in column {} of dataset.".format(column),
795-
]
793+
(
794+
f"Failed to put vector of type {vector.dtype} "
795+
f"in column {column} of dataset."
796796
)
797797
)
798798

0 commit comments

Comments
 (0)