GeezerVison is showing github.com — Convert ctypes array to numpy array using np.ctypeslib.as_array · GenericMappingTools/pygmt@9ad29c9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pygmt/datatypes/grid.py +37-28Lines changed: 37 additions & 28 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,16 +40,13 @@ class _GMT_GRID(ctp.Structure): # noqa: N801 | 40 | 40 | ... print(header.pad[:]) | 41 | 41 | ... print(header.mem_layout, header.nan_value, header.xy_off) | 42 | 42 | ... # The x and y coordinates | 43 | | - ... print(grid.x[: header.n_columns]) | 44 | | - ... print(grid.y[: header.n_rows]) | | 43 | + ... x = np.ctypeslib.as_array(grid.x, (header.n_columns,)).copy() | | 44 | + ... y = np.ctypeslib.as_array(grid.y, (header.n_rows,)).copy() | 45 | 45 | ... # The data array (with paddings) | 46 | | - ... data = np.reshape( | 47 | | - ... grid.data[: header.mx * header.my], (header.my, header.mx) | 48 | | - ... ) | | 46 | + ... data = np.ctypeslib.as_array(grid.data, (header.my, header.mx)).copy() | 49 | 47 | ... # The data array (without paddings) | 50 | 48 | ... pad = header.pad[:] | 51 | 49 | ... data = data[pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1]] | 52 | | - ... print(data) | 53 | 50 | 14 8 1 | 54 | 51 | [-55.0, -47.0, -24.0, -10.0] 190.0 981.0 [1.0, 1.0] | 55 | 52 | 1.0 0.0 | @@ -61,22 +58,26 @@ class _GMT_GRID(ctp.Structure): # noqa: N801 | 61 | 58 | 18 1 12 18 | 62 | 59 | [2, 2, 2, 2] | 63 | 60 | 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. ]] | | 61 | + >>> x | | 62 | + array([-54.5, -53.5, -52.5, -51.5, -50.5, -49.5, -48.5, -47.5]) | | 63 | + >>> y | | 64 | + array([-10.5, -11.5, -12.5, -13.5, ..., -20.5, -21.5, -22.5, -23.5]) | | 65 | + >>> data | | 66 | + array([[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 | + dtype=float32) | 80 | 81 | """ | 81 | 82 |
| 82 | 83 | _fields_: ClassVar = [ | @@ -126,7 +127,8 @@ def to_dataarray(self) -> xr.DataArray: | 126 | 127 | [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5], | 127 | 128 | [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5], | 128 | 129 | [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5], | 129 | | - [347.5, 331.5, 309. , 282. , 190. , 208. , 299.5, 348. ]]) | | 130 | + [347.5, 331.5, 309. , 282. , 190. , 208. , 299.5, 348. ]], | | 131 | + dtype=float32) | 130 | 132 | Coordinates: | 131 | 133 | * lat (lat) float64... -23.5 -22.5 -21.5 -20.5 ... -12.5 -11.5 -10.5 | 132 | 134 | * lon (lon) float64... -54.5 -53.5 -52.5 -51.5 -50.5 -49.5 -48.5 -47.5 | @@ -170,15 +172,22 @@ def to_dataarray(self) -> xr.DataArray: | 170 | 172 | dims, dim_attrs = header.dims, header.dim_attrs | 171 | 173 | # The coordinates, given as a tuple of the form (dims, data, attrs) | 172 | 174 | 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 | + dims[0], | | 177 | + np.ctypeslib.as_array(self.y, shape=(header.n_rows,)).copy(), | | 178 | + dim_attrs[0], | | 179 | + ), | | 180 | + ( | | 181 | + dims[1], | | 182 | + np.ctypeslib.as_array(self.x, shape=(header.n_columns,)).copy(), | | 183 | + dim_attrs[1], | | 184 | + ), | 175 | 185 | ] | 176 | 186 |
| 177 | 187 | # The data array without paddings | | 188 | + data = np.ctypeslib.as_array(self.data, shape=(header.my, header.mx)).copy() | 178 | 189 | 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 | | - ] | | 190 | + data = data[pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1]] | 182 | 191 |
| 183 | 192 | # Create the xarray.DataArray object | 184 | 193 | grid = xr.DataArray( | |
Collapse file Copy file name to clipboardExpand all lines: pygmt/datatypes/image.py +9-10Lines changed: 9 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,13 +38,12 @@ class _GMT_IMAGE(ctp.Structure): # noqa: N801 | 38 | 38 | ... # Image-specific attributes. | 39 | 39 | ... print(image.type, image.n_indexed_colors) | 40 | 40 | ... # The x and y coordinates | 41 | | - ... x = image.x[: header.n_columns] | 42 | | - ... y = image.y[: header.n_rows] | | 41 | + ... x = np.ctypeslib.as_array(image.x, (header.n_columns,)).copy() | | 42 | + ... y = np.ctypeslib.as_array(image.y, (header.n_rows,)).copy() | 43 | 43 | ... # The data array (with paddings) | 44 | | - ... data = np.reshape( | 45 | | - ... image.data[: header.n_bands * header.mx * header.my], | 46 | | - ... (header.my, header.mx, header.n_bands), | 47 | | - ... ) | | 44 | + ... data = np.ctypeslib.as_array( | | 45 | + ... image.data, (header.my, header.mx, header.n_bands) | | 46 | + ... ).copy() | 48 | 47 | ... # The data array (without paddings) | 49 | 48 | ... pad = header.pad[:] | 50 | 49 | ... data = data[pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1], :] | @@ -60,10 +59,10 @@ class _GMT_IMAGE(ctp.Structure): # noqa: N801 | 60 | 59 | [2, 2, 2, 2] | 61 | 60 | b'BRPa' 0.5 | 62 | 61 | 1 0 | 63 | | - >>> x | 64 | | - [-179.5, -178.5, ..., 178.5, 179.5] | 65 | | - >>> y | 66 | | - [89.5, 88.5, ..., -88.5, -89.5] | | 62 | + >>> x # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS | | 63 | + array([-179.5, -178.5, ..., 178.5, 179.5]) | | 64 | + >>> y # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS | | 65 | + array([ 89.5, 88.5, ..., -88.5, -89.5]) | 67 | 66 | >>> data.shape | 68 | 67 | (180, 360, 3) | 69 | 68 | >>> data.min(), data.max() | |
|