From dedf242af0c9d64323f352f92ea0cef5442d1d21 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Sun, 12 Sep 2021 22:31:25 +1200 Subject: [PATCH] Add tests for load_dataarray to increase code coverage --- pygmt/tests/test_io.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 pygmt/tests/test_io.py diff --git a/pygmt/tests/test_io.py b/pygmt/tests/test_io.py new file mode 100644 index 00000000000..82975442613 --- /dev/null +++ b/pygmt/tests/test_io.py @@ -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) + ds = load_dataarray(tmpfile.name) + assert ds.gmt.gtype == 0 # Cartesian grid + assert ds.gmt.registration == 1 # Pixel registration + # this would fail if we used xr.open_dataarray instead of + # load_dataarray + ds.to_netcdf(tmpfile.name) + + +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)