Add a test for Session.write_data() writing netCDF grids
`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.
@@ -4,6 +4,7 @@ Test the functions that put matrix data into GMT.
import numpy as np
import numpy.testing as npt
import pytest
+import xarray as xr
from .test_clib import mock
@@ -55,7 +56,7 @@ def test_put_matrix_fails():
def test_put_matrix_grid():
- "Check that assigning a numpy 2d array to a grid works"
+ "Check that assigning a numpy 2d array to an ASCII and NetCDF grid works"
dtypes = "float32 float64 int32 int64 uint32 uint64".split()
wesn = [10, 15, 30, 40, 0, 0]
inc = [1, 1]
@@ -85,3 +86,23 @@ def test_put_matrix_grid():
# Load the data and check that it's correct
newdata = tmp_file.loadtxt(dtype=dtype)
npt.assert_allclose(newdata, data)
+
+ # Save the data to a netCDF grid and check that xarray can load it
+ with GMTTempFile() as tmp_grid:
+ lib.write_data(
+ "GMT_IS_MATRIX",
+ "GMT_IS_SURFACE",
+ "GMT_CONTAINER_AND_DATA",
+ wesn,
+ tmp_grid.name,
+ grid,
+ )
+ with xr.open_dataarray(tmp_grid.name) as dataarray:
+ assert dataarray.shape == shape
+ npt.assert_allclose(dataarray.data, np.flipud(data))
+ npt.assert_allclose(
+ dataarray.coords["x"].actual_range, np.array(wesn[0:2])
+ )
+ npt.assert_allclose(
+ dataarray.coords["y"].actual_range, np.array(wesn[2:4])
+ )
