From 8ef9703a5aafdea616a96840c042199fb6ab4bab Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 7 Aug 2021 08:30:23 +0100 Subject: [PATCH 01/20] add sphinterpolate.py --- pygmt/src/sphinterpolate.py | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 pygmt/src/sphinterpolate.py diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py new file mode 100644 index 00000000000..767ce1a0481 --- /dev/null +++ b/pygmt/src/sphinterpolate.py @@ -0,0 +1,80 @@ +""" +sphinterpolate - Spherical gridding in tension of data on a sphere +""" +import xarray as xr +from pygmt.clib import Session +from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import ( + GMTTempFile, + build_arg_string, + data_kind, + dummy_context, + fmt_docstring, + kwargs_to_strings, + use_alias, +) + + +@fmt_docstring +@use_alias( + G="outgrid", + I="increment", + 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 + ---------- + 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``) + """ + kind = data_kind(table) + with GMTTempFile(suffix=".nc") as tmpfile: + with Session() as lib: + if kind == "file": + file_context = dummy_context(table) + elif kind == "matrix": + file_context = lib.virtualfile_from_matrix(matrix=table) + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(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) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() + _ = result.gmt # load GMTDataArray accessor information + else: + result = None # if user sets an outgrid, return None + + return result From 92a001b2db98b4583d3556fcf1baf21f7232666f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 7 Aug 2021 08:30:35 +0100 Subject: [PATCH 02/20] add import statements for sphinterpolate --- doc/api/index.rst | 1 + pygmt/__init__.py | 1 + pygmt/src/__init__.py | 1 + 3 files changed, 3 insertions(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index 40ab49ac430..3fcf0f7d793 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -95,6 +95,7 @@ Operations on grids: grdlandmask grdgradient grdtrack + sphinterpolate Crossover analysis with x2sys: diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 972f6474286..6eadab79e3c 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -43,6 +43,7 @@ grdtrack, info, makecpt, + sphinterpolate, surface, which, x2sys_cross, diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index f8bae667f02..b6f149bcf09 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -34,6 +34,7 @@ from pygmt.src.rose import rose from pygmt.src.solar import solar from pygmt.src.subplot import set_panel, subplot +from pygmt.src.sphinterpolate import sphinterpolate from pygmt.src.surface import surface from pygmt.src.text import text_ as text # "text" is an argument within "text_" from pygmt.src.velo import velo From 6f8ebb89f7d1f9b33902b5a66c51befc16c9789a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 7 Aug 2021 11:42:14 +0100 Subject: [PATCH 03/20] run make format --- pygmt/src/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index b6f149bcf09..3bb26fd1eee 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -33,8 +33,8 @@ from pygmt.src.plot3d import plot3d from pygmt.src.rose import rose from pygmt.src.solar import solar -from pygmt.src.subplot import set_panel, subplot from pygmt.src.sphinterpolate import sphinterpolate +from pygmt.src.subplot import set_panel, subplot from pygmt.src.surface import surface from pygmt.src.text import text_ as text # "text" is an argument within "text_" from pygmt.src.velo import velo From 3fe9fe82ef28e5c8fe758b4a5762a7ee46a9d189 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 8 Aug 2021 07:41:05 +0100 Subject: [PATCH 04/20] add tests for sphinterpolate --- pygmt/tests/test_sphinterpolate.py | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 pygmt/tests/test_sphinterpolate.py diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py new file mode 100644 index 00000000000..ecd7715441a --- /dev/null +++ b/pygmt/tests/test_sphinterpolate.py @@ -0,0 +1,33 @@ +""" +Tests for sphinterpolate. +""" +import os + +import numpy as np +import pytest +from pygmt import grdinfo, sphinterpolate +from pygmt.exceptions import GMTInvalidInput +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, increment=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", increment=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 + result = grdinfo(grid=temp_grid, force_scan="a", per_column="n").strip().split() From bcb0d2dbe32ebd2f22bcdaadd8849f3f688ac8bb Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 8 Aug 2021 07:51:36 +0100 Subject: [PATCH 05/20] add tests in test_sphinterpolate.py --- pygmt/tests/test_sphinterpolate.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index ecd7715441a..79d56aa51f6 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -4,6 +4,7 @@ import os import numpy as np +import numpy.testing as npt import pytest from pygmt import grdinfo, sphinterpolate from pygmt.exceptions import GMTInvalidInput @@ -31,3 +32,9 @@ def test_sphinterpolate_no_outgrid(): assert temp_grid.gmt.gtype == 1 # Geographic grid assert temp_grid.gmt.registration == 0 # Gridline registration result = grdinfo(grid=temp_grid, force_scan="a", per_column="n").strip().split() + assert int(result[0]) == 0 # x minimum + assert int(result[1]) == 360 # x maximum + assert int(result[2]) == -90 # y minimum + assert int(result[3]) == 90 # y maximum + npt.assert_approx_equal(float(result[4]), -6908.19873047) # v minimum + npt.assert_approx_equal(float(result[5]), 14628.1435547) # v maximum From 0318bf57149c1bf46fd6c4e5a3b36e7598f10b2e Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 8 Aug 2021 07:56:56 +0100 Subject: [PATCH 06/20] remove unused imports in test_sphinterpolate.py --- pygmt/tests/test_sphinterpolate.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index 79d56aa51f6..3c539b7a1cc 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -5,9 +5,7 @@ import numpy as np import numpy.testing as npt -import pytest from pygmt import grdinfo, sphinterpolate -from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile From 23787e862759cb16950b9ea910ff7c10e89d18e8 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 8 Aug 2021 07:57:17 +0100 Subject: [PATCH 07/20] remove unused imports in test_sphinterpolate.py --- pygmt/tests/test_sphinterpolate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index 3c539b7a1cc..cb42fb3a684 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -3,7 +3,6 @@ """ import os -import numpy as np import numpy.testing as npt from pygmt import grdinfo, sphinterpolate from pygmt.helpers import GMTTempFile From 059120a9c714c4dea89ca93cb775a0c1f9956b01 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 10 Aug 2021 21:09:43 +0100 Subject: [PATCH 08/20] update loading table from virtualfile --- pygmt/src/sphinterpolate.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index 767ce1a0481..75282be1ec3 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -56,12 +56,7 @@ def sphinterpolate(table, **kwargs): kind = data_kind(table) with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: - if kind == "file": - file_context = dummy_context(table) - elif kind == "matrix": - file_context = lib.virtualfile_from_matrix(matrix=table) - else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(table))) + 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}) From 5140ac1d8167caed1859438408fb00876ee19cf7 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 10 Aug 2021 21:19:12 +0100 Subject: [PATCH 09/20] remove unused imports --- pygmt/src/sphinterpolate.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index 75282be1ec3..4aeb57a56b5 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -7,8 +7,6 @@ from pygmt.helpers import ( GMTTempFile, build_arg_string, - data_kind, - dummy_context, fmt_docstring, kwargs_to_strings, use_alias, @@ -53,7 +51,6 @@ def sphinterpolate(table, **kwargs): - None if ``outgrid`` is set (grid output will be stored in file set by ``outgrid``) """ - kind = data_kind(table) with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="vector", data=table) From 947d8ad934687d8e6d9b252be0e548fad38ef550 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 2 Sep 2021 08:20:14 +0100 Subject: [PATCH 10/20] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/sphinterpolate.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index 4aeb57a56b5..619a2d76cc7 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -1,7 +1,6 @@ """ sphinterpolate - Spherical gridding in tension of data on a sphere """ -import xarray as xr from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( @@ -11,6 +10,7 @@ kwargs_to_strings, use_alias, ) +from pygmt.io import load_dataarray @fmt_docstring @@ -62,11 +62,4 @@ def sphinterpolate(table, **kwargs): arg_str = " ".join([infile, arg_str]) lib.call_module("sphinterpolate", arg_str) - if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray - with xr.open_dataarray(outgrid) as dataarray: - result = dataarray.load() - _ = result.gmt # load GMTDataArray accessor information - else: - result = None # if user sets an outgrid, return None - - return result + return load_dataarray(outgrid) if outgrid == tmpfile.name else None From 71418f06fca4c7b39cb1e0a95af70817c7ec1005 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 7 Sep 2021 10:51:55 +0100 Subject: [PATCH 11/20] refactor test_sphinterpolate.py to remove grdinfo --- pygmt/tests/test_sphinterpolate.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index cb42fb3a684..2360ec3a69e 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -4,7 +4,7 @@ import os import numpy.testing as npt -from pygmt import grdinfo, sphinterpolate +from pygmt import sphinterpolate from pygmt.helpers import GMTTempFile @@ -28,10 +28,7 @@ def test_sphinterpolate_no_outgrid(): assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == 1 # Geographic grid assert temp_grid.gmt.registration == 0 # Gridline registration - result = grdinfo(grid=temp_grid, force_scan="a", per_column="n").strip().split() - assert int(result[0]) == 0 # x minimum - assert int(result[1]) == 360 # x maximum - assert int(result[2]) == -90 # y minimum - assert int(result[3]) == 90 # y maximum - npt.assert_approx_equal(float(result[4]), -6908.19873047) # v minimum - npt.assert_approx_equal(float(result[5]), 14628.1435547) # v maximum + 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) From 97309b0f83320d573068cfb48659eb41f9e3604a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 10 Sep 2021 20:24:18 -0400 Subject: [PATCH 12/20] move sphdistance to tabular data section in index --- doc/api/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/index.rst b/doc/api/index.rst index 98a92777399..7f7ae9f6bc1 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -81,6 +81,7 @@ Operations on tabular data: blockmean blockmedian + sphinterpolate surface Operations on grids: @@ -97,7 +98,6 @@ Operations on grids: grdproject grdsample grdtrack - sphinterpolate xyz2grd Crossover analysis with x2sys: From cd19877a634f021a06db3ba85bd10b1dedc35f46 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 10 Sep 2021 20:25:56 -0400 Subject: [PATCH 13/20] add table description to docstring --- pygmt/src/sphinterpolate.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index 619a2d76cc7..287e41321f5 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -36,6 +36,10 @@ def sphinterpolate(table, **kwargs): 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. From 25cad5e5549233cfb03b015a1df51c1ee9e76a9a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 19 Sep 2021 07:14:48 -0400 Subject: [PATCH 14/20] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/sphinterpolate.py | 3 ++- pygmt/tests/test_sphinterpolate.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index 287e41321f5..eaa728539b9 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -16,7 +16,7 @@ @fmt_docstring @use_alias( G="outgrid", - I="increment", + I="spacing", R="region", V="verbose", ) @@ -51,6 +51,7 @@ def sphinterpolate(table, **kwargs): ------- 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``) diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index 2360ec3a69e..3e701c25e6c 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -14,7 +14,7 @@ def test_sphinterpolate_outgrid(): """ with GMTTempFile(suffix=".nc") as tmpfile: result = sphinterpolate( - table="@mars370d.txt", outgrid=tmpfile.name, increment=1, region="g" + 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 @@ -24,7 +24,7 @@ def test_sphinterpolate_no_outgrid(): """ Test sphinterpolate with no set outgrid. """ - temp_grid = sphinterpolate(table="@mars370d.txt", increment=1, region="g") + 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 From fb43d6f57c1f1007d7fdeaafe5fea579e73249c5 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 23 Sep 2021 08:22:34 +0100 Subject: [PATCH 15/20] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/__init__.py | 2 +- pygmt/src/__init__.py | 2 +- pygmt/src/sphinterpolate.py | 9 ++++----- pygmt/tests/test_sphinterpolate.py | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 30dd0f58946..6c8287102f4 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -47,8 +47,8 @@ grdtrack, info, makecpt, - sphinterpolate, sphdistance, + sphinterpolate, surface, which, x2sys_cross, diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index 69cee5c76af..50746c8f020 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -35,8 +35,8 @@ 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.sphinterpolate import sphinterpolate from pygmt.src.subplot import set_panel, subplot from pygmt.src.surface import surface from pygmt.src.text import text_ as text # "text" is an argument within "text_" diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index eaa728539b9..1239b393f3d 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -21,7 +21,7 @@ V="verbose", ) @kwargs_to_strings(I="sequence", R="sequence") -def sphinterpolate(table, **kwargs): +def sphinterpolate(data, **kwargs): r""" Create spherical grid files in tension of data. @@ -36,7 +36,7 @@ def sphinterpolate(table, **kwargs): Parameters ---------- - table : str or {table-like} + data : 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}. @@ -58,13 +58,12 @@ def sphinterpolate(table, **kwargs): """ with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: - file_context = lib.virtualfile_from_data(check_kind="vector", data=table) + 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 = build_arg_string(kwargs) - arg_str = " ".join([infile, arg_str]) + arg_str = " ".join([infile, build_arg_string(kwargs)]) lib.call_module("sphinterpolate", arg_str) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index 3e701c25e6c..13de9fecde6 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -14,7 +14,7 @@ def test_sphinterpolate_outgrid(): """ with GMTTempFile(suffix=".nc") as tmpfile: result = sphinterpolate( - table="@mars370d.txt", outgrid=tmpfile.name, spacing=1, region="g" + data="@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 @@ -24,7 +24,7 @@ def test_sphinterpolate_no_outgrid(): """ Test sphinterpolate with no set outgrid. """ - temp_grid = sphinterpolate(table="@mars370d.txt", spacing=1, region="g") + temp_grid = sphinterpolate(data="@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 From a7b12e38c38d1699277e8b3eece8d9f44f0eaa47 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 23 Sep 2021 13:14:14 +0100 Subject: [PATCH 16/20] create fixture function for Mars data --- pygmt/tests/test_sphinterpolate.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index 13de9fecde6..abce041bfa1 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -4,27 +4,35 @@ import os import numpy.testing as npt +import pytest from pygmt import sphinterpolate +from pygmt.datasets import load_mars_shape from pygmt.helpers import GMTTempFile -def test_sphinterpolate_outgrid(): +@pytest.fixture(scope="module", name="mars") +def fixture_mars_shape(): + """ + Load the data from the sample bathymetry dataset. + """ + return load_mars_shape() + + +def test_sphinterpolate_outgrid(mars): """ Test sphinterpolate with a set outgrid. """ with GMTTempFile(suffix=".nc") as tmpfile: - result = sphinterpolate( - data="@mars370d.txt", outgrid=tmpfile.name, spacing=1, region="g" - ) + result = sphinterpolate(data=mars, 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(): +def test_sphinterpolate_no_outgrid(mars): """ Test sphinterpolate with no set outgrid. """ - temp_grid = sphinterpolate(data="@mars370d.txt", spacing=1, region="g") + temp_grid = sphinterpolate(data=mars, 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 From d6aeb0a1ef1e3ae311d10090cb02285ec5d31dae Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 23 Sep 2021 13:20:32 +0100 Subject: [PATCH 17/20] remove unused import --- pygmt/src/sphinterpolate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index 1239b393f3d..abbceb0420b 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -2,7 +2,6 @@ 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, From 10083cf9cf70dbee7f96fe2ddc09a9937992a882 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 25 Sep 2021 07:59:37 +0100 Subject: [PATCH 18/20] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/sphinterpolate.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index abbceb0420b..eaf589bb278 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -31,6 +31,8 @@ def sphinterpolate(data, **kwargs): versus global gradient estimation or optimize the tension selection to satisfy one of four criteria. + Full option list at :gmt-docs:`sphinterpolate.html` + {aliases} Parameters From c0cfcb605e901f5f7f5731ddf5918dc011a242c4 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 26 Sep 2021 08:10:48 +0100 Subject: [PATCH 19/20] Apply suggestions from code review Co-authored-by: Dongdong Tian --- pygmt/src/sphinterpolate.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index eaf589bb278..ea294a435f9 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -26,10 +26,9 @@ def sphinterpolate(data, **kwargs): 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. + interpolation in tension. 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. Full option list at :gmt-docs:`sphinterpolate.html` From e7887c388fdb925761ec7539b7077e3078156ce6 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 27 Sep 2021 16:58:29 +0100 Subject: [PATCH 20/20] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/sphinterpolate.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pygmt/src/sphinterpolate.py b/pygmt/src/sphinterpolate.py index ea294a435f9..3e58dcf24c4 100644 --- a/pygmt/src/sphinterpolate.py +++ b/pygmt/src/sphinterpolate.py @@ -24,11 +24,11 @@ def sphinterpolate(data, **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. 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. + Reads a table containing *lon, lat, z* columns and performs a Delaunay + triangulation to set up a spherical interpolation in tension. 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. Full option list at :gmt-docs:`sphinterpolate.html`