-
Notifications
You must be signed in to change notification settings - Fork 228
Wrap sphinterpolate #1418
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 sphinterpolate #1418
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8ef9703
add sphinterpolate.py
willschlitzer 92a001b
add import statements for sphinterpolate
willschlitzer 6f8ebb8
run make format
willschlitzer 3fe9fe8
add tests for sphinterpolate
willschlitzer bcb0d2d
add tests in test_sphinterpolate.py
willschlitzer 0318bf5
remove unused imports in test_sphinterpolate.py
willschlitzer 23787e8
remove unused imports in test_sphinterpolate.py
willschlitzer 059120a
update loading table from virtualfile
willschlitzer 5140ac1
remove unused imports
willschlitzer 947d8ad
Apply suggestions from code review
willschlitzer af04348
Merge branch 'main' into wrap-sphinterpolate
willschlitzer 71418f0
refactor test_sphinterpolate.py to remove grdinfo
willschlitzer 97309b0
move sphdistance to tabular data section in index
willschlitzer cd19877
add table description to docstring
willschlitzer 584bd4b
Merge branch 'main' into wrap-sphinterpolate
willschlitzer 25cad5e
Apply suggestions from code review
willschlitzer fb43d6f
Apply suggestions from code review
willschlitzer f35a8eb
Merge branch 'main' into wrap-sphinterpolate
willschlitzer a7b12e3
create fixture function for Mars data
willschlitzer d6aeb0a
remove unused import
willschlitzer 10083cf
Apply suggestions from code review
willschlitzer c0cfcb6
Apply suggestions from code review
willschlitzer e7887c3
Apply suggestions from code review
willschlitzer 0d90227
Merge branch 'main' into wrap-sphinterpolate
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
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 |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
makecpt, | ||
nearneighbor, | ||
sphdistance, | ||
sphinterpolate, | ||
surface, | ||
which, | ||
x2sys_cross, | ||
|
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,68 @@ | ||
""" | ||
sphinterpolate - Spherical gridding in tension of data on a sphere | ||
""" | ||
from pygmt.clib import Session | ||
from pygmt.helpers import ( | ||
GMTTempFile, | ||
build_arg_string, | ||
fmt_docstring, | ||
kwargs_to_strings, | ||
use_alias, | ||
) | ||
from pygmt.io import load_dataarray | ||
|
||
|
||
@fmt_docstring | ||
@use_alias( | ||
G="outgrid", | ||
I="spacing", | ||
R="region", | ||
V="verbose", | ||
) | ||
@kwargs_to_strings(I="sequence", R="sequence") | ||
def sphinterpolate(data, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to support |
||
r""" | ||
Create spherical grid files in tension of data. | ||
|
||
Reads one or more ASCII [or binary] files (or standard input) containing | ||
*lon, lat, z* and performs a Delaunay triangulation to set up a spherical | ||
interpolation in tension. The final grid is saved to the specified file. | ||
Several options may be used to affect the outcome, such as choosing local | ||
versus global gradient estimation or optimize the tension selection to | ||
satisfy one of four criteria. | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{aliases} | ||
|
||
Parameters | ||
---------- | ||
data : str or {table-like} | ||
Pass in (x, y, z) or (longitude, latitude, elevation) values by | ||
providing a file name to an ASCII data table, a 2D | ||
{table-classes}. | ||
outgrid : str or None | ||
The name of the output netCDF file with extension .nc to store the grid | ||
in. | ||
{I} | ||
{R} | ||
{V} | ||
|
||
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="vector", data=data) | ||
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("sphinterpolate", arg_str) | ||
|
||
return load_dataarray(outgrid) if outgrid == tmpfile.name else None |
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,42 @@ | ||
""" | ||
Tests for sphinterpolate. | ||
""" | ||
import os | ||
|
||
import numpy.testing as npt | ||
import pytest | ||
from pygmt import sphinterpolate | ||
from pygmt.datasets import load_mars_shape | ||
from pygmt.helpers import GMTTempFile | ||
|
||
|
||
@pytest.fixture(scope="module", name="mars") | ||
def fixture_mars_shape(): | ||
""" | ||
Load the data from the sample bathymetry dataset. | ||
""" | ||
return load_mars_shape() | ||
|
||
|
||
def test_sphinterpolate_outgrid(mars): | ||
""" | ||
Test sphinterpolate with a set outgrid. | ||
""" | ||
with GMTTempFile(suffix=".nc") as tmpfile: | ||
result = sphinterpolate(data=mars, outgrid=tmpfile.name, spacing=1, region="g") | ||
assert result is None # return value is None | ||
assert os.path.exists(path=tmpfile.name) # check that outgrid exists | ||
|
||
|
||
def test_sphinterpolate_no_outgrid(mars): | ||
""" | ||
Test sphinterpolate with no set outgrid. | ||
""" | ||
temp_grid = sphinterpolate(data=mars, spacing=1, region="g") | ||
assert temp_grid.dims == ("lat", "lon") | ||
assert temp_grid.gmt.gtype == 1 # Geographic grid | ||
assert temp_grid.gmt.registration == 0 # Gridline registration | ||
npt.assert_allclose(temp_grid.max(), 14628.144) | ||
npt.assert_allclose(temp_grid.min(), -6908.1987) | ||
npt.assert_allclose(temp_grid.median(), 118.96849) | ||
npt.assert_allclose(temp_grid.mean(), 272.60593) |
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.