Skip to content

Commit f7d5526

Browse files
authored
Add tests for load_dataarray to increase code coverage (#1518)
Patches missing code coverage of pygmt.load_dataarray function wrapped in #1439. Checks that a NetCDF grid can be read with GMTDataArrayAccessor information loaded, and that load_dataarray fails when the cache argument is used.
1 parent 8409764 commit f7d5526

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pygmt/tests/test_io.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Tests for input/output (I/O) utilities.
3+
"""
4+
import numpy as np
5+
import pytest
6+
import xarray as xr
7+
from pygmt.helpers import GMTTempFile
8+
from pygmt.io import load_dataarray
9+
10+
11+
def test_io_load_dataarray():
12+
"""
13+
Check that load_dataarray works to read a NetCDF grid with
14+
GMTDataArrayAccessor information loaded.
15+
"""
16+
with GMTTempFile(suffix=".nc") as tmpfile:
17+
grid = xr.DataArray(
18+
data=np.random.rand(2, 2), coords=[[0.1, 0.2], [0.3, 0.4]], dims=("x", "y")
19+
)
20+
grid.to_netcdf(tmpfile.name)
21+
dataarray = load_dataarray(tmpfile.name)
22+
assert dataarray.gmt.gtype == 0 # Cartesian grid
23+
assert dataarray.gmt.registration == 1 # Pixel registration
24+
# this would fail if we used xr.open_dataarray instead of
25+
# load_dataarray
26+
dataarray.to_netcdf(tmpfile.name)
27+
28+
29+
def test_io_load_dataarray_cache():
30+
"""
31+
Check that load_dataarray fails when the cache argument is used.
32+
"""
33+
with pytest.raises(TypeError):
34+
_ = load_dataarray("somefile.nc", cache=True)

0 commit comments

Comments
 (0)