-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow passing RGB xarray.DataArray images into grdimage (#2590)
Saving 3-band xarray.DataArray images to a temporary GeoTIFF so that they can be plotted with grdimage. * Refactor to use tempfile_from_image Putting the temporary GeoTIFF creation logic in a dedicated tempfile_from_image helper function, so that it can be reused by other GMT modules besides grdimage. Also ensure that an ImportError is raised when the `.rio` attribute cannot be found when rioxarray is not installed. * Let tilemap use tempfile_from_image func in virtualfile_from_data Refactor Figure.tilemap to use the same tempfile_from_image function that generates a temporary GeoTIFF file from the 3-band xarray.DataArray images. * Update docstring of grdimage with upstream GMT 6.4.0 Various updates from upstream GMT at GenericMappingTools/gmt#6258, GenericMappingTools/gmt@9069967, GenericMappingTools/gmt#7260. * Raise RuntimeWarning when input image dtype is not uint8 Plotting a non-uint8 dtype xarray.DataArray works in grdimage, but the results will likely be incorrect. Warning the user about the incorrect dtype, and suggest recasting to uint8 with 0-255 range, e.g. using a histogram equalization function like skimage.exposure.equalize_hist. --------- Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
- Loading branch information
Showing
8 changed files
with
161 additions
and
33 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
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,4 @@ | ||
outs: | ||
- md5: 2e919645d5af956ec4f8aa054a86a70a | ||
size: 110214 | ||
path: test_grdimage_image.png |
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,79 @@ | ||
""" | ||
Test Figure.grdimage on 3-band RGB images. | ||
""" | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
import xarray as xr | ||
from pygmt import Figure, which | ||
|
||
rasterio = pytest.importorskip("rasterio") | ||
rioxarray = pytest.importorskip("rioxarray") | ||
|
||
|
||
@pytest.fixture(scope="module", name="xr_image") | ||
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) == 1: | ||
with rasterio.open(fp=geotiff) as src: | ||
df_colormap = pd.DataFrame.from_dict( | ||
data=src.colormap(1), orient="index" | ||
) | ||
array = src.read() | ||
|
||
red = np.vectorize(df_colormap[0].get)(array) | ||
green = np.vectorize(df_colormap[1].get)(array) | ||
blue = np.vectorize(df_colormap[2].get)(array) | ||
# alpha = np.vectorize(df_colormap[3].get)(array) | ||
|
||
rda.data = red | ||
da_red = rda.astype(dtype=np.uint8).copy() | ||
rda.data = green | ||
da_green = rda.astype(dtype=np.uint8).copy() | ||
rda.data = blue | ||
da_blue = rda.astype(dtype=np.uint8).copy() | ||
|
||
xr_image = xr.concat(objs=[da_red, da_green, da_blue], dim="band") | ||
assert xr_image.sizes == {"band": 3, "y": 180, "x": 360} | ||
return xr_image | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_grdimage_image(): | ||
""" | ||
Plot a 3-band RGB image using file input. | ||
""" | ||
fig = Figure() | ||
fig.grdimage(grid="@earth_day_01d") | ||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare(filename="test_grdimage_image.png") | ||
def test_grdimage_image_dataarray(xr_image): | ||
""" | ||
Plot a 3-band RGB image using xarray.DataArray input. | ||
""" | ||
fig = Figure() | ||
fig.grdimage(grid=xr_image) | ||
return fig | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dtype", | ||
["int8", "uint16", "int16", "uint32", "int32", "float32", "float64"], | ||
) | ||
def test_grdimage_image_dataarray_unsupported_dtype(dtype, xr_image): | ||
""" | ||
Plot a 3-band RGB image using xarray.DataArray input, with an unsupported | ||
data type. | ||
""" | ||
fig = Figure() | ||
image = xr_image.astype(dtype=dtype) | ||
with pytest.warns(expected_warning=RuntimeWarning) as record: | ||
fig.grdimage(grid=image) | ||
assert len(record) == 1 |