-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to load Blue Marble dataset (#2235)
Function to load the `@earth_day_` NASA Blue Marble mosaic RGB images stored as GeoTIFF files. --------- Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
- Loading branch information
1 parent
f97c3a4
commit 66f258a
Showing
6 changed files
with
171 additions
and
11 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,97 @@ | ||
""" | ||
Function to download the NASA Blue Marble image datasets from the GMT data server, and | ||
load as :class:`xarray.DataArray`. | ||
The images are available in various resolutions. | ||
""" | ||
|
||
from collections.abc import Sequence | ||
from typing import Literal | ||
|
||
import xarray as xr | ||
from pygmt.datasets.load_remote_dataset import _load_remote_dataset | ||
|
||
__doctest_skip__ = ["load_blue_marble"] | ||
|
||
|
||
def load_blue_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 Blue 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_day/``) 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_day**\_\ *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 Blue 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_blue_marble | ||
>>> # load the default image (pixel-registered 1 arc-degree image) | ||
>>> image = load_blue_marble() | ||
""" | ||
image = _load_remote_dataset( | ||
name="earth_day", | ||
prefix="earth_day", | ||
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 Blue Marble datasets. | ||
""" | ||
|
||
import numpy as np | ||
import numpy.testing as npt | ||
from pygmt.datasets import load_blue_marble | ||
|
||
|
||
def test_blue_marble_01d(): | ||
""" | ||
Test some properties of the Blue Marble 01d data. | ||
""" | ||
data = load_blue_marble(resolution="01d") | ||
assert data.name == "z" | ||
assert data.long_name == "blue_marble" | ||
assert data.attrs["horizontal_datum"] == "WGS84" | ||
assert data.attrs["description"] == "NASA Day 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(), 10, atol=1) | ||
npt.assert_allclose(data.max(), 255, atol=1) | ||
|
||
|
||
def test_blue_marble_01d_with_region(): | ||
""" | ||
Test loading low-resolution Blue Marble with 'region'. | ||
""" | ||
data = load_blue_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(), 10, atol=1) | ||
npt.assert_allclose(data.max(), 77, atol=1) |
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