Skip to content

Commit f7ad824

Browse files
Add load_moon_relief to load Moon relief dataset in various resolutions and registrations (#2674)
Co-authored-by: Yvonne Fröhlich <[email protected]>
1 parent fcc756b commit f7ad824

File tree

6 files changed

+183
-0
lines changed

6 files changed

+183
-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_moon_relief
230231
datasets.load_sample_data
231232

232233
In addition, there is also a special function to load XYZ tile maps via

pygmt/datasets/__init__.py

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

pygmt/datasets/load_remote_dataset.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,29 @@ class GMTRemoteDataset(NamedTuple):
249249
"12s": Resolution("12s", registrations=["pixel"], tiled=True),
250250
},
251251
),
252+
"moon_relief": GMTRemoteDataset(
253+
title="Moon relief",
254+
name="moon_relief",
255+
long_name="USGS Moon (LOLA) relief",
256+
units="meters",
257+
extra_attributes={},
258+
resolutions={
259+
"01d": Resolution("01d"),
260+
"30m": Resolution("30m"),
261+
"20m": Resolution("20m"),
262+
"15m": Resolution("15m"),
263+
"10m": Resolution("10m"),
264+
"06m": Resolution("06m"),
265+
"05m": Resolution("05m", tiled=True),
266+
"04m": Resolution("04m", tiled=True),
267+
"03m": Resolution("03m", tiled=True),
268+
"02m": Resolution("02m", tiled=True),
269+
"01m": Resolution("01m", tiled=True),
270+
"30s": Resolution("30s", tiled=True),
271+
"15s": Resolution("15s", tiled=True),
272+
"14s": Resolution("14s", registrations=["pixel"], tiled=True),
273+
},
274+
),
252275
}
253276

254277

pygmt/datasets/moon_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 Moon 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_moon_relief"]
13+
14+
15+
@kwargs_to_strings(region="sequence")
16+
def load_moon_relief(
17+
resolution="01d",
18+
region=None,
19+
registration: Literal["gridline", "pixel", None] = None,
20+
):
21+
r"""
22+
Load the Moon relief dataset in various resolutions.
23+
24+
.. figure:: https://www.generic-mapping-tools.org/remote-datasets/_images/GMT_moon_relief.jpg
25+
:width: 80%
26+
:align: center
27+
28+
Moon relief dataset.
29+
30+
The grids are downloaded to a user data directory (usually
31+
``~/.gmt/server/moon/moon_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+
**@moon_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 *@moon_relief.cpt*. It's
41+
implicitly used when passing in the file name of the dataset to any grid plotting
42+
method if no CPT is explicitly specified. When the dataset is loaded and plotted as
43+
an :class:`xarray.DataArray` object, the default CPT is ignored, and GMT's default
44+
CPT (*turbo*) is used. To use the dataset-specific CPT, you need to explicitly set
45+
``cmap="@moon_relief.cpt"``.
46+
47+
Refer to :gmt-datasets:`moon-relief.html` for more details about available datasets,
48+
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"``, ``"30s"``, ``"15s"``, and ``"14s"``.
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 ``"14s"`` which is ``"pixel"``
65+
only.
66+
67+
Returns
68+
-------
69+
grid : :class:`xarray.DataArray`
70+
The Moon relief grid. Coordinates are latitude and longitude in degrees. Relief
71+
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_moon_relief
86+
>>> # load the default grid (gridline-registered 1 arc-degree grid)
87+
>>> grid = load_moon_relief()
88+
>>> # load the 30 arc-minutes grid with "gridline" registration
89+
>>> grid = load_moon_relief(resolution="30m", registration="gridline")
90+
>>> # load high-resolution (5 arc-minutes) grid for a specific region
91+
>>> grid = load_moon_relief(
92+
... resolution="05m",
93+
... region=[120, 160, 30, 60],
94+
... registration="gridline",
95+
... )
96+
"""
97+
grid = _load_remote_dataset(
98+
dataset_name="moon_relief",
99+
dataset_prefix="moon_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+
# Moon relief grids
66+
"@moon_relief_01d_g",
67+
"@N00W030.moon_relief_01m_p.nc", # Specific grid for 01m test
6568
# Other cache files
6669
"@capitals.gmt",
6770
"@circuit.png",
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 Moon relief datasets.
3+
"""
4+
import numpy as np
5+
import numpy.testing as npt
6+
from pygmt.datasets import load_moon_relief
7+
8+
9+
def test_moon_relief_01d():
10+
"""
11+
Test some properties of the Moon relief 01d data.
12+
"""
13+
data = load_moon_relief(resolution="01d")
14+
assert data.name == "moon_relief"
15+
assert data.attrs["units"] == "meters"
16+
assert data.attrs["long_name"] == "USGS Moon (LOLA) 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(), -7669.0, atol=0.5)
22+
npt.assert_allclose(data.max(), 8507.5, atol=0.5)
23+
24+
25+
def test_moon_relief_01d_with_region():
26+
"""
27+
Test loading low-resolution Moon relief with 'region'.
28+
"""
29+
data = load_moon_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(), -1122.0, atol=0.5)
35+
npt.assert_allclose(data.max(), 943.0, atol=0.5)
36+
37+
38+
def test_moon_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_moon_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(), -1840.5, atol=0.5)
51+
npt.assert_allclose(data.max(), -235.0, atol=0.5)

0 commit comments

Comments
 (0)