|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import ctypes as ctp
|
| 6 | +from typing import ClassVar |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import xarray as xr |
| 10 | +from pygmt.datatypes.header import _GMT_GRID_HEADER, gmt_grdfloat |
6 | 11 |
|
7 | 12 |
|
8 | 13 | class _GMT_GRID(ctp.Structure): # noqa: N801
|
9 |
| - pass |
| 14 | + """ |
| 15 | + GMT grid structure for holding a grid and its header. |
| 16 | +
|
| 17 | + This class is only meant for internal use and is not exposed to users. See the GMT |
| 18 | + source code gmt_resources.h for the original C structure definitions. |
| 19 | +
|
| 20 | + Examples |
| 21 | + -------- |
| 22 | + >>> from pygmt.clib import Session |
| 23 | + >>> with Session() as lib: |
| 24 | + ... with lib.virtualfile_out(kind="grid") as voutgrd: |
| 25 | + ... lib.call_module("read", f"@static_earth_relief.nc {voutgrd} -Tg") |
| 26 | + ... # Read the grid from the virtual file |
| 27 | + ... grid = lib.read_virtualfile(voutgrd, kind="grid").contents |
| 28 | + ... # The grid header |
| 29 | + ... header = grid.header.contents |
| 30 | + ... # Access the header properties |
| 31 | + ... print(header.n_rows, header.n_columns, header.registration) |
| 32 | + ... print(header.wesn[:], header.z_min, header.z_max, header.inc[:]) |
| 33 | + ... print(header.z_scale_factor, header.z_add_offset) |
| 34 | + ... print(header.x_units, header.y_units, header.z_units) |
| 35 | + ... print(header.title) |
| 36 | + ... print(header.command) |
| 37 | + ... print(header.remark) |
| 38 | + ... print(header.nm, header.size, header.complex_mode) |
| 39 | + ... print(header.type, header.n_bands, header.mx, header.my) |
| 40 | + ... print(header.pad[:]) |
| 41 | + ... print(header.mem_layout, header.nan_value, header.xy_off) |
| 42 | + ... # The x and y coordinates |
| 43 | + ... print(grid.x[: header.n_columns]) |
| 44 | + ... print(grid.y[: header.n_rows]) |
| 45 | + ... # The data array (with paddings) |
| 46 | + ... data = np.reshape( |
| 47 | + ... grid.data[: header.mx * header.my], (header.my, header.mx) |
| 48 | + ... ) |
| 49 | + ... # The data array (without paddings) |
| 50 | + ... pad = header.pad[:] |
| 51 | + ... data = data[pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1]] |
| 52 | + ... print(data) |
| 53 | + 14 8 1 |
| 54 | + [-55.0, -47.0, -24.0, -10.0] 190.0 981.0 [1.0, 1.0] |
| 55 | + 1.0 0.0 |
| 56 | + b'longitude [degrees_east]' b'latitude [degrees_north]' b'elevation (m)' |
| 57 | + b'Produced by grdcut' |
| 58 | + b'grdcut @earth_relief_01d_p -R-55/-47/-24/-10 -Gstatic_earth_relief.nc' |
| 59 | + b'Reduced by Gaussian Cartesian filtering (111.2 km fullwidth) from ...' |
| 60 | + 112 216 0 |
| 61 | + 18 1 12 18 |
| 62 | + [2, 2, 2, 2] |
| 63 | + b'' nan 0.5 |
| 64 | + [-54.5, -53.5, -52.5, -51.5, -50.5, -49.5, -48.5, -47.5] |
| 65 | + [-10.5, -11.5, -12.5, -13.5, -14.5, -15.5, ..., -22.5, -23.5] |
| 66 | + [[347.5 331.5 309. 282. 190. 208. 299.5 348. ] |
| 67 | + [349. 313. 325.5 247. 191. 225. 260. 452.5] |
| 68 | + [345.5 320. 335. 292. 207.5 247. 325. 346.5] |
| 69 | + [450.5 395.5 366. 248. 250. 354.5 550. 797.5] |
| 70 | + [494.5 488.5 357. 254.5 286. 484.5 653.5 930. ] |
| 71 | + [601. 526.5 535. 299. 398.5 645. 797.5 964. ] |
| 72 | + [308. 595.5 555.5 556. 580. 770. 927. 920. ] |
| 73 | + [521.5 682.5 796. 886. 571.5 638.5 739.5 881.5] |
| 74 | + [310. 521.5 757. 570.5 538.5 524. 686.5 794. ] |
| 75 | + [561.5 539. 446.5 481.5 439.5 553. 726.5 981. ] |
| 76 | + [557. 435. 385.5 345.5 413.5 496. 519.5 833.5] |
| 77 | + [373. 367.5 349. 352.5 419.5 428. 570. 667.5] |
| 78 | + [383. 284.5 344.5 394. 491. 556.5 578.5 618.5] |
| 79 | + [347.5 344.5 386. 640.5 617. 579. 646.5 671. ]] |
| 80 | + """ |
| 81 | + |
| 82 | + _fields_: ClassVar = [ |
| 83 | + # Pointer to full GMT header for grid |
| 84 | + ("header", ctp.POINTER(_GMT_GRID_HEADER)), |
| 85 | + # Pointer to grid data |
| 86 | + ("data", ctp.POINTER(gmt_grdfloat)), |
| 87 | + # Pointer to x coordinate vector |
| 88 | + ("x", ctp.POINTER(ctp.c_double)), |
| 89 | + # Pointer to y coordinate vector |
| 90 | + ("y", ctp.POINTER(ctp.c_double)), |
| 91 | + # Low-level information for GMT use only |
| 92 | + ("hidden", ctp.c_void_p), |
| 93 | + ] |
| 94 | + |
| 95 | + def to_dataarray(self) -> xr.DataArray: |
| 96 | + """ |
| 97 | + Convert a _GMT_GRID object to a :class:`xarray.DataArray` object. |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + dataarray |
| 102 | + A :class:`xr.DataArray` object. |
| 103 | +
|
| 104 | + Examples |
| 105 | + -------- |
| 106 | + >>> from pygmt.clib import Session |
| 107 | + >>> with Session() as lib: |
| 108 | + ... with lib.virtualfile_out(kind="grid") as voutgrd: |
| 109 | + ... lib.call_module("read", f"@static_earth_relief.nc {voutgrd} -Tg") |
| 110 | + ... # Read the grid from the virtual file |
| 111 | + ... grid = lib.read_virtualfile(voutgrd, kind="grid") |
| 112 | + ... # Convert to xarray.DataArray and use it later |
| 113 | + ... da = grid.contents.to_dataarray() |
| 114 | + >>> da # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS |
| 115 | + <xarray.DataArray 'z' (lat: 14, lon: 8)>... |
| 116 | + array([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ], |
| 117 | + [383. , 284.5, 344.5, 394. , 491. , 556.5, 578.5, 618.5], |
| 118 | + [373. , 367.5, 349. , 352.5, 419.5, 428. , 570. , 667.5], |
| 119 | + [557. , 435. , 385.5, 345.5, 413.5, 496. , 519.5, 833.5], |
| 120 | + [561.5, 539. , 446.5, 481.5, 439.5, 553. , 726.5, 981. ], |
| 121 | + [310. , 521.5, 757. , 570.5, 538.5, 524. , 686.5, 794. ], |
| 122 | + [521.5, 682.5, 796. , 886. , 571.5, 638.5, 739.5, 881.5], |
| 123 | + [308. , 595.5, 555.5, 556. , 580. , 770. , 927. , 920. ], |
| 124 | + [601. , 526.5, 535. , 299. , 398.5, 645. , 797.5, 964. ], |
| 125 | + [494.5, 488.5, 357. , 254.5, 286. , 484.5, 653.5, 930. ], |
| 126 | + [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5], |
| 127 | + [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5], |
| 128 | + [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5], |
| 129 | + [347.5, 331.5, 309. , 282. , 190. , 208. , 299.5, 348. ]]) |
| 130 | + Coordinates: |
| 131 | + * lat (lat) float64... -23.5 -22.5 -21.5 -20.5 ... -12.5 -11.5 -10.5 |
| 132 | + * lon (lon) float64... -54.5 -53.5 -52.5 -51.5 -50.5 -49.5 -48.5 -47.5 |
| 133 | + Attributes: |
| 134 | + Conventions: CF-1.7 |
| 135 | + title: Produced by grdcut |
| 136 | + history: grdcut @earth_relief_01d_p -R-55/-47/-24/-10 -Gstatic_ea... |
| 137 | + description: Reduced by Gaussian Cartesian filtering (111.2 km fullwi... |
| 138 | + long_name: elevation (m) |
| 139 | + actual_range: [190. 981.] |
| 140 | + >>> da.coords["lon"] # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS |
| 141 | + <xarray.DataArray 'lon' (lon: 8)>... |
| 142 | + array([-54.5, -53.5, -52.5, -51.5, -50.5, -49.5, -48.5, -47.5]) |
| 143 | + Coordinates: |
| 144 | + * lon (lon) float64... -54.5 -53.5 -52.5 -51.5 -50.5 -49.5 -48.5 -47.5 |
| 145 | + Attributes: |
| 146 | + long_name: longitude |
| 147 | + units: degrees_east |
| 148 | + standard_name: longitude |
| 149 | + axis: X |
| 150 | + actual_range: [-55. -47.] |
| 151 | + >>> da.coords["lat"] # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS |
| 152 | + <xarray.DataArray 'lat' (lat: 14)>... |
| 153 | + array([-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5, -15.5, -14.5, |
| 154 | + -13.5, -12.5, -11.5, -10.5]) |
| 155 | + Coordinates: |
| 156 | + * lat (lat) float64... -23.5 -22.5 -21.5 -20.5 ... -12.5 -11.5 -10.5 |
| 157 | + Attributes: |
| 158 | + long_name: latitude |
| 159 | + units: degrees_north |
| 160 | + standard_name: latitude |
| 161 | + axis: Y |
| 162 | + actual_range: [-24. -10.] |
| 163 | + >>> da.gmt.registration, da.gmt.gtype |
| 164 | + (1, 1) |
| 165 | + """ |
| 166 | + # The grid header |
| 167 | + header = self.header.contents |
| 168 | + |
| 169 | + # Get dimensions and their attributes from the header. |
| 170 | + dims, dim_attrs = header.dims, header.dim_attrs |
| 171 | + # The coordinates, given as a tuple of the form (dims, data, attrs) |
| 172 | + coords = [ |
| 173 | + (dims[0], self.y[: header.n_rows], dim_attrs[0]), |
| 174 | + (dims[1], self.x[: header.n_columns], dim_attrs[1]), |
| 175 | + ] |
| 176 | + |
| 177 | + # The data array without paddings |
| 178 | + pad = header.pad[:] |
| 179 | + data = np.reshape(self.data[: header.mx * header.my], (header.my, header.mx))[ |
| 180 | + pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1] |
| 181 | + ] |
| 182 | + |
| 183 | + # Create the xarray.DataArray object |
| 184 | + grid = xr.DataArray( |
| 185 | + data, coords=coords, name=header.name, attrs=header.data_attrs |
| 186 | + ) |
| 187 | + |
| 188 | + # Flip the coordinates and data if necessary so that coordinates are ascending. |
| 189 | + # `grid.sortby(list(grid.dims))` sometimes causes crashes. |
| 190 | + # The solution comes from https://github.com/pydata/xarray/discussions/6695. |
| 191 | + for dim in grid.dims: |
| 192 | + if grid[dim][0] > grid[dim][1]: |
| 193 | + grid = grid.isel({dim: slice(None, None, -1)}) |
| 194 | + |
| 195 | + # Set GMT accessors. |
| 196 | + # Must put at the end, otherwise info gets lost after certain grid operations. |
| 197 | + grid.gmt.registration = header.registration |
| 198 | + grid.gmt.gtype = header.gtype |
| 199 | + return grid |
0 commit comments