Skip to content

Commit df786bb

Browse files
committed
Wrap GMT's data structure GMT_GRID_HEADER for grid/image/cube headers
1 parent 7fd8253 commit df786bb

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

pygmt/datatypes/grid.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,99 @@
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 holding a grid header.
26+
"""
27+
28+
_fields_: ClassVar = [
29+
# Number of columns
30+
("n_columns", ctp.c_uint32),
31+
# Number of rows
32+
("n_rows", ctp.c_uint32),
33+
# Grid registration, 0 for gridline and 1 for pixel
34+
("registration", ctp.c_uint32),
35+
# Minimum/maximum x and y coordinates
36+
("wesn", ctp.c_double * 4),
37+
# Minimum z value
38+
("z_min", ctp.c_double),
39+
# Maximum z value
40+
("z_max", ctp.c_double),
41+
# x and y increments
42+
("inc", ctp.c_double * 2),
43+
# Grid values must be multiplied by this
44+
("z_scale_factor", ctp.c_double),
45+
# After scaling, add this
46+
("z_add_offset", ctp.c_double),
47+
# Units in x-directions, in the form "long_name [units]"
48+
("x_units", ctp.c_char * GMT_GRID_UNIT_LEN80),
49+
# Units in y-direction, in the form "long_name [units]"
50+
("y_units", ctp.c_char * GMT_GRID_UNIT_LEN80),
51+
# Grid value units, in the form "long_name [units]"
52+
("z_units", ctp.c_char * GMT_GRID_UNIT_LEN80),
53+
# Name of data set
54+
("title", ctp.c_char * GMT_GRID_TITLE_LEN80),
55+
# Name of generating command
56+
("command", ctp.c_char * GMT_GRID_COMMAND_LEN320),
57+
# Comments for this data set
58+
("remark", ctp.c_char * GMT_GRID_REMARK_LEN160),
59+
# Below are itmes used internally by GMT
60+
# Number of data points (n_columns * n_rows) [paddings are excluded]
61+
("nm", ctp.c_size_t),
62+
# Actual number of items (not bytes) required to hold this grid (mx * my),
63+
# per band (for images)
64+
("size", ctp.c_size_t),
65+
# Bits per data value (e.g., 32 for ints/floats; 8 for bytes).
66+
# Only used for ERSI ArcInfo ASCII Exchange grids.
67+
("bits", ctp.c_uint),
68+
# For complex grid.
69+
# 0 for normal
70+
# GMT_GRID_IS_COMPLEX_REAL = real part of complex grid
71+
# GMT_GRID_IS_COMPLEX_IMAG = imag part of complex grid
72+
("complex_mode", ctp.c_uint),
73+
# Grid format
74+
("type", ctp.c_uint),
75+
# Number of bands [1]. Used with GMT_IMAGE containers
76+
("n_bands", ctp.c_uint),
77+
# Actual x-dimension in memory. mx = n_columns + pad[0] + pad[1]
78+
("mx", ctp.c_uint),
79+
# Actual y-dimension in memory. my = n_rows + pad[2] + pad[3]
80+
("my", ctp.c_uint),
81+
# Paddings on west, east, south, north sides [2,2,2,2]
82+
("pad", ctp.c_uint * 4),
83+
# Three or four char codes T|B R|C S|R|S (grd) or B|L|P + A|a (img)
84+
# describing array layout in mem and interleaving
85+
("mem_layout", ctp.c_char * 4),
86+
# Missing value as stored in grid file
87+
("nan_value", gmt_grdfloat),
88+
# 0.0 for gridline grids and 0.5 for pixel grids
89+
("xy_off", ctp.c_double),
90+
# Referencing system string in PROJ.4 format
91+
("ProjRefPROJ4", ctp.c_char_p),
92+
# Referencing system string in WKT format
93+
("ProjRefWKT", ctp.c_char_p),
94+
# Referencing system EPSG code
95+
("ProjRefEPSG", ctp.c_int),
96+
# Lower-level information for GMT use only
97+
("hidden", ctp.c_void_p),
98+
]
699

7100

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

0 commit comments

Comments
 (0)