Skip to content

Commit 69417b4

Browse files
core-manseismanweiji14
authored
Allow load_earth_relief() to load the original land-only 01s or 03s SRTM tiles (#976)
Allow the load_earth_relief function to load the original land-only 01s or 03s SRTM tiles, by adding a new parameter `use_srtm` (default to False). Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent 5202c64 commit 69417b4

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

pygmt/datasets/earth_relief.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from pygmt.src import grdcut, which
1111

1212

13-
@kwargs_to_strings(region="sequence")
14-
def load_earth_relief(resolution="01d", region=None, registration=None):
13+
@kwargs_to_strings(convert_bools=False, region="sequence")
14+
def load_earth_relief(resolution="01d", region=None, registration=None, use_srtm=False):
1515
r"""
1616
Load Earth relief grids (topography and bathymetry) in various resolutions.
1717
@@ -50,6 +50,13 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
5050
a pixel-registered grid is returned unless only the
5151
gridline-registered grid is available.
5252
53+
use_srtm : bool
54+
By default, the land-only SRTM tiles from NASA are used to generate the
55+
``'03s'`` and ``'01s'`` grids, and the missing ocean values are filled
56+
by up-sampling the SRTM15+V2.1 tiles which have a resolution of 15
57+
arc-second (i.e., ``'15s'``). If True, will only load the original
58+
land-only SRTM tiles.
59+
5360
Returns
5461
-------
5562
grid : :class:`xarray.DataArray`
@@ -73,12 +80,21 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
7380
>>> grid = load_earth_relief(
7481
... "05m", region=[120, 160, 30, 60], registration="gridline"
7582
... )
83+
>>> # load the original 3 arc-second land-only SRTM tiles from NASA
84+
>>> grid = load_earth_relief(
85+
... "03s",
86+
... region=[135, 136, 35, 36],
87+
... registration="gridline",
88+
... use_srtm=True,
89+
... )
7690
"""
7791

7892
# earth relief data stored as single grids for low resolutions
7993
non_tiled_resolutions = ["01d", "30m", "20m", "15m", "10m", "06m"]
8094
# earth relief data stored as tiles for high resolutions
8195
tiled_resolutions = ["05m", "04m", "03m", "02m", "01m", "30s", "15s", "03s", "01s"]
96+
# resolutions of original land-only SRTM tiles from NASA
97+
land_only_srtm_resolutions = ["03s", "01s"]
8298

8399
if registration in ("pixel", "gridline", None):
84100
# If None, let GMT decide on Pixel/Gridline type
@@ -103,21 +119,25 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
103119
f"resolution '{resolution}' is not supported."
104120
)
105121

122+
# Choose earth relief data prefix
123+
earth_relief_prefix = "earth_relief_"
124+
if use_srtm and resolution in land_only_srtm_resolutions:
125+
earth_relief_prefix = "srtm_relief_"
126+
106127
# different ways to load tiled and non-tiled earth relief data
107128
# Known issue: tiled grids don't support slice operation
108129
# See https://github.com/GenericMappingTools/pygmt/issues/524
109130
if region is None:
110-
if resolution in non_tiled_resolutions:
111-
fname = which(f"@earth_relief_{resolution}{reg}", download="a")
112-
with xr.open_dataarray(fname) as dataarray:
113-
grid = dataarray.load()
114-
_ = grid.gmt # load GMTDataArray accessor information
115-
else:
131+
if resolution not in non_tiled_resolutions:
116132
raise GMTInvalidInput(
117133
f"'region' is required for Earth relief resolution '{resolution}'."
118134
)
135+
fname = which(f"@earth_relief_{resolution}{reg}", download="a")
136+
with xr.open_dataarray(fname) as dataarray:
137+
grid = dataarray.load()
138+
_ = grid.gmt # load GMTDataArray accessor information
119139
else:
120-
grid = grdcut(f"@earth_relief_{resolution}{reg}", region=region)
140+
grid = grdcut(f"@{earth_relief_prefix}{resolution}{reg}", region=region)
121141

122142
# Add some metadata to the grid
123143
grid.name = "elevation"

pygmt/tests/test_datasets_earth_relief.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ def test_earth_relief_05m_without_region():
8383
load_earth_relief("05m")
8484

8585

86+
def test_earth_relief_03s_landonly_srtm():
87+
"""
88+
Test loading original 3 arc-second land-only SRTM tiles.
89+
"""
90+
data = load_earth_relief(
91+
"03s", region=[135, 136, 35, 36], registration="gridline", use_srtm=True
92+
)
93+
94+
assert data.coords["lat"].data.min() == 35.0
95+
assert data.coords["lat"].data.max() == 36.0
96+
assert data.coords["lon"].data.min() == 135.0
97+
assert data.coords["lon"].data.max() == 136.0
98+
# data.data.min() == -305.51846 if use_srtm is False.
99+
assert data.data.min() == -6.0
100+
assert data.data.max() == 1191.0
101+
assert data.sizes["lat"] == 1201
102+
assert data.sizes["lon"] == 1201
103+
104+
86105
def test_earth_relief_incorrect_registration():
87106
"""
88107
Test loading earth relief with incorrect registration type.

0 commit comments

Comments
 (0)