Skip to content

Commit e716971

Browse files
willschlitzerMeghan Jonesweiji14seisman
authored
Wrap sph2grd (#1434)
Wrapping the sph2grd function. *Add sph2grd tests *Wrap required and common aliases *Add sph2grd to API index Co-authored-by: Meghan Jones <[email protected]> Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent 3861935 commit e716971

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Operations on tabular data:
8383
blockmedian
8484
blockmode
8585
nearneighbor
86+
sph2grd
8687
surface
8788

8889
Operations on grids:

pygmt/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
info,
5050
makecpt,
5151
nearneighbor,
52+
sph2grd,
5253
sphdistance,
5354
surface,
5455
which,

pygmt/helpers/testing.py

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def download_test_data():
165165
"@N37W120.earth_relief_03s_g.nc",
166166
"@N00W090.earth_relief_03m_p.nc",
167167
# Other cache files
168+
"@EGM96_to_36.txt",
168169
"@fractures_06.txt",
169170
"@hotspots.txt",
170171
"@ridge.txt",

pygmt/src/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from pygmt.src.plot3d import plot3d
3838
from pygmt.src.rose import rose
3939
from pygmt.src.solar import solar
40+
from pygmt.src.sph2grd import sph2grd
4041
from pygmt.src.sphdistance import sphdistance
4142
from pygmt.src.subplot import set_panel, subplot
4243
from pygmt.src.surface import surface

pygmt/src/sph2grd.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
sph2grd - Compute grid from spherical harmonic coefficients
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+
b="binary",
22+
h="header",
23+
i="incols",
24+
r="registration",
25+
x="cores",
26+
)
27+
@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma")
28+
def sph2grd(data, **kwargs):
29+
r"""
30+
Create spherical grid files in tension of data.
31+
32+
Reads a spherical harmonics coefficient table with records of L, M,
33+
C[L,M], S[L,M] and evaluates the spherical harmonic model on the
34+
specified grid.
35+
36+
Full option list at :gmt-docs:`sph2grd.html`
37+
38+
{aliases}
39+
40+
Parameters
41+
----------
42+
data : str or {table-like}
43+
Pass in data with L, M, C[L,M], S[L,M] values by
44+
providing a file name to an ASCII data table, a 2D
45+
{table-classes}.
46+
outgrid : str or None
47+
The name of the output netCDF file with extension .nc to store the grid
48+
in.
49+
{I}
50+
{R}
51+
{V}
52+
{b}
53+
{h}
54+
{i}
55+
{r}
56+
{x}
57+
58+
Returns
59+
-------
60+
ret: xarray.DataArray or None
61+
Return type depends on whether the ``outgrid`` parameter is set:
62+
63+
- :class:`xarray.DataArray` if ``outgrid`` is not set
64+
- None if ``outgrid`` is set (grid output will be stored in file set by
65+
``outgrid``)
66+
"""
67+
with GMTTempFile(suffix=".nc") as tmpfile:
68+
with Session() as lib:
69+
file_context = lib.virtualfile_from_data(check_kind="vector", data=data)
70+
with file_context as infile:
71+
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
72+
kwargs.update({"G": tmpfile.name})
73+
outgrid = kwargs["G"]
74+
arg_str = " ".join([infile, build_arg_string(kwargs)])
75+
lib.call_module("sph2grd", arg_str)
76+
77+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/tests/test_sph2grd.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Tests for sph2grd.
3+
"""
4+
import os
5+
6+
import numpy.testing as npt
7+
from pygmt import sph2grd
8+
from pygmt.helpers import GMTTempFile
9+
10+
11+
def test_sph2grd_outgrid():
12+
"""
13+
Test sph2grd with a set outgrid.
14+
"""
15+
with GMTTempFile(suffix=".nc") as tmpfile:
16+
result = sph2grd(
17+
data="@EGM96_to_36.txt", outgrid=tmpfile.name, spacing=1, region="g"
18+
)
19+
assert result is None # return value is None
20+
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
21+
22+
23+
def test_sph2grd_no_outgrid():
24+
"""
25+
Test sph2grd with no set outgrid.
26+
"""
27+
temp_grid = sph2grd(data="@EGM96_to_36.txt", spacing=1, region="g")
28+
assert temp_grid.dims == ("y", "x")
29+
assert temp_grid.gmt.gtype == 0 # Cartesian grid
30+
assert temp_grid.gmt.registration == 0 # Gridline registration
31+
npt.assert_allclose(temp_grid.max(), 0.00021961, rtol=1e-4)
32+
npt.assert_allclose(temp_grid.min(), -0.0004326, rtol=1e-4)
33+
npt.assert_allclose(temp_grid.median(), -0.00010894, rtol=1e-4)
34+
npt.assert_allclose(temp_grid.mean(), -0.00010968, rtol=1e-4)

0 commit comments

Comments
 (0)