Skip to content

Commit bfb641e

Browse files
seismanweiji14
andauthored
Wrap GMT's data structure GMT_GRID_HEADER for grid/image/cube headers (#3127)
Co-authored-by: Wei Ji <[email protected]>
1 parent 598154a commit bfb641e

File tree

1 file changed

+97
-1
lines changed

1 file changed

+97
-1
lines changed

pygmt/datatypes/grid.py

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,104 @@
11
"""
2-
Wrapper for the GMT_GRID data type.
2+
Wrapper for the GMT_GRID data type and the GMT_GRID_HEADER data structure.
33
"""
44

55
import ctypes as ctp
6+
from typing import ClassVar
7+
8+
# Constants for lengths of grid header variables.
9+
#
10+
# Note: Ideally we should be able to get these constants from the GMT shared library
11+
# using the ``lib["GMT_GRID_UNIT_LEN80"]`` syntax, but it causes cyclic import error.
12+
# So we have to hardcode the values here.
13+
GMT_GRID_UNIT_LEN80 = 80
14+
GMT_GRID_TITLE_LEN80 = 80
15+
GMT_GRID_COMMAND_LEN320 = 320
16+
GMT_GRID_REMARK_LEN160 = 160
17+
18+
# GMT uses single-precision for grids by default, but can be built to use
19+
# double-precision. Currently, only single-precision is supported.
20+
gmt_grdfloat = ctp.c_float
21+
22+
23+
class _GMT_GRID_HEADER(ctp.Structure): # noqa: N801
24+
"""
25+
GMT grid header structure for metadata about the grid.
26+
27+
The class is used in the `GMT_GRID`/`GMT_IMAGE`/`GMT_CUBE` data structure. See the
28+
GMT source code gmt_resources.h for the original C structure definitions.
29+
"""
30+
31+
_fields_: ClassVar = [
32+
# Number of columns
33+
("n_columns", ctp.c_uint32),
34+
# Number of rows
35+
("n_rows", ctp.c_uint32),
36+
# Grid registration, 0 for gridline and 1 for pixel
37+
("registration", ctp.c_uint32),
38+
# Minimum/maximum x and y coordinates
39+
("wesn", ctp.c_double * 4),
40+
# Minimum z value
41+
("z_min", ctp.c_double),
42+
# Maximum z value
43+
("z_max", ctp.c_double),
44+
# x and y increments
45+
("inc", ctp.c_double * 2),
46+
# Grid values must be multiplied by this factor
47+
("z_scale_factor", ctp.c_double),
48+
# After scaling, add this offset
49+
("z_add_offset", ctp.c_double),
50+
# Units in x-directions, in the form "long_name [units]"
51+
("x_units", ctp.c_char * GMT_GRID_UNIT_LEN80),
52+
# Units in y-direction, in the form "long_name [units]"
53+
("y_units", ctp.c_char * GMT_GRID_UNIT_LEN80),
54+
# Grid value units, in the form "long_name [units]"
55+
("z_units", ctp.c_char * GMT_GRID_UNIT_LEN80),
56+
# Name of data set
57+
("title", ctp.c_char * GMT_GRID_TITLE_LEN80),
58+
# Name of generating command
59+
("command", ctp.c_char * GMT_GRID_COMMAND_LEN320),
60+
# Comments for this data set
61+
("remark", ctp.c_char * GMT_GRID_REMARK_LEN160),
62+
# Below are items used internally by GMT
63+
# Number of data points (n_columns * n_rows) [paddings are excluded]
64+
("nm", ctp.c_size_t),
65+
# Actual number of items (not bytes) required to hold this grid (mx * my),
66+
# per band (for images)
67+
("size", ctp.c_size_t),
68+
# Bits per data value (e.g., 32 for ints/floats; 8 for bytes).
69+
# Only used for ERSI ArcInfo ASCII Exchange grids.
70+
("bits", ctp.c_uint),
71+
# For complex grid.
72+
# 0 for normal
73+
# GMT_GRID_IS_COMPLEX_REAL = real part of complex grid
74+
# GMT_GRID_IS_COMPLEX_IMAG = imag part of complex grid
75+
("complex_mode", ctp.c_uint),
76+
# Grid format
77+
("type", ctp.c_uint),
78+
# Number of bands [1]. Used with GMT_IMAGE containers
79+
("n_bands", ctp.c_uint),
80+
# Actual x-dimension in memory. mx = n_columns + pad[0] + pad[1]
81+
("mx", ctp.c_uint),
82+
# Actual y-dimension in memory. my = n_rows + pad[2] + pad[3]
83+
("my", ctp.c_uint),
84+
# Paddings on west, east, south, north sides [2,2,2,2]
85+
("pad", ctp.c_uint * 4),
86+
# Three or four char codes T|B R|C S|R|S (grd) or B|L|P + A|a (img)
87+
# describing array layout in mem and interleaving
88+
("mem_layout", ctp.c_char * 4),
89+
# Missing value as stored in grid file
90+
("nan_value", gmt_grdfloat),
91+
# 0.0 for gridline grids and 0.5 for pixel grids
92+
("xy_off", ctp.c_double),
93+
# Referencing system string in PROJ.4 format
94+
("ProjRefPROJ4", ctp.c_char_p),
95+
# Referencing system string in WKT format
96+
("ProjRefWKT", ctp.c_char_p),
97+
# Referencing system EPSG code
98+
("ProjRefEPSG", ctp.c_int),
99+
# Lower-level information for GMT use only
100+
("hidden", ctp.c_void_p),
101+
]
6102

7103

8104
class _GMT_GRID(ctp.Structure): # noqa: N801

0 commit comments

Comments
 (0)