-
Notifications
You must be signed in to change notification settings - Fork 219
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 load Black Marble dataset #3469
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
43c40dc
Implement load_black_marble
weiji14 ef0f5c9
Add entry for datasets.load_black_marble in doc/api/index.rst
weiji14 f11decc
Add unit tests for load_black_marble
weiji14 66183ed
Import rioxarray to register rio accessor
weiji14 61a5b44
Add @earth_day_01d to pygmt/helpers/caching.py
weiji14 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
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,102 @@ | ||
""" | ||
Function to download the NASA Black Marble image datasets from the GMT data server, and | ||
load as :class:`xarray.DataArray`. | ||
|
||
The images are available in various resolutions. | ||
""" | ||
|
||
import contextlib | ||
from collections.abc import Sequence | ||
from typing import Literal | ||
|
||
import xarray as xr | ||
from pygmt.datasets.load_remote_dataset import _load_remote_dataset | ||
|
||
with contextlib.suppress(ImportError): | ||
# rioxarray is needed to register the rio accessor | ||
import rioxarray # noqa: F401 | ||
|
||
__doctest_skip__ = ["load_black_marble"] | ||
|
||
|
||
def load_black_marble( | ||
resolution: Literal[ | ||
"01d", | ||
"30m", | ||
"20m", | ||
"15m", | ||
"10m", | ||
"06m", | ||
"05m", | ||
"04m", | ||
"03m", | ||
"02m", | ||
"01m", | ||
"30s", | ||
] = "01d", | ||
region: Sequence[float] | str | None = None, | ||
) -> xr.DataArray: | ||
r""" | ||
Load NASA Black Marble images in various resolutions. | ||
|
||
.. figure:: https://www.generic-mapping-tools.org/remote-datasets/_images/GMT_earth_daynight.jpg | ||
:width: 80% | ||
:align: center | ||
|
||
Earth day/night dataset. | ||
|
||
The images are downloaded to a user data directory (usually | ||
``~/.gmt/server/earth/earth_night/``) the first time you invoke this function. | ||
Afterwards, it will load the image from the data directory. So you'll need an | ||
internet connection the first time around. | ||
|
||
These images can also be accessed by passing in the file name | ||
**@earth_night**\_\ *res* to any image processing function or plotting method. *res* | ||
is the image resolution (see below). | ||
|
||
Refer to :gmt-datasets:`earth-daynight.html` for more details about available | ||
datasets, including version information and references. | ||
|
||
Parameters | ||
---------- | ||
resolution | ||
The image resolution. The suffix ``d``, ``m``, and ``s`` stand for arc-degree, | ||
arc-minute, and arc-second. | ||
|
||
region | ||
The subregion of the image to load, in the form of a sequence [*xmin*, *xmax*, | ||
*ymin*, *ymax*]. | ||
|
||
Returns | ||
------- | ||
image | ||
The NASA Black Marble image. Coordinates are latitude and longitude in degrees. | ||
|
||
Note | ||
---- | ||
The registration and coordinate system type of the returned | ||
:class:`xarray.DataArray` image can be accessed via the GMT accessors (i.e., | ||
``image.gmt.registration`` and ``image.gmt.gtype`` respectively). However, these | ||
properties may be lost after specific image operations (such as slicing) and will | ||
need to be manually set before passing the image to any PyGMT data processing or | ||
plotting functions. Refer to :class:`pygmt.GMTDataArrayAccessor` for detailed | ||
explanations and workarounds. | ||
|
||
Examples | ||
-------- | ||
|
||
>>> from pygmt.datasets import load_black_marble | ||
>>> # load the default image (pixel-registered 1 arc-degree image) | ||
>>> image = load_black_marble() | ||
""" | ||
image = _load_remote_dataset( | ||
name="earth_night", | ||
prefix="earth_night", | ||
resolution=resolution, | ||
region=region, | ||
registration="pixel", | ||
) | ||
# If rioxarray is installed, set the coordinate reference system | ||
if hasattr(image, "rio"): | ||
image = image.rio.write_crs(input_crs="OGC:CRS84") | ||
return image |
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,41 @@ | ||
""" | ||
Test basic functionality for loading Black Marble datasets. | ||
""" | ||
|
||
import numpy as np | ||
import numpy.testing as npt | ||
from pygmt.datasets import load_black_marble | ||
|
||
|
||
def test_black_marble_01d(): | ||
""" | ||
Test some properties of the Black Marble 01d data. | ||
""" | ||
data = load_black_marble(resolution="01d") | ||
assert data.name == "z" | ||
assert data.long_name == "black_marble" | ||
assert data.attrs["horizontal_datum"] == "WGS84" | ||
assert data.attrs["description"] == "NASA Night Images" | ||
assert data.shape == (3, 180, 360) | ||
assert data.dtype == "uint8" | ||
assert data.gmt.registration == 1 | ||
assert data.gmt.gtype == 1 | ||
npt.assert_allclose(data.y, np.arange(89.5, -90.5, -1)) | ||
npt.assert_allclose(data.x, np.arange(-179.5, 180.5, 1)) | ||
npt.assert_allclose(data.min(), 3, atol=1) | ||
npt.assert_allclose(data.max(), 174, atol=1) | ||
|
||
|
||
def test_black_marble_01d_with_region(): | ||
""" | ||
Test loading low-resolution Black Marble with 'region'. | ||
""" | ||
data = load_black_marble(resolution="01d", region=[-10, 10, -5, 5]) | ||
assert data.shape == (3, 10, 20) | ||
assert data.dtype == "uint8" | ||
assert data.gmt.registration == 1 | ||
assert data.gmt.gtype == 1 | ||
npt.assert_allclose(data.y, np.arange(4.5, -5.5, -1)) | ||
npt.assert_allclose(data.x, np.arange(-9.5, 10.5, 1)) | ||
npt.assert_allclose(data.min(), 3, atol=1) | ||
npt.assert_allclose(data.max(), 40, atol=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 line is not covered by a unit test. Should I add one, or we can ignore it?
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.
It should be covered when rioxarray is installed, right?
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.
Or do we have to import rioxarray to register the rio accessor
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.
The equivalent line in
load_blue_marble
is covered here intest_grdimage_image.py
which has animport rioxarray
at the top of the file:pygmt/pygmt/tests/test_grdimage_image.py
Line 18 in 66f258a
We don't test
load_black_marble
anywhere else except intest_datasets_earth_night.py
, and sincerioxarray
is not imported in that file, this line isn't covered because there is norio
accessor.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.
Maybe we can cover this line in the
test_grdcut_image.py
file at https://github.com/GenericMappingTools/pygmt/pull/3115/files#r1780370539?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 is not ideal. So even if rioxarray is installed, the
rio
accessor may still not exist because users don't import the rioxarray package.I think we should add the following lines to this file (and any other files that try to access the
rio
accessor):pygmt/pygmt/datasets/tile_map.py
Lines 21 to 23 in 66f258a
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.
xref: #3323
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.
Ok, done at commit 66183ed. I've also applied this to
pygmt/datasets/earth_day.py
, hopefully you don't mind.