-
Notifications
You must be signed in to change notification settings - Fork 224
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 grdsample #1380
Merged
Merged
Wrap grdsample #1380
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9891e49
add grdsample.py and import statements
willschlitzer 8b2a778
add grdsample to index.rst
willschlitzer 54fde72
fix comma spacing
willschlitzer bc60076
add grdsample docstring
willschlitzer 33a9960
add parameter docstrings
willschlitzer 5d0aeb5
formatting fix
willschlitzer 7886931
add test_grdsample.py
willschlitzer 22aafe5
remove unused imports
willschlitzer 54f2ada
add single line description for module in docstring
willschlitzer 9f8c0c7
Apply suggestions from code review
willschlitzer 593d712
add bool option to registration parameter
willschlitzer 2ff1550
move "interpolation" to COMMON_OPTIONS in decorators.py
willschlitzer ad609c3
Apply suggestions from code review
willschlitzer 2c464c0
Merge branch 'master' into wrap-grdsample
michaelgrund 9636ac1
Apply suggestions from code review
willschlitzer ddcb3c5
Merge branch 'main' into wrap-grdsample
willschlitzer ac5fee4
Apply suggestions from code review
willschlitzer 4727aae
Apply suggestions from code review
willschlitzer 09a43de
Apply suggestions from code review
willschlitzer 75c3a51
Merge branch 'main' into wrap-grdsample
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 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 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 |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
grdgradient, | ||
grdinfo, | ||
grdlandmask, | ||
grdsample, | ||
grdtrack, | ||
info, | ||
makecpt, | ||
|
This file contains 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 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,95 @@ | ||
""" | ||
grdsample - Resample a grid onto a new lattice | ||
""" | ||
|
||
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", | ||
J="projection", | ||
I="increment", | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
R="region", | ||
T="translate", | ||
V="verbose", | ||
f="coltypes", | ||
n="interpolation", | ||
r="registration", | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x="cores", | ||
) | ||
@kwargs_to_strings(I="sequence", R="sequence") | ||
def grdsample(grid, **kwargs): | ||
r""" | ||
Change the registration, spacing, or nodes in a grid file. | ||
|
||
This reads a grid file and interpolates it to create a new grid | ||
file. It can change the registration with ``translate`` or | ||
``registration``, change the grid-spacing or number of nodes with | ||
``increment``, and set a new sub-region using ``region``. A bicubic | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[Default], bilinear, B-spline or nearest-neighbor interpolation is set | ||
with ``interpolation``. | ||
|
||
When ``region`` is omitted, the output grid will cover the same region as | ||
the input grid. When ``increment`` is omitted, the grid spacing of the | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
output grid will be the same as the input grid. Either ``registration`` or | ||
``translate`` can be used to change the grid registration. When omitted, | ||
the output grid will have the same registration as the input grid. | ||
|
||
{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. | ||
{I} | ||
{R} | ||
translate : bool | ||
Translate between grid and pixel registration; if the input is | ||
grid-registered, the output will be pixel-registered and vice-versa. | ||
registration : str or bool | ||
[**g**\ |\ **p**\ ]. | ||
Set registration to **g**\ ridline or **p**\ ixel. | ||
{V} | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{f} | ||
{n} | ||
{x} | ||
|
||
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 = " ".join([infile, build_arg_string(kwargs)]) | ||
lib.call_module("grdsample", 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 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,43 @@ | ||
""" | ||
Tests for grdsample. | ||
""" | ||
import os | ||
|
||
import pytest | ||
from pygmt import grdinfo, grdsample | ||
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], registration="pixel" | ||
) | ||
|
||
|
||
def test_grdsample_file_out(grid): | ||
""" | ||
grdsample with an outgrid set and the increment is changed. | ||
""" | ||
with GMTTempFile(suffix=".nc") as tmpfile: | ||
result = grdsample(grid=grid, outgrid=tmpfile.name, increment=[1, 0.5]) | ||
assert result is None # return value is None | ||
assert os.path.exists(path=tmpfile.name) # check that outgrid exists | ||
result = grdinfo(tmpfile.name, per_column=True).strip().split() | ||
assert float(result[6]) == 1 # x-increment | ||
assert float(result[7]) == 0.5 # y-increment | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_grdsample_no_outgrid(grid): | ||
""" | ||
Test grdsample with no set outgrid and applying registration changes. | ||
""" | ||
assert grid.gmt.registration == 1 # Pixel registration | ||
translated_grid = grdsample(grid=grid, translate=True) | ||
assert translated_grid.gmt.registration == 0 # Gridline registration | ||
registration_grid = grdsample(grid=translated_grid, registration="p") | ||
assert registration_grid.gmt.registration == 1 # Pixel registration |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems other modules (e.g.,
grdfilter
) use the aliasspacing
rather thanincrement
. We should keep the alias consistent, no?