Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for Session.write_data() writing netCDF grids #583

Merged
merged 2 commits into from
Sep 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably use pytest parametrize for this, but we can leave it to a separate PR. Our test suite is getting larger and we might want to consider more efficient ways of testing (I'll open an issue for it).

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])
)