Skip to content

Commit cd1076a

Browse files
seismanweiji14
andauthored
Add load_mercury_relief to load Mercury relief dataset in various resolutions and registrations (#3028)
Co-authored-by: Wei Ji <[email protected]>
1 parent 598e770 commit cd1076a

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ and store them in GMT's user data directory.
227227
datasets.load_earth_relief
228228
datasets.load_earth_vertical_gravity_gradient
229229
datasets.load_mars_relief
230+
datasets.load_mercury_relief
230231
datasets.load_moon_relief
231232
datasets.load_pluto_relief
232233
datasets.load_venus_relief

pygmt/datasets/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
load_earth_vertical_gravity_gradient,
1515
)
1616
from pygmt.datasets.mars_relief import load_mars_relief
17+
from pygmt.datasets.mercury_relief import load_mercury_relief
1718
from pygmt.datasets.moon_relief import load_moon_relief
1819
from pygmt.datasets.pluto_relief import load_pluto_relief
1920
from pygmt.datasets.samples import list_sample_data, load_sample_data

pygmt/datasets/load_remote_dataset.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,27 @@ class GMTRemoteDataset(NamedTuple):
272272
"14s": Resolution("14s", registrations=["pixel"], tiled=True),
273273
},
274274
),
275+
"mercury_relief": GMTRemoteDataset(
276+
title="Mercury relief",
277+
name="mercury_relief",
278+
long_name="USGS Mercury relief",
279+
units="meters",
280+
extra_attributes={},
281+
resolutions={
282+
"01d": Resolution("01d"),
283+
"30m": Resolution("30m"),
284+
"20m": Resolution("20m"),
285+
"15m": Resolution("15m"),
286+
"10m": Resolution("10m"),
287+
"06m": Resolution("06m"),
288+
"05m": Resolution("05m", tiled=True),
289+
"04m": Resolution("04m", tiled=True),
290+
"03m": Resolution("03m", tiled=True),
291+
"02m": Resolution("02m", tiled=True),
292+
"01m": Resolution("01m", tiled=True),
293+
"56s": Resolution("56s", registrations=["pixel"], tiled=True),
294+
},
295+
),
275296
"pluto_relief": GMTRemoteDataset(
276297
title="Pluto relief",
277298
name="pluto_relief",

pygmt/datasets/mercury_relief.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
Function to download the Mercury relief dataset from the GMT data server, and load as
3+
:class:`xarray.DataArray`.
4+
5+
The grids are available in various resolutions.
6+
"""
7+
from typing import Literal
8+
9+
from pygmt.datasets.load_remote_dataset import _load_remote_dataset
10+
from pygmt.helpers import kwargs_to_strings
11+
12+
__doctest_skip__ = ["load_mercury_relief"]
13+
14+
15+
@kwargs_to_strings(region="sequence")
16+
def load_mercury_relief(
17+
resolution="01d",
18+
region=None,
19+
registration: Literal["gridline", "pixel", None] = None,
20+
):
21+
r"""
22+
Load the Mercury relief dataset in various resolutions.
23+
24+
.. figure:: https://www.generic-mapping-tools.org/remote-datasets/_images/GMT_mercury_relief.jpg
25+
:width: 80%
26+
:align: center
27+
28+
Mercury relief dataset.
29+
30+
The grids are downloaded to a user data directory (usually
31+
``~/.gmt/server/mercury/mercury_relief/``) the first time you invoke this function.
32+
Afterwards, it will load the grid from the data directory. So you'll need an
33+
internet connection the first time around.
34+
35+
These grids can also be accessed by passing in the file name
36+
**@mercury_relief**\_\ *res*\[_\ *reg*] to any grid processing function or plotting
37+
method. *res* is the grid resolution (see below), and *reg* is the grid registration
38+
type (**p** for pixel registration or **g** for gridline registration).
39+
40+
The default color palette table (CPT) for this dataset is *@mercury_relief.cpt*.
41+
It's implicitly used when passing in the file name of the dataset to any grid
42+
plotting method if no CPT is explicitly specified. When the dataset is loaded and
43+
plotted as an :class:`xarray.DataArray` object, the default CPT is ignored, and
44+
GMT's default CPT (*turbo*) is used. To use the dataset-specific CPT, you need to
45+
explicitly set ``cmap="@mercury_relief.cpt"``.
46+
47+
Refer to :gmt-datasets:`mercury-relief.html` for more details about available
48+
datasets, including version information and references.
49+
50+
Parameters
51+
----------
52+
resolution : str
53+
The grid resolution. The suffix ``d``, ``m`` and ``s`` stand for arc-degrees,
54+
arc-minutes and arc-seconds. It can be ``"01d"``, ``"30m"``, ``"20m"``,
55+
``"15m"``, ``"10m"``, ``"06m"``, ``"05m"``, ``"04m"``, ``"03m"``, ``"02m"``,
56+
``"01m"``, and ``"56s"``.
57+
region : str or list
58+
The subregion of the grid to load, in the form of a list
59+
[*xmin*, *xmax*, *ymin*, *ymax*] or a string *xmin/xmax/ymin/ymax*. Required for
60+
grids with resolutions higher than 5 arc-minutes (i.e., ``"05m"``).
61+
registration
62+
Grid registration type. Either ``"pixel"`` for pixel registration or
63+
``"gridline"`` for gridline registration. Default is ``None``, means
64+
``"gridline"`` for all resolutions except for ``"56s"`` which is ``"pixel"``
65+
only.
66+
67+
Returns
68+
-------
69+
grid : :class:`xarray.DataArray`
70+
The Mercury relief grid. Coordinates are latitude and longitude in degrees.
71+
Relief is in meters.
72+
73+
Note
74+
----
75+
The registration and coordinate system type of the returned
76+
:class:`xarray.DataArray` grid can be accessed via the GMT accessors (i.e.,
77+
``grid.gmt.registration`` and ``grid.gmt.gtype`` respectively). However, these
78+
properties may be lost after specific grid operations (such as slicing) and will
79+
need to be manually set before passing the grid to any PyGMT data processing or
80+
plotting functions. Refer to :class:`pygmt.GMTDataArrayAccessor` for detailed
81+
explanations and workarounds.
82+
83+
Examples
84+
--------
85+
>>> from pygmt.datasets import load_mercury_relief
86+
>>> # load the default grid (gridline-registered 1 arc-degree grid)
87+
>>> grid = load_mercury_relief()
88+
>>> # load the 30 arc-minutes grid with "gridline" registration
89+
>>> grid = load_mercury_relief(resolution="30m", registration="gridline")
90+
>>> # load high-resolution (5 arc-minutes) grid for a specific region
91+
>>> grid = load_mercury_relief(
92+
... resolution="05m",
93+
... region=[120, 160, 30, 60],
94+
... registration="gridline",
95+
... )
96+
"""
97+
grid = _load_remote_dataset(
98+
dataset_name="mercury_relief",
99+
dataset_prefix="mercury_relief_",
100+
resolution=resolution,
101+
region=region,
102+
registration=registration,
103+
)
104+
return grid

pygmt/helpers/caching.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def cache_data():
6262
# Mars relief grids
6363
"@mars_relief_01d_g",
6464
"@N00W030.mars_relief_01m_g.nc", # Specific grid for 01m tes
65+
# Mercury relief grids
66+
"@mercury_relief_01d_g",
67+
"@N00W030.mercury_relief_01m_p.nc", # Specific grid for 01m test
6568
# Moon relief grids
6669
"@moon_relief_01d_g",
6770
"@N00W030.moon_relief_01m_p.nc", # Specific grid for 01m test
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Test basic functionality for loading Mercury relief datasets.
3+
"""
4+
import numpy as np
5+
import numpy.testing as npt
6+
from pygmt.datasets import load_mercury_relief
7+
8+
9+
def test_mercury_relief_01d():
10+
"""
11+
Test some properties of the Mercury relief 01d data.
12+
"""
13+
data = load_mercury_relief(resolution="01d")
14+
assert data.name == "mercury_relief"
15+
assert data.attrs["units"] == "meters"
16+
assert data.attrs["long_name"] == "USGS Mercury relief"
17+
assert data.shape == (181, 361)
18+
assert data.gmt.registration == 0
19+
npt.assert_allclose(data.lat, np.arange(-90, 91, 1))
20+
npt.assert_allclose(data.lon, np.arange(-180, 181, 1))
21+
npt.assert_allclose(data.min(), -5103.5, atol=0.5)
22+
npt.assert_allclose(data.max(), 3416.0, atol=0.5)
23+
24+
25+
def test_mercury_relief_01d_with_region():
26+
"""
27+
Test loading low-resolution Mercury relief with 'region'.
28+
"""
29+
data = load_mercury_relief(resolution="01d", region=[-10, 10, -5, 5])
30+
assert data.shape == (11, 21)
31+
assert data.gmt.registration == 0
32+
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
33+
npt.assert_allclose(data.lon, np.arange(-10, 11, 1))
34+
npt.assert_allclose(data.min(), -327.0, atol=0.5)
35+
npt.assert_allclose(data.max(), 3027.5, atol=0.5)
36+
37+
38+
def test_mercury_relief_01m_default_registration():
39+
"""
40+
Test that the grid returned by default for the 1 arc-minute resolution has
41+
a "gridline" registration.
42+
"""
43+
data = load_mercury_relief(resolution="01m", region=[-10, -9, 3, 5])
44+
assert data.shape == (121, 61)
45+
assert data.gmt.registration == 0
46+
assert data.coords["lat"].data.min() == 3.0
47+
assert data.coords["lat"].data.max() == 5.0
48+
assert data.coords["lon"].data.min() == -10.0
49+
assert data.coords["lon"].data.max() == -9.0
50+
npt.assert_allclose(data.min(), 1953.0, atol=0.5)
51+
npt.assert_allclose(data.max(), 3721.0, atol=0.5)

0 commit comments

Comments
 (0)