Skip to content

Commit 46f739a

Browse files
willschlitzerweiji14seismancore-manMeghan Jones
authored
Wrap grdlandmask (#1273)
* Add grdlandmask.py and test_grdlandmask.py Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Yao Jiayuan <[email protected]> Co-authored-by: Meghan Jones <[email protected]>
1 parent f747e71 commit 46f739a

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Operations on grids:
9292
grdcut
9393
grdfill
9494
grdfilter
95+
grdlandmask
9596
grdgradient
9697
grdtrack
9798

pygmt/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
grdfilter,
4040
grdgradient,
4141
grdinfo,
42+
grdlandmask,
4243
grdtrack,
4344
info,
4445
makecpt,

pygmt/src/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pygmt.src.grdgradient import grdgradient
1919
from pygmt.src.grdimage import grdimage
2020
from pygmt.src.grdinfo import grdinfo
21+
from pygmt.src.grdlandmask import grdlandmask
2122
from pygmt.src.grdtrack import grdtrack
2223
from pygmt.src.grdview import grdview
2324
from pygmt.src.histogram import histogram

pygmt/src/grdlandmask.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
grdlandmask - Create a "wet-dry" mask grid from shoreline data base
3+
"""
4+
5+
import xarray as xr
6+
from pygmt.clib import Session
7+
from pygmt.exceptions import GMTInvalidInput
8+
from pygmt.helpers import (
9+
GMTTempFile,
10+
build_arg_string,
11+
fmt_docstring,
12+
kwargs_to_strings,
13+
use_alias,
14+
)
15+
16+
17+
@fmt_docstring
18+
@use_alias(
19+
G="outgrid",
20+
I="spacing",
21+
R="region",
22+
r="registration",
23+
)
24+
@kwargs_to_strings(R="sequence")
25+
def grdlandmask(**kwargs):
26+
r"""
27+
Create a grid file with set values for land and water.
28+
29+
Read the selected shoreline database and create a grid to specify which
30+
nodes in the specified grid are over land or over water. The nodes defined
31+
by the selected region and lattice spacing
32+
will be set according to one of two criteria: (1) land vs water, or
33+
(2) the more detailed (hierarchical) ocean vs land vs lake
34+
vs island vs pond.
35+
36+
Full option list at :gmt-docs:`grdlandmask.html`
37+
38+
{aliases}
39+
40+
Parameters
41+
----------
42+
outgrid : str or None
43+
The name of the output netCDF file with extension .nc to store the grid
44+
in.
45+
{I}
46+
{R}
47+
{r}
48+
49+
Returns
50+
-------
51+
ret: xarray.DataArray or None
52+
Return type depends on whether the ``outgrid`` parameter is set:
53+
54+
- :class:`xarray.DataArray` if ``outgrid`` is not set
55+
- None if ``outgrid`` is set (grid output will be stored in file set by
56+
``outgrid``)
57+
"""
58+
if "I" not in kwargs.keys() or "R" not in kwargs.keys():
59+
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
60+
61+
with GMTTempFile(suffix=".nc") as tmpfile:
62+
with Session() as lib:
63+
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
64+
kwargs.update({"G": tmpfile.name})
65+
outgrid = kwargs["G"]
66+
arg_str = build_arg_string(kwargs)
67+
lib.call_module("grdlandmask", arg_str)
68+
69+
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
70+
with xr.open_dataarray(outgrid) as dataarray:
71+
result = dataarray.load()
72+
_ = result.gmt # load GMTDataArray accessor information
73+
else:
74+
result = None # if user sets an outgrid, return None
75+
76+
return result

pygmt/tests/test_grdlandmask.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Tests for grdlandmask.
3+
"""
4+
import os
5+
6+
import pytest
7+
from pygmt import grdinfo, grdlandmask
8+
from pygmt.exceptions import GMTInvalidInput
9+
from pygmt.helpers import GMTTempFile
10+
11+
12+
def test_grdlandmask_outgrid():
13+
"""
14+
Creates a grid land mask with an outgrid argument.
15+
"""
16+
with GMTTempFile(suffix=".nc") as tmpfile:
17+
result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[-5, 5, -5, 5])
18+
assert result is None # return value is None
19+
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
20+
result = (
21+
grdinfo(grid=tmpfile.name, force_scan=0, per_column="n").strip().split()
22+
)
23+
assert result == ["-5", "5", "-5", "5", "0", "1", "1", "1", "11", "11", "0", "1"]
24+
25+
26+
def test_grdlandmask_no_outgrid():
27+
"""
28+
Test grdlandmask with no set outgrid.
29+
"""
30+
temp_grid = grdlandmask(spacing=1, region=[-5, 5, -5, 5])
31+
assert temp_grid.dims == ("lat", "lon")
32+
assert temp_grid.gmt.gtype == 1 # Geographic grid
33+
assert temp_grid.gmt.registration == 0 # Pixel registration
34+
assert temp_grid.min() == 0
35+
assert temp_grid.max() == 1
36+
37+
38+
def test_grdlandmask_fails():
39+
"""
40+
Check that grdlandmask fails correctly when region and spacing are not
41+
given.
42+
"""
43+
with pytest.raises(GMTInvalidInput):
44+
grdlandmask()

0 commit comments

Comments
 (0)