Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap grdlandmask #1273

Merged
merged 33 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
985779a
add grdlandmask.py
willschlitzer May 14, 2021
6b9bc8d
import grdlandmask and add to api docs
willschlitzer May 14, 2021
32b8b8e
remove file context
willschlitzer May 14, 2021
f71a414
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 17, 2021
a0f64fb
Merge remote-tracking branch 'origin/wrap-grdlandmask' into wrap-grdl…
willschlitzer May 17, 2021
0b69b6f
Apply suggestions from code review
willschlitzer May 18, 2021
b221b7d
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 18, 2021
4662689
add test_grdlandmask.py
willschlitzer May 18, 2021
5795626
remove unused import
willschlitzer May 18, 2021
eb262f1
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 18, 2021
f53206d
update grdlandmask docstring
willschlitzer May 18, 2021
3f3640c
adding increment docstring
willschlitzer May 18, 2021
02ab9f9
add GMTInvalidInput raising and test
willschlitzer May 18, 2021
1e13a39
format fix
willschlitzer May 18, 2021
ef8bcfc
Update pygmt/src/grdlandmask.py
willschlitzer May 19, 2021
3c8b987
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 19, 2021
03ad411
Merge remote-tracking branch 'origin/wrap-grdlandmask' into wrap-grdl…
willschlitzer May 19, 2021
276615b
run make formate
willschlitzer May 19, 2021
788c980
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 21, 2021
1bd9290
Update pygmt/src/grdlandmask.py
willschlitzer May 22, 2021
b2336ad
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 22, 2021
aaff96a
fix if statement for GMTInvalidInput
willschlitzer May 22, 2021
9c94963
Update pygmt/src/grdlandmask.py
willschlitzer May 23, 2021
dd47b3d
Update pygmt/tests/test_grdlandmask.py
willschlitzer May 23, 2021
f1c3210
Apply suggestions from code review
willschlitzer May 24, 2021
2c771a9
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 24, 2021
268c2bd
Merge branch 'master' into wrap-grdlandmask
seisman May 24, 2021
518833d
Update pygmt/src/grdlandmask.py
willschlitzer May 25, 2021
686bf2a
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 25, 2021
6ac49b9
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 25, 2021
736bfae
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 26, 2021
42e2ca8
Apply suggestions from code review
willschlitzer May 26, 2021
eea6122
Merge branch 'master' into wrap-grdlandmask
willschlitzer May 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Operations on grids:
grdclip
grdcut
grdfilter
grdlandmask
grdtrack

Crossover analysis with x2sys:
Expand Down
1 change: 1 addition & 0 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
grdcut,
grdfilter,
grdinfo,
grdlandmask,
grdtrack,
info,
makecpt,
Expand Down
1 change: 1 addition & 0 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pygmt.src.grdfilter import grdfilter
from pygmt.src.grdimage import grdimage
from pygmt.src.grdinfo import grdinfo
from pygmt.src.grdlandmask import grdlandmask
from pygmt.src.grdtrack import grdtrack
from pygmt.src.grdview import grdview
from pygmt.src.histogram import histogram
Expand Down
92 changes: 92 additions & 0 deletions pygmt/src/grdlandmask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
grdlandmask - Create a "wet-dry" mask grid from shoreline data base
"""

import xarray as xr
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
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 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
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.
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
-------
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:
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})
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
43 changes: 43 additions & 0 deletions pygmt/tests/test_grdlandmask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Tests for grdlandmask.
"""
import os

import pytest
from pygmt import grdinfo, grdlandmask
from pygmt.exceptions import GMTInvalidInput
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


def test_grdlandmask_fails():
"""
Check that grdlandmask fails correctly.
"""
with pytest.raises(GMTInvalidInput):
grdlandmask()