Skip to content

Commit 526e38b

Browse files
authored
Add a test for Session.write_data() writing netCDF grids (#583)
`write_data()` can write a netCDF grid since GMT 6.1.1, if `geometry` is `GMT_IS_SURFACE`. This PR adds a test to make sure `write_data()` works as expected.
1 parent 08aa36f commit 526e38b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

pygmt/tests/test_clib_put_matrix.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55
import numpy.testing as npt
66
import pytest
7+
import xarray as xr
78

89

910
from .test_clib import mock
@@ -55,7 +56,7 @@ def test_put_matrix_fails():
5556

5657

5758
def test_put_matrix_grid():
58-
"Check that assigning a numpy 2d array to a grid works"
59+
"Check that assigning a numpy 2d array to an ASCII and NetCDF grid works"
5960
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
6061
wesn = [10, 15, 30, 40, 0, 0]
6162
inc = [1, 1]
@@ -85,3 +86,23 @@ def test_put_matrix_grid():
8586
# Load the data and check that it's correct
8687
newdata = tmp_file.loadtxt(dtype=dtype)
8788
npt.assert_allclose(newdata, data)
89+
90+
# Save the data to a netCDF grid and check that xarray can load it
91+
with GMTTempFile() as tmp_grid:
92+
lib.write_data(
93+
"GMT_IS_MATRIX",
94+
"GMT_IS_SURFACE",
95+
"GMT_CONTAINER_AND_DATA",
96+
wesn,
97+
tmp_grid.name,
98+
grid,
99+
)
100+
with xr.open_dataarray(tmp_grid.name) as dataarray:
101+
assert dataarray.shape == shape
102+
npt.assert_allclose(dataarray.data, np.flipud(data))
103+
npt.assert_allclose(
104+
dataarray.coords["x"].actual_range, np.array(wesn[0:2])
105+
)
106+
npt.assert_allclose(
107+
dataarray.coords["y"].actual_range, np.array(wesn[2:4])
108+
)

0 commit comments

Comments
 (0)