Skip to content

Commit 5748429

Browse files
willschlitzerseismanweiji14actions-bot
authored
Add WDMAM dataset to load_earth_magnetic_anomaly (#2241)
Add the World Digital Magnetic Anomaly Map (WDMAM) dataset as a data_source in the `load_earth_magnetic_anomaly` function. Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]> Co-authored-by: actions-bot <[email protected]>
1 parent 16fc565 commit 5748429

File tree

4 files changed

+176
-23
lines changed

4 files changed

+176
-23
lines changed

pygmt/datasets/earth_magnetic_anomaly.py

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,36 @@
55
The grids are available in various resolutions.
66
"""
77
from pygmt.datasets.load_remote_dataset import _load_remote_dataset
8+
from pygmt.exceptions import GMTInvalidInput
89
from pygmt.helpers import kwargs_to_strings
910

1011
__doctest_skip__ = ["load_earth_magnetic_anomaly"]
1112

1213

1314
@kwargs_to_strings(region="sequence")
1415
def load_earth_magnetic_anomaly(
15-
resolution="01d", region=None, registration=None, mag4km=False
16+
resolution="01d", region=None, registration=None, data_source="emag2"
1617
):
1718
r"""
1819
Load an Earth magnetic anomaly grid in various resolutions.
1920
2021
The grids are downloaded to a user data directory
21-
(usually ``~/.gmt/server/earth/earth_mag/`` or
22-
``~/.gmt/server/earth/earth_mag4km/``) the first time you invoke
22+
(usually ``~/.gmt/server/earth/earth_mag/``,
23+
``~/.gmt/server/earth/earth_mag4km/``,
24+
or ``~/.gmt/server/earth/earth_wdmam/``) the first time you invoke
2325
this function. Afterwards, it will load the grid from the data directory.
2426
So you'll need an internet connection the first time around.
2527
2628
These grids can also be accessed by passing in the file name
2729
**@**\ *earth_mag_type*\_\ *res*\[_\ *reg*] to any grid plotting/processing
2830
function. *earth_mag_type* is the GMT name
29-
for the dataset. The available options are **earth_mag** and
30-
**earth_mag4km**. *res* is the grid resolution (see below), and *reg* is
31-
grid registration type (**p** for pixel registration or **g** for gridline
32-
registration).
31+
for the dataset. The available options are **earth_mag**,
32+
**earth_mag4km**, and **earth_wdmam**. *res* is the grid resolution
33+
(see below), and *reg* is grid registration type (**p** for pixel
34+
registration or **g** for gridline registration).
3335
34-
Refer to :gmt-datasets:`earth-mag.html` for more details.
36+
Refer to :gmt-datasets:`earth-mag.html`
37+
and :gmt-datasets:`earth-wdmam.html` for more details.
3538
3639
Parameters
3740
----------
@@ -52,12 +55,21 @@ def load_earth_magnetic_anomaly(
5255
``"gridline"`` for gridline registration. Default is ``"gridline"``
5356
for all resolutions except ``"02m"`` which is ``"pixel"`` only.
5457
55-
mag4km : bool
56-
Choose the data version to use. The default is ``False``, which is
57-
observed at sea level over oceanic regions and has no data over land.
58-
Set ``mag4km`` to ``True`` to use a version where all observations
59-
are relative to an altitude of 4 km above the geoid and include data
60-
over land.
58+
data_source : str
59+
Select the source of the magnetic anomaly data.
60+
61+
Available options:
62+
63+
- **emag2** : EMAG2 Global Earth Magnetic Anomaly Model [Default
64+
option]. Only includes data is observed from sea level over
65+
oceanic regions. See :gmt-datasets:`earth-mag.html`.
66+
67+
- **emag2_4km** : Use a version of EMAG2 where all observations
68+
are relative to an altitude of 4 km above the geoid and include
69+
data over land.
70+
71+
- **wdmam** : World Digital Magnetic Anomaly Map (WDMAM).
72+
See :gmt-datasets:`earth-wdmam.html`
6173
6274
Returns
6375
-------
@@ -87,14 +99,32 @@ def load_earth_magnetic_anomaly(
8799
... region=[120, 160, 30, 60],
88100
... registration="gridline",
89101
... )
90-
>>> # load the 20 arc-minutes grid of the mag4km dataset
102+
>>> # load the 20 arc-minutes grid of the emag2_4km dataset
103+
>>> grid = load_earth_magnetic_anomaly(
104+
... resolution="20m", registration="gridline", data_source="emag2_4km"
105+
... )
106+
>>> # load the 20 arc-minutes grid of the WDMAM dataset
91107
>>> grid = load_earth_magnetic_anomaly(
92-
... resolution="20m", registration="gridline", mag4km=True
108+
... resolution="20m", registration="gridline", data_source="wdmam"
93109
... )
94110
"""
95-
dataset_prefix = "earth_mag4km_" if mag4km is True else "earth_mag_"
111+
magnetic_anomaly_sources = {
112+
"emag2": "earth_mag_",
113+
"emag2_4km": "earth_mag4km_",
114+
"wdmam": "earth_wdmam_",
115+
}
116+
if data_source not in magnetic_anomaly_sources:
117+
raise GMTInvalidInput(
118+
f"Invalid earth magnetic anomaly 'data_source' {data_source}, "
119+
"valid values are 'emag2', 'emag2_4km', and 'wdmam'."
120+
)
121+
dataset_prefix = magnetic_anomaly_sources[data_source]
122+
if data_source == "wdmam":
123+
dataset_name = "earth_wdmam"
124+
else:
125+
dataset_name = "earth_magnetic_anomaly"
96126
grid = _load_remote_dataset(
97-
dataset_name="earth_magnetic_anomaly",
127+
dataset_name=dataset_name,
98128
dataset_prefix=dataset_prefix,
99129
resolution=resolution,
100130
region=region,

pygmt/datasets/load_remote_dataset.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,24 @@ class GMTRemoteDataset(NamedTuple):
187187
"01m": Resolution(["pixel"], True),
188188
},
189189
),
190+
"earth_wdmam": GMTRemoteDataset(
191+
title="WDMAM magnetic anomaly",
192+
name="wdmam",
193+
long_name="World Digital Magnetic Anomaly Map",
194+
units="nT",
195+
extra_attributes={"horizontal_datum": "WGS84"},
196+
resolutions={
197+
"01d": Resolution(["gridline", "pixel"], False),
198+
"30m": Resolution(["gridline", "pixel"], False),
199+
"20m": Resolution(["gridline", "pixel"], False),
200+
"15m": Resolution(["gridline", "pixel"], False),
201+
"10m": Resolution(["gridline", "pixel"], False),
202+
"06m": Resolution(["gridline", "pixel"], False),
203+
"05m": Resolution(["gridline", "pixel"], True),
204+
"04m": Resolution(["gridline", "pixel"], True),
205+
"03m": Resolution(["gridline"], True),
206+
},
207+
),
190208
}
191209

192210

pygmt/helpers/testing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ def download_test_data():
195195
# Earth vertical gravity gradient grids
196196
"@earth_vgg_01d_g",
197197
"@N00W030.earth_vgg_01m_p.nc", # Specific grid for 01m test
198+
# Earth WDMAM grids
199+
"@earth_wdmam_01d_g",
200+
"@S90E000.earth_wdmam_03m_g.nc", # Specific grid for 03m test
198201
# Other cache files
199202
"@capitals.gmt",
200203
"@earth_relief_20m_holes.grd",

pygmt/tests/test_datasets_earth_magnetic_anomaly.py

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,23 @@ def test_earth_mag_02m_without_region():
6868

6969
def test_earth_mag_incorrect_resolution_registration():
7070
"""
71-
Test that an error is raised when trying to load a grid registration with
72-
an unavailable resolution.
71+
Test that an error is raised when trying to load a EMAG2 grid registration
72+
with an unavailable resolution.
7373
"""
7474
with pytest.raises(GMTInvalidInput):
7575
load_earth_magnetic_anomaly(
76-
resolution="02m", region=[0, 1, 3, 5], registration="gridline", mag4km=False
76+
resolution="02m",
77+
region=[0, 1, 3, 5],
78+
registration="gridline",
79+
data_source="emag2_4km",
7780
)
7881

7982

8083
def test_earth_mag4km_01d():
8184
"""
8285
Test some properties of the magnetic anomaly 4km 01d data.
8386
"""
84-
data = load_earth_magnetic_anomaly(resolution="01d", mag4km=True)
87+
data = load_earth_magnetic_anomaly(resolution="01d", data_source="emag2_4km")
8588
assert data.name == "magnetic_anomaly"
8689
assert data.attrs["long_name"] == "Earth magnetic anomaly"
8790
assert data.attrs["units"] == "nT"
@@ -102,7 +105,7 @@ def test_earth_mag4km_01d_with_region():
102105
resolution="01d",
103106
region=[-10, 10, -5, 5],
104107
registration="gridline",
105-
mag4km=True,
108+
data_source="emag2_4km",
106109
)
107110
assert data.shape == (11, 21)
108111
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
@@ -125,3 +128,102 @@ def test_earth_mag_02m_default_registration():
125128
npt.assert_allclose(data.coords["lon"].data.max(), -9.01666667)
126129
npt.assert_allclose(data.min(), -231)
127130
npt.assert_allclose(data.max(), 131.79999)
131+
132+
data = load_earth_magnetic_anomaly(
133+
resolution="05m",
134+
region=[-115, -112, 4, 6],
135+
registration="gridline",
136+
data_source="emag2_4km",
137+
)
138+
assert data.shape == (25, 37)
139+
assert data.lat.min() == 4
140+
assert data.lat.max() == 6
141+
assert data.lon.min() == -115
142+
assert data.lon.max() == -112
143+
npt.assert_allclose(data.min(), -128.40015)
144+
npt.assert_allclose(data.max(), 76.80005)
145+
146+
147+
def test_earth_mag_01d_wdmam():
148+
"""
149+
Test some properties of the WDMAM 01d data.
150+
"""
151+
data = load_earth_magnetic_anomaly(
152+
resolution="01d", registration="gridline", data_source="wdmam"
153+
)
154+
assert data.name == "wdmam"
155+
assert data.attrs["long_name"] == "World Digital Magnetic Anomaly Map"
156+
assert data.attrs["units"] == "nT"
157+
assert data.attrs["horizontal_datum"] == "WGS84"
158+
assert data.shape == (181, 361)
159+
npt.assert_allclose(data.lat, np.arange(-90, 91, 1))
160+
npt.assert_allclose(data.lon, np.arange(-180, 181, 1))
161+
npt.assert_allclose(data.min(), -773.5)
162+
npt.assert_allclose(data.max(), 1751.3)
163+
164+
165+
def test_earth_mag_01d_wdmam_with_region():
166+
"""
167+
Test loading low-resolution WDMAM grid with 'region'.
168+
"""
169+
data = load_earth_magnetic_anomaly(
170+
resolution="01d",
171+
region=[-10, 10, -5, 5],
172+
registration="gridline",
173+
data_source="wdmam",
174+
)
175+
assert data.shape == (11, 21)
176+
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
177+
npt.assert_allclose(data.lon, np.arange(-10, 11, 1))
178+
npt.assert_allclose(data.min(), -103.900024)
179+
npt.assert_allclose(data.max(), 102.19995)
180+
181+
182+
def test_earth_mag_03m_wdmam_with_region():
183+
"""
184+
Test loading a subregion of high-resolution WDMAM data.
185+
"""
186+
data = load_earth_magnetic_anomaly(
187+
resolution="03m", region=[10, 13, -60, -58], data_source="wdmam"
188+
)
189+
assert data.gmt.registration == 0
190+
assert data.shape == (41, 61)
191+
assert data.lat.min() == -60
192+
assert data.lat.max() == -58
193+
assert data.lon.min() == 10
194+
assert data.lon.max() == 13
195+
npt.assert_allclose(data.min(), -639.7001)
196+
npt.assert_allclose(data.max(), 629.6)
197+
198+
199+
def test_earth_mag_05m_wdmam_without_region():
200+
"""
201+
Test loading a high-resolution WDMAM grid without passing 'region'.
202+
"""
203+
with pytest.raises(GMTInvalidInput):
204+
load_earth_magnetic_anomaly(
205+
resolution="05m", registration="gridline", data_source="wdmam"
206+
)
207+
208+
209+
def test_earth_mag_wdmam_incorrect_resolution_registration():
210+
"""
211+
Test that an error is raised when trying to load a WDMAM grid registration
212+
with an unavailable resolution.
213+
"""
214+
with pytest.raises(GMTInvalidInput):
215+
load_earth_magnetic_anomaly(
216+
resolution="03m",
217+
region=[0, 1, 3, 5],
218+
registration="pixel",
219+
data_source="wdmam",
220+
)
221+
222+
223+
def test_earth_mag_data_source_error():
224+
"""
225+
Test that an error is raised when an invalid argument is passed to
226+
'data_source'.
227+
"""
228+
with pytest.raises(GMTInvalidInput):
229+
load_earth_magnetic_anomaly(resolution="01d", data_source="invalid")

0 commit comments

Comments
 (0)