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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 18 additions & 0 deletions
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

Lines changed: 11 additions & 9 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)