Skip to content

Commit 1f6117b

Browse files
committed
Wrap GMT_Read_Data
1 parent 9c13eb0 commit 1f6117b

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

pygmt/clib/conversion.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ def as_c_contiguous(array):
247247
return array
248248

249249

250-
def sequence_to_ctypes_array(sequence: Sequence, ctype, size: int) -> ctp.Array | None:
250+
def sequence_to_ctypes_array(
251+
sequence: Sequence | None, ctype, size: int
252+
) -> ctp.Array | None:
251253
"""
252254
Convert a sequence of numbers into a ctypes array variable.
253255

pygmt/clib/session.py

+72
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pathlib
1111
import sys
1212
import warnings
13+
from collections.abc import Sequence
1314
from typing import Literal
1415

1516
import numpy as np
@@ -1068,6 +1069,77 @@ def put_matrix(self, dataset, matrix, pad=0):
10681069
if status != 0:
10691070
raise GMTCLibError(f"Failed to put matrix of type {matrix.dtype}.")
10701071

1072+
def read_data(
1073+
self,
1074+
family: str,
1075+
geometry: str,
1076+
mode: str,
1077+
wesn: Sequence[float] | None,
1078+
infile: str,
1079+
data=None,
1080+
):
1081+
"""
1082+
Read a data file into a GMT data container.
1083+
1084+
Wraps ``GMT_Read_Data`` but only allows reading from a file, so the ``method``
1085+
``method`` argument is omitted.
1086+
1087+
Parameters
1088+
----------
1089+
family
1090+
A valid GMT data family name (e.g., ``"GMT_IS_DATASET"``). See the
1091+
``FAMILIES`` attribute for valid names.
1092+
geometry
1093+
A valid GMT data geometry name (e.g., ``"GMT_IS_POINT"``). See the
1094+
``GEOMETRIES`` attribute for valid names.
1095+
mode
1096+
How the data is to be read from the file. This option varies depending on
1097+
the given family. See the GMT API documentation for details.
1098+
wesn
1099+
Subregion of the data, in the form of [xmin, xmax, ymin, ymax, zmin, zmax].
1100+
If ``None``, the whole data is read.
1101+
input
1102+
The input file name.
1103+
data
1104+
``None`` or the pointer returned by this function after a first call. It's
1105+
useful when reading grids in two steps (get a grid structure with a header,
1106+
then read the data).
1107+
1108+
Returns
1109+
-------
1110+
Pointer to the data container, or ``None`` if there were errors.
1111+
"""
1112+
c_read_data = self.get_libgmt_func(
1113+
"GMT_Read_Data",
1114+
argtypes=[
1115+
ctp.c_void_p,
1116+
ctp.c_uint,
1117+
ctp.c_uint,
1118+
ctp.c_uint,
1119+
ctp.c_uint,
1120+
ctp.POINTER(ctp.c_double),
1121+
ctp.c_char_p,
1122+
ctp.c_void_p,
1123+
],
1124+
restype=ctp.c_void_p,
1125+
)
1126+
1127+
family_int = self._parse_constant(family, valid=FAMILIES, valid_modifiers=VIAS)
1128+
geometry_int = self._parse_constant(geometry, valid=GEOMETRIES)
1129+
method = self["GMT_IS_FILE"] # Reading from a file.
1130+
1131+
data_ptr = c_read_data(
1132+
self.session_pointer,
1133+
family_int,
1134+
method,
1135+
geometry_int,
1136+
self[mode],
1137+
sequence_to_ctypes_array(wesn, ctp.c_double, 6),
1138+
infile.encode(),
1139+
data,
1140+
)
1141+
return data_ptr
1142+
10711143
def write_data(self, family, geometry, mode, wesn, output, data):
10721144
"""
10731145
Write a GMT data container to a file.

0 commit comments

Comments
 (0)