Skip to content

Commit 16919ad

Browse files
Remote datasets: Add "load_earth_mean_sea_surface" to load "CNES Earth Mean Sea Surface" dataset (#3717)
1 parent 874b6a4 commit 16919ad

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ and store them in GMT's user data directory.
239239
datasets.load_earth_magnetic_anomaly
240240
datasets.load_earth_mask
241241
datasets.load_earth_mean_dynamic_topography
242+
datasets.load_earth_mean_sea_surface
242243
datasets.load_earth_relief
243244
datasets.load_earth_vertical_gravity_gradient
244245
datasets.load_mars_relief

pygmt/datasets/__init__.py

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

pygmt/datasets/load_remote_dataset.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,24 @@ class GMTRemoteDataset(NamedTuple):
227227
"15s": Resolution("15s"),
228228
},
229229
),
230+
"earth_mss": GMTRemoteDataset(
231+
description="CNES Earth mean sea surface",
232+
units="meters",
233+
extra_attributes={"horizontal_datum": "WGS84"},
234+
resolutions={
235+
"01d": Resolution("01d"),
236+
"30m": Resolution("30m"),
237+
"20m": Resolution("20m"),
238+
"15m": Resolution("15m"),
239+
"10m": Resolution("10m"),
240+
"06m": Resolution("06m"),
241+
"05m": Resolution("05m", tiled=True),
242+
"04m": Resolution("04m", tiled=True),
243+
"03m": Resolution("03m", tiled=True),
244+
"02m": Resolution("02m", tiled=True),
245+
"01m": Resolution("01m", tiled=True, registrations=["gridline"]),
246+
},
247+
),
230248
"earth_night": GMTRemoteDataset(
231249
description="NASA Night Images",
232250
units=None,

pygmt/helpers/caching.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def cache_data():
2525
"@earth_mask_01d_g",
2626
"@earth_mdt_01d_g",
2727
"@earth_mdt_07m_g",
28+
"@earth_mss_01d_g",
2829
"@earth_night_01d",
2930
"@earth_relief_01d_g",
3031
"@earth_relief_01d_p",
@@ -53,6 +54,7 @@ def cache_data():
5354
"@N00W030.earth_geoid_01m_g.nc",
5455
"@S30W060.earth_mag_02m_p.nc",
5556
"@S30W120.earth_mag4km_02m_p.nc",
57+
"@N30E090.earth_mss_01m_g.nc",
5658
"@N00W090.earth_relief_03m_p.nc",
5759
"@N00E135.earth_relief_30s_g.nc",
5860
"@N00W010.earth_relief_15s_p.nc",
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Test basic functionality for loading Earth mean sea surface datasets.
3+
"""
4+
5+
import numpy as np
6+
import numpy.testing as npt
7+
from pygmt.datasets import load_earth_mean_sea_surface
8+
9+
10+
def test_earth_mss_01d():
11+
"""
12+
Test some properties of the Earth mean sea surface 01d data.
13+
"""
14+
data = load_earth_mean_sea_surface(resolution="01d")
15+
assert data.name == "z"
16+
assert data.attrs["description"] == "CNES Earth mean sea surface"
17+
assert data.attrs["units"] == "meters"
18+
assert data.attrs["horizontal_datum"] == "WGS84"
19+
assert data.shape == (181, 361)
20+
assert data.gmt.registration == 0
21+
npt.assert_allclose(data.lat, np.arange(-90, 91, 1))
22+
npt.assert_allclose(data.lon, np.arange(-180, 181, 1))
23+
npt.assert_allclose(data.min(), -104.71, atol=0.01)
24+
npt.assert_allclose(data.max(), 82.38, atol=0.01)
25+
26+
27+
def test_earth_mss_01d_with_region():
28+
"""
29+
Test loading low-resolution Earth mean sea surface with "region".
30+
"""
31+
data = load_earth_mean_sea_surface(resolution="01d", region=[-10, 10, -5, 5])
32+
assert data.shape == (11, 21)
33+
assert data.gmt.registration == 0
34+
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
35+
npt.assert_allclose(data.lon, np.arange(-10, 11, 1))
36+
npt.assert_allclose(data.min(), 6.53, atol=0.01)
37+
npt.assert_allclose(data.max(), 29.31, atol=0.01)
38+
39+
40+
def test_earth_mss_01m_default_registration():
41+
"""
42+
Test that the grid returned by default for the 1 arc-minute resolution has a
43+
"gridline" registration.
44+
"""
45+
data = load_earth_mean_sea_surface(resolution="01m", region=[-10, -9, 3, 5])
46+
assert data.shape == (121, 61)
47+
assert data.gmt.registration == 0
48+
assert data.coords["lat"].data.min() == 3.0
49+
assert data.coords["lat"].data.max() == 5.0
50+
assert data.coords["lon"].data.min() == -10.0
51+
assert data.coords["lon"].data.max() == -9.0
52+
npt.assert_allclose(data.min(), 21.27, atol=0.01)
53+
npt.assert_allclose(data.max(), 31.11, atol=0.01)

0 commit comments

Comments
 (0)