Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load_earth_magnetic_anomaly function for Earth magnetic anomaly dataset #2196

Merged
merged 21 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9de5e07
add earth_magnetic_anomaly.py
willschlitzer Nov 19, 2022
6de890d
add test_datasets_earth_magnetic_anomaly.py
willschlitzer Nov 19, 2022
f099e4a
Merge branch 'main' into load-remote-dataset/magnetic-anomaly
willschlitzer Dec 5, 2022
aeebb6a
Apply suggestions from code review
willschlitzer Dec 6, 2022
9d41121
Merge branch 'main' into load-remote-dataset/magnetic-anomaly
willschlitzer Dec 6, 2022
0ac7afb
add magnetic anomaly to index.rst
willschlitzer Dec 6, 2022
0a22439
integrate load_remote_dataset.py into earth_magnetic_anomaly.py
willschlitzer Dec 6, 2022
666e1b7
update test
willschlitzer Dec 6, 2022
d297910
Apply suggestions from code review
willschlitzer Dec 6, 2022
b815b8b
Merge branch 'main' into load-remote-dataset/magnetic-anomaly
willschlitzer Dec 6, 2022
078c175
move dataset_name to pass the string directly to _load_remote_dataset()
willschlitzer Dec 7, 2022
4bd9d75
Merge branch 'main' into load-remote-dataset/magnetic-anomaly
willschlitzer Dec 7, 2022
3e8fe66
change test title to match loaded resolution
willschlitzer Dec 7, 2022
9d73a2e
Merge remote-tracking branch 'origin/load-remote-dataset/magnetic-ano…
willschlitzer Dec 7, 2022
952d34c
update test to use 05m resolution
willschlitzer Dec 7, 2022
0e1417b
add cache files to testing.py
willschlitzer Dec 7, 2022
f8f6b41
uncomment "pull request"
willschlitzer Dec 7, 2022
4e0e2ae
recomment "pull request"
willschlitzer Dec 7, 2022
4edfb7b
Update .github/workflows/cache_data.yaml
willschlitzer Dec 8, 2022
a44a8c4
Merge branch 'main' into load-remote-dataset/magnetic-anomaly
willschlitzer Dec 8, 2022
4df2865
Merge branch 'load-remote-dataset/magnetic-anomaly' of github.com:Gen…
willschlitzer Dec 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ and store them in GMT's user data directory.

datasets.list_sample_data
datasets.load_earth_age
datasets.load_earth_magnetic_anomaly
datasets.load_earth_relief
datasets.load_sample_data

Expand Down
1 change: 1 addition & 0 deletions pygmt/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Load sample data included with GMT (downloaded from the GMT cache server).

