From 31d5347f7f73e0d4081c66d1a81f0cdce3e21c00 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 10 May 2021 17:50:14 +0100 Subject: [PATCH 01/20] create grdgradient.py --- pygmt/src/grdgradient.py | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 pygmt/src/grdgradient.py diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py new file mode 100644 index 00000000000..c0e57a99eac --- /dev/null +++ b/pygmt/src/grdgradient.py @@ -0,0 +1,57 @@ +""" +grdgradient - Create a gradient from a grid. +""" + +import xarray as xr +from pygmt.clib import Session +from pygmt.helpers import ( + GMTTempFile, + build_arg_string, + fmt_docstring, + kwargs_to_strings, + use_alias, +) + + +@fmt_docstring +@use_alias( + G="outgrid", + R="region", + V="verbose", +) +@kwargs_to_strings( + R="sequence", +) +def grdgradient(grid, **kwargs): + r""" + Full option list at :gmt-docs:`grdgradient.html` + + {aliases} + Parameters + ---------- + grid : str or xarray.DataArray + The file name of the input grid or the grid loaded as a DataArray. + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {R} + {V} + """ + with GMTTempFile(suffix=".nc") as tmpfile: + with Session() as lib: + file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) + with file_context as infile: + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grdgradient", arg_str) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() + _ = result.gmt # load GMTDataArray accessor information + else: + result = None # if user sets an outgrid, return None + + return result From e95cc994e2842f6e145dd98ad729f48b39639c10 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 10 May 2021 17:51:38 +0100 Subject: [PATCH 02/20] add grdgradient to init files --- pygmt/__init__.py | 1 + pygmt/src/__init__.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 1dc2ed79ac4..4fff975031e 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -35,6 +35,7 @@ grd2cpt, grdcut, grdfilter, + grdgradient, grdinfo, grdtrack, info, diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index 083a2970109..c85c616e9a6 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -13,6 +13,7 @@ from pygmt.src.grdcontour import grdcontour from pygmt.src.grdcut import grdcut from pygmt.src.grdfilter import grdfilter +from pygmt.src.grdgradient import grdgradient from pygmt.src.grdimage import grdimage from pygmt.src.grdinfo import grdinfo from pygmt.src.grdtrack import grdtrack From f9ae69eb5e5e10152ca0ec4dfcb71fd665ece2f6 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 10 May 2021 17:51:46 +0100 Subject: [PATCH 03/20] add grdgradient to API docs --- doc/api/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index b6cb79a26d6..1085a9a9665 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -90,6 +90,7 @@ Operations on grids: grdcut grdfilter + grdgradient grdtrack Crossover analysis with x2sys: From 8eb8ecd14a35383e0547f140c68c2bda8dfa3fd1 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 11 May 2021 08:13:07 +0100 Subject: [PATCH 04/20] add azimuth, direction, and radiance --- pygmt/src/grdgradient.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index c0e57a99eac..665e5802484 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -15,12 +15,15 @@ @fmt_docstring @use_alias( + A="azimuth", + D="direction", + E="radiance", G="outgrid", R="region", V="verbose", ) @kwargs_to_strings( - R="sequence", + A="sequence", R="sequence", ) def grdgradient(grid, **kwargs): r""" From e9d4ddf175f305bb56dc178839cc876c992174ea Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 11 May 2021 11:57:41 +0100 Subject: [PATCH 05/20] add azimuth docstring --- pygmt/src/grdgradient.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 665e5802484..0342e708b2b 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -37,6 +37,24 @@ def grdgradient(grid, **kwargs): outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. + azimuth : str or list or xarray.DataArray + *azim*\ [/*azim2*]. + Azimuthal direction for a directional derivative; *azim* is the + angle in the x,y plane measured in degrees positive clockwise from + north (the +y direction) toward east (the +x direction). The + negative of the directional derivative, -[dz/dx\*sin(*azim*) + + dz/dy\*cos(\ *azim*)], is found; negation yields positive values + when the slope of z(x,y) is downhill in the *azim* direction, the + correct sense for shading the illumination of an image by a light + source above the x,y plane shining from the *azim* direction. + Optionally, supply two azimuths, *azim*/*azim2*, in which case the + gradients in each of these directions are calculated and the one + larger in magnitude is retained; this is useful for illuminating data + with two directions of lineated structures, e.g., *0*/*270* + illuminates from the north (top) and west (left). Finally, if *azim* + is a file it must be a grid of the same domain, spacing and + registration as *ingrid* that will update the azimuth at each output + node when computing the directional derivatives. {R} {V} """ From 4d59891220c907bdbf10aebf98a9256a52d9198a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 11 May 2021 11:59:31 +0100 Subject: [PATCH 06/20] add direction docstring --- pygmt/src/grdgradient.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 0342e708b2b..8bbb55cac28 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -55,6 +55,16 @@ def grdgradient(grid, **kwargs): is a file it must be a grid of the same domain, spacing and registration as *ingrid* that will update the azimuth at each output node when computing the directional derivatives. + direction : str + [**a**][**c**][**o**][**n**]. + Find the direction of the positive (up-slope) gradient of the data. + To instead find the aspect (the down-slope direction), use **a**. + By default, directions are measured clockwise from north, as *azim* + in ``azimuth``. Append **c** to use conventional Cartesian angles + measured counterclockwise from the positive x (east) direction. + Append **o** to report orientations (0-180) rather than + directions (0-360). Append **n** to add 90 degrees to all angles + (e.g., to give local strikes of the surface). {R} {V} """ From 198dce3fb624f3d6c399b09d6c43d4679a4c6749 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 11 May 2021 12:02:33 +0100 Subject: [PATCH 07/20] add radiance docstring --- pygmt/src/grdgradient.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 8bbb55cac28..51b5714bf7e 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -65,6 +65,23 @@ def grdgradient(grid, **kwargs): Append **o** to report orientations (0-180) rather than directions (0-360). Append **n** to add 90 degrees to all angles (e.g., to give local strikes of the surface). + radiance : str + [**m**\|\ **s**\|\ **p**]\ *azim/elev*\ [**+a**\ *ambient*][**+d**\ + *diffuse*][**+p**\ *specular*][**+s**\ *shine*]. + Compute Lambertian radiance appropriate to use with ``grdimage`` + and ``grdview``. The Lambertian Reflection assumes an ideal surface + that reflects all the light that strikes it and the surface appears + equally bright from all viewing directions. Here, *azim* and *elev* are + the azimuth and elevation of the light vector. Optionally, supply + *ambient* [0.55], *diffuse* [0.6], *specular* [0.4], or *shine* [10], + which are parameters that control the reflectance properties of the + surface. Default values are given in the brackets. Use **s** for a + simpler Lambertian algorithm. Note that with this form you only have + to provide azimuth and elevation. Alternatively, use **p** for + the Peucker piecewise linear approximation (simpler but faster + algorithm; in this case the *azim* and *elev* are hardwired to 315 + and 45 degrees. This means that even if you provide other values + they will be ignored.) {R} {V} """ From 9aeb560f90aad58ce5ba83f2f65a1ae1ad6c918a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 11 May 2021 12:03:01 +0100 Subject: [PATCH 08/20] run make format --- pygmt/src/grdgradient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 51b5714bf7e..a8bddd8d864 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -23,7 +23,8 @@ V="verbose", ) @kwargs_to_strings( - A="sequence", R="sequence", + A="sequence", + R="sequence", ) def grdgradient(grid, **kwargs): r""" From e53201f82969a7ad67fc5edeec531bbb10552d9f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 12 May 2021 07:41:10 +0100 Subject: [PATCH 09/20] add args_in_kwargs for required argument in grdgradient --- pygmt/src/grdgradient.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index a8bddd8d864..6585920fe39 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -4,8 +4,10 @@ import xarray as xr from pygmt.clib import Session +from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( GMTTempFile, + args_in_kwargs, build_arg_string, fmt_docstring, kwargs_to_strings, @@ -87,6 +89,11 @@ def grdgradient(grid, **kwargs): {V} """ with GMTTempFile(suffix=".nc") as tmpfile: + if not args_in_kwargs(args=["A", "D", "E"], kwargs=kwargs): + raise GMTInvalidInput( + """At least one of the following parameters must be specified: + azimuth, direction, or radiance""" + ) with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) with file_context as infile: From e22bd5742d4865c151df73bc067633baf5a617ae Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 12 May 2021 08:10:47 +0100 Subject: [PATCH 10/20] add test_grdgradient.py --- pygmt/tests/test_grdgradient.py | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 pygmt/tests/test_grdgradient.py diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py new file mode 100644 index 00000000000..4f8f1990a53 --- /dev/null +++ b/pygmt/tests/test_grdgradient.py @@ -0,0 +1,41 @@ +""" +Tests for grdgradient. +""" +import os + +import numpy as np +import numpy.testing as npt +import pytest +import xarray as xr +from pygmt import grdgradient, grdinfo +from pygmt.datasets import load_earth_relief +from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import GMTTempFile + + +@pytest.fixture(scope="module", name="grid") +def fixture_grid(): + """ + Load the grid data from the sample earth_relief file. + """ + return load_earth_relief(resolution="01d", region=[-5, 5, -5, 5]) + + +def test_grdgradient_no_outgrid(grid): + """ + Test the below and above parameters for grdgradient with no set outgrid. + """ + temp_grid = grdgradient(grid=grid, azimuth=10, direction="c") + assert temp_grid.dims == ("lat", "lon") + assert temp_grid.gmt.gtype == 1 # Geographic grid + assert temp_grid.gmt.registration == 1 # Pixel registration + npt.assert_allclose(temp_grid.min(), -0.0045060496) + npt.assert_allclose(temp_grid.max(), 0.0575332976) + + +def test_grdgradient_fails(grid): + """ + Check that grdgradient fails correctly. + """ + with pytest.raises(GMTInvalidInput): + grdgradient(grid=grid) From 43585a0cfbd2c1e62cd20b40526ea3e2c0d53aae Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 12 May 2021 09:03:03 +0100 Subject: [PATCH 11/20] remove unused imports --- pygmt/tests/test_grdgradient.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 4f8f1990a53..64d68072892 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -1,16 +1,11 @@ """ Tests for grdgradient. """ -import os - -import numpy as np import numpy.testing as npt import pytest -import xarray as xr -from pygmt import grdgradient, grdinfo +from pygmt import grdgradient from pygmt.datasets import load_earth_relief from pygmt.exceptions import GMTInvalidInput -from pygmt.helpers import GMTTempFile @pytest.fixture(scope="module", name="grid") From 77d4cc942e40723ae0eb7f3a61aca500dcdf3e8c Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 12 May 2021 11:50:28 +0100 Subject: [PATCH 12/20] update grdgradient docstring --- pygmt/src/grdgradient.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 6585920fe39..5fe581412d7 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -30,6 +30,9 @@ ) def grdgradient(grid, **kwargs): r""" + Compute the directional derivative in a given ``azimuth`` or + ``direction`` of the vector gradient of the data. + Full option list at :gmt-docs:`grdgradient.html` {aliases} From 0d26dc118efb611b77082c6373ff34fdfaf8be02 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 14 May 2021 19:14:48 +0100 Subject: [PATCH 13/20] split up lead in docstring --- pygmt/src/grdgradient.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 5fe581412d7..2920229bda4 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -30,8 +30,10 @@ ) def grdgradient(grid, **kwargs): r""" - Compute the directional derivative in a given ``azimuth`` or - ``direction`` of the vector gradient of the data. + Compute the directional derivative of the vector gradient of the data. + + Can accept ``azimuth``, ``direction``, and ``radiance`` input to create + the resulting gradient. Full option list at :gmt-docs:`grdgradient.html` From f433363c925806ffdf0c12c94ba9335d4089e89c Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 14 May 2021 19:15:32 +0100 Subject: [PATCH 14/20] change "ingrid" to "grid" --- pygmt/src/grdgradient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 2920229bda4..4cd844eb63a 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -61,7 +61,7 @@ def grdgradient(grid, **kwargs): with two directions of lineated structures, e.g., *0*/*270* illuminates from the north (top) and west (left). Finally, if *azim* is a file it must be a grid of the same domain, spacing and - registration as *ingrid* that will update the azimuth at each output + registration as *grid* that will update the azimuth at each output node when computing the directional derivatives. direction : str [**a**][**c**][**o**][**n**]. From 801684afeddd61f4ef7a4ecc2b379b9198b07003 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 15 May 2021 08:06:32 +0100 Subject: [PATCH 15/20] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/grdgradient.py | 12 ++++++------ pygmt/tests/test_grdgradient.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 4cd844eb63a..996522982a1 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -23,11 +23,9 @@ G="outgrid", R="region", V="verbose", + n="interpolation", ) -@kwargs_to_strings( - A="sequence", - R="sequence", -) +@kwargs_to_strings(A="sequence", E="sequence", R="sequence") def grdgradient(grid, **kwargs): r""" Compute the directional derivative of the vector gradient of the data. @@ -38,6 +36,7 @@ def grdgradient(grid, **kwargs): Full option list at :gmt-docs:`grdgradient.html` {aliases} + Parameters ---------- grid : str or xarray.DataArray @@ -45,7 +44,7 @@ def grdgradient(grid, **kwargs): outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. - azimuth : str or list or xarray.DataArray + azimuth : int or float or str or list or xarray.DataArray *azim*\ [/*azim2*]. Azimuthal direction for a directional derivative; *azim* is the angle in the x,y plane measured in degrees positive clockwise from @@ -73,7 +72,7 @@ def grdgradient(grid, **kwargs): Append **o** to report orientations (0-180) rather than directions (0-360). Append **n** to add 90 degrees to all angles (e.g., to give local strikes of the surface). - radiance : str + radiance : str or list [**m**\|\ **s**\|\ **p**]\ *azim/elev*\ [**+a**\ *ambient*][**+d**\ *diffuse*][**+p**\ *specular*][**+s**\ *shine*]. Compute Lambertian radiance appropriate to use with ``grdimage`` @@ -92,6 +91,7 @@ def grdgradient(grid, **kwargs): they will be ignored.) {R} {V} + {n} """ with GMTTempFile(suffix=".nc") as tmpfile: if not args_in_kwargs(args=["A", "D", "E"], kwargs=kwargs): diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 64d68072892..714c0f9e109 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -18,7 +18,7 @@ def fixture_grid(): def test_grdgradient_no_outgrid(grid): """ - Test the below and above parameters for grdgradient with no set outgrid. + Test the azimuth and direction parameters for grdgradient with no set outgrid. """ temp_grid = grdgradient(grid=grid, azimuth=10, direction="c") assert temp_grid.dims == ("lat", "lon") From ce4592922d2b3a24addc538cd41d3c47aa9c9f69 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 15 May 2021 08:11:26 +0100 Subject: [PATCH 16/20] style fix --- pygmt/tests/test_grdgradient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 714c0f9e109..4b8f85e4bc8 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -18,7 +18,8 @@ def fixture_grid(): def test_grdgradient_no_outgrid(grid): """ - Test the azimuth and direction parameters for grdgradient with no set outgrid. + Test the azimuth and direction parameters for grdgradient with no set + outgrid. """ temp_grid = grdgradient(grid=grid, azimuth=10, direction="c") assert temp_grid.dims == ("lat", "lon") From 9287fc6c4a47227a938b0609fbac5b8e95e1c534 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 15 May 2021 08:21:09 +0100 Subject: [PATCH 17/20] add test_grdgradient_outgrid() --- pygmt/tests/test_grdgradient.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 4b8f85e4bc8..7eecdb39d6a 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -16,6 +16,22 @@ def fixture_grid(): return load_earth_relief(resolution="01d", region=[-5, 5, -5, 5]) +def test_grdgradient_outgrid(grid): + """ + Test the azimuth and direction parameters for grdgradient with a set + outgrid. + """ + with GMTTempFile(suffix=".nc") as tmpfile: + result = grdgradient(grid=grid, outgrid=tmpfile.name, azimuth=10, direction="c") + assert result is None # return value is None + assert os.path.exists(path=tmpfile.name) # check that outgrid exists + result = ( + grdinfo(grid=tmpfile.name, force_scan=0, per_column="n").strip().split() + ) + npt.assert_allclose(float(result[4]), -0.0045060496) + npt.assert_allclose(float(result[5]), 0.0575332976) + + def test_grdgradient_no_outgrid(grid): """ Test the azimuth and direction parameters for grdgradient with no set From 1fd6d32052b99655250800812fb914785ffaf435 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 15 May 2021 08:26:15 +0100 Subject: [PATCH 18/20] fix imports for test_grdgradient.py --- pygmt/tests/test_grdgradient.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 7eecdb39d6a..5f174581e05 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -1,11 +1,14 @@ """ Tests for grdgradient. """ +import os + import numpy.testing as npt import pytest -from pygmt import grdgradient +from pygmt import grdgradient, grdinfo from pygmt.datasets import load_earth_relief from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import GMTTempFile @pytest.fixture(scope="module", name="grid") From fb751e118fd3ec9b04df26d7f4c1d871380bf449 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 17 May 2021 06:59:09 +0100 Subject: [PATCH 19/20] Update pygmt/src/grdgradient.py Co-authored-by: Dongdong Tian --- pygmt/src/grdgradient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 996522982a1..c87b5266675 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -44,7 +44,7 @@ def grdgradient(grid, **kwargs): outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. - azimuth : int or float or str or list or xarray.DataArray + azimuth : int or float or str or list *azim*\ [/*azim2*]. Azimuthal direction for a directional derivative; *azim* is the angle in the x,y plane measured in degrees positive clockwise from From 8b2a772b40084c375565e5735c7f612c2ac8a5e0 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 21 May 2021 09:14:38 +0100 Subject: [PATCH 20/20] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/grdgradient.py | 2 +- pygmt/tests/test_grdgradient.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index c87b5266675..8dca2018c10 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -1,5 +1,5 @@ """ -grdgradient - Create a gradient from a grid. +grdgradient - Compute directional gradients from a grid. """ import xarray as xr diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 5f174581e05..4b01038ab6b 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -29,10 +29,13 @@ def test_grdgradient_outgrid(grid): assert result is None # return value is None assert os.path.exists(path=tmpfile.name) # check that outgrid exists result = ( - grdinfo(grid=tmpfile.name, force_scan=0, per_column="n").strip().split() + grdinfo(grid=tmpfile.name, force_scan="a", per_column="n").strip().split() ) - npt.assert_allclose(float(result[4]), -0.0045060496) - npt.assert_allclose(float(result[5]), 0.0575332976) + npt.assert_allclose(float(result[4]), -0.0045060496) # min + npt.assert_allclose(float(result[5]), 0.0575332976) # max + # Check spherically weighted statistics below + npt.assert_allclose(float(result[10]), 0.000384754501283) # median + npt.assert_allclose(float(result[12]), 0.00285958005568) # mean def test_grdgradient_no_outgrid(grid): @@ -46,11 +49,14 @@ def test_grdgradient_no_outgrid(grid): assert temp_grid.gmt.registration == 1 # Pixel registration npt.assert_allclose(temp_grid.min(), -0.0045060496) npt.assert_allclose(temp_grid.max(), 0.0575332976) + npt.assert_allclose(temp_grid.median(), 0.0004889865522272885) + npt.assert_allclose(temp_grid.mean(), 0.0028633063193410635) def test_grdgradient_fails(grid): """ - Check that grdgradient fails correctly. + Check that grdgradient fails correctly when neither of azimuth, direction + or radiance is given. """ with pytest.raises(GMTInvalidInput): grdgradient(grid=grid)