forked from GenericMappingTools/pygmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add load_earth_geoid function for Earth Geoid dataset (GenericMapping…
…Tools#2236) Co-authored-by: Dongdong Tian <[email protected]>
- Loading branch information
1 parent
262f1a3
commit fa37285
Showing
6 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Function to download the EGM2008 Global Earth Geoid 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_geoid(resolution="01d", region=None, registration=None): | ||
r""" | ||
Load the EGM2008 Global Earth Geoid in various resolutions. | ||
The grids are downloaded to a user data directory | ||
(usually ``~/.gmt/server/earth/earth_geoid/``) 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_geoid**\_\ *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-geoid.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"``, ``"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 geoid grid. Coordinates are latitude and | ||
longitude in degrees. Units are in meters. | ||
Note | ||
---- | ||
The :class:`xarray.DataArray` grid doesn't support slice operation, for | ||
Earth geoid grids with resolutions of 5 arc-minutes or higher, | ||
which are stored as smaller tiles. | ||
""" | ||
grid = _load_remote_dataset( | ||
dataset_name="earth_geoid", | ||
dataset_prefix="earth_geoid_", | ||
resolution=resolution, | ||
region=region, | ||
registration=registration, | ||
) | ||
return grid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
Test basic functionality for loading Earth geoid datasets. | ||
""" | ||
import numpy as np | ||
import numpy.testing as npt | ||
import pytest | ||
from pygmt.datasets import load_earth_geoid | ||
from pygmt.exceptions import GMTInvalidInput | ||
|
||
|
||
def test_earth_geoid_fails(): | ||
""" | ||
Make sure load_earth_geoid 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_geoid(resolution=resolution) | ||
|
||
|
||
def test_earth_geoid_incorrect_registration(): | ||
""" | ||
Test loading load_earth_geoid with incorrect registration type. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
load_earth_geoid(registration="improper_type") | ||
|
||
|
||
def test_earth_geoid_01d(): | ||
""" | ||
Test some properties of the earth geoid 01d data. | ||
""" | ||
data = load_earth_geoid(resolution="01d", registration="gridline") | ||
assert data.name == "earth_geoid" | ||
assert data.attrs["units"] == "m" | ||
assert data.attrs["long_name"] == "EGM2008 Global Earth Geoid" | ||
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(), -106.45) | ||
npt.assert_allclose(data.max(), 83.619995) | ||
|
||
|
||
def test_earth_geoid_01d_with_region(): | ||
""" | ||
Test loading low-resolution earth geoid with 'region'. | ||
""" | ||
data = load_earth_geoid( | ||
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(), 4.87) | ||
npt.assert_allclose(data.max(), 29.89) | ||
|
||
|
||
def test_earth_geoid_05m_with_region(): | ||
""" | ||
Test loading a subregion of high-resolution earth geoid. | ||
""" | ||
data = load_earth_geoid( | ||
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.min(), -32.79) | ||
npt.assert_allclose(data.max(), 16.57) | ||
assert data.sizes["lat"] == 121 | ||
assert data.sizes["lon"] == 121 | ||
|
||
|
||
def test_earth_geoid_05m_without_region(): | ||
""" | ||
Test loading high-resolution earth geoid without passing 'region'. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
load_earth_geoid("05m") | ||
|
||
|
||
def test_earth_geoid_incorrect_resolution_registration(): | ||
""" | ||
Test that an error is raised when trying to load a grid registration with | ||
an unavailable resolution. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
load_earth_geoid(resolution="01m", region=[0, 1, 3, 5], registration="pixel") |