|
10 | 10 | import pathlib
|
11 | 11 | import sys
|
12 | 12 | import warnings
|
| 13 | +from collections.abc import Sequence |
13 | 14 | from typing import Literal
|
14 | 15 |
|
15 | 16 | import numpy as np
|
@@ -1068,6 +1069,77 @@ def put_matrix(self, dataset, matrix, pad=0):
|
1068 | 1069 | if status != 0:
|
1069 | 1070 | raise GMTCLibError(f"Failed to put matrix of type {matrix.dtype}.")
|
1070 | 1071 |
|
| 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 | + |
1071 | 1143 | def write_data(self, family, geometry, mode, wesn, output, data):
|
1072 | 1144 | """
|
1073 | 1145 | Write a GMT data container to a file.
|
|
0 commit comments