-
Notifications
You must be signed in to change notification settings - Fork 224
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_mask function for GSHHG Global Earth Mask dataset #2310
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c02e7d2
add earth_mask.py
willschlitzer 38c99e4
add test_datasets_earth_mask.py
willschlitzer 5378e8f
add Earth mask cache file
willschlitzer 95357a0
apply code suggestions
willschlitzer 887ab22
Apply suggestions from code review
willschlitzer fd4146e
Merge branch 'main' into load-remote-dataset/earth_mask
willschlitzer b0fa1a6
Change units to 'None'
willschlitzer d4fcbaf
Merge branch 'main' into load-remote-dataset/earth_mask
willschlitzer 52b2eba
Update pygmt/datasets/earth_mask.py
willschlitzer 39603dc
Merge branch 'main' into load-remote-dataset/earth_mask
willschlitzer a55e6bd
change dtype in grid xarray to int8, add test
willschlitzer a4608a9
formatting fix
willschlitzer d024299
Merge branch 'main' into load-remote-dataset/earth_mask
willschlitzer dd6c885
Apply suggestions from code review
willschlitzer dacd982
add pylint disable to too-many-branches
willschlitzer 898d8dd
run make format
willschlitzer d63295a
disable pylint for correct file
willschlitzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 GSHHG Global Earth Mask 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 | ||
|
||
__doctest_skip__ = ["load_earth_mask"] | ||
|
||
|
||
@kwargs_to_strings(region="sequence") | ||
def load_earth_mask(resolution="01d", region=None, registration=None): | ||
r""" | ||
Load the GSHHG Global Earth Mask in various resolutions. | ||
|
||
The grids are downloaded to a user data directory | ||
(usually ``~/.gmt/server/earth/earth_mask/``) 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_mask**\_\ *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-mask.html` for more details. | ||
|
||
Parameters | ||
---------- | ||
resolution : str | ||
The grid resolution. The suffix ``d``, ``m``, and ``s`` stand for | ||
arc-degrees, arc-minutes, and arc-seconds. It can be ``"01d"``, | ||
``"30m"``, ``"20m"``, ``"15m"``, ``"10m"``, ``"06m"``, ``"05m"``, | ||
``"04m"``, ``"03m"``, ``"02m"``, ``"01m"``, ``"30s"``, or ``"15s"``. | ||
|
||
region : str or list | ||
The subregion of the grid to load, in the form of a list | ||
[*xmin*, *xmax*, *ymin*, *ymax*] or a string *xmin/xmax/ymin/ymax*. | ||
|
||
registration : str | ||
Grid registration type. Either ``"pixel"`` for pixel registration or | ||
``"gridline"`` for gridline registration. Default is ``"gridline"``. | ||
|
||
Returns | ||
------- | ||
grid : :class:`xarray.DataArray` | ||
The Earth mask grid. Coordinates are latitude and | ||
longitude in degrees. The node values in the mask grids are all in | ||
the 0-4 range and reflect different surface types: | ||
|
||
- 0: Oceanic areas beyond the shoreline | ||
- 1: Land areas inside the shoreline | ||
- 2: Lakes inside the land areas | ||
- 3: Islands in lakes in the land areas | ||
- 4: Smaller lakes in islands that are found within lakes | ||
inside the land area | ||
""" | ||
grid = _load_remote_dataset( | ||
dataset_name="earth_mask", | ||
dataset_prefix="earth_mask_", | ||
resolution=resolution, | ||
region=region, | ||
registration=registration, | ||
) | ||
return grid.astype("int8") |
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,57 @@ | ||
""" | ||
Test basic functionality for loading Earth mask datasets. | ||
""" | ||
import numpy as np | ||
import numpy.testing as npt | ||
import pytest | ||
from pygmt.datasets import load_earth_mask | ||
from pygmt.exceptions import GMTInvalidInput | ||
|
||
|
||
def test_earth_mask_fails(): | ||
""" | ||
Make sure load_earth_mask 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_mask(resolution=resolution) | ||
|
||
|
||
def test_earth_mask_incorrect_registration(): | ||
""" | ||
Test loading load_earth_mask with incorrect registration type. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
load_earth_mask(registration="improper_type") | ||
|
||
|
||
def test_earth_mask_01d(): | ||
""" | ||
Test some properties of the Earth mask 01d data. | ||
""" | ||
data = load_earth_mask(resolution="01d") | ||
assert data.name == "earth_mask" | ||
assert data.attrs["long_name"] == "Mask of land and water features" | ||
assert data.attrs["horizontal_datum"] == "WGS84" | ||
assert data.shape == (181, 361) | ||
assert data.gmt.registration == 0 | ||
assert data.dtype == "int8" | ||
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) | ||
npt.assert_allclose(data.max(), 2) | ||
npt.assert_allclose(data[36, 45], 0) | ||
|
||
|
||
def test_earth_mask_01d_with_region(): | ||
""" | ||
Test loading low-resolution Earth mask with 'region'. | ||
""" | ||
data = load_earth_mask(resolution="01d", region=[-7, 4, 13, 19]) | ||
assert data.shape == (7, 12) | ||
assert data.gmt.registration == 0 | ||
npt.assert_allclose(data.lat, np.arange(13, 20, 1)) | ||
npt.assert_allclose(data.lon, np.arange(-7, 5, 1)) | ||
npt.assert_allclose(data[1, 5], 1) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function now returns a dataarray like below:
The data dtype is float32, but the original grid
@earth_mask_01d_g
contains int8 values. Ideally, the function should return a grid with integer values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would I change the value types across the entire array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. Need to read the xarray docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likely we need to use xarray.DataArray.astype to convert the data from
float32
toint8
, i.e.,return grid.astype("int8")
.