Skip to content

Commit e10df16

Browse files
willschlitzerweiji14seisman
authored
Expand table-like input options for sphdistance (#1491)
*Add parameter and tests for x/y inputs *Rename "table" parameter to "data" *Improve docstring for new parameters Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent c23a7d6 commit e10df16

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

doc/api/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Operations on tabular data:
8484
blockmode
8585
nearneighbor
8686
sph2grd
87+
sphdistance
8788
sphinterpolate
8889
surface
8990

@@ -102,7 +103,6 @@ Operations on grids:
102103
grdproject
103104
grdsample
104105
grdtrack
105-
sphdistance
106106
xyz2grd
107107

108108
Crossover analysis with x2sys:

pygmt/src/sphdistance.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,28 @@
2222
V="verbose",
2323
)
2424
@kwargs_to_strings(I="sequence", R="sequence")
25-
def sphdistance(table, **kwargs):
25+
def sphdistance(data=None, x=None, y=None, **kwargs):
2626
r"""
27-
Create Voroni polygons from lat/lon coordinates.
27+
Create Voronoi distance, node, or natural nearest-neighbor grid on a
28+
sphere.
2829
29-
Reads one or more ASCII [or binary] files (or standard
30-
input) containing lon, lat and performs the construction of Voronoi
31-
polygons. These polygons are then processed to calculate the nearest
32-
distance to each node of the lattice and written to the specified grid.
30+
Reads a table containing *lon, lat* columns and performs
31+
the construction of Voronoi polygons. These polygons are
32+
then processed to calculate the nearest distance to each
33+
node of the lattice and written to the specified grid.
34+
35+
Full option list at :gmt-docs:`sphdistance.html
3336
3437
{aliases}
3538
3639
Parameters
3740
----------
41+
data : str or {table-like}
42+
Pass in (x, y) or (longitude, latitude) values by
43+
providing a file name to an ASCII data table, a 2D
44+
{table-classes}.
45+
x/y : 1d arrays
46+
Arrays of x and y coordinates.
3847
outgrid : str or None
3948
The name of the output netCDF file with extension .nc to store the grid
4049
in.
@@ -46,6 +55,7 @@ def sphdistance(table, **kwargs):
4655
-------
4756
ret: xarray.DataArray or None
4857
Return type depends on whether the ``outgrid`` parameter is set:
58+
4959
- :class:`xarray.DataArray` if ``outgrid`` is not set
5060
- None if ``outgrid`` is set (grid output will be stored in file set by
5161
``outgrid``)
@@ -54,7 +64,9 @@ def sphdistance(table, **kwargs):
5464
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
5565
with GMTTempFile(suffix=".nc") as tmpfile:
5666
with Session() as lib:
57-
file_context = lib.virtualfile_from_data(check_kind="vector", data=table)
67+
file_context = lib.virtualfile_from_data(
68+
check_kind="vector", data=data, x=x, y=y
69+
)
5870
with file_context as infile:
5971
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
6072
kwargs.update({"G": tmpfile.name})

pygmt/tests/test_sphdistance.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ def fixture_table():
2020
return np.array(coords_list)
2121

2222

23+
def test_sphdistance_xy_inputs():
24+
"""
25+
Test inputs using separate xy arguments.
26+
"""
27+
y = [22.3, 22.6, 22.4, 23.3]
28+
x = [85.5, 82.3, 85.8, 86.5]
29+
temp_grid = sphdistance(x=x, y=y, spacing=[1, 2], region=[82, 87, 22, 24])
30+
assert temp_grid.dims == ("lat", "lon")
31+
assert temp_grid.gmt.gtype == 1 # Geographic grid
32+
assert temp_grid.gmt.registration == 0 # Gridline registration
33+
npt.assert_allclose(temp_grid.max(), 232977.546875)
34+
npt.assert_allclose(temp_grid.min(), 0)
35+
npt.assert_allclose(temp_grid.median(), 0)
36+
npt.assert_allclose(temp_grid.mean(), 62469.17)
37+
38+
2339
def test_sphdistance_outgrid(array):
2440
"""
2541
Test sphdistance with a set outgrid.
2642
"""
2743
with GMTTempFile(suffix=".nc") as tmpfile:
2844
result = sphdistance(
29-
table=array, outgrid=tmpfile.name, spacing=1, region=[82, 87, 22, 24]
45+
data=array, outgrid=tmpfile.name, spacing=1, region=[82, 87, 22, 24]
3046
)
3147
assert result is None # return value is None
3248
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
@@ -36,7 +52,7 @@ def test_sphdistance_no_outgrid(array):
3652
"""
3753
Test sphdistance with no set outgrid.
3854
"""
39-
temp_grid = sphdistance(table=array, spacing=[1, 2], region=[82, 87, 22, 24])
55+
temp_grid = sphdistance(data=array, spacing=[1, 2], region=[82, 87, 22, 24])
4056
assert temp_grid.dims == ("lat", "lon")
4157
assert temp_grid.gmt.gtype == 1 # Geographic grid
4258
assert temp_grid.gmt.registration == 0 # Gridline registration
@@ -52,4 +68,4 @@ def test_sphdistance_fails(array):
5268
given.
5369
"""
5470
with pytest.raises(GMTInvalidInput):
55-
sphdistance(table=array)
71+
sphdistance(data=array)

0 commit comments

Comments
 (0)