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 grdclip #1261

Merged
merged 36 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
079f75e
add grdclip imports
willschlitzer May 6, 2021
e3a42af
create grdclip.py
willschlitzer May 6, 2021
7bace79
fix import statements
willschlitzer May 6, 2021
9ee7f1d
wrap remaining parameters for grdclip
willschlitzer May 6, 2021
b4b9557
add top portion of grdclip docstring
willschlitzer May 7, 2021
4768a59
Merge branch 'master' into wrap-grdclip
willschlitzer May 7, 2021
af35c58
Merge branch 'wrap-grdclip' of github.com:GenericMappingTools/pygmt i…
willschlitzer May 7, 2021
69c7de2
add additional parameters to docstring
willschlitzer May 7, 2021
2cdadab
add grdclip to api list
willschlitzer May 8, 2021
85b6950
add test_grdclip.py
willschlitzer May 8, 2021
f922b22
add docstring for test_grdclip_below()
willschlitzer May 8, 2021
8169d21
Merge branch 'master' into wrap-grdclip
willschlitzer May 8, 2021
b1aa82f
remove unused import statements
willschlitzer May 8, 2021
3bb2678
update test_grdclip()
willschlitzer May 8, 2021
908e711
add test for not setting outgrid
willschlitzer May 8, 2021
07936b9
Apply suggestions from code review
willschlitzer May 9, 2021
5b9e83b
change clip parameters to accept lists
willschlitzer May 9, 2021
e87c778
change test_grdclip_no_outgrid() to use list arguments for the clippi…
willschlitzer May 9, 2021
a1c0bdf
Apply suggestions from code review
willschlitzer May 10, 2021
ac0e20c
Apply suggestions from code review
willschlitzer May 11, 2021
e2027a6
update grd file in test_grdclip.py to use 01d resolution
willschlitzer May 11, 2021
f25990d
Apply suggestions from code review
willschlitzer May 11, 2021
38c245e
update grdclip.py docstrings
willschlitzer May 12, 2021
d233ab2
add tuple as acceptable value to parameters
willschlitzer May 12, 2021
17d926c
Merge branch 'master' into wrap-grdclip
willschlitzer May 12, 2021
74284b7
Apply suggestions from code review
willschlitzer May 14, 2021
fe8c334
change example code for below, interval, and old to use list format
willschlitzer May 14, 2021
ce774d4
Merge branch 'master' into wrap-grdclip
willschlitzer May 14, 2021
a1094cf
Merge branch 'master' into wrap-grdclip
willschlitzer May 15, 2021
d32a7ff
Apply suggestions from code review
willschlitzer May 15, 2021
b8683d6
fix alias names
willschlitzer May 15, 2021
1046419
remove "repeat" wording from docstring
willschlitzer May 16, 2021
aa75369
Merge branch 'master' into wrap-grdclip
seisman May 17, 2021
e327245
Update pygmt/src/grdclip.py
willschlitzer May 17, 2021
d08de92
Merge branch 'master' into wrap-grdclip
seisman May 18, 2021
49dd0ff
Merge branch 'master' into wrap-grdclip
willschlitzer May 18, 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 @@ -88,6 +88,7 @@ Operations on grids:
.. autosummary::
:toctree: generated

grdclip
grdcut
grdfilter
grdtrack
Expand Down
1 change: 1 addition & 0 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
blockmedian,
config,
grd2cpt,
grdclip,
grdcut,
grdfilter,
grdinfo,
Expand Down
1 change: 1 addition & 0 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pygmt.src.config import config
from pygmt.src.contour import contour
from pygmt.src.grd2cpt import grd2cpt
from pygmt.src.grdclip import grdclip
from pygmt.src.grdcontour import grdcontour
from pygmt.src.grdcut import grdcut
from pygmt.src.grdfilter import grdfilter
Expand Down
89 changes: 89 additions & 0 deletions pygmt/src/grdclip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
grdclip - Change the range and extremes of grid values.
"""

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",
Sa="above",
Sb="below",
Si="between",
Sr="new",
V="verbose",
)
@kwargs_to_strings(
R="sequence",
Sa="sequence",
Sb="sequence",
Si="sequence",
Sr="sequence",
)
def grdclip(grid, **kwargs):
r"""
Sets values in a grid that meet certain criteria to a new value.

Produce a clipped ``outgrid`` or :class:`xarray.DataArray` version of the
input ``grid`` file.

The parameters ``above`` and ``below`` allow for a given value to be set
for values above or below a set amount, respectively. This allows for
extreme values in a grid, such as points below a certain depth when
plotting Earth relief, to all be set to the same value.

Full option list at :gmt-docs:`grdclip.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}
above : str or list or tuple
[*high*, *above*].
Set all data[i] > *high* to *above*.
below : str or list or tuple
[*low*, *below*].
Set all data[i] < *low* to *below*.
between : str or list or tuple
[*low*, *high*, *between*].
Set all data[i] >= *low* and <= *high* to *between*.
new : str or list or tuple
[*old*, *new*].
Set all data[i] == *old* to *new*. This is mostly useful when
your data are known to be integer values.
{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("grdclip", 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
47 changes: 47 additions & 0 deletions pygmt/tests/test_grdclip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Tests for grdclip.
"""
import os

import numpy.testing as npt
import pytest
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved
from pygmt import grdclip, grdinfo
from pygmt.datasets import load_earth_relief
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_grdclip_outgrid(grid):
"""
Test the below and above parameters for grdclip and creates a test outgrid.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = grdclip(
grid=grid, outgrid=tmpfile.name, below=[-1500, -1800], above=[-200, 40]
)
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 int(result[4]) == -1800 # minimum value
assert int(result[5]) == 40 # maximum value


def test_grdclip_no_outgrid(grid):
"""
Test the below and above parameters for grdclip with no set outgrid.
"""
temp_grid = grdclip(grid=grid, below=[-1500, -1800], above=[-200, 40])
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(), -1800)
npt.assert_allclose(temp_grid.max(), 40)