Skip to content

Commit ae82fd6

Browse files
authored
Deprecate pygmt.io.load_dataarray, use xarray.load_dataarray instead (#3922)
Deprecate usage of `pygmt.io.load_dataarray` for loading NetCDF and other grids, use `xr.load_dataarray(..., engine="gmt")` instead. * Switch from pygmt.io.load_dataarray to xr.load_dataarray Specifically with arguments (..., engine="gmt", decode_kind="grid"). * Suggest passing raster_kind="grid" or "image" in FutureWarning message * Move test_io_load_dataarray benchmark test to test_xarray_backend * Replace pygmt.io.load_dataarray with xr.load_dataarray in tests * Replace xr.open_dataarray with xr.load_dataarray in tests
1 parent f389e05 commit ae82fd6

22 files changed

+100
-57
lines changed

pygmt/datasets/samples.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import pandas as pd
99
import xarray as xr
1010
from pygmt.exceptions import GMTInvalidInput
11-
from pygmt.io import load_dataarray
1211
from pygmt.src import which
1312

1413

@@ -204,7 +203,7 @@ def _load_earth_relief_holes() -> xr.DataArray:
204203
is in meters.
205204
"""
206205
fname = which("@earth_relief_20m_holes.grd", download="c")
207-
return load_dataarray(fname, engine="netcdf4")
206+
return xr.load_dataarray(fname, engine="gmt", raster_kind="grid")
208207

209208

210209
class GMTSampleData(NamedTuple):

pygmt/helpers/testing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import string
88
from pathlib import Path
99

10+
import xarray as xr
1011
from pygmt.exceptions import GMTImageComparisonFailure
11-
from pygmt.io import load_dataarray
1212
from pygmt.src import which
1313

1414

@@ -154,7 +154,7 @@ def load_static_earth_relief():
154154
A grid of Earth relief for internal tests.
155155
"""
156156
fname = which("@static_earth_relief.nc", download="c")
157-
return load_dataarray(fname)
157+
return xr.load_dataarray(fname, engine="gmt", raster_kind="grid")
158158

159159

160160
def skip_if_no(package):

pygmt/io.py

+18
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
PyGMT input/output (I/O) utilities.
33
"""
44

5+
import warnings
6+
57
import xarray as xr
68

79

10+
# TODO(PyGMT>=0.20.0): Remove pygmt.io.load_dataarray
811
def load_dataarray(filename_or_obj, **kwargs):
912
"""
1013
Open, load into memory, and close a DataArray from a file or file-like object
@@ -19,6 +22,12 @@ def load_dataarray(filename_or_obj, **kwargs):
1922
to :py:func:`xarray.open_dataarray`. See that documentation for further
2023
details.
2124
25+
.. deprecated:: v0.16.0
26+
The 'pygmt.io.load_dataarray' function will be removed in v0.20.0. Please use
27+
`xarray.load_dataarray(..., engine='gmt', raster_kind='grid')` instead if you
28+
were reading grids using the engine='netcdf'; otherwise use `raster_kind='image'`
29+
if you were reading multi-band images using engine='rasterio'.
30+
2231
Parameters
2332
----------
2433
filename_or_obj : str or pathlib.Path or file-like or DataStore
@@ -37,6 +46,15 @@ def load_dataarray(filename_or_obj, **kwargs):
3746
--------
3847
xarray.open_dataarray
3948
"""
49+
msg = (
50+
"The 'pygmt.io.load_dataarray' function will be removed in v0.20.0. Please use "
51+
"`xarray.load_dataarray(..., engine='gmt', raster_kind='grid')` instead if you "
52+
"were reading grids using the engine='netcdf'; otherwise use "
53+
"`raster_kind='image'` if you were reading multi-band images using "
54+
"engine='rasterio'."
55+
)
56+
warnings.warn(message=msg, category=FutureWarning, stacklevel=1)
57+
4058
if "cache" in kwargs:
4159
msg = "'cache' has no effect in this context."
4260
raise TypeError(msg)

pygmt/tests/test_clib_put_matrix.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,14 @@ def test_put_matrix_grid(dtypes):
111111
tmp_grid.name,
112112
grid,
113113
)
114-
with xr.open_dataarray(tmp_grid.name) as dataarray:
115-
assert dataarray.shape == shape
116-
npt.assert_allclose(dataarray.data, np.flipud(data))
117-
npt.assert_allclose(
118-
dataarray.coords["x"].actual_range, np.array(wesn[0:2])
119-
)
120-
npt.assert_allclose(
121-
dataarray.coords["y"].actual_range, np.array(wesn[2:4])
122-
)
114+
dataarray = xr.load_dataarray(
115+
tmp_grid.name, engine="gmt", raster_kind="grid"
116+
)
117+
assert dataarray.shape == shape
118+
npt.assert_allclose(dataarray.data, np.flipud(data))
119+
npt.assert_allclose(
120+
dataarray.coords["x"].actual_range, np.array(wesn[0:2])
121+
)
122+
npt.assert_allclose(
123+
dataarray.coords["y"].actual_range, np.array(wesn[2:4])
124+
)

pygmt/tests/test_clib_read_data.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pygmt.clib import Session
1212
from pygmt.exceptions import GMTCLibError
1313
from pygmt.helpers import GMTTempFile
14-
from pygmt.io import load_dataarray
1514
from pygmt.src import which
1615

1716
try:
@@ -27,7 +26,9 @@ def fixture_expected_xrgrid():
2726
"""
2827
The expected xr.DataArray object for the static_earth_relief.nc file.
2928
"""
30-
return load_dataarray(which("@static_earth_relief.nc"))
29+
return xr.load_dataarray(
30+
"@static_earth_relief.nc", engine="gmt", raster_kind="grid"
31+
)
3132

3233

3334
@pytest.fixture(scope="module", name="expected_xrimage")

pygmt/tests/test_dimfilter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88
import xarray as xr
9-
from pygmt import dimfilter, load_dataarray
9+
from pygmt import dimfilter
1010
from pygmt.enums import GridRegistration, GridType
1111
from pygmt.exceptions import GMTInvalidInput
1212
from pygmt.helpers import GMTTempFile
@@ -57,7 +57,7 @@ def test_dimfilter_outgrid(grid, expected_grid):
5757
)
5858
assert result is None # return value is None
5959
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
60-
temp_grid = load_dataarray(tmpfile.name)
60+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
6161
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
6262

6363

pygmt/tests/test_grdclip.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy.testing as npt
99
import pytest
1010
import xarray as xr
11-
from pygmt import grdclip, load_dataarray
11+
from pygmt import grdclip
1212
from pygmt.datasets import load_earth_mask
1313
from pygmt.enums import GridRegistration, GridType
1414
from pygmt.exceptions import GMTInvalidInput
@@ -54,7 +54,7 @@ def test_grdclip_outgrid(grid, expected_grid):
5454
)
5555
assert result is None # return value is None
5656
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
57-
temp_grid = load_dataarray(tmpfile.name)
57+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
5858
assert temp_grid.dims == ("lat", "lon")
5959
assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC
6060
assert temp_grid.gmt.registration == GridRegistration.PIXEL

pygmt/tests/test_grdcut.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
import pytest
77
import xarray as xr
8-
from pygmt import grdcut, load_dataarray
8+
from pygmt import grdcut
99
from pygmt.exceptions import GMTInvalidInput
1010
from pygmt.helpers import GMTTempFile
1111
from pygmt.helpers.testing import load_static_earth_relief
@@ -50,7 +50,7 @@ def test_grdcut_dataarray_in_file_out(grid, expected_grid, region):
5050
with GMTTempFile(suffix=".nc") as tmpfile:
5151
result = grdcut(grid, outgrid=tmpfile.name, region=region)
5252
assert result is None # grdcut returns None if output to a file
53-
temp_grid = load_dataarray(tmpfile.name)
53+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
5454
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
5555

5656

pygmt/tests/test_grdfill.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy.testing as npt
99
import pytest
1010
import xarray as xr
11-
from pygmt import grdfill, load_dataarray
11+
from pygmt import grdfill
1212
from pygmt.enums import GridRegistration, GridType
1313
from pygmt.exceptions import GMTInvalidInput
1414
from pygmt.helpers import GMTTempFile
@@ -96,7 +96,7 @@ def test_grdfill_file_out(grid, expected_grid):
9696
result = grdfill(grid=grid, constantfill=20, outgrid=tmpfile.name)
9797
assert result is None # return value is None
9898
assert Path(tmpfile.name).stat().st_size > 0 # check that outfile exists
99-
temp_grid = load_dataarray(tmpfile.name)
99+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
100100
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
101101

102102

pygmt/tests/test_grdfilter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88
import pytest
99
import xarray as xr
10-
from pygmt import grdfilter, load_dataarray
10+
from pygmt import grdfilter
1111
from pygmt.enums import GridRegistration, GridType
1212
from pygmt.exceptions import GMTInvalidInput
1313
from pygmt.helpers import GMTTempFile
@@ -71,7 +71,7 @@ def test_grdfilter_dataarray_in_file_out(grid, expected_grid):
7171
)
7272
assert result is None # return value is None
7373
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
74-
temp_grid = load_dataarray(tmpfile.name)
74+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
7575
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
7676

7777

pygmt/tests/test_grdgradient.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88
import xarray as xr
9-
from pygmt import grdgradient, load_dataarray
9+
from pygmt import grdgradient
1010
from pygmt.enums import GridRegistration, GridType
1111
from pygmt.exceptions import GMTInvalidInput
1212
from pygmt.helpers import GMTTempFile
@@ -50,7 +50,7 @@ def test_grdgradient_outgrid(grid, expected_grid):
5050
)
5151
assert result is None # return value is None
5252
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
53-
temp_grid = load_dataarray(tmpfile.name)
53+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
5454
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
5555

5656

pygmt/tests/test_grdhisteq.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pandas as pd
99
import pytest
1010
import xarray as xr
11-
from pygmt import grdhisteq, load_dataarray
11+
from pygmt import grdhisteq
1212
from pygmt.enums import GridRegistration, GridType
1313
from pygmt.exceptions import GMTInvalidInput
1414
from pygmt.helpers import GMTTempFile
@@ -67,7 +67,7 @@ def test_equalize_grid_outgrid_file(grid, expected_grid, region):
6767
)
6868
assert result is None # return value is None
6969
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
70-
temp_grid = load_dataarray(tmpfile.name)
70+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
7171
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
7272

7373

pygmt/tests/test_grdlandmask.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88
import xarray as xr
9-
from pygmt import grdlandmask, load_dataarray
9+
from pygmt import grdlandmask
1010
from pygmt.enums import GridRegistration, GridType
1111
from pygmt.exceptions import GMTInvalidInput
1212
from pygmt.helpers import GMTTempFile
@@ -42,7 +42,7 @@ def test_grdlandmask_outgrid(expected_grid):
4242
result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[125, 130, 30, 35])
4343
assert result is None # return value is None
4444
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
45-
temp_grid = load_dataarray(tmpfile.name)
45+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
4646
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
4747

4848

pygmt/tests/test_grdproject.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88
import xarray as xr
9-
from pygmt import grdproject, load_dataarray
9+
from pygmt import grdproject
1010
from pygmt.enums import GridRegistration, GridType
1111
from pygmt.exceptions import GMTInvalidInput
1212
from pygmt.helpers import GMTTempFile
@@ -56,7 +56,7 @@ def test_grdproject_file_out(grid, expected_grid):
5656
)
5757
assert result is None # return value is None
5858
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
59-
temp_grid = load_dataarray(tmpfile.name)
59+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
6060
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
6161

6262

pygmt/tests/test_grdsample.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88
import xarray as xr
9-
from pygmt import grdsample, load_dataarray
9+
from pygmt import grdsample
1010
from pygmt.enums import GridRegistration, GridType
1111
from pygmt.helpers import GMTTempFile
1212
from pygmt.helpers.testing import load_static_earth_relief
@@ -67,7 +67,7 @@ def test_grdsample_file_out(grid, expected_grid, region, spacing):
6767
)
6868
assert result is None # return value is None
6969
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
70-
temp_grid = load_dataarray(tmpfile.name)
70+
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
7171
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
7272

7373

pygmt/tests/test_io.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pygmt.io import load_dataarray
1111

1212

13-
@pytest.mark.benchmark
13+
# TODO(PyGMT>=0.20.0): Remove test_io_load_dataarray
1414
def test_io_load_dataarray():
1515
"""
1616
Check that load_dataarray works to read a netCDF grid with GMTDataArrayAccessor
@@ -22,7 +22,10 @@ def test_io_load_dataarray():
2222
data=rng.random((2, 2)), coords=[[0.1, 0.2], [0.3, 0.4]], dims=("x", "y")
2323
)
2424
grid.to_netcdf(tmpfile.name)
25-
dataarray = load_dataarray(tmpfile.name)
25+
26+
with pytest.warns(FutureWarning):
27+
dataarray = load_dataarray(tmpfile.name)
28+
2629
assert dataarray.gmt.gtype == GridType.CARTESIAN
2730
assert dataarray.gmt.registration == GridRegistration.PIXEL
2831
# this would fail if we used xr.open_dataarray instead of load_dataarray

