Skip to content

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 24 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8ef9703
add sphinterpolate.py
willschlitzer Aug 7, 2021
92a001b
add import statements for sphinterpolate
willschlitzer Aug 7, 2021
6f8ebb8
run make format
willschlitzer Aug 7, 2021
3fe9fe8
add tests for sphinterpolate
willschlitzer Aug 8, 2021
bcb0d2d
add tests in test_sphinterpolate.py
willschlitzer Aug 8, 2021
0318bf5
remove unused imports in test_sphinterpolate.py
willschlitzer Aug 8, 2021
23787e8
remove unused imports in test_sphinterpolate.py
willschlitzer Aug 8, 2021
059120a
update loading table from virtualfile
willschlitzer Aug 10, 2021
5140ac1
remove unused imports
willschlitzer Aug 10, 2021
947d8ad
Apply suggestions from code review
willschlitzer Sep 2, 2021
af04348
Merge branch 'main' into wrap-sphinterpolate
willschlitzer Sep 7, 2021
71418f0
refactor test_sphinterpolate.py to remove grdinfo
willschlitzer Sep 7, 2021
97309b0
move sphdistance to tabular data section in index
willschlitzer Sep 11, 2021
cd19877
add table description to docstring
willschlitzer Sep 11, 2021
584bd4b
Merge branch 'main' into wrap-sphinterpolate
willschlitzer Sep 19, 2021
25cad5e
Apply suggestions from code review
willschlitzer Sep 19, 2021
fb43d6f
Apply suggestions from code review
willschlitzer Sep 23, 2021
f35a8eb
Merge branch 'main' into wrap-sphinterpolate
willschlitzer Sep 23, 2021
a7b12e3
create fixture function for Mars data
willschlitzer Sep 23, 2021
d6aeb0a
remove unused import
willschlitzer Sep 23, 2021
10083cf
Apply suggestions from code review
willschlitzer Sep 25, 2021
c0cfcb6
Apply suggestions from code review
willschlitzer Sep 26, 2021
e7887c3
Apply suggestions from code review
willschlitzer Sep 27, 2021
0d90227
Merge branch 'main' into wrap-sphinterpolate
willschlitzer Sep 27, 2021
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
1 change: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Operations on tabular data:
blockmean
blockmedian
blockmode
sphinterpolate
surface

Operations on grids:
Expand Down
1 change: 1 addition & 0 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
grdtrack,
info,
makecpt,
sphinterpolate,
sphdistance,
surface,
which,
Expand Down
1 change: 1 addition & 0 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from pygmt.src.plot3d import plot3d
from pygmt.src.rose import rose
from pygmt.src.solar import solar
from pygmt.src.sphinterpolate import sphinterpolate
from pygmt.src.sphdistance import sphdistance
from pygmt.src.subplot import set_panel, subplot
from pygmt.src.surface import surface
Expand Down
70 changes: 70 additions & 0 deletions pygmt/src/sphinterpolate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
sphinterpolate - Spherical gridding in tension of data 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="spacing",
R="region",
V="verbose",
)
@kwargs_to_strings(I="sequence", R="sequence")
def sphinterpolate(table, **kwargs):
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.

{aliases}

Parameters
----------
table : 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=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("sphinterpolate", arg_str)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
34 changes: 34 additions & 0 deletions pygmt/tests/test_sphinterpolate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Tests for sphinterpolate.
"""
import os

import numpy.testing as npt
from pygmt import sphinterpolate
from pygmt.helpers import GMTTempFile


def test_sphinterpolate_outgrid():
"""
Test sphinterpolate with a set outgrid.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = sphinterpolate(
table="@mars370d.txt", 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():
"""
Test sphinterpolate with no set outgrid.
"""
temp_grid = sphinterpolate(table="@mars370d.txt", 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)