Skip to content

Commit 0926206

Browse files
willschlitzerweiji14seisman
authored
Wrap sphinterpolate (#1418)
Wrapping the sphinterpolate function which creates spherical grid files in tension of data. Official GMT documentation is at https://docs.generic-mapping-tools.org/6.2/sphdistance.html. Aliased outgrid (G), spacing (I), region (R), verbose (V). Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent e716971 commit 0926206

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

doc/api/index.rst

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

8990
Operations on grids:

pygmt/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
nearneighbor,
5252
sph2grd,
5353
sphdistance,
54+
sphinterpolate,
5455
surface,
5556
which,
5657
x2sys_cross,

pygmt/src/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from pygmt.src.solar import solar
4040
from pygmt.src.sph2grd import sph2grd
4141
from pygmt.src.sphdistance import sphdistance
42+
from pygmt.src.sphinterpolate import sphinterpolate
4243
from pygmt.src.subplot import set_panel, subplot
4344
from pygmt.src.surface import surface
4445
from pygmt.src.text import text_ as text # "text" is an argument within "text_"

pygmt/src/sphinterpolate.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
sphinterpolate - Spherical gridding in tension of data on a sphere
3+
"""
4+
from pygmt.clib import Session
5+
from pygmt.helpers import (
6+
GMTTempFile,
7+
build_arg_string,
8+
fmt_docstring,
9+
kwargs_to_strings,
10+
use_alias,
11+
)
12+
from pygmt.io import load_dataarray
13+
14+
15+
@fmt_docstring
16+
@use_alias(
17+
G="outgrid",
18+
I="spacing",
19+
R="region",
20+
V="verbose",
21+
)
22+
@kwargs_to_strings(I="sequence", R="sequence")
23+
def sphinterpolate(data, **kwargs):
24+
r"""
25+
Create spherical grid files in tension of data.
26+
27+
Reads a table containing *lon, lat, z* columns and performs a Delaunay
28+
triangulation to set up a spherical interpolation in tension. Several
29+
options may be used to affect the outcome, such as choosing local versus
30+
global gradient estimation or optimize the tension selection to satisfy one
31+
of four criteria.
32+
33+
Full option list at :gmt-docs:`sphinterpolate.html`
34+
35+
{aliases}
36+
37+
Parameters
38+
----------
39+
data : str or {table-like}
40+
Pass in (x, y, z) or (longitude, latitude, elevation) values by
41+
providing a file name to an ASCII data table, a 2D
42+
{table-classes}.
43+
outgrid : str or None
44+
The name of the output netCDF file with extension .nc to store the grid
45+
in.
46+
{I}
47+
{R}
48+
{V}
49+
50+
Returns
51+
-------
52+
ret: xarray.DataArray or None
53+
Return type depends on whether the ``outgrid`` parameter is set:
54+
55+
- :class:`xarray.DataArray` if ``outgrid`` is not set
56+
- None if ``outgrid`` is set (grid output will be stored in file set by
57+
``outgrid``)
58+
"""
59+
with GMTTempFile(suffix=".nc") as tmpfile:
60+
with Session() as lib:
61+
file_context = lib.virtualfile_from_data(check_kind="vector", data=data)
62+
with file_context as infile:
63+
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
64+
kwargs.update({"G": tmpfile.name})
65+
outgrid = kwargs["G"]
66+
arg_str = " ".join([infile, build_arg_string(kwargs)])
67+
lib.call_module("sphinterpolate", arg_str)
68+
69+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/tests/test_sphinterpolate.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Tests for sphinterpolate.
3+
"""
4+
import os
5+
6+
import numpy.testing as npt
7+
import pytest
8+
from pygmt import sphinterpolate
9+
from pygmt.datasets import load_mars_shape
10+
from pygmt.helpers import GMTTempFile
11+
12+
13+
@pytest.fixture(scope="module", name="mars")
14+
def fixture_mars_shape():
15+
"""
16+
Load the data from the sample bathymetry dataset.
17+
"""
18+
return load_mars_shape()
19+
20+
21+
def test_sphinterpolate_outgrid(mars):
22+
"""
23+
Test sphinterpolate with a set outgrid.
24+
"""
25+
with GMTTempFile(suffix=".nc") as tmpfile:
26+
result = sphinterpolate(data=mars, outgrid=tmpfile.name, spacing=1, region="g")
27+
assert result is None # return value is None
28+
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
29+
30+
31+
def test_sphinterpolate_no_outgrid(mars):
32+
"""
33+
Test sphinterpolate with no set outgrid.
34+
"""
35+
temp_grid = sphinterpolate(data=mars, spacing=1, region="g")
36+
assert temp_grid.dims == ("lat", "lon")
37+
assert temp_grid.gmt.gtype == 1 # Geographic grid
38+
assert temp_grid.gmt.registration == 0 # Gridline registration
39+
npt.assert_allclose(temp_grid.max(), 14628.144)
40+
npt.assert_allclose(temp_grid.min(), -6908.1987)
41+
npt.assert_allclose(temp_grid.median(), 118.96849)
42+
npt.assert_allclose(temp_grid.mean(), 272.60593)

0 commit comments

Comments
 (0)