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

Expand table-like input options for sphdistance #1491

Merged
merged 22 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Operations on tabular data:
blockmode
nearneighbor
sph2grd
sphdistance
surface

Operations on grids:
Expand All @@ -101,7 +102,6 @@ Operations on grids:
grdproject
grdsample
grdtrack
sphdistance
xyz2grd

Crossover analysis with x2sys:
Expand Down
13 changes: 11 additions & 2 deletions pygmt/src/sphdistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
V="verbose",
)
@kwargs_to_strings(I="sequence", R="sequence")
def sphdistance(table, **kwargs):
def sphdistance(data=None, x=None, y=None, **kwargs):
r"""
Create Voroni polygons from lat/lon coordinates.
Copy link
Member

@weiji14 weiji14 Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here. Also, just noticed that this summary doesn't seem correct. It seems to suggest Voronoi polygons are created, but the output is a raster grid, not a vector polygon. Maybe follow https://docs.generic-mapping-tools.org/6.2/sphdistance.html.

Suggested change
Create Voroni polygons from lat/lon coordinates.
Create Voronoi polygons from lat/lon coordinates.


Expand All @@ -35,6 +35,12 @@ def sphdistance(table, **kwargs):

Parameters
weiji14 marked this conversation as resolved.
Show resolved Hide resolved
----------
data : str or {table-like}
Pass in (x, y) or (longitude, latitude) values by
providing a file name to an ASCII data table, a 2D
{table-classes}.
x/y : 1d arrays
Arrays of x and y coordinates.
outgrid : str or None
The name of the output netCDF file with extension .nc to store the grid
in.
Expand All @@ -46,6 +52,7 @@ def sphdistance(table, **kwargs):
-------
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``)
Expand All @@ -54,7 +61,9 @@ def sphdistance(table, **kwargs):
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="vector", data=table)
file_context = lib.virtualfile_from_data(
check_kind="vector", data=data, x=x, y=y
)
with file_context as infile:
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
Expand Down
22 changes: 19 additions & 3 deletions pygmt/tests/test_sphdistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@ def fixture_table():
return np.array(coords_list)


def test_sphdistance_xy_inputs():
"""
Test inputs using separate xy arguments.
"""
y = [22.3, 22.6, 22.4, 23.3]
x = [85.5, 82.3, 85.8, 86.5]
temp_grid = sphdistance(x=x, y=y, spacing=[1, 2], region=[82, 87, 22, 24])
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_outgrid(array):
"""
Test sphdistance with a set outgrid.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = sphdistance(
table=array, outgrid=tmpfile.name, spacing=1, region=[82, 87, 22, 24]
data=array, outgrid=tmpfile.name, spacing=1, region=[82, 87, 22, 24]
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
Expand All @@ -36,7 +52,7 @@ def test_sphdistance_no_outgrid(array):
"""
Test sphdistance with no set outgrid.
"""
temp_grid = sphdistance(table=array, spacing=[1, 2], region=[82, 87, 22, 24])
temp_grid = sphdistance(data=array, spacing=[1, 2], region=[82, 87, 22, 24])
assert temp_grid.dims == ("lat", "lon")
assert temp_grid.gmt.gtype == 1 # Geographic grid
assert temp_grid.gmt.registration == 0 # Gridline registration
Expand All @@ -52,4 +68,4 @@ def test_sphdistance_fails(array):
given.
"""
with pytest.raises(GMTInvalidInput):
sphdistance(table=array)
sphdistance(data=array)