Skip to content
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
34 changes: 34 additions & 0 deletions pygmt/tests/test_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Tests for input/output (I/O) utilities.
"""
import numpy as np
import pytest
import xarray as xr
from pygmt.helpers import GMTTempFile
from pygmt.io import load_dataarray


def test_io_load_dataarray():
"""
Check that load_dataarray works to read a NetCDF grid with
GMTDataArrayAccessor information loaded.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
grid = xr.DataArray(
data=np.random.rand(2, 2), coords=[[0.1, 0.2], [0.3, 0.4]], dims=("x", "y")
)
grid.to_netcdf(tmpfile.name)
dataarray = load_dataarray(tmpfile.name)
assert dataarray.gmt.gtype == 0 # Cartesian grid
assert dataarray.gmt.registration == 1 # Pixel registration
# this would fail if we used xr.open_dataarray instead of
# load_dataarray
dataarray.to_netcdf(tmpfile.name)
Comment on lines +11 to +26
Copy link
Member

Choose a reason for hiding this comment

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

Can you clarify why we need this test? To me, it seems redundant with the other tests that read NetCDF grids with GMTDataArrayAccessor information loaded with the exception of the to_netcdf line, but that is xarray functionality rather than pygmt.

Copy link
Member Author

Choose a reason for hiding this comment

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

This test was based on xarray's similar test at https://github.com/pydata/xarray/blob/befd1b98bd84047d62307419a30bcda7a0727926/xarray/tests/test_backends.py#L3729-L3736. I agree that this doesn't increase test coverage and seems redundant, but I feel like we should still have an explicit unit test for load_dataarray here, rather than just testing it indirectly in other functions.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good



def test_io_load_dataarray_cache():
"""
Check that load_dataarray fails when the cache argument is used.
"""
with pytest.raises(TypeError):
_ = load_dataarray("somefile.nc", cache=True)