Skip to content

Commit 805f533

Browse files
authored
Let load_earth_relief() support 'region' argument for all resolutions (#873)
* Let load_earth_relief() support 'region' argument for all resolutions * Improve docstrings of load_earth_relief()
1 parent 0990847 commit 805f533

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

pygmt/datasets/earth_relief.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Function to download the Earth relief datasets from the GMT data server, and
3-
load as DataArray.
3+
load as :class:`xarray.DataArray`.
44
55
The grids are available in various resolutions.
66
"""
@@ -12,7 +12,7 @@
1212

1313
@kwargs_to_strings(region="sequence")
1414
def load_earth_relief(resolution="01d", region=None, registration=None):
15-
"""
15+
r"""
1616
Load Earth relief grids (topography and bathymetry) in various resolutions.
1717
1818
The grids are downloaded to a user data directory
@@ -21,8 +21,11 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
2121
So you'll need an internet connection the first time around.
2222
2323
These grids can also be accessed by passing in the file name
24-
``'@earth_relief_rru[_reg]'`` to any grid plotting/processing function.
25-
Refer to :gmt-docs:`datasets/remote-data.html` for more details.
24+
**@earth_relief**\_\ *res*\[_\ *reg*] to any grid plotting/processing
25+
function. *res* is the grid resolution (see below), and *reg* is grid
26+
registration type (**p** for pixel registration or *g* for gridline
27+
registration). Refer to :gmt-docs:`datasets/remote-data.html` for more
28+
details.
2629
2730
Parameters
2831
----------
@@ -35,7 +38,7 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
3538
3639
region : str or list
3740
The subregion of the grid to load. Required for Earth relief grids with
38-
resolutions <= 05m.
41+
resolutions higher than 5 arc-minute (i.e., ``05m``).
3942
4043
registration : str
4144
Grid registration type. Either ``pixel`` for pixel registration or
@@ -45,14 +48,15 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
4548
4649
Returns
4750
-------
48-
grid : xarray.DataArray
51+
grid : :class:`xarray.DataArray`
4952
The Earth relief grid. Coordinates are latitude and longitude in
5053
degrees. Relief is in meters.
5154
5255
Notes
5356
-----
54-
The DataArray doesn's support slice operation, for Earth relief data with
55-
resolutions higher than "05m", which are stored as smaller tiles.
57+
The :class:`xarray.DataArray` grid doesn't support slice operation, for
58+
Earth relief data with resolutions higher than "05m", which are stored as
59+
smaller tiles.
5660
5761
Examples
5862
--------
@@ -83,26 +87,24 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
8387
"gridline-registered grid is available."
8488
)
8589

90+
if resolution not in non_tiled_resolutions + tiled_resolutions:
91+
raise GMTInvalidInput(f"Invalid Earth relief resolution '{resolution}'.")
92+
8693
# different ways to load tiled and non-tiled earth relief data
87-
if resolution in non_tiled_resolutions:
88-
if region is not None:
89-
raise NotImplementedError(
90-
f"'region' is not supported for Earth relief resolution '{resolution}'"
91-
)
92-
fname = which(f"@earth_relief_{resolution}{reg}", download="a")
93-
with xr.open_dataarray(fname) as dataarray:
94-
grid = dataarray.load()
95-
_ = grid.gmt # load GMTDataArray accessor information
96-
elif resolution in tiled_resolutions:
97-
# Titled grid can't be sliced.
98-
# See https://github.com/GenericMappingTools/pygmt/issues/524
99-
if region is None:
94+
# Known issue: tiled grids don't support slice operation
95+
# See https://github.com/GenericMappingTools/pygmt/issues/524
96+
if region is None:
97+
if resolution in non_tiled_resolutions:
98+
fname = which(f"@earth_relief_{resolution}{reg}", download="a")
99+
with xr.open_dataarray(fname) as dataarray:
100+
grid = dataarray.load()
101+
_ = grid.gmt # load GMTDataArray accessor information
102+
else:
100103
raise GMTInvalidInput(
101-
f"'region' is required for Earth relief resolution '{resolution}'"
104+
f"'region' is required for Earth relief resolution '{resolution}'."
102105
)
103-
grid = grdcut(f"@earth_relief_{resolution}{reg}", region=region)
104106
else:
105-
raise GMTInvalidInput(f'Invalid Earth relief resolution "{resolution}"')
107+
grid = grdcut(f"@earth_relief_{resolution}{reg}", region=region)
106108

107109
# Add some metadata to the grid
108110
grid.name = "elevation"

pygmt/tests/test_datasets.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,14 @@ def test_earth_relief_01d_with_region():
9393
"""
9494
Test loading low-resolution earth relief with 'region'.
9595
"""
96-
with pytest.raises(NotImplementedError):
97-
load_earth_relief("01d", region=[0, 180, 0, 90])
96+
data = load_earth_relief(
97+
resolution="01d", region=[-10, 10, -5, 5], registration="gridline"
98+
)
99+
assert data.shape == (11, 21)
100+
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
101+
npt.assert_allclose(data.lon, np.arange(-10, 11, 1))
102+
npt.assert_allclose(data.min(), -5145)
103+
npt.assert_allclose(data.max(), 805.5)
98104

99105

100106
def test_earth_relief_30m():

0 commit comments

Comments
 (0)