Skip to content

Commit 230ff31

Browse files
Remote datasets: Add "uncertainty" parameter to "load_earth_free_air_anomaly" to load the "free-air anomaly uncertainty" dataset (#3727)
1 parent 2c0f75f commit 230ff31

File tree

4 files changed

+122
-42
lines changed

4 files changed

+122
-42
lines changed

pygmt/datasets/earth_free_air_anomaly.py

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
Function to download the IGPP Earth free-air anomaly dataset from the GMT data server,
3-
and load as :class:`xarray.DataArray`.
2+
Function to download the IGPP Earth free-air anomaly and uncertainty datasets from
3+
the GMT data server, and load as :class:`xarray.DataArray`.
44
55
The grids are available in various resolutions.
66
"""
@@ -20,36 +20,43 @@ def load_earth_free_air_anomaly(
2020
] = "01d",
2121
region: Sequence[float] | str | None = None,
2222
registration: Literal["gridline", "pixel", None] = None,
23+
uncertainty: bool = False,
2324
) -> xr.DataArray:
2425
r"""
25-
Load the IGPP Earth free-air anomaly dataset in various resolutions.
26+
Load the IGPP Earth free-air anomaly and uncertainty datasets in various
27+
resolutions.
2628
27-
.. figure:: https://www.generic-mapping-tools.org/remote-datasets/_images/GMT_earth_faa.jpg
28-
:width: 80 %
29-
:align: center
29+
.. list-table::
30+
:widths: 50 50
31+
:header-rows: 1
3032
31-
IGPP Earth free-air anomaly dataset.
33+
* - IGPP Earth free-Air anomaly
34+
- IGPP Earth free-Air anomaly uncertainty
35+
* - .. figure:: https://www.generic-mapping-tools.org/remote-datasets/_images/GMT_earth_faa.jpg
36+
- .. figure:: https://www.generic-mapping-tools.org/remote-datasets/_images/GMT_earth_faaerror.jpg
3237
33-
The grids are downloaded to a user data directory
34-
(usually ``~/.gmt/server/earth/earth_faa/``) the first time you invoke
35-
this function. Afterwards, it will load the grid from the data directory.
36-
So you'll need an internet connection the first time around.
38+
The grids are downloaded to a user data directory (usually
39+
``~/.gmt/server/earth/earth_faa/`` or ``~/.gmt/server/earth/earth_faaerror/``) the
40+
first time you invoke this function. Afterwards, it will load the grid from data
41+
directory. So you'll need an internet connection the first time around.
3742
3843
These grids can also be accessed by passing in the file name
39-
**@earth_faa**\_\ *res*\[_\ *reg*] to any grid processing function or
40-
plotting method. *res* is the grid resolution (see below), and *reg* is
41-
the grid registration type (**p** for pixel registration or **g** for
42-
gridline registration).
43-
44-
The default color palette table (CPT) for this dataset is *@earth_faa.cpt*.
45-
It's implicitly used when passing in the file name of the dataset to any
46-
grid plotting method if no CPT is explicitly specified. When the dataset
47-
is loaded and plotted as an :class:`xarray.DataArray` object, the default
48-
CPT is ignored, and GMT's default CPT (*turbo*) is used. To use the
49-
dataset-specific CPT, you need to explicitly set ``cmap="@earth_faa.cpt"``.
50-
51-
Refer to :gmt-datasets:`earth-faa.html` for more details about available
52-
datasets, including version information and references.
44+
**@earth_faa_type**\_\ *res*\[_\ *reg*] to any grid processing function or
45+
plotting method. *earth_faa_type* is the GMT name for the dataset. The available
46+
options are **earth_faa** and **earth_faaerror**. *res* is the grid resolution (see
47+
below), and *reg* is the grid registration type (**p** for pixel registration or
48+
**g** for gridline registration).
49+
50+
The default color palette tables (CPTs) for these datasets are *@earth_faa.cpt* and
51+
*@earth_faaerror.cpt*. The dataset-specific CPT is implicitly used when passing in
52+
the file name of the dataset to any grid plotting method if no CPT is explicitly
53+
specified. When the dataset is loaded and plotted as an :class:`xarray.DataArray`
54+
object, the default CPT is ignored, and GMT's default CPT (*turbo*) is used. To use
55+
the dataset-specific CPT, you need to explicitly set ``cmap="@earth_faa.cpt"`` or
56+
``cmap="@earth_faaerror.cpt"``.
57+
58+
Refer to :gmt-datasets:`earth-faa.html` and :gmt-datasets:`earth-faaerror.html` for
59+
more details about available datasets, including version information and references.
5360
5461
Parameters
5562
----------
@@ -62,45 +69,47 @@ def load_earth_free_air_anomaly(
6269
higher than 5 arc-minutes (i.e., ``"05m"``).
6370
registration
6471
Grid registration type. Either ``"pixel"`` for pixel registration or
65-
``"gridline"`` for gridline registration. Default is ``None``, means
66-
``"gridline"`` for all resolutions except ``"01m"`` which is
67-
``"pixel"`` only.
72+
``"gridline"`` for gridline registration. Default is ``None`` which means
73+
``"gridline"`` for all resolutions except ``"01m"`` which is ``"pixel"`` only.
74+
uncertainty
75+
By default, the Earth free-air anomaly values are returned. Set to ``True`` to
76+
return the related uncertainties instead.
6877
6978
Returns
7079
-------
7180
grid
72-
The Earth free-air anomaly grid. Coordinates are latitude and
73-
longitude in degrees. Units are in mGal.
81+
The Earth free-air anomaly (uncertainty) grid. Coordinates are latitude and
82+
longitude in degrees. Values and uncertainties are in mGal.
7483
7584
Note
7685
----
7786
The registration and coordinate system type of the returned
78-
:class:`xarray.DataArray` grid can be accessed via the GMT accessors
79-
(i.e., ``grid.gmt.registration`` and ``grid.gmt.gtype`` respectively).
80-
However, these properties may be lost after specific grid operations (such
81-
as slicing) and will need to be manually set before passing the grid to any
82-
PyGMT data processing or plotting functions. Refer to
83-
:class:`pygmt.GMTDataArrayAccessor` for detailed explanations and
84-
workarounds.
87+
:class:`xarray.DataArray` grid can be accessed via the GMT accessors (i.e.,
88+
``grid.gmt.registration`` and ``grid.gmt.gtype`` respectively). However, these
89+
properties may be lost after specific grid operations (such as slicing) and will
90+
need to be manually set before passing the grid to any PyGMT data processing or
91+
plotting functions. Refer to :class:`pygmt.GMTDataArrayAccessor` for detailed
92+
explanations and workarounds.
8593
8694
Examples
8795
--------
8896
8997
>>> from pygmt.datasets import load_earth_free_air_anomaly
9098
>>> # load the default grid (gridline-registered 1 arc-degree grid)
9199
>>> grid = load_earth_free_air_anomaly()
100+
>>> # load the uncertainties related to the default grid
101+
>>> grid = load_earth_free_air_anomaly(uncertainty=True)
92102
>>> # load the 30 arc-minutes grid with "gridline" registration
93103
>>> grid = load_earth_free_air_anomaly(resolution="30m", registration="gridline")
94104
>>> # load high-resolution (5 arc-minutes) grid for a specific region
95105
>>> grid = load_earth_free_air_anomaly(
96-
... resolution="05m",
97-
... region=[120, 160, 30, 60],
98-
... registration="gridline",
106+
... resolution="05m", region=[120, 160, 30, 60], registration="gridline"
99107
... )
100108
"""
109+
prefix = "earth_faaerror" if uncertainty is True else "earth_faa"
101110
grid = _load_remote_dataset(
102-
name="earth_faa",
103-
prefix="earth_faa",
111+
name=prefix,
112+
prefix=prefix,
104113
resolution=resolution,
105114
region=region,
106115
registration=registration,

pygmt/datasets/load_remote_dataset.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ class GMTRemoteDataset(NamedTuple):
128128
"01m": Resolution("01m", registrations=["pixel"], tiled=True),
129129
},
130130
),
131+
"earth_faaerror": GMTRemoteDataset(
132+
description="IGPP Earth free-air anomaly errors",
133+
units="mGal",
134+
extra_attributes={"horizontal_datum": "WGS84"},
135+
resolutions={
136+
"01d": Resolution("01d"),
137+
"30m": Resolution("30m"),
138+
"20m": Resolution("20m"),
139+
"15m": Resolution("15m"),
140+
"10m": Resolution("10m"),
141+
"06m": Resolution("06m"),
142+
"05m": Resolution("05m", tiled=True),
143+
"04m": Resolution("04m", tiled=True),
144+
"03m": Resolution("03m", tiled=True),
145+
"02m": Resolution("02m", tiled=True),
146+
"01m": Resolution("01m", registrations=["pixel"], tiled=True),
147+
},
148+
),
131149
"earth_gebco": GMTRemoteDataset(
132150
description="GEBCO Earth relief",
133151
units="meters",

pygmt/helpers/caching.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def cache_data():
1616
"@earth_day_01d",
1717
"@earth_dist_01d",
1818
"@earth_faa_01d_g",
19+
"@earth_faaerror_01d_g",
1920
"@earth_gebco_01d_g",
2021
"@earth_gebcosi_01d_g",
2122
"@earth_gebcosi_15m_p",
@@ -51,6 +52,7 @@ def cache_data():
5152
"@N30E090.earth_age_01m_g.nc",
5253
"@N00W030.earth_dist_01m_g.nc",
5354
"@N00W030.earth_faa_01m_p.nc",
55+
"@N00W030.earth_faaerror_01m_p.nc",
5456
"@N00W030.earth_geoid_01m_g.nc",
5557
"@S30W060.earth_mag_02m_p.nc",
5658
"@S30W120.earth_mag4km_02m_p.nc",

pygmt/tests/test_datasets_earth_free_air_anomaly.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,54 @@ def test_earth_faa_01m_default_registration():
5252
npt.assert_allclose(data.coords["lon"].data.max(), -9.00833333)
5353
npt.assert_allclose(data.min(), -49.225, atol=0.025)
5454
npt.assert_allclose(data.max(), 115.0, atol=0.025)
55+
56+
57+
def test_earth_faaerror_01d():
58+
"""
59+
Test some properties of the free air anomaly error 01d data.
60+
"""
61+
data = load_earth_free_air_anomaly(resolution="01d", uncertainty=True)
62+
assert data.name == "z"
63+
assert data.attrs["long_name"] == "faaerror (mGal)"
64+
assert data.attrs["description"] == "IGPP Earth free-air anomaly errors"
65+
assert data.attrs["units"] == "mGal"
66+
assert data.attrs["horizontal_datum"] == "WGS84"
67+
assert data.shape == (181, 361)
68+
assert data.gmt.registration == 0
69+
npt.assert_allclose(data.lat, np.arange(-90, 91, 1))
70+
npt.assert_allclose(data.lon, np.arange(-180, 181, 1))
71+
npt.assert_allclose(data.min(), 0.0, atol=0.04)
72+
npt.assert_allclose(data.max(), 49.16, atol=0.04)
73+
74+
75+
def test_earth_faaerror_01d_with_region():
76+
"""
77+
Test loading low-resolution earth free air anomaly error with 'region'.
78+
"""
79+
data = load_earth_free_air_anomaly(
80+
resolution="01d", region=[-10, 10, -5, 5], uncertainty=True
81+
)
82+
assert data.shape == (11, 21)
83+
assert data.gmt.registration == 0
84+
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
85+
npt.assert_allclose(data.lon, np.arange(-10, 11, 1))
86+
npt.assert_allclose(data.min(), 0.72, atol=0.04)
87+
npt.assert_allclose(data.max(), 21.04, atol=0.04)
88+
89+
90+
def test_earth_faaerror_01m_default_registration():
91+
"""
92+
Test that the grid returned by default for the 1 arc-minute resolution has a "pixel"
93+
registration.
94+
"""
95+
data = load_earth_free_air_anomaly(
96+
resolution="01m", region=[-10, -9, 3, 5], uncertainty=True
97+
)
98+
assert data.shape == (120, 60)
99+
assert data.gmt.registration == 1
100+
npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333)
101+
npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666)
102+
npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666)
103+
npt.assert_allclose(data.coords["lon"].data.max(), -9.00833333)
104+
npt.assert_allclose(data.min(), 0.40, atol=0.04)
105+
npt.assert_allclose(data.max(), 13.36, atol=0.04)

0 commit comments

Comments
 (0)