-
Notifications
You must be signed in to change notification settings - Fork 228
Wrap grdlandmask #1273
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
Merged
Merged
Wrap grdlandmask #1273
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
985779a
add grdlandmask.py
willschlitzer 6b9bc8d
import grdlandmask and add to api docs
willschlitzer 32b8b8e
remove file context
willschlitzer f71a414
Merge branch 'master' into wrap-grdlandmask
willschlitzer a0f64fb
Merge remote-tracking branch 'origin/wrap-grdlandmask' into wrap-grdl…
willschlitzer 0b69b6f
Apply suggestions from code review
willschlitzer b221b7d
Merge branch 'master' into wrap-grdlandmask
willschlitzer 4662689
add test_grdlandmask.py
willschlitzer 5795626
remove unused import
willschlitzer eb262f1
Merge branch 'master' into wrap-grdlandmask
willschlitzer f53206d
update grdlandmask docstring
willschlitzer 3f3640c
adding increment docstring
willschlitzer 02ab9f9
add GMTInvalidInput raising and test
willschlitzer 1e13a39
format fix
willschlitzer ef8bcfc
Update pygmt/src/grdlandmask.py
willschlitzer 3c8b987
Merge branch 'master' into wrap-grdlandmask
willschlitzer 03ad411
Merge remote-tracking branch 'origin/wrap-grdlandmask' into wrap-grdl…
willschlitzer 276615b
run make formate
willschlitzer 788c980
Merge branch 'master' into wrap-grdlandmask
willschlitzer 1bd9290
Update pygmt/src/grdlandmask.py
willschlitzer b2336ad
Merge branch 'master' into wrap-grdlandmask
willschlitzer aaff96a
fix if statement for GMTInvalidInput
willschlitzer 9c94963
Update pygmt/src/grdlandmask.py
willschlitzer dd47b3d
Update pygmt/tests/test_grdlandmask.py
willschlitzer f1c3210
Apply suggestions from code review
willschlitzer 2c771a9
Merge branch 'master' into wrap-grdlandmask
willschlitzer 268c2bd
Merge branch 'master' into wrap-grdlandmask
seisman 518833d
Update pygmt/src/grdlandmask.py
willschlitzer 686bf2a
Merge branch 'master' into wrap-grdlandmask
willschlitzer 6ac49b9
Merge branch 'master' into wrap-grdlandmask
willschlitzer 736bfae
Merge branch 'master' into wrap-grdlandmask
willschlitzer 42e2ca8
Apply suggestions from code review
willschlitzer eea6122
Merge branch 'master' into wrap-grdlandmask
willschlitzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,7 @@ Operations on grids: | |
grdcut | ||
grdfill | ||
grdfilter | ||
grdlandmask | ||
grdgradient | ||
grdtrack | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
grdfilter, | ||
grdgradient, | ||
grdinfo, | ||
grdlandmask, | ||
grdtrack, | ||
info, | ||
makecpt, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
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", | ||
I="spacing", | ||
R="region", | ||
r="registration", | ||
) | ||
@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. | ||
{I} | ||
{R} | ||
{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``) | ||
""" | ||
if "I" not in kwargs.keys() or "R" not in kwargs.keys(): | ||
raise GMTInvalidInput("Both 'region' and 'spacing' 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 | ||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
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 outgrid argument. | ||
""" | ||
with GMTTempFile(suffix=".nc") as tmpfile: | ||
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 = ( | ||
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(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 # Pixel registration | ||
assert temp_grid.min() == 0 | ||
assert temp_grid.max() == 1 | ||
|
||
|
||
def test_grdlandmask_fails(): | ||
""" | ||
Check that grdlandmask fails correctly when region and spacing are not | ||
given. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
grdlandmask() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.