-
Notifications
You must be signed in to change notification settings - Fork 228
Wrap sphdistance #1383
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 sphdistance #1383
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4d37512
add sphdistance.py and import statements
willschlitzer 5b829b1
add sphdistance to index.rst
willschlitzer 41491db
add if statement for required arguments
willschlitzer 6a58014
add sequence for increment in sphdistance.py
willschlitzer 164d414
add test_sphdistance.py
willschlitzer ae2afcf
add sphdistance docstring to sphdistance.py
willschlitzer 66748eb
add verbose parameter
willschlitzer 76dfd9a
update loading table from virtualfile
willschlitzer d75ada0
Apply suggestions from code review
willschlitzer 54c6e35
Merge branch 'main' into wrap-sphdistance
willschlitzer cef091b
change test format to avoid using grdinfo
willschlitzer 16d8c06
remove unused import
willschlitzer 1c17cf7
Merge remote-tracking branch 'origin/wrap-sphdistance' into wrap-sphd…
willschlitzer b247dc4
Apply suggestions from code review
willschlitzer 6a403e8
Merge branch 'main' into wrap-sphdistance
willschlitzer 5f35b08
Apply suggestions from code review
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 |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
grdtrack, | ||
info, | ||
makecpt, | ||
sphdistance, | ||
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,66 @@ | ||
""" | ||
sphdistance - Create Voronoi distance, node, | ||
or natural nearest-neighbor grid on a sphere | ||
""" | ||
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, | ||
) | ||
from pygmt.io import load_dataarray | ||
|
||
|
||
@fmt_docstring | ||
@use_alias( | ||
G="outgrid", | ||
I="increment", | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
R="region", | ||
V="verbose", | ||
) | ||
@kwargs_to_strings(I="sequence", R="sequence") | ||
def sphdistance(table, **kwargs): | ||
r""" | ||
Create Voroni polygons from lat/lon coordinates. | ||
|
||
Reads one or more ASCII [or binary] files (or standard | ||
input) containing lon, lat and performs the construction of Voronoi | ||
polygons. These polygons are then processed to calculate the nearest | ||
distance to each node of the lattice and written to the specified grid. | ||
|
||
{aliases} | ||
|
||
Parameters | ||
---------- | ||
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``) | ||
""" | ||
if "I" not in kwargs.keys() or "R" not in kwargs.keys(): | ||
raise GMTInvalidInput("Both 'region' and 'increment' must be specified.") | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with GMTTempFile(suffix=".nc") as tmpfile: | ||
with Session() as lib: | ||
file_context = lib.virtualfile_from_data(check_kind="vector", data=table) | ||
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 = build_arg_string(kwargs) | ||
arg_str = " ".join([infile, arg_str]) | ||
lib.call_module("sphdistance", 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,55 @@ | ||
""" | ||
Tests for sphdistance. | ||
""" | ||
import os | ||
|
||
import numpy as np | ||
import numpy.testing as npt | ||
import pytest | ||
from pygmt import sphdistance | ||
from pygmt.exceptions import GMTInvalidInput | ||
from pygmt.helpers import GMTTempFile | ||
|
||
|
||
@pytest.fixture(scope="module", name="array") | ||
def fixture_table(): | ||
""" | ||
Load the table data. | ||
""" | ||
coords_list = [[85.5, 22.3], [82.3, 22.6], [85.8, 22.4], [86.5, 23.3]] | ||
return np.array(coords_list) | ||
|
||
|
||
def test_sphdistance_outgrid(array): | ||
""" | ||
Test sphdistance with a set outgrid. | ||
""" | ||
with GMTTempFile(suffix=".nc") as tmpfile: | ||
result = sphdistance( | ||
table=array, outgrid=tmpfile.name, increment=1, region=[82, 87, 22, 24] | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
assert result is None # return value is None | ||
assert os.path.exists(path=tmpfile.name) # check that outgrid exists | ||
|
||
|
||
def test_sphdistance_no_outgrid(array): | ||
""" | ||
Test sphdistance with no set outgrid. | ||
""" | ||
temp_grid = sphdistance(table=array, increment=[1, 2], region=[82, 87, 22, 24]) | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(), 232977.546875) | ||
npt.assert_allclose(temp_grid.min(), 0) | ||
npt.assert_allclose(temp_grid.median(), 0) | ||
npt.assert_allclose(temp_grid.mean(), 62469.17) | ||
|
||
|
||
def test_sphdistance_fails(array): | ||
""" | ||
Check that sphdistance fails correctly when neither increment nor region is | ||
given. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
sphdistance(table=array) |
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.