-
Notifications
You must be signed in to change notification settings - Fork 235
Add function to load Blue Marble dataset #2235
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
Changes from 29 commits
9bde7f7
d8c8759
c6bd235
093fc26
2f29458
1c99f21
e839ef7
aa8a474
cd4cab6
fc060ea
c3017d5
c3e2e66
a2a18a9
7c38754
5c65097
767d7c7
a8600d3
971ec11
6eabbf4
4ef65b0
54184fb
e6c2964
19a7755
01d1b80
0ca210a
28d668d
a6a08e3
f13d12b
471c6e8
e886408
5b6bb42
755275e
f7e381e
9a5e36b
a0fa29b
70f6041
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| """ | ||
| Function to download the NASA Blue Marble image datasets from the GMT data | ||
| server, and load as :class:`xarray.DataArray`. | ||
|
|
||
| The grids 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*]. Required for images with resolutions higher than 5 arc-minutes | ||
| (i.e., ``"05m"``). | ||
|
|
||
| Returns | ||
| ------- | ||
| image | ||
| The NASA Blue Marble image. Coordinates are latitude and longitude in degrees. | ||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """ | ||
| Test basic functionality for loading Blue and Black Marble datasets. | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import numpy.testing as npt | ||
| import pytest | ||
| from pygmt.datasets import load_blue_marble | ||
|
|
||
| rioxarray = pytest.importorskip("rioxarray") | ||
|
|
||
|
|
||
| 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) | ||
|
|
||
|
|
||
| def test_blue_marble_01m_default_registration(): | ||
| """ | ||
| Test that the grid returned by default for the 1 arc-minute resolution has | ||
| a "pixel" registration. | ||
| """ | ||
| data = load_blue_marble(resolution="01m", region=[-10, -9, 3, 5]) | ||
|
||
| assert data.shape == (3, 120, 60) | ||
| assert data.dtype == "uint8" | ||
| assert data.gmt.registration == 1 | ||
| assert data.gmt.gtype == 1 | ||
| npt.assert_allclose(data.coords["y"].data.min(), 3.0083333333333333) | ||
| npt.assert_allclose(data.coords["y"].data.max(), 4.991666666666666) | ||
| npt.assert_allclose(data.coords["x"].data.min(), -9.991666666666667) | ||
| npt.assert_allclose(data.coords["x"].data.max(), -9.008333333333335) | ||
| npt.assert_allclose(data.min(), 10, atol=1) | ||
| npt.assert_allclose(data.max(), 79, atol=1) | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -3,7 +3,8 @@ | |||
| """ | ||||
|
|
||||
| import pytest | ||||
| from pygmt import Figure, which | ||||
| from pygmt import Figure | ||||
| from pygmt.datasets import load_blue_marble | ||||
|
|
||||
| rioxarray = pytest.importorskip("rioxarray") | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this line be removed?
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet, because we are still using |
||||
|
|
||||
|
|
@@ -14,12 +15,9 @@ def fixture_xr_image(): | |||
| Load the image data from Blue Marble as an xarray.DataArray with shape {"band": 3, | ||||
| "y": 180, "x": 360}. | ||||
| """ | ||||
| geotiff = which(fname="@earth_day_01d_p", download="c") | ||||
| with rioxarray.open_rasterio(filename=geotiff) as rda: | ||||
| if len(rda.band) == 3: | ||||
| xr_image = rda.load() | ||||
| assert xr_image.sizes == {"band": 3, "y": 180, "x": 360} | ||||
| return xr_image | ||||
| xr_image = load_blue_marble(resolution="01d") | ||||
| assert xr_image.sizes == {"band": 3, "y": 180, "x": 360} | ||||
| return xr_image | ||||
|
|
||||
|
|
||||
| @pytest.mark.mpl_image_compare | ||||
|
|
||||
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.
Are you going to support earth_night in this function?
Uh oh!
There was an error while loading. Please reload this page.
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'll open a separate PR for
load_black_marbleafter this one is done.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.
Sorry for the confusion. I meant whether you plan to add the
load_black_marblefunction in this filepygmt/datasets/earth_daynight.pyor haveload_blue_marble/load_black_marblein two separate files.Uh oh!
There was an error while loading. Please reload this page.
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'd put them in the same file, and in
pygmt/datasets/__init__.py, have a line like:Does that sound ok?
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 prefer to having them in separate files. The two files are almost identical, except the differences in the dataset name.
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, renamed in 5b6bb42.