from pygmt.datasets.earth_age import load_earth_age
from pygmt.datasets.earth_magnetic_anomaly import load_earth_magnetic_anomaly
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved
from pygmt.datasets.earth_relief import load_earth_relief
from pygmt.datasets.samples import (
list_sample_data,
Expand Down
69 changes: 69 additions & 0 deletions pygmt/datasets/earth_magnetic_anomaly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
Function to download the Earth magnetic anomaly datasets from the GMT data
server, and load as :class:`xarray.DataArray`.

The grids are available in various resolutions.
"""
from pygmt.datasets.load_remote_dataset import _load_remote_dataset
from pygmt.helpers import kwargs_to_strings


@kwargs_to_strings(region="sequence")
def load_earth_magnetic_anomaly(resolution="01d", region=None, registration=None):
r"""
Load an Earth magnetic anomaly grid in various resolutions.

The grids are downloaded to a user data directory
(usually ``~/.gmt/server/earth/earth_mag/``) the first time you invoke
this function. Afterwards, it will load the grid from the data directory.
So you'll need an internet connection the first time around.

These grids can also be accessed by passing in the file name
**@earth_mag**\_\ *res*\[_\ *reg*] to any grid plotting/processing
function. *res* is the grid resolution (see below), and *reg* is grid
registration type (**p** for pixel registration or **g** for gridline
registration).

Refer to :gmt-datasets:`earth-mag.html` for more details.

Parameters
----------
resolution : str
The grid resolution. The suffix ``d`` and ``m`` stand for
arc-degree and arc-minute. It can be ``"01d"``, ``"30m"``,
``"20m"``, ``"15m"``, ``"10m"``, ``"06m"``, ``"05m"``, ``"04m"``,
``"03m"``, or ``"02m"``.

region : str or list
The subregion of the grid to load, in the forms of a list
[*xmin*, *xmax*, *ymin*, *ymax*] or a string *xmin/xmax/ymin/ymax*.
Required for grids with resolutions higher than 5
arc-minute (i.e., ``"05m"``).

registration : str
Grid registration type. Either ``"pixel"`` for pixel registration or
``"gridline"`` for gridline registration. Default is ``None``, where
a pixel-registered grid is returned unless only the
gridline-registered grid is available.

Returns
-------
grid : :class:`xarray.DataArray`
The Earth magnetic anomaly grid. Coordinates are latitude and
longitude in degrees. Units are in nano Teslas (nT).

Note
----
The :class:`xarray.DataArray` grid doesn't support slice operation, for
Earth magnetic anomaly with resolutions of 5 arc-minutes or higher,
which are stored as smaller tiles.
"""
dataset_prefix = "earth_mag_"
grid = _load_remote_dataset(
dataset_name="earth_magnetic_anomaly",
dataset_prefix=dataset_prefix,
resolution=resolution,
region=region,
registration=registration,
)
return grid
19 changes: 19 additions & 0 deletions pygmt/datasets/load_remote_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ class GMTRemoteDataset(NamedTuple):
"01m": Resolution(["gridline"], True),
},
),
"earth_magnetic_anomaly": GMTRemoteDataset(
title="Earth magnetic anomaly",
name="magnetic_anomaly",
long_name="Earth magnetic anomaly",
units="nT",
extra_attributes={"horizontal_datum": "WGS84"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert in magnetic anomalies, but having a horizontal_datum seems strange. Is the magnetic anomaly referenced to the geoid somehow?

Also, not related to this PR, but I notice that horizontal_datum is also set as an attribute for the earth_age dataset at L96. Is seafloor crustal age also measured relative to a datum?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comment #2200 (comment).

I think horizontal_datum means the coordinate system for longitude/latitude.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, the datum in GMT is mainly related to the ellipsoid and its positioning. As you can see from gmt mapproject -Qd, the positioning of the ellipsoid is given as the difference in three directions between the center of the ellipsoid and the center of the earth. As to earth_age, I think it does not have height information, horizontal_datum is enough. I don't konw much about magnetic anomalies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to leave horizontal_datum in; per my understanding (and from the comments above) it is the system that aligns coordinates with the actual locations on Earth. So I assume it still applies to the magnetic anomaly map?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, leave it in. Thanks for the clarification everyone!

resolutions={
"01d": Resolution(["pixel", "gridline"], False),
"30m": Resolution(["pixel", "gridline"], False),
"20m": Resolution(["pixel", "gridline"], False),
"15m": Resolution(["pixel", "gridline"], False),
"10m": Resolution(["pixel", "gridline"], False),
"06m": Resolution(["pixel", "gridline"], False),
"05m": Resolution(["pixel", "gridline"], True),
"04m": Resolution(["pixel", "gridline"], True),
"03m": Resolution(["pixel", "gridline"], True),
"02m": Resolution(["pixel"], True),
},
),
}


Expand Down
80 changes: 80 additions & 0 deletions pygmt/tests/test_datasets_earth_magnetic_anomaly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Test basic functionality for loading Earth magnetic anomaly datasets.
"""
import numpy as np
import numpy.testing as npt
import pytest
from pygmt.datasets import load_earth_magnetic_anomaly
from pygmt.exceptions import GMTInvalidInput


def test_earth_mag_fails():
"""
Make sure earth_magnetic_anomaly fails for invalid resolutions.
"""
resolutions = "1m 1d bla 60d 001m 03".split()
resolutions.append(60)
for resolution in resolutions:
with pytest.raises(GMTInvalidInput):
load_earth_magnetic_anomaly(resolution=resolution)


def test_earth_mag_incorrect_registration():
"""
Test loading earth_magnetic_anomaly with incorrect registration type.
"""
with pytest.raises(GMTInvalidInput):
load_earth_magnetic_anomaly(registration="improper_type")


def test_earth_mag_01d():
"""
Test some properties of the magnetic anomaly 01d data.
"""
data = load_earth_magnetic_anomaly(resolution="01d", registration="gridline")
assert data.name == "magnetic_anomaly"
assert data.attrs["long_name"] == "Earth magnetic anomaly"
assert data.attrs["units"] == "nT"
assert data.attrs["horizontal_datum"] == "WGS84"
assert data.shape == (181, 361)
npt.assert_allclose(data.lat, np.arange(-90, 91, 1))
npt.assert_allclose(data.lon, np.arange(-180, 181, 1))
npt.assert_allclose(data.min(), -384)
npt.assert_allclose(data.max(), 1057.2)


def test_earth_mag_01d_with_region():
"""
Test loading low-resolution earth magnetic anomaly with 'region'.
"""
data = load_earth_magnetic_anomaly(
resolution="01d", region=[-10, 10, -5, 5], registration="gridline"
)
assert data.shape == (11, 21)
npt.assert_allclose(data.lat, np.arange(-5, 6, 1))
npt.assert_allclose(data.lon, np.arange(-10, 11, 1))
npt.assert_allclose(data.min(), -180.40002)
npt.assert_allclose(data.max(), 127.39996)


def test_earth_mag_03m_with_region():
seisman marked this conversation as resolved.
Show resolved Hide resolved
"""
Test loading a subregion of high-resolution earth magnetic anomaly data.
"""
data = load_earth_magnetic_anomaly(
resolution="03m", region=[-115, -112, 4, 6], registration="gridline"
)
assert data.shape == (41, 61)
npt.assert_allclose(data.lat, np.arange(4, 6.01, 0.05))
npt.assert_allclose(data.lon, np.arange(-115, -111.99, 0.05))
npt.assert_allclose(data.data.min(), -193)
npt.assert_allclose(data.data.max(), 110)


def test_earth_mag_05m_without_region():
"""
Test loading high-resolution earth magnetic anomaly without passing
'region'.
"""
with pytest.raises(GMTInvalidInput):
load_earth_magnetic_anomaly("05m")