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 function to import seafloor crustal age dataset #1471

Merged
merged 18 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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 @@ -159,6 +159,7 @@ and store them in the GMT cache folder.
datasets.load_japan_quakes
datasets.load_ocean_ridge_points
datasets.load_sample_bathymetry
datasets.load_seafloor_crust_age
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
datasets.load_usgs_quakes
datasets.load_fractures_compilation
datasets.load_hotspots
Expand Down
1 change: 1 addition & 0 deletions pygmt/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
load_sample_bathymetry,
load_usgs_quakes,
)
from pygmt.datasets.seafloor_crust_age import load_seafloor_crust_age
113 changes: 113 additions & 0 deletions pygmt/datasets/seafloor_crust_age.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
Function to download the Earth seafloor age datasets from the GMT data server,
and load as :class:`xarray.DataArray`.

The grids are available in various resolutions.
"""
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import kwargs_to_strings
from pygmt.io import load_dataarray
from pygmt.src import grdcut, which


@kwargs_to_strings(region="sequence")
def load_seafloor_crust_age(resolution="01d", region=None, registration=None):
r"""
Load Earth seafloor crustal ages in various resolutions.

The grids are downloaded to a user data directory
(usually ``~/.gmt/server/earth/earth_age/``) 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_age**\_\ *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-docs:`datasets/remote-data.html#global-earth-seafloor-crustal-age-grids`
for more details.

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

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 seafloor crustal age grid. Coordinates are latitude and
longitude in degrees. Age is in millions of years (Myr).

Notes
-----
The :class:`xarray.DataArray` grid doesn't support slice operation, for
Earth seafloor crustal age with resolutions of 5 arc-minutes or higher,
which are stored as smaller tiles.
"""

# earth seafloor crust age data stored as single grids for low resolutions
non_tiled_resolutions = ["01d", "30m", "20m", "15m", "10m", "06m"]
# earth seafloor crust age data stored as tiles for high resolutions
tiled_resolutions = ["05m", "04m", "03m", "02m", "01m"]

if registration in ("pixel", "gridline", None):
# If None, let GMT decide on Pixel/Gridline type
reg = f"_{registration[0]}" if registration else ""
else:
raise GMTInvalidInput(
f"Invalid grid registration: '{registration}', should be either "
"'pixel', 'gridline' or None. Default is None, where a "
"pixel-registered grid is returned unless only the "
"gridline-registered grid is available."
)

if resolution not in non_tiled_resolutions + tiled_resolutions:
raise GMTInvalidInput(f"Invalid Earth relief resolution '{resolution}'.")

# Choose earth relief data prefix
earth_age_prefix = "earth_age_"

# different ways to load tiled and non-tiled earth relief data
# Known issue: tiled grids don't support slice operation
# See https:/GenericMappingTools/pygmt/issues/524
if region is None:
if resolution not in non_tiled_resolutions:
raise GMTInvalidInput(
f"'region' is required for Earth age resolution '{resolution}'."
)
fname = which(f"@earth_age_{resolution}{reg}", download="a")
grid = load_dataarray(fname, engine="netcdf4")
else:
grid = grdcut(f"@{earth_age_prefix}{resolution}{reg}", region=region)

# Add some metadata to the grid
grid.name = "seafloor_age"
grid.attrs["long_name"] = "age of seafloor crust"
grid.attrs["units"] = "Myr"
grid.attrs["vertical_datum"] = "EMG96"
grid.attrs["horizontal_datum"] = "WGS84"
# Remove the actual range because it gets outdated when indexing the grid,
# which causes problems when exporting it to netCDF for usage on the
# command-line.
grid.attrs.pop("actual_range")
for coord in grid.coords:
grid[coord].attrs.pop("actual_range")
return grid
3 changes: 3 additions & 0 deletions pygmt/helpers/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def download_test_data():
# The naming scheme may change. DO NOT USE IT IN YOUR SCRIPTS.
"@N35E135.earth_relief_03s_g.nc",
"@N00W090.earth_relief_03m_p.nc",
# Earth seafloor age grids
"@earth_age_01d_g",
"S90W180.earth_age_05m_g.nc" # Specific grid for 05m test
# Other cache files
"@fractures_06.txt",
"@hotspots.txt",
Expand Down
78 changes: 78 additions & 0 deletions pygmt/tests/test_datasets_seafloor_crust_age.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Test basic functionality for loading Earth seafloor crust age datasets.
"""
import numpy as np
import numpy.testing as npt
import pytest
from pygmt.datasets import load_seafloor_crust_age
from pygmt.exceptions import GMTInvalidInput


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


def test_seafloor_crust_age_incorrect_registration():
"""
Test loading seafloor_curst_age with incorrect registration type.
"""
with pytest.raises(GMTInvalidInput):
load_seafloor_crust_age(registration="improper_type")


def test_seafloor_crust_age_01d():
"""
Test some properties of the seafloor crust age 01d data.
"""
data = load_seafloor_crust_age(resolution="01d", registration="gridline")
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(), 0.167381, rtol=1e-5)
npt.assert_allclose(data.max(), 338.0274, rtol=1e-5)


def test_seafloor_crust_age_01d_with_region():
"""
Test loading low-resolution seafloor crust age with 'region'.
"""
data = load_seafloor_crust_age(
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(), 11.293945)
npt.assert_allclose(data.max(), 125.1189)


def test_seafloor_crust_age_05m_with_region():
"""
Test loading a subregion of high-resolution seafloor crust age.
"""
data = load_seafloor_crust_age(
resolution="05m", region=[-50, -40, 20, 30], registration="gridline"
)
assert data.coords["lat"].data.min() == 20.0
assert data.coords["lat"].data.max() == 30.0
assert data.coords["lon"].data.min() == -50.0
assert data.coords["lon"].data.max() == -40.0
npt.assert_allclose(data.data.min(), 0.040000916)
npt.assert_allclose(data.data.max(), 46.530003)
assert data.sizes["lat"] == 121
assert data.sizes["lon"] == 121


def test_seafloor_crust_age_05m_without_region():
"""
Test loading high-resolution seafloor crust age without passing 'region'.
"""
with pytest.raises(GMTInvalidInput):
load_seafloor_crust_age("05m")