From bb99fa19ae941981a534ddc1ca9f03000099baae Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 18 Dec 2020 22:45:13 -0500 Subject: [PATCH 1/8] Pass xr.DataArray shading to grdimage --- pygmt/base_plotting.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pygmt/base_plotting.py b/pygmt/base_plotting.py index 45f0cc6ef35..9dba600ab33 100644 --- a/pygmt/base_plotting.py +++ b/pygmt/base_plotting.py @@ -482,8 +482,15 @@ def grdimage(self, grid, **kwargs): elif kind == "grid": file_context = lib.virtualfile_from_grid(grid) else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) - with file_context as fname: + raise GMTInvalidInput(f"Unrecognized data type: {type(grid)}") + + with contextlib.ExitStack() as stack: + # shading using an xr.DataArray + if "I" in kwargs and data_kind(kwargs["I"]) == "grid": + shading_context = lib.virtualfile_from_grid(kwargs["I"]) + kwargs["I"] = stack.enter_context(shading_context) + + fname = stack.enter_context(file_context) arg_str = " ".join([fname, build_arg_string(kwargs)]) lib.call_module("grdimage", arg_str) From 5c74e0a927142e03eaa30dd2f7dc68f0b7bcbfc7 Mon Sep 17 00:00:00 2001 From: Wei Ji Date: Sat, 30 Jan 2021 15:18:03 +1300 Subject: [PATCH 2/8] Test grdimage with xarray.DataArray input to both grid and shading --- pygmt/tests/test_grdimage.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index 2927c95e1d1..078422a95c5 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -119,6 +119,20 @@ def test_grdimage_shading_xarray(grid, shading): return fig_ref, fig_test +@check_figures_equal() +def test_grdimage_grid_and_shading_with_xarray(grid, xrgrid): + """ + Test that shading works well when xarray.DataArray is input to both the + ``grid`` and ``shading`` arguments. + """ + fig_ref, fig_test = Figure(), Figure() + fig_ref.grdimage( + grid="@earth_relief_01d_g", region="GL", cmap="geo", shading=xrgrid + ) + fig_test.grdimage(grid=grid, region="GL", cmap="geo", shading=xrgrid) + return fig_ref, fig_test + + def test_grdimage_fails(): """ Should fail for unrecognized input. From 38585fcc52c7b95b03479c0b6101c524c1a818e2 Mon Sep 17 00:00:00 2001 From: Wei Ji Date: Sat, 30 Jan 2021 15:45:24 +1300 Subject: [PATCH 3/8] Update grdimage shading docstring to mention xarray.DataArray input Also added new extra sentence on image shading as per upstream GMT 6.2.0. --- pygmt/base_plotting.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/pygmt/base_plotting.py b/pygmt/base_plotting.py index 34405569479..007ef0fc6ce 100644 --- a/pygmt/base_plotting.py +++ b/pygmt/base_plotting.py @@ -540,19 +540,21 @@ def grdimage(self, grid, **kwargs): paint the mask with the given color. Append **+b** to paint the background pixels (1) or **+f** for the foreground pixels [Default]. - shading : str + shading : str or xarray.DataArray ``[intensfile|intensity|modifiers]``. - Give the name of a grid file with intensities in the (-1,+1) range, - or a constant intensity to apply everywhere (affects the ambient - light). Alternatively, derive an intensity grid from the input data - grid via a call to `grdgradient`; append **+a**\\ *azimuth*, - **+n**\\ *args*, and **+m**\\ *ambient* to specify azimuth, - intensity, and ambient arguments for that module, or just give - **+d** to select the default arguments (``+a-45+nt1+m0``). If you - want a more specific intensity scenario then run `grdgradient` - separately first. If we should derive intensities from another file - than grid, specify the file with suitable modifiers [Default is no - illumination]. + Give the name of a grid file or a DataArray with intensities in the + (-1,+1) range, or a constant intensity to apply everywhere (affects + the ambient light). Alternatively, derive an intensity grid from the + input data grid via a call to `grdgradient`; append + **+a**\\ *azimuth*, **+n**\\ *args*, and **+m**\\ *ambient* to + specify azimuth, intensity, and ambient arguments for that module, + or just give **+d** to select the default arguments + (``+a-45+nt1+m0``). If you want a more specific intensity scenario + then run `grdgradient` separately first. If we should derive + intensities from another file than grid, specify the file with + suitable modifiers [Default is no illumination]. Note: If the input + data is an image then an intensfile or constant intensity must be + provided. {J} monochrome : bool Force conversion to monochrome image using the (television) YIQ From d2c2b450464005cf57c0d8fd89bb6980f3e6c722 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Thu, 3 Jun 2021 16:19:36 +1200 Subject: [PATCH 4/8] Temporarily add verbosity and colorbar to debug failing test --- pygmt/tests/test_grdimage.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index 304c85cef7a..521050c5d62 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -125,9 +125,11 @@ def test_grdimage_grid_and_shading_with_xarray(grid, xrgrid): """ fig_ref, fig_test = Figure(), Figure() fig_ref.grdimage( - grid="@earth_relief_01d_g", region="GL", cmap="geo", shading=xrgrid + grid="@earth_relief_01d_g", region="GL", cmap="geo", shading=xrgrid, verbose="i" ) - fig_test.grdimage(grid=grid, region="GL", cmap="geo", shading=xrgrid) + fig_ref.colorbar() + fig_test.grdimage(grid=grid, region="GL", cmap="geo", shading=xrgrid, verbose="i") + fig_test.colorbar() return fig_ref, fig_test From 80dfb50d2fed5270059712ab1fb6eff94db5579f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 9 Jun 2021 17:27:57 -0400 Subject: [PATCH 5/8] Mark the grdimage shading test as xfail [skip ci] Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/tests/test_grdimage.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index 521050c5d62..04e017a39cf 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -117,6 +117,10 @@ def test_grdimage_shading_xarray(grid, shading): return fig_ref, fig_test +@pytest.mark.xfail( + reason="Incorrect scaling of geo CPT on xarray.DataArray grdimage plot." + "See https://github.com/GenericMappingTools/gmt/issues/5294", +) @check_figures_equal() def test_grdimage_grid_and_shading_with_xarray(grid, xrgrid): """ From caf1f2d9f2917d36c0fca89e1de93b6280a9dee9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 9 Jun 2021 21:44:36 -0400 Subject: [PATCH 6/8] Update docstrings Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/grdimage.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pygmt/src/grdimage.py b/pygmt/src/grdimage.py index 236533f91bb..beccd4cb0c2 100644 --- a/pygmt/src/grdimage.py +++ b/pygmt/src/grdimage.py @@ -49,8 +49,8 @@ def grdimage(self, grid, **kwargs): added by providing a file with intensities in the (-1,+1) range or instructions to derive intensities from the input data grid. Values outside this range will be clipped. Such intensity files can be created from the - grid using `grdgradient` and, optionally, modified by `grdmath` or - `grdhisteq`. If GMT is built with GDAL support, ``grid`` can be an image + grid using :meth:`pygmt.grdgradient` and, optionally, modified by ``grdmath`` + or ``grdhisteq``. If GMT is built with GDAL support, ``grid`` can be an image file (geo-referenced or not). In this case the image can optionally be illuminated with the file provided via the ``shading`` parameter. Here, if image has no coordinates then those of the intensity file will be used. @@ -130,15 +130,15 @@ def grdimage(self, grid, **kwargs): Give the name of a grid file or a DataArray with intensities in the (-1,+1) range, or a constant intensity to apply everywhere (affects the ambient light). Alternatively, derive an intensity grid from the input - data grid via a call to ``grdgradient``; append **+a**\ *azimuth*, - **+n**\ *args*, and **+m**\ *ambient* to specify azimuth, intensity, - and ambient arguments for that module, or just give **+d** to select - the default arguments (``+a-45+nt1+m0``). If you want a more specific - intensity scenario then run ``grdgradient`` separately first. If we - should derive intensities from another file than grid, specify the file - with suitable modifiers [Default is no illumination]. Note: If the - input data is an *image* then an *intensfile* or constant *intensity* - must be provided. + data grid via a call to :meth:`pygmt.grdgradient`; append + **+a**\ *azimuth*, **+n**\ *args*, and **+m**\ *ambient* to specify + azimuth, intensity, and ambient arguments for that module, or just give + **+d** to select the default arguments (``+a-45+nt1+m0``). If you want + a more specific intensity scenario then run :meth:`pygmt.grdgradient` + separately first. If we should derive intensities from another file + than grid, specify the file with suitable modifiers [Default is no + illumination]. Note: If the input data is an *image* then an + *intensfile* or constant *intensity* must be provided. {J} monochrome : bool Force conversion to monochrome image using the (television) YIQ From d09c20dbcbe6e74596cb0773bf4b63d5e75e38bb Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 9 Jun 2021 22:05:44 -0400 Subject: [PATCH 7/8] Fix docstring styles --- pygmt/src/grdimage.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pygmt/src/grdimage.py b/pygmt/src/grdimage.py index beccd4cb0c2..b8464bc3568 100644 --- a/pygmt/src/grdimage.py +++ b/pygmt/src/grdimage.py @@ -49,11 +49,12 @@ def grdimage(self, grid, **kwargs): added by providing a file with intensities in the (-1,+1) range or instructions to derive intensities from the input data grid. Values outside this range will be clipped. Such intensity files can be created from the - grid using :meth:`pygmt.grdgradient` and, optionally, modified by ``grdmath`` - or ``grdhisteq``. If GMT is built with GDAL support, ``grid`` can be an image - file (geo-referenced or not). In this case the image can optionally be - illuminated with the file provided via the ``shading`` parameter. Here, if - image has no coordinates then those of the intensity file will be used. + grid using :meth:`pygmt.grdgradient` and, optionally, modified by + ``grdmath`` or ``grdhisteq``. If GMT is built with GDAL support, ``grid`` + can be an image file (geo-referenced or not). In this case the image can + optionally be illuminated with the file provided via the ``shading`` + parameter. Here, if image has no coordinates then those of the intensity + file will be used. When using map projections, the grid is first resampled on a new rectangular grid with the same dimensions. Higher resolution images can From 2e9fba839516ac3d65e5df3ecc7101ff4a84b5f7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 9 Jun 2021 22:08:00 -0400 Subject: [PATCH 8/8] Fix docstrings --- pygmt/src/grdimage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grdimage.py b/pygmt/src/grdimage.py index b8464bc3568..25acdfb40c4 100644 --- a/pygmt/src/grdimage.py +++ b/pygmt/src/grdimage.py @@ -127,7 +127,7 @@ def grdimage(self, grid, **kwargs): background pixels (1) or **+f** for the foreground pixels [Default is **+f**]. shading : str or xarray.DataArray - ``[intensfile|intensity|modifiers]``. + [*intensfile*\|\ *intensity*\|\ *modifiers*]. Give the name of a grid file or a DataArray with intensities in the (-1,+1) range, or a constant intensity to apply everywhere (affects the ambient light). Alternatively, derive an intensity grid from the input @@ -135,7 +135,7 @@ def grdimage(self, grid, **kwargs): **+a**\ *azimuth*, **+n**\ *args*, and **+m**\ *ambient* to specify azimuth, intensity, and ambient arguments for that module, or just give **+d** to select the default arguments (``+a-45+nt1+m0``). If you want - a more specific intensity scenario then run :meth:`pygmt.grdgradient` + a more specific intensity scenario then run :meth:`pygmt.grdgradient` separately first. If we should derive intensities from another file than grid, specify the file with suitable modifiers [Default is no illumination]. Note: If the input data is an *image* then an