pygmt/tests/test_nearneighbor.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_nearneighbor_with_outgrid_param(ship_data):
8282
)
8383
assert output is None # check that output is None since outgrid is set
8484
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
85-
with xr.open_dataarray(tmpfile.name) as grid:
86-
assert isinstance(grid, xr.DataArray) # ensure netCDF grid loads ok
87-
assert grid.shape == (121, 121)
88-
npt.assert_allclose(grid.mean(), -2378.2385)
85+
grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
86+
assert isinstance(grid, xr.DataArray) # ensure netCDF grid loads ok
87+
assert grid.shape == (121, 121)
88+
npt.assert_allclose(grid.mean(), -2378.2385)

pygmt/tests/test_surface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,5 @@ def test_surface_with_outgrid_param(data, region, spacing, expected_grid):
145145
)
146146
assert output is None # check that output is None since outgrid is set
147147
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
148-
with xr.open_dataarray(tmpfile.name) as grid:
149-
check_values(grid, expected_grid)
148+
grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
149+
check_values(grid, expected_grid)

pygmt/tests/test_triangulate.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def test_regular_grid_with_outgrid_param(dataframe, expected_grid):
156156
)
157157
assert output is None # check that output is None since outgrid is set
158158
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
159-
with xr.open_dataarray(tmpfile.name) as grid:
160-
assert isinstance(grid, xr.DataArray)
161-
assert grid.gmt.registration == GridRegistration.GRIDLINE
162-
assert grid.gmt.gtype == GridType.CARTESIAN
163-
xr.testing.assert_allclose(a=grid, b=expected_grid)
159+
grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
160+
assert isinstance(grid, xr.DataArray)
161+
assert grid.gmt.registration == GridRegistration.GRIDLINE
162+
assert grid.gmt.gtype == GridType.CARTESIAN
163+
xr.testing.assert_allclose(a=grid, b=expected_grid)

pygmt/tests/test_xarray_accessor.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ def test_xarray_accessor_gridline_cartesian():
2020
Check that the accessor returns the correct registration and gtype values for a
2121
Cartesian, gridline-registered grid.
2222
"""
23-
fname = which(fname="@test.dat.nc", download="a")
24-
grid = xr.open_dataarray(fname, engine="netcdf4")
23+
grid = xr.load_dataarray("@test.dat.nc", engine="gmt", raster_kind="grid")
2524
assert grid.gmt.registration == GridRegistration.GRIDLINE
2625
assert grid.gmt.gtype == GridType.CARTESIAN
2726

@@ -31,8 +30,7 @@ def test_xarray_accessor_pixel_geographic():
3130
Check that the accessor returns the correct registration and gtype values for a
3231
geographic, pixel-registered grid.
3332
"""
34-
fname = which(fname="@earth_relief_01d_p", download="a")
35-
grid = xr.open_dataarray(fname, engine="netcdf4")
33+
grid = xr.load_dataarray("@earth_relief_01d_p", engine="gmt", raster_kind="grid")
3634
assert grid.gmt.registration == GridRegistration.PIXEL
3735
assert grid.gmt.gtype == GridType.GEOGRAPHIC
3836

0 commit comments

Comments
 (0)