GeezerVison is showing github.com — Add function to load Black Marble dataset (#3469) · GenericMappingTools/pygmt@923111a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -218,6 +218,7 @@ and store them in GMT's user data directory. | 218 | 218 | :toctree: generated | 219 | 219 |
| 220 | 220 | datasets.list_sample_data | | 221 | + datasets.load_black_marble | 221 | 222 | datasets.load_blue_marble | 222 | 223 | datasets.load_earth_age | 223 | 224 | datasets.load_earth_free_air_anomaly | |
Collapse file Copy file name to clipboardExpand all lines: pygmt/datasets/__init__.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | from pygmt.datasets.earth_geoid import load_earth_geoid | 11 | 11 | from pygmt.datasets.earth_magnetic_anomaly import load_earth_magnetic_anomaly | 12 | 12 | from pygmt.datasets.earth_mask import load_earth_mask | | 13 | +from pygmt.datasets.earth_night import load_black_marble | 13 | 14 | from pygmt.datasets.earth_relief import load_earth_relief | 14 | 15 | from pygmt.datasets.earth_vertical_gravity_gradient import ( | 15 | 16 | load_earth_vertical_gravity_gradient, | |
Collapse file Copy file name to clipboardExpand all lines: pygmt/datasets/earth_day.py +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | The images are available in various resolutions. | 6 | 6 | """ | 7 | 7 |
| | 8 | +import contextlib | 8 | 9 | from collections.abc import Sequence | 9 | 10 | from typing import Literal | 10 | 11 |
| 11 | 12 | import xarray as xr | 12 | 13 | from pygmt.datasets.load_remote_dataset import _load_remote_dataset | 13 | 14 |
| | 15 | +with contextlib.suppress(ImportError): | | 16 | + # rioxarray is needed to register the rio accessor | | 17 | + import rioxarray # noqa: F401 | | 18 | + | 14 | 19 | __doctest_skip__ = ["load_blue_marble"] | 15 | 20 |
| 16 | 21 |
| |
Collapse file Copy file name to clipboard +102Lines changed: 102 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboardExpand all lines: pygmt/datasets/load_remote_dataset.py +20-1Lines changed: 20 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -214,6 +214,25 @@ class GMTRemoteDataset(NamedTuple): | 214 | 214 | "15s": Resolution("15s"), | 215 | 215 | }, | 216 | 216 | ), | | 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 | + ), | 217 | 236 | "earth_vgg": GMTRemoteDataset( | 218 | 237 | description="IGPP Earth vertical gravity gradient", | 219 | 238 | units="Eotvos", | @@ -428,7 +447,7 @@ def _load_remote_dataset( | 428 | 447 | f"'region' is required for {dataset.description} resolution '{resolution}'." | 429 | 448 | ) | 430 | 449 |
| 431 | | - kind = "image" if name in {"earth_day"} else "grid" | | 450 | + kind = "image" if name in {"earth_day", "earth_night"} else "grid" | 432 | 451 | kwdict = { | 433 | 452 | "R": region, # region can be None | 434 | 453 | "T": "i" if kind == "image" else "g", | |
Collapse file Copy file name to clipboardExpand all lines: pygmt/helpers/caching.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,6 +22,7 @@ def cache_data(): | 22 | 22 | "@earth_mag_01d_g", | 23 | 23 | "@earth_mag4km_01d_g", | 24 | 24 | "@earth_mask_01d_g", | | 25 | + "@earth_night_01d", | 25 | 26 | "@earth_relief_01d_g", | 26 | 27 | "@earth_relief_01d_p", | 27 | 28 | "@earth_relief_10m_g", | |
Collapse file Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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) |
|