@@ -40,16 +40,15 @@ 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, shape=( header.n_columns,)).copy( )
44
+ ... y = np.ctypeslib.as_array (grid.y, shape=( 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 (
47
+ ... grid.data, shape= (header.my, header.mx)
48
+ ... ).copy()
49
49
... # The data array (without paddings)
50
50
... pad = header.pad[:]
51
51
... data = data[pad[2] : header.my - pad[3], pad[0] : header.mx - pad[1]]
52
- ... print(data)
53
52
14 8 1
54
53
[-55.0, -47.0, -24.0, -10.0] 190.0 981.0 [1.0, 1.0]
55
54
1.0 0.0
@@ -61,22 +60,26 @@ class _GMT_GRID(ctp.Structure): # noqa: N801
61
60
18 1 12 18
62
61
[2, 2, 2, 2]
63
62
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. ]]
63
+ >>> x
64
+ array([-54.5, -53.5, -52.5, -51.5, -50.5, -49.5, -48.5, -47.5])
65
+ >>> y
66
+ array([-10.5, -11.5, -12.5, -13.5, ..., -20.5, -21.5, -22.5, -23.5])
67
+ >>> data
68
+ array([[347.5, 331.5, 309. , 282. , 190. , 208. , 299.5, 348. ],
69
+ [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5],
70
+ [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5],
71
+ [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5],
72
+ [494.5, 488.5, 357. , 254.5, 286. , 484.5, 653.5, 930. ],
73
+ [601. , 526.5, 535. , 299. , 398.5, 645. , 797.5, 964. ],
74
+ [308. , 595.5, 555.5, 556. , 580. , 770. , 927. , 920. ],
75
+ [521.5, 682.5, 796. , 886. , 571.5, 638.5, 739.5, 881.5],
76
+ [310. , 521.5, 757. , 570.5, 538.5, 524. , 686.5, 794. ],
77
+ [561.5, 539. , 446.5, 481.5, 439.5, 553. , 726.5, 981. ],
78
+ [557. , 435. , 385.5, 345.5, 413.5, 496. , 519.5, 833.5],
79
+ [373. , 367.5, 349. , 352.5, 419.5, 428. , 570. , 667.5],
80
+ [383. , 284.5, 344.5, 394. , 491. , 556.5, 578.5, 618.5],
81
+ [347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ]],
82
+ dtype=float32)
80
83
"""
81
84
82
85
_fields_ : ClassVar = [
@@ -126,7 +129,8 @@ def to_dataarray(self) -> xr.DataArray:
126
129
[450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5],
127
130
[345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5],
128
131
[349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5],
129
- [347.5, 331.5, 309. , 282. , 190. , 208. , 299.5, 348. ]])
132
+ [347.5, 331.5, 309. , 282. , 190. , 208. , 299.5, 348. ]],
133
+ dtype=float32)
130
134
Coordinates:
131
135
* lat (lat) float64... -23.5 -22.5 -21.5 -20.5 ... -12.5 -11.5 -10.5
132
136
* lon (lon) float64... -54.5 -53.5 -52.5 -51.5 -50.5 -49.5 -48.5 -47.5
@@ -169,16 +173,14 @@ def to_dataarray(self) -> xr.DataArray:
169
173
# Get dimensions and their attributes from the header.
170
174
dims , dim_attrs = header .dims , header .dim_attrs
171
175
# 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
+ x = np .ctypeslib .as_array (self .x , shape = (header .n_columns ,)).copy ()
177
+ y = np .ctypeslib .as_array (self .y , shape = (header .n_rows ,)).copy ()
178
+ coords = [(dims [0 ], y , dim_attrs [0 ]), (dims [1 ], x , dim_attrs [1 ])]
176
179
177
180
# The data array without paddings
181
+ data = np .ctypeslib .as_array (self .data , shape = (header .my , header .mx )).copy ()
178
182
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
- ]
183
+ data = data [pad [2 ] : header .my - pad [3 ], pad [0 ] : header .mx - pad [1 ]]
182
184
183
185
# Create the xarray.DataArray object
184
186
grid = xr .DataArray (
0 commit comments