-
Notifications
You must be signed in to change notification settings - Fork 229
Wrap sph2grd #1434
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
Wrap sph2grd #1434
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
52fae64
wrap sph2grd
willschlitzer e87afc9
add test_sph2grd.py
willschlitzer aee5e27
format fix
willschlitzer 1526f7f
add sph2grd to index.rst
willschlitzer f7a3e9b
Remove unused import
willschlitzer 50e0382
Add xarray functions to test outgrid info
willschlitzer b4f0284
Add rtol parameter for npt assertions
willschlitzer 0f1f7ad
remove unused import in test_sph2grd.py
willschlitzer f98323c
move sph2grd to tabular data section in index
willschlitzer 974f908
add table description to docstring
willschlitzer 41400f9
Apply suggestions from code review
willschlitzer 24f0565
Apply suggestions from code review
willschlitzer 6018650
Merge branch 'main' into wrap-sph2grd
willschlitzer ad9ad50
fix indentation
willschlitzer 7180fc0
fix imports
willschlitzer f848701
fix indentation again
willschlitzer 1fda5b6
remove unused import
willschlitzer 25d15dd
Apply suggestions from code review
willschlitzer f93b98c
add file cache
willschlitzer 9315af0
Update pygmt/src/sph2grd.py
willschlitzer 4f22a4e
change data docstring to reflect sph2grd input data
willschlitzer f3072f2
Merge branch 'main' into wrap-sph2grd
willschlitzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ Operations on tabular data: | |
blockmedian | ||
blockmode | ||
nearneighbor | ||
sph2grd | ||
surface | ||
|
||
Operations on grids: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ | |
info, | ||
makecpt, | ||
nearneighbor, | ||
sph2grd, | ||
sphdistance, | ||
surface, | ||
which, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
sph2grd - Compute grid from spherical harmonic coefficients | ||
""" | ||
from pygmt.clib import Session | ||
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", | ||
b="binary", | ||
h="header", | ||
i="incols", | ||
r="registration", | ||
x="cores", | ||
) | ||
@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma") | ||
def sph2grd(data, **kwargs): | ||
r""" | ||
Create spherical grid files in tension of data. | ||
|
||
Reads a spherical harmonics coefficient table with records of L, M, | ||
C[L,M], S[L,M] and evaluates the spherical harmonic model on the | ||
specified grid. | ||
|
||
Full option list at :gmt-docs:`sph2grd.html` | ||
|
||
{aliases} | ||
|
||
Parameters | ||
---------- | ||
data : str or {table-like} | ||
Pass in data with L, M, C[L,M], S[L,M] 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} | ||
willschlitzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{b} | ||
{h} | ||
{i} | ||
{r} | ||
{x} | ||
|
||
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=data) | ||
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 = " ".join([infile, build_arg_string(kwargs)]) | ||
lib.call_module("sph2grd", arg_str) | ||
|
||
return load_dataarray(outgrid) if outgrid == tmpfile.name else None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
Tests for sph2grd. | ||
""" | ||
import os | ||
|
||
import numpy.testing as npt | ||
from pygmt import sph2grd | ||
from pygmt.helpers import GMTTempFile | ||
|
||
|
||
def test_sph2grd_outgrid(): | ||
""" | ||
Test sph2grd with a set outgrid. | ||
""" | ||
with GMTTempFile(suffix=".nc") as tmpfile: | ||
result = sph2grd( | ||
data="@EGM96_to_36.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_sph2grd_no_outgrid(): | ||
""" | ||
Test sph2grd with no set outgrid. | ||
""" | ||
temp_grid = sph2grd(data="@EGM96_to_36.txt", spacing=1, region="g") | ||
assert temp_grid.dims == ("y", "x") | ||
assert temp_grid.gmt.gtype == 0 # Cartesian grid | ||
assert temp_grid.gmt.registration == 0 # Gridline registration | ||
npt.assert_allclose(temp_grid.max(), 0.00021961, rtol=1e-4) | ||
npt.assert_allclose(temp_grid.min(), -0.0004326, rtol=1e-4) | ||
npt.assert_allclose(temp_grid.median(), -0.00010894, rtol=1e-4) | ||
npt.assert_allclose(temp_grid.mean(), -0.00010968, rtol=1e-4) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.