GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add function to load Black Marble dataset (#3469) · GenericMappingTools/pygmt@923111a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • index.rst
  • pygmt
    • datasets
      • __init__.py
      • earth_day.py
      • earth_night.py
      • load_remote_dataset.py
    • helpers
      • caching.py
    • tests
      • test_datasets_earth_night.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/index.rst‎

Copy file name to clipboardExpand all lines: doc/api/index.rst
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ and store them in GMT's user data directory.
218218
:toctree: generated
219219

220220
datasets.list_sample_data
221+
datasets.load_black_marble
221222
datasets.load_blue_marble
222223
datasets.load_earth_age
223224
datasets.load_earth_free_air_anomaly
Collapse file

‎pygmt/datasets/__init__.py‎

Copy file name to clipboardExpand all lines: pygmt/datasets/__init__.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pygmt.datasets.earth_geoid import load_earth_geoid
1111
from pygmt.datasets.earth_magnetic_anomaly import load_earth_magnetic_anomaly
1212
from pygmt.datasets.earth_mask import load_earth_mask
13+
from pygmt.datasets.earth_night import load_black_marble
1314
from pygmt.datasets.earth_relief import load_earth_relief
1415
from pygmt.datasets.earth_vertical_gravity_gradient import (
1516
load_earth_vertical_gravity_gradient,
Collapse file

‎pygmt/datasets/earth_day.py‎

Copy file name to clipboardExpand all lines: pygmt/datasets/earth_day.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
The images are available in various resolutions.
66
"""
77

8+
import contextlib
89
from collections.abc import Sequence
910
from typing import Literal
1011

1112
import xarray as xr
1213
from pygmt.datasets.load_remote_dataset import _load_remote_dataset
1314

15+
with contextlib.suppress(ImportError):
16+
# rioxarray is needed to register the rio accessor
17+
import rioxarray # noqa: F401
18+
1419
__doctest_skip__ = ["load_blue_marble"]
1520

1621

Collapse file

‎pygmt/datasets/earth_night.py‎

Copy file name to clipboard
+102Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Function to download the NASA Black Marble image datasets from the GMT data server, and
3+
load as :class:`xarray.DataArray`.
4+
5+
The images are available in various resolutions.
6+
"""
7+
8+
import contextlib
9+
from collections.abc import Sequence
10+
from typing import Literal
11+
12+
import xarray as xr
13+
from pygmt.datasets.load_remote_dataset import _load_remote_dataset
14+
15+
with contextlib.suppress(ImportError):
16+
# rioxarray is needed to register the rio accessor
17+
import rioxarray # noqa: F401
18+
19+
__doctest_skip__ = ["load_black_marble"]
20+
21+
22+
def load_black_marble(
23+
resolution: Literal[
24+
"01d",
25+
"30m",
26+
"20m",
27+
"15m",
28+
"10m",
29+
"06m",
30+
"05m",
31+
"04m",
32+
"03m",
33+
"02m",
34+
"01m",
35+
"30s",
36+
] = "01d",
37+
region: Sequence[float] | str | None = None,
38+
) -> xr.DataArray:
39+
r"""
40+
Load NASA Black Marble images in various resolutions.
41+
42+
.. figure:: https://www.generic-mapping-tools.org/remote-datasets/_images/GMT_earth_daynight.jpg
43+
:width: 80%
44+
:align: center
45+
46+
Earth day/night dataset.
47+
48+
The images are downloaded to a user data directory (usually
49+
``~/.gmt/server/earth/earth_night/``) the first time you invoke this function.
50+
Afterwards, it will load the image from the data directory. So you'll need an
51+
internet connection the first time around.
52+
53+
These images can also be accessed by passing in the file name
54+
**@earth_night**\_\ *res* to any image processing function or plotting method. *res*
55+
is the image resolution (see below).
56+
57+
Refer to :gmt-datasets:`earth-daynight.html` for more details about available
58+
datasets, including version information and references.
59+
60+
Parameters
61+
----------
62+
resolution
63+
The image resolution. The suffix ``d``, ``m``, and ``s`` stand for arc-degree,
64+
arc-minute, and arc-second.
65+
66+
region
67+
The subregion of the image to load, in the form of a sequence [*xmin*, *xmax*,
68+
*ymin*, *ymax*].
69+
70+
Returns
71+
-------
72+
image
73+
The NASA Black Marble image. Coordinates are latitude and longitude in degrees.
74+
75+
Note
76+
----
77+
The registration and coordinate system type of the returned
78+
:class:`xarray.DataArray` image can be accessed via the GMT accessors (i.e.,
79+
``image.gmt.registration`` and ``image.gmt.gtype`` respectively). However, these
80+
properties may be lost after specific image operations (such as slicing) and will
81+
need to be manually set before passing the image to any PyGMT data processing or
82+
plotting functions. Refer to :class:`pygmt.GMTDataArrayAccessor` for detailed
83+
explanations and workarounds.
84+
85+
Examples
86+
--------
87+
88+
>>> from pygmt.datasets import load_black_marble
89+
>>> # load the default image (pixel-registered 1 arc-degree image)
90+
>>> image = load_black_marble()
91+
"""
92+
image = _load_remote_dataset(
93+
name="earth_night",
94+
prefix="earth_night",
95+
resolution=resolution,
96+
region=region,
97+
registration="pixel",
98+
)
99+
# If rioxarray is installed, set the coordinate reference system
100+
if hasattr(image, "rio"):
101+
image = image.rio.write_crs(input_crs="OGC:CRS84")
102+
return image
Collapse file

‎pygmt/datasets/load_remote_dataset.py‎

Copy file name to clipboardExpand all lines: pygmt/datasets/load_remote_dataset.py
+20-1Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ class GMTRemoteDataset(NamedTuple):
214214
"15s": Resolution("15s"),
215215
},
216216
),
217+
"earth_night": GMTRemoteDataset(
218+
description="NASA Night Images",
219+
units=None,
220+
extra_attributes={"long_name": "black_marble", "horizontal_datum": "WGS84"},
221+
resolutions={
222+
"01d": Resolution("01d", registrations=["pixel"]),
223+
"30m": Resolution("30m", registrations=["pixel"]),
224+
"20m": Resolution("20m", registrations=["pixel"]),
225+
"15m": Resolution("15m", registrations=["pixel"]),
226+
"10m": Resolution("10m", registrations=["pixel"]),
227+
"06m": Resolution("06m", registrations=["pixel"]),
228+
"05m": Resolution("05m", registrations=["pixel"]),
229+
"04m": Resolution("04m", registrations=["pixel"]),
230+
"03m": Resolution("03m", registrations=["pixel"]),
231+
"02m": Resolution("02m", registrations=["pixel"]),
232+
"01m": Resolution("01m", registrations=["pixel"]),
233+
"30s": Resolution("30s", registrations=["pixel"]),
234+
},
235+
),
217236
"earth_vgg": GMTRemoteDataset(
218237
description="IGPP Earth vertical gravity gradient",
219238
units="Eotvos",
@@ -428,7 +447,7 @@ def _load_remote_dataset(
428447
f"'region' is required for {dataset.description} resolution '{resolution}'."
429448
)
430449

431-
kind = "image" if name in {"earth_day"} else "grid"
450+
kind = "image" if name in {"earth_day", "earth_night"} else "grid"
432451
kwdict = {
433452
"R": region, # region can be None
434453
"T": "i" if kind == "image" else "g",
Collapse file

‎pygmt/helpers/caching.py‎

Copy file name to clipboardExpand all lines: pygmt/helpers/caching.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def cache_data():
2222
"@earth_mag_01d_g",
2323
"@earth_mag4km_01d_g",
2424
"@earth_mask_01d_g",
25+
"@earth_night_01d",
2526
"@earth_relief_01d_g",
2627
"@earth_relief_01d_p",
2728
"@earth_relief_10m_g",
Collapse file

‎pygmt/tests/test_datasets_earth_night.py‎

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Test basic functionality for loading Black Marble datasets.
3+
"""
4+
5+
import numpy as np
6+
import numpy.testing as npt
7+
from pygmt.datasets import load_black_marble
8+
9+
10+
def test_black_marble_01d():
11+
"""
12+
Test some properties of the Black Marble 01d data.
13+
"""
14+
data = load_black_marble(resolution="01d")
15+
assert data.name == "z"
16+
assert data.long_name == "black_marble"
17+
assert data.attrs["horizontal_datum"] == "WGS84"
18+
assert data.attrs["description"] == "NASA Night Images"
19+
assert data.shape == (3, 180, 360)
20+
assert data.dtype == "uint8"
21+
assert data.gmt.registration == 1
22+
assert data.gmt.gtype == 1
23+
npt.assert_allclose(data.y, np.arange(89.5, -90.5, -1))
24+
npt.assert_allclose(data.x, np.arange(-179.5, 180.5, 1))
25+
npt.assert_allclose(data.min(), 3, atol=1)
26+
npt.assert_allclose(data.max(), 174, atol=1)
27+
28+
29+
def test_black_marble_01d_with_region():
30+
"""
31+
Test loading low-resolution Black Marble with 'region'.
32+
"""
33+
data = load_black_marble(resolution="01d", region=[-10, 10, -5, 5])
34+
assert data.shape == (3, 10, 20)
35+
assert data.dtype == "uint8"
36+
assert data.gmt.registration == 1
37+
assert data.gmt.gtype == 1
38+
npt.assert_allclose(data.y, np.arange(4.5, -5.5, -1))
39+
npt.assert_allclose(data.x, np.arange(-9.5, 10.5, 1))
40+
npt.assert_allclose(data.min(), 3, atol=1)
41+
npt.assert_allclose(data.max(), 40, atol=1)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.