Skip to content

Commit

Permalink
Add a test for Session.write_data() writing netCDF grids (#583)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
seisman authored Sep 5, 2020
1 parent 08aa36f commit 526e38b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pygmt/tests/test_clib_put_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import numpy.testing as npt
import pytest
import xarray as xr


from .test_clib import mock
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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])
)

0 comments on commit 526e38b

Please sign in to comment.