From 985779a9d0e57a7e6f7571c3e3854f879e7a06bc Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 14 May 2021 08:08:56 +0100 Subject: [PATCH 01/19] add grdlandmask.py --- pygmt/src/grdlandmask.py | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 pygmt/src/grdlandmask.py diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py new file mode 100644 index 00000000000..24a819483f1 --- /dev/null +++ b/pygmt/src/grdlandmask.py @@ -0,0 +1,69 @@ +""" +grdlandmask - Create a "wet-dry" mask grid from shoreline data base +""" + +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", + I="increment", +) +@kwargs_to_strings(R="sequence") +def grlandmask(**kwargs): + r""" + Read the selected shoreline database and create a grid to specify which + nodes in the specified grid are over land or over water. The nodes defined + by the selected region and lattice spacing + will be set according to one of two criteria: (1) land vs water, or + (2) the more detailed (hierarchical) ocean vs land vs lake + vs island vs pond. + + Full option list at :gmt-docs:`grdlandmask.html` + + {aliases} + + Parameters + ---------- + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {R} + + Returns + ------- + ret: xarray.DataArray or None + Return type depends on whether the ``outgrid`` parameter is set: + + - :class:`xarray.DataArray` if ``outgrid`` is not set + - None if ``outgrid`` is set (grid output will be stored in file set by + ``outgrid``) + """ + 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 = build_arg_string(kwargs) + lib.call_module("grdlandmask", 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 6b9bc8d7fbf456fd2d83f551ea5b474abc84016d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 14 May 2021 08:11:07 +0100 Subject: [PATCH 02/19] import grdlandmask and add to api docs --- doc/api/index.rst | 1 + pygmt/__init__.py | 1 + pygmt/src/__init__.py | 1 + 3 files changed, 3 insertions(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index b6cb79a26d6..e03dee64722 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -90,6 +90,7 @@ Operations on grids: grdcut grdfilter + grdlandmask grdtrack Crossover analysis with x2sys: diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 1dc2ed79ac4..e6e2d347e3d 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -36,6 +36,7 @@ grdcut, grdfilter, grdinfo, + grdlandmask, grdtrack, info, makecpt, diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index 083a2970109..54e22ce13af 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -15,6 +15,7 @@ from pygmt.src.grdfilter import grdfilter from pygmt.src.grdimage import grdimage from pygmt.src.grdinfo import grdinfo +from pygmt.src.grdlandmask import grlandmask from pygmt.src.grdtrack import grdtrack from pygmt.src.grdview import grdview from pygmt.src.histogram import histogram From 32b8b8ea8b585e95b13821ebccc7951122b28909 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 14 May 2021 19:13:03 +0100 Subject: [PATCH 03/19] remove file context --- pygmt/src/grdlandmask.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index 24a819483f1..aa33a1e2908 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -51,13 +51,11 @@ def grlandmask(**kwargs): """ 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 = build_arg_string(kwargs) - lib.call_module("grdlandmask", arg_str) + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = build_arg_string(kwargs) + lib.call_module("grdlandmask", arg_str) if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray with xr.open_dataarray(outgrid) as dataarray: From 0b69b6fda4ad104e6030a85ed420a3a45ed4d746 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 18 May 2021 07:00:18 +0100 Subject: [PATCH 04/19] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/__init__.py | 2 +- pygmt/src/grdlandmask.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index 54e22ce13af..10ea4fc3936 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -15,7 +15,7 @@ from pygmt.src.grdfilter import grdfilter from pygmt.src.grdimage import grdimage from pygmt.src.grdinfo import grdinfo -from pygmt.src.grdlandmask import grlandmask +from pygmt.src.grdlandmask import grdlandmask from pygmt.src.grdtrack import grdtrack from pygmt.src.grdview import grdview from pygmt.src.histogram import histogram diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index aa33a1e2908..4f5867530eb 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -20,7 +20,7 @@ I="increment", ) @kwargs_to_strings(R="sequence") -def grlandmask(**kwargs): +def grdlandmask(**kwargs): r""" Read the selected shoreline database and create a grid to specify which nodes in the specified grid are over land or over water. The nodes defined From 466268991527e1213a1a2165c0f07715ebab37aa Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 18 May 2021 07:48:02 +0100 Subject: [PATCH 05/19] add test_grdlandmask.py --- pygmt/tests/test_grdlandmask.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 pygmt/tests/test_grdlandmask.py diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py new file mode 100644 index 00000000000..476e2537957 --- /dev/null +++ b/pygmt/tests/test_grdlandmask.py @@ -0,0 +1,34 @@ +""" +Tests for grdlandmask. +""" +import os + +import pytest +from pygmt import grdinfo, grdlandmask +from pygmt.helpers import GMTTempFile + + +def test_grdlandmask_outgrid(): + """ + Creates a grid land mask with an outgride argument. + """ + with GMTTempFile(suffix=".nc") as tmpfile: + result = grdlandmask(outgrid=tmpfile.name, increment=1, region=[-5, 5, -5, 5]) + 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() + ) + assert result == ["-5", "5", "-5", "5", "0", "1", "1", "1", "11", "11", "0", "1"] + + +def test_grdlandmask_no_outgrid(): + """ + Test grdlandmask with no set outgrid. + """ + temp_grid = grdlandmask(increment=1, region=[-5, 5, -5, 5]) + assert temp_grid.dims == ("lat", "lon") + assert temp_grid.gmt.gtype == 1 # Geographic grid + assert temp_grid.gmt.registration == 0 + assert temp_grid.min() == 0 + assert temp_grid.max() == 1 From 57956267ed47e5a302f8ceb49b10d914c2dcd3b5 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 18 May 2021 07:50:04 +0100 Subject: [PATCH 06/19] remove unused import --- pygmt/tests/test_grdlandmask.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index 476e2537957..01b3cb74275 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -3,7 +3,6 @@ """ import os -import pytest from pygmt import grdinfo, grdlandmask from pygmt.helpers import GMTTempFile From f53206d974ee13cd24e99dce7a7dadf9732652b2 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 18 May 2021 08:30:46 +0100 Subject: [PATCH 07/19] update grdlandmask docstring --- pygmt/src/grdlandmask.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index 4f5867530eb..d3309da3007 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -22,6 +22,8 @@ @kwargs_to_strings(R="sequence") def grdlandmask(**kwargs): r""" + Create a grid file with set values for land and water. + Read the selected shoreline database and create a grid to specify which nodes in the specified grid are over land or over water. The nodes defined by the selected region and lattice spacing From 3f3640ca95b151435ddf848b9911a6893b6e50cb Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 18 May 2021 11:32:09 +0100 Subject: [PATCH 08/19] adding increment docstring --- pygmt/src/grdlandmask.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index d3309da3007..fd86be86733 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -40,6 +40,26 @@ def grdlandmask(**kwargs): outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. + increment : str + *xinc*\ [**+e**\|\ **n**][/\ *yinc*\ [**+e**\|\ **n**]]. + *x_inc* [and optionally *y_inc*] is the grid spacing. **Geographical + (degrees) coordinates**: Optionally, append a increment unit. Choose among + **m** to indicate arc minutes or **s** to indicate arc seconds. If one + of the units **e**, **f**, **k**, **M**, **n** or **u** is appended + instead, the increment is assumed to be given in meter, foot, km, Mile, + nautical mile or US survey foot, respectively, and will be converted to + the equivalent degrees longitude at the middle latitude of the region. + If *y_inc* is given but set to 0 it will be reset equal to *x_inc*; + otherwise it will be converted to degrees latitude. **All + coordinates**: If **+e** is appended then the corresponding max + *x* (*east*) or *y* (*north*) may be slightly adjusted to fit exactly + the given increment [by default the increment may be adjusted slightly + to fit the given domain]. Finally, instead of giving an increment you + may specify the *number of nodes* desired by appending **+n** to the + supplied integer argument; the increment is then recalculated from the + number of nodes, the *registration*, and the domain. The resulting + increment value depends on whether you have selected a + gridline-registered or pixel-registered grid. {R} Returns From 02ab9f998c54bc615c585a70b293475e4ff202a0 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 18 May 2021 11:38:05 +0100 Subject: [PATCH 09/19] add GMTInvalidInput raising and test --- pygmt/src/grdlandmask.py | 3 +++ pygmt/tests/test_grdlandmask.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index fd86be86733..fbccab6df41 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -4,6 +4,7 @@ import xarray as xr from pygmt.clib import Session +from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( GMTTempFile, build_arg_string, @@ -72,6 +73,8 @@ def grdlandmask(**kwargs): ``outgrid``) """ with GMTTempFile(suffix=".nc") as tmpfile: + if "I" not in kwargs.keys() and "R" not in kwargs.keys(): + raise GMTInvalidInput("""Region and increment must be specified.""") with Session() as lib: if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile kwargs.update({"G": tmpfile.name}) diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index 01b3cb74275..598fb3c83b3 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -3,7 +3,9 @@ """ import os +import pytest from pygmt import grdinfo, grdlandmask +from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -31,3 +33,11 @@ def test_grdlandmask_no_outgrid(): assert temp_grid.gmt.registration == 0 assert temp_grid.min() == 0 assert temp_grid.max() == 1 + + +def test_grdlandmask_fails(): + """ + Check that grdlandmask fails correctly. + """ + with pytest.raises(GMTInvalidInput): + grdlandmask() From 1e13a3968e6c1d135dee836b515e6dae86437e73 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 18 May 2021 12:12:32 +0100 Subject: [PATCH 10/19] format fix --- pygmt/src/grdlandmask.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index fbccab6df41..a1f2b5f4e8d 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -44,14 +44,14 @@ def grdlandmask(**kwargs): increment : str *xinc*\ [**+e**\|\ **n**][/\ *yinc*\ [**+e**\|\ **n**]]. *x_inc* [and optionally *y_inc*] is the grid spacing. **Geographical - (degrees) coordinates**: Optionally, append a increment unit. Choose among - **m** to indicate arc minutes or **s** to indicate arc seconds. If one - of the units **e**, **f**, **k**, **M**, **n** or **u** is appended - instead, the increment is assumed to be given in meter, foot, km, Mile, - nautical mile or US survey foot, respectively, and will be converted to - the equivalent degrees longitude at the middle latitude of the region. - If *y_inc* is given but set to 0 it will be reset equal to *x_inc*; - otherwise it will be converted to degrees latitude. **All + (degrees) coordinates**: Optionally, append a increment unit. Choose + among **m** to indicate arc minutes or **s** to indicate arc seconds. + If one of the units **e**, **f**, **k**, **M**, **n** or **u** is + appended instead, the increment is assumed to be given in meter, foot, + km, mile, nautical mile or US survey foot, respectively, and will be + converted to the equivalent degrees longitude at the middle latitude + of the region. If *y_inc* is given but set to 0 it will be reset equal + to *x_inc*; otherwise it will be converted to degrees latitude. **All coordinates**: If **+e** is appended then the corresponding max *x* (*east*) or *y* (*north*) may be slightly adjusted to fit exactly the given increment [by default the increment may be adjusted slightly From ef8bcfc38eec916029f9cb1c165c8bc678e0406d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 19 May 2021 07:02:25 +0100 Subject: [PATCH 11/19] Update pygmt/src/grdlandmask.py Co-authored-by: Dongdong Tian --- pygmt/src/grdlandmask.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index fbccab6df41..59ffdad9de0 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -72,9 +72,10 @@ def grdlandmask(**kwargs): - None if ``outgrid`` is set (grid output will be stored in file set by ``outgrid``) """ + if "I" not in kwargs.keys() and "R" not in kwargs.keys(): + raise GMTInvalidInput("Region and increment must be specified.") + with GMTTempFile(suffix=".nc") as tmpfile: - if "I" not in kwargs.keys() and "R" not in kwargs.keys(): - raise GMTInvalidInput("""Region and increment must be specified.""") with Session() as lib: if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile kwargs.update({"G": tmpfile.name}) From 276615baaa2884d24901946e951db4196ed918e9 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 19 May 2021 07:34:53 +0100 Subject: [PATCH 12/19] run make formate --- pygmt/src/grdlandmask.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index 284d10f0aac..bc2c6c482ce 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -74,7 +74,7 @@ def grdlandmask(**kwargs): """ if "I" not in kwargs.keys() and "R" not in kwargs.keys(): raise GMTInvalidInput("Region and increment must be specified.") - + with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile From 1bd92901ff22c4a1257fd724ba35e25484391b92 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 22 May 2021 10:05:27 +0100 Subject: [PATCH 13/19] Update pygmt/src/grdlandmask.py Co-authored-by: Dongdong Tian --- pygmt/src/grdlandmask.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index bc2c6c482ce..f7b30917d82 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -19,6 +19,7 @@ G="outgrid", R="region", I="increment", + r="registration", ) @kwargs_to_strings(R="sequence") def grdlandmask(**kwargs): From aaff96a40786272f118019fcc65c3423bfe6c4ce Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 22 May 2021 10:06:41 +0100 Subject: [PATCH 14/19] fix if statement for GMTInvalidInput --- pygmt/src/grdlandmask.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index f7b30917d82..b7a7f20776a 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -73,7 +73,7 @@ def grdlandmask(**kwargs): - None if ``outgrid`` is set (grid output will be stored in file set by ``outgrid``) """ - if "I" not in kwargs.keys() and "R" not in kwargs.keys(): + if "I" not in kwargs.keys() or "R" not in kwargs.keys(): raise GMTInvalidInput("Region and increment must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: From 9c949636e76269504310bd27b2943d9bf7051139 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 23 May 2021 06:22:19 +0100 Subject: [PATCH 15/19] Update pygmt/src/grdlandmask.py Co-authored-by: Dongdong Tian --- pygmt/src/grdlandmask.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index b7a7f20776a..3cd68d2142e 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -63,6 +63,7 @@ def grdlandmask(**kwargs): increment value depends on whether you have selected a gridline-registered or pixel-registered grid. {R} + {r} Returns ------- From dd47b3df3bb513d48332be520339779dd7fa4fa0 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 23 May 2021 11:22:05 +0100 Subject: [PATCH 16/19] Update pygmt/tests/test_grdlandmask.py Co-authored-by: Yao Jiayuan --- pygmt/tests/test_grdlandmask.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index 598fb3c83b3..ba76ca0d3c0 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -11,7 +11,7 @@ def test_grdlandmask_outgrid(): """ - Creates a grid land mask with an outgride argument. + Creates a grid land mask with an outgrid argument. """ with GMTTempFile(suffix=".nc") as tmpfile: result = grdlandmask(outgrid=tmpfile.name, increment=1, region=[-5, 5, -5, 5]) From f1c3210db482eea0ce4cc4410006aeadcc753cbb Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 24 May 2021 06:53:11 +0100 Subject: [PATCH 17/19] Apply suggestions from code review Co-authored-by: Meghan Jones --- pygmt/src/grdlandmask.py | 6 +++--- pygmt/tests/test_grdlandmask.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index 3cd68d2142e..0d8640c331f 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -18,7 +18,7 @@ @use_alias( G="outgrid", R="region", - I="increment", + I="spacing", r="registration", ) @kwargs_to_strings(R="sequence") @@ -42,7 +42,7 @@ def grdlandmask(**kwargs): outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. - increment : str + spacing : str *xinc*\ [**+e**\|\ **n**][/\ *yinc*\ [**+e**\|\ **n**]]. *x_inc* [and optionally *y_inc*] is the grid spacing. **Geographical (degrees) coordinates**: Optionally, append a increment unit. Choose @@ -75,7 +75,7 @@ def grdlandmask(**kwargs): ``outgrid``) """ if "I" not in kwargs.keys() or "R" not in kwargs.keys(): - raise GMTInvalidInput("Region and increment must be specified.") + raise GMTInvalidInput("Region and spacing must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index ba76ca0d3c0..99837bc75b7 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -14,7 +14,7 @@ def test_grdlandmask_outgrid(): Creates a grid land mask with an outgrid argument. """ with GMTTempFile(suffix=".nc") as tmpfile: - result = grdlandmask(outgrid=tmpfile.name, increment=1, region=[-5, 5, -5, 5]) + result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[-5, 5, -5, 5]) assert result is None # return value is None assert os.path.exists(path=tmpfile.name) # check that outgrid exists result = ( @@ -27,7 +27,7 @@ def test_grdlandmask_no_outgrid(): """ Test grdlandmask with no set outgrid. """ - temp_grid = grdlandmask(increment=1, region=[-5, 5, -5, 5]) + temp_grid = grdlandmask(spacing=1, region=[-5, 5, -5, 5]) assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == 1 # Geographic grid assert temp_grid.gmt.registration == 0 From 518833dcb994fae0dc15fdc677a31caa772a2e2a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 25 May 2021 06:05:06 +0100 Subject: [PATCH 18/19] Update pygmt/src/grdlandmask.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/grdlandmask.py | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index 0d8640c331f..9e9ec6a682b 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -42,26 +42,7 @@ def grdlandmask(**kwargs): outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. - spacing : str - *xinc*\ [**+e**\|\ **n**][/\ *yinc*\ [**+e**\|\ **n**]]. - *x_inc* [and optionally *y_inc*] is the grid spacing. **Geographical - (degrees) coordinates**: Optionally, append a increment unit. Choose - among **m** to indicate arc minutes or **s** to indicate arc seconds. - If one of the units **e**, **f**, **k**, **M**, **n** or **u** is - appended instead, the increment is assumed to be given in meter, foot, - km, mile, nautical mile or US survey foot, respectively, and will be - converted to the equivalent degrees longitude at the middle latitude - of the region. If *y_inc* is given but set to 0 it will be reset equal - to *x_inc*; otherwise it will be converted to degrees latitude. **All - coordinates**: If **+e** is appended then the corresponding max - *x* (*east*) or *y* (*north*) may be slightly adjusted to fit exactly - the given increment [by default the increment may be adjusted slightly - to fit the given domain]. Finally, instead of giving an increment you - may specify the *number of nodes* desired by appending **+n** to the - supplied integer argument; the increment is then recalculated from the - number of nodes, the *registration*, and the domain. The resulting - increment value depends on whether you have selected a - gridline-registered or pixel-registered grid. + {I} {R} {r} From 42e2ca872e02e0d5adb21b0707dfcaf943bf6f3d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 26 May 2021 10:28:25 +0100 Subject: [PATCH 19/19] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/grdlandmask.py | 4 ++-- pygmt/tests/test_grdlandmask.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index 9e9ec6a682b..ad6fb693fdc 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -17,8 +17,8 @@ @fmt_docstring @use_alias( G="outgrid", - R="region", I="spacing", + R="region", r="registration", ) @kwargs_to_strings(R="sequence") @@ -56,7 +56,7 @@ def grdlandmask(**kwargs): ``outgrid``) """ if "I" not in kwargs.keys() or "R" not in kwargs.keys(): - raise GMTInvalidInput("Region and spacing must be specified.") + raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index 99837bc75b7..4b019525ea3 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -30,14 +30,15 @@ def test_grdlandmask_no_outgrid(): temp_grid = grdlandmask(spacing=1, region=[-5, 5, -5, 5]) assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == 1 # Geographic grid - assert temp_grid.gmt.registration == 0 + assert temp_grid.gmt.registration == 0 # Pixel registration assert temp_grid.min() == 0 assert temp_grid.max() == 1 def test_grdlandmask_fails(): """ - Check that grdlandmask fails correctly. + Check that grdlandmask fails correctly when region and spacing are not + given. """ with pytest.raises(GMTInvalidInput): grdlandmask()