From 4d37512e4974846e7afa85b3f3e842173d62646f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 11 Jul 2021 14:33:26 +0100 Subject: [PATCH 01/23] add sphdistance.py and import statements --- pygmt/__init__.py | 1 + pygmt/src/__init__.py | 1 + pygmt/src/sphdistance.py | 69 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 pygmt/src/sphdistance.py diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 972f6474286..f1f12e2cf99 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -43,6 +43,7 @@ grdtrack, info, makecpt, + sphdistance, surface, which, x2sys_cross, diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index f8bae667f02..69d757c77e3 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -32,6 +32,7 @@ from pygmt.src.plot import plot from pygmt.src.plot3d import plot3d from pygmt.src.rose import rose +from pygmt.src.sphdistance import sphdistance from pygmt.src.solar import solar from pygmt.src.subplot import set_panel, subplot from pygmt.src.surface import surface diff --git a/pygmt/src/sphdistance.py b/pygmt/src/sphdistance.py new file mode 100644 index 00000000000..3409fee27b9 --- /dev/null +++ b/pygmt/src/sphdistance.py @@ -0,0 +1,69 @@ +""" +sphdistance - Create Voronoi distance, node, +or natural nearest-neighbor grid 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", + R="region", + I="increment", +) +@kwargs_to_strings(R="sequence") +def sphdistance(table, **kwargs): + r""" + {aliases} + Parameters + ---------- + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {I} + {R} + + 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("sphdistance", 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 \ No newline at end of file From 5b829b191e39600ea85ac3a53d89832e43c142d9 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 11 Jul 2021 14:54:54 +0100 Subject: [PATCH 02/23] add sphdistance to index.rst --- doc/api/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index 40ab49ac430..507eae60ea9 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -95,6 +95,7 @@ Operations on grids: grdlandmask grdgradient grdtrack + sphdistance Crossover analysis with x2sys: From 41491db0f5873863d4b829c1576332f08f70103c Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 11 Jul 2021 14:57:52 +0100 Subject: [PATCH 03/23] add if statement for required arguments --- pygmt/src/__init__.py | 2 +- pygmt/src/sphdistance.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index 69d757c77e3..d920b4e5158 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -32,8 +32,8 @@ from pygmt.src.plot import plot from pygmt.src.plot3d import plot3d from pygmt.src.rose import rose -from pygmt.src.sphdistance import sphdistance from pygmt.src.solar import solar +from pygmt.src.sphdistance import sphdistance 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/sphdistance.py b/pygmt/src/sphdistance.py index 3409fee27b9..b5a7bbb666d 100644 --- a/pygmt/src/sphdistance.py +++ b/pygmt/src/sphdistance.py @@ -42,6 +42,8 @@ def sphdistance(table, **kwargs): - None if ``outgrid`` is set (grid output will be stored in file set by ``outgrid``) """ + if "I" not in kwargs.keys() or "R" not in kwargs.keys(): + raise GMTInvalidInput("Both 'region' and 'increment' must be specified.") kind = data_kind(table) with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: @@ -66,4 +68,4 @@ def sphdistance(table, **kwargs): else: result = None # if user sets an outgrid, return None - return result \ No newline at end of file + return result From 6a5801440b51f79473b480b47cf6272a81bf398e Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 11 Jul 2021 15:17:51 +0100 Subject: [PATCH 04/23] add sequence for increment in sphdistance.py --- pygmt/src/sphdistance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/sphdistance.py b/pygmt/src/sphdistance.py index b5a7bbb666d..26775d1c378 100644 --- a/pygmt/src/sphdistance.py +++ b/pygmt/src/sphdistance.py @@ -22,7 +22,7 @@ R="region", I="increment", ) -@kwargs_to_strings(R="sequence") +@kwargs_to_strings(I="sequence", R="sequence") def sphdistance(table, **kwargs): r""" {aliases} From 164d414ff81cb289b0923055c785f8a1d9a22c8b Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 11 Jul 2021 15:24:37 +0100 Subject: [PATCH 05/23] add test_sphdistance.py --- pygmt/tests/test_sphdistance.py | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 pygmt/tests/test_sphdistance.py diff --git a/pygmt/tests/test_sphdistance.py b/pygmt/tests/test_sphdistance.py new file mode 100644 index 00000000000..f4d18546883 --- /dev/null +++ b/pygmt/tests/test_sphdistance.py @@ -0,0 +1,57 @@ +""" +Tests for sphdistance. +""" +import os + +import numpy as np +import pytest +from pygmt import grdinfo, sphdistance +from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import GMTTempFile + + +@pytest.fixture(scope="module", name="array") +def fixture_table(): + """ + Load the table data. + """ + coords_list = [[85.5, 22.3], [82.3, 22.6], [85.8, 22.4], [86.5, 23.3]] + return np.array(coords_list) + + +def test_sphdistance_outgrid(array): + """ + Test sphdistance with a set outgrid. + """ + with GMTTempFile(suffix=".nc") as tmpfile: + result = sphdistance( + table=array, outgrid=tmpfile.name, increment=1, region=[82, 87, 22, 24] + ) + assert result is None # return value is None + assert os.path.exists(path=tmpfile.name) # check that outgrid exists + + +def test_sphdistance_no_outgrid(array): + """ + Test sphdistance with no set outgrid. + """ + temp_grid = sphdistance(table=array, increment=[1, 2], region=[82, 87, 22, 24]) + 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]) == 82 # x minimum + assert int(result[1]) == 87 # x maximum + assert int(result[2]) == 22 # y minimum + assert int(result[3]) == 24 # y maximum + assert int(result[6]) == 1 # x increment + assert int(result[7]) == 2 # y increment + + +def test_sphdistance_fails(array): + """ + Check that sphdistance fails correctly when neither increment nor region is + given. + """ + with pytest.raises(GMTInvalidInput): + sphdistance(table=array) From ae2afcf303095281895319c9b8bc99cf04b5dad6 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 12 Jul 2021 17:40:26 +0100 Subject: [PATCH 06/23] add sphdistance docstring to sphdistance.py --- pygmt/src/sphdistance.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pygmt/src/sphdistance.py b/pygmt/src/sphdistance.py index 26775d1c378..f4c31b0065c 100644 --- a/pygmt/src/sphdistance.py +++ b/pygmt/src/sphdistance.py @@ -19,13 +19,21 @@ @fmt_docstring @use_alias( G="outgrid", - R="region", I="increment", + R="region", ) @kwargs_to_strings(I="sequence", R="sequence") def sphdistance(table, **kwargs): r""" + Create Voroni polygons from lat/long coordinates. + + Reads one or more ASCII [or binary] files (or standard + input) containing lon, lat and performs the construction of Voronoi + polygons. These polygons are then processed to calculate the nearest + distance to each node of the lattice and written to the specified grid. + {aliases} + Parameters ---------- outgrid : str or None From 0113d485ec6f50822d234b213aac96a7badb44ed Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 14 Jul 2021 17:11:03 +0100 Subject: [PATCH 07/23] add load_hotspot to samples.py --- pygmt/datasets/__init__.py | 1 + pygmt/datasets/samples.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pygmt/datasets/__init__.py b/pygmt/datasets/__init__.py index 1d06e7f08fe..1ac58d23b87 100644 --- a/pygmt/datasets/__init__.py +++ b/pygmt/datasets/__init__.py @@ -5,6 +5,7 @@ from pygmt.datasets.earth_relief import load_earth_relief from pygmt.datasets.samples import ( load_fractures_compilation, + load_hotspots, load_japan_quakes, load_ocean_ridge_points, load_sample_bathymetry, diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 6df2c43354c..b45ef5810a7 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -123,3 +123,30 @@ def load_fractures_compilation(): fname = which("@fractures_06.txt", download="c") data = pd.read_csv(fname, header=None, sep=r"\s+", names=["azimuth", "length"]) return data[["length", "azimuth"]] + + +def load_hotspots(): + """ + + Returns + ------- + data : pandas.DataFrame + The data table. Use ``print(data.describe())`` to see the available + columns. + """ + fname = which("@hotspots.txt", download="c") + with open(fname) as f: + f.readline() + f.readline() + f.readline() + hotspots = [] + for line in f: + line_split = line.strip().split("\t") + # Add coordinates and icon_size of hotspot + hotspot = [float(item.strip()) for item in line_split[0].split()] + hotspot.append(line_split[1].title()) # Add name of hotspot + hotspots.append(hotspot) + data = pd.DataFrame( + hotspots, columns=["longitude", "latitude", "icon_size", "name"] + ) + return data From 244b8bb84e1c4c738e5d68a55fc89c6d1813f2f7 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 16 Jul 2021 16:17:32 +0100 Subject: [PATCH 08/23] add docstring to load_hotspots() --- pygmt/datasets/samples.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index b45ef5810a7..96abf2cb92b 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -127,6 +127,14 @@ def load_fractures_compilation(): def load_hotspots(): """ + Load a table with the locations, names, and suggested icon sizes of + hotspots. + + This is the ``@hotspots.txt`` dataset used in the GMT tutorials. + + The data are downloaded to a cache directory (usually ``~/.gmt/cache``) the + first time you invoke this function. Afterwards, it will load the data from + the cache. So you'll need an internet connection the first time around. Returns ------- From 7c77a8e98a434e22ba1da9a8fb39eab691c043d9 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 16 Jul 2021 16:21:35 +0100 Subject: [PATCH 09/23] add test_hotspots() to test_datasets_samples.py --- pygmt/tests/test_datasets_samples.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index ff55c64d652..f31b9632393 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -3,6 +3,7 @@ """ from pygmt.datasets import ( load_fractures_compilation, + load_hotspots, load_japan_quakes, load_ocean_ridge_points, load_sample_bathymetry, @@ -72,3 +73,10 @@ def test_fractures_compilation(): assert summary.loc["max", "length"] == 984.652 assert summary.loc["min", "azimuth"] == 0.0 assert summary.loc["max", "azimuth"] == 360.0 + +def test_hotspots(): + """ + Check that the @hotspots.txt dataset loads without errors. + """ + data = load_hotspots() + assert data.shape == (55, 4) From 60e381624480797b151f10d09cd6e778a2fb8fcf Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 16 Jul 2021 17:39:45 +0100 Subject: [PATCH 10/23] run make format --- pygmt/datasets/samples.py | 2 +- pygmt/tests/test_datasets_samples.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 96abf2cb92b..560ef2c31c3 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -152,7 +152,7 @@ def load_hotspots(): line_split = line.strip().split("\t") # Add coordinates and icon_size of hotspot hotspot = [float(item.strip()) for item in line_split[0].split()] - hotspot.append(line_split[1].title()) # Add name of hotspot + hotspot.append(line_split[1].title()) # Add name of hotspot hotspots.append(hotspot) data = pd.DataFrame( hotspots, columns=["longitude", "latitude", "icon_size", "name"] diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index f31b9632393..79449e86580 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -74,6 +74,7 @@ def test_fractures_compilation(): assert summary.loc["min", "azimuth"] == 0.0 assert summary.loc["max", "azimuth"] == 360.0 + def test_hotspots(): """ Check that the @hotspots.txt dataset loads without errors. From 1ccc5125f5767ecb60a96fc3b10e159bde9b3d4d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 17 Jul 2021 07:13:52 +0100 Subject: [PATCH 11/23] rename variable --- pygmt/datasets/samples.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 560ef2c31c3..76b0f116901 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -143,12 +143,12 @@ def load_hotspots(): columns. """ fname = which("@hotspots.txt", download="c") - with open(fname) as f: - f.readline() - f.readline() - f.readline() + with open(fname) as hotspot_text: + hotspot_text.readline() + hotspot_text.readline() + hotspot_text.readline() hotspots = [] - for line in f: + for line in hotspot_text: line_split = line.strip().split("\t") # Add coordinates and icon_size of hotspot hotspot = [float(item.strip()) for item in line_split[0].split()] From c4dc675ec86ef67a894cec885182da7f4221c20f Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 17 Jul 2021 07:14:03 +0100 Subject: [PATCH 12/23] add additional tests for test_hotspots() --- pygmt/tests/test_datasets_samples.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index 79449e86580..45123a9552a 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -1,6 +1,8 @@ """ Test basic functionality for loading sample datasets. """ +import pandas as pd + from pygmt.datasets import ( load_fractures_compilation, load_hotspots, @@ -81,3 +83,10 @@ def test_hotspots(): """ data = load_hotspots() assert data.shape == (55, 4) + assert data.columns.values.tolist() == [ + "longitude", + "latitude", + "icon_size", + "name", + ] + assert isinstance(data, pd.DataFrame) From 411b4004f0235f7dfe5f2af2d7afa9d3de63cdab Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 17 Jul 2021 07:21:31 +0100 Subject: [PATCH 13/23] format imports in test_datasets_samples.py --- pygmt/tests/test_datasets_samples.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index 45123a9552a..72a635f0056 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -2,7 +2,6 @@ Test basic functionality for loading sample datasets. """ import pandas as pd - from pygmt.datasets import ( load_fractures_compilation, load_hotspots, From 751f83227ab560e5283922a48b4e125ffd1c8aa0 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 27 Jul 2021 17:30:42 +0100 Subject: [PATCH 14/23] Update pygmt/datasets/samples.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/datasets/samples.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 76b0f116901..88a540a94f0 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -143,18 +143,18 @@ def load_hotspots(): columns. """ fname = which("@hotspots.txt", download="c") - with open(fname) as hotspot_text: - hotspot_text.readline() - hotspot_text.readline() - hotspot_text.readline() - hotspots = [] - for line in hotspot_text: - line_split = line.strip().split("\t") - # Add coordinates and icon_size of hotspot - hotspot = [float(item.strip()) for item in line_split[0].split()] - hotspot.append(line_split[1].title()) # Add name of hotspot - hotspots.append(hotspot) - data = pd.DataFrame( - hotspots, columns=["longitude", "latitude", "icon_size", "name"] + + # Read first 3 columns (space-separated): longitude, latitude and icon_size + lon_lat_size = pd.read_table( + fname, + sep="\s+", + comment="#", + usecols=[0, 1, 2], + names=["longitude", "latitude", "icon_size"], ) + # Read last column (tab-separated): placename + placename = pd.read_table(fname, comment="#", usecols=[1], names=["placename"]) + # Concat first 3 columns with last column to get a 4-column dataframe + data = pd.concat(objs=[lon_lat_size, placename], axis="columns") + return data From 2fa63b07ca6cf30e08c4cf7d52efedf67c433393 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 27 Jul 2021 17:38:55 +0100 Subject: [PATCH 15/23] remove sphdistance --- doc/api/index.rst | 1 - pygmt/__init__.py | 1 - pygmt/src/__init__.py | 1 - pygmt/src/sphdistance.py | 79 --------------------------------- pygmt/tests/test_sphdistance.py | 57 ------------------------ 5 files changed, 139 deletions(-) delete mode 100644 pygmt/src/sphdistance.py delete mode 100644 pygmt/tests/test_sphdistance.py diff --git a/doc/api/index.rst b/doc/api/index.rst index 507eae60ea9..40ab49ac430 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -95,7 +95,6 @@ Operations on grids: grdlandmask grdgradient grdtrack - sphdistance Crossover analysis with x2sys: diff --git a/pygmt/__init__.py b/pygmt/__init__.py index f1f12e2cf99..972f6474286 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -43,7 +43,6 @@ grdtrack, info, makecpt, - sphdistance, surface, which, x2sys_cross, diff --git a/pygmt/src/__init__.py b/pygmt/src/__init__.py index d920b4e5158..f8bae667f02 100644 --- a/pygmt/src/__init__.py +++ b/pygmt/src/__init__.py @@ -33,7 +33,6 @@ from pygmt.src.plot3d import plot3d from pygmt.src.rose import rose from pygmt.src.solar import solar -from pygmt.src.sphdistance import sphdistance 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/sphdistance.py b/pygmt/src/sphdistance.py deleted file mode 100644 index f4c31b0065c..00000000000 --- a/pygmt/src/sphdistance.py +++ /dev/null @@ -1,79 +0,0 @@ -""" -sphdistance - Create Voronoi distance, node, -or natural nearest-neighbor grid 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", -) -@kwargs_to_strings(I="sequence", R="sequence") -def sphdistance(table, **kwargs): - r""" - Create Voroni polygons from lat/long coordinates. - - Reads one or more ASCII [or binary] files (or standard - input) containing lon, lat and performs the construction of Voronoi - polygons. These polygons are then processed to calculate the nearest - distance to each node of the lattice and written to the specified grid. - - {aliases} - - Parameters - ---------- - outgrid : str or None - The name of the output netCDF file with extension .nc to store the grid - in. - {I} - {R} - - 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``) - """ - if "I" not in kwargs.keys() or "R" not in kwargs.keys(): - raise GMTInvalidInput("Both 'region' and 'increment' must be specified.") - 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("sphdistance", 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 diff --git a/pygmt/tests/test_sphdistance.py b/pygmt/tests/test_sphdistance.py deleted file mode 100644 index f4d18546883..00000000000 --- a/pygmt/tests/test_sphdistance.py +++ /dev/null @@ -1,57 +0,0 @@ -""" -Tests for sphdistance. -""" -import os - -import numpy as np -import pytest -from pygmt import grdinfo, sphdistance -from pygmt.exceptions import GMTInvalidInput -from pygmt.helpers import GMTTempFile - - -@pytest.fixture(scope="module", name="array") -def fixture_table(): - """ - Load the table data. - """ - coords_list = [[85.5, 22.3], [82.3, 22.6], [85.8, 22.4], [86.5, 23.3]] - return np.array(coords_list) - - -def test_sphdistance_outgrid(array): - """ - Test sphdistance with a set outgrid. - """ - with GMTTempFile(suffix=".nc") as tmpfile: - result = sphdistance( - table=array, outgrid=tmpfile.name, increment=1, region=[82, 87, 22, 24] - ) - assert result is None # return value is None - assert os.path.exists(path=tmpfile.name) # check that outgrid exists - - -def test_sphdistance_no_outgrid(array): - """ - Test sphdistance with no set outgrid. - """ - temp_grid = sphdistance(table=array, increment=[1, 2], region=[82, 87, 22, 24]) - 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]) == 82 # x minimum - assert int(result[1]) == 87 # x maximum - assert int(result[2]) == 22 # y minimum - assert int(result[3]) == 24 # y maximum - assert int(result[6]) == 1 # x increment - assert int(result[7]) == 2 # y increment - - -def test_sphdistance_fails(array): - """ - Check that sphdistance fails correctly when neither increment nor region is - given. - """ - with pytest.raises(GMTInvalidInput): - sphdistance(table=array) From 15f9325af9c32040f82cdc834005ad8d32b5f6bb Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 28 Jul 2021 14:44:22 +0100 Subject: [PATCH 16/23] update test_hotspots() in test_datasets_samples.py --- pygmt/tests/test_datasets_samples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index 72a635f0056..af3e0aac0df 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -86,6 +86,6 @@ def test_hotspots(): "longitude", "latitude", "icon_size", - "name", + "placename", ] assert isinstance(data, pd.DataFrame) From 23f63434360acadaf5d31f2bfa5af32e7d61b79a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 31 Jul 2021 06:42:55 +0100 Subject: [PATCH 17/23] add "@hotspots.txt" to testing.py --- pygmt/helpers/testing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pygmt/helpers/testing.py b/pygmt/helpers/testing.py index 858ee786275..270b21c3f39 100644 --- a/pygmt/helpers/testing.py +++ b/pygmt/helpers/testing.py @@ -163,6 +163,7 @@ def download_test_data(): "@N35E135.earth_relief_03s_g.nc", # Other cache files "@fractures_06.txt", + "@hotspots.txt", "@ridge.txt", "@srtm_tiles.nc", # needed for 03s and 01s relief data "@Table_5_11.txt", From e4f8584b2ccb6f1d3c3cc6da4c7ac6b08696cc54 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 9 Aug 2021 09:35:37 +0100 Subject: [PATCH 18/23] Update pygmt/datasets/samples.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/datasets/samples.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 88a540a94f0..8aa49c4e248 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -139,8 +139,8 @@ def load_hotspots(): Returns ------- data : pandas.DataFrame - The data table. Use ``print(data.describe())`` to see the available - columns. + The data table with columns "longitude", "latitude", "icon_size", and + "placename". """ fname = which("@hotspots.txt", download="c") From 8089ef9ecbfe8fff8323e6d37db6dd342dde47fa Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sun, 22 Aug 2021 07:33:50 +0100 Subject: [PATCH 19/23] update load_hotspots() for new format of source file --- pygmt/datasets/samples.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 8aa49c4e248..9dcb35dde6e 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -143,18 +143,6 @@ def load_hotspots(): "placename". """ fname = which("@hotspots.txt", download="c") - - # Read first 3 columns (space-separated): longitude, latitude and icon_size - lon_lat_size = pd.read_table( - fname, - sep="\s+", - comment="#", - usecols=[0, 1, 2], - names=["longitude", "latitude", "icon_size"], - ) - # Read last column (tab-separated): placename - placename = pd.read_table(fname, comment="#", usecols=[1], names=["placename"]) - # Concat first 3 columns with last column to get a 4-column dataframe - data = pd.concat(objs=[lon_lat_size, placename], axis="columns") - + columns = ["longitude", "latitude", "icon_size", "placename"] + data = pd.read_table(filepath_or_buffer=fname, sep="\t", skiprows=3, names=columns) return data From 9330943a2f93c31d52ce6d95b8984592d813861d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 24 Aug 2021 16:17:10 +0100 Subject: [PATCH 20/23] Apply suggestions from code review Co-authored-by: Meghan Jones --- pygmt/datasets/samples.py | 6 +++--- pygmt/tests/test_datasets_samples.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 9dcb35dde6e..9f39eb5a20c 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -127,7 +127,7 @@ def load_fractures_compilation(): def load_hotspots(): """ - Load a table with the locations, names, and suggested icon sizes of + Load a table with the locations, names, and suggested symbol sizes of hotspots. This is the ``@hotspots.txt`` dataset used in the GMT tutorials. @@ -139,10 +139,10 @@ def load_hotspots(): Returns ------- data : pandas.DataFrame - The data table with columns "longitude", "latitude", "icon_size", and + The data table with columns "longitude", "latitude", "symbol_size", and "placename". """ fname = which("@hotspots.txt", download="c") - columns = ["longitude", "latitude", "icon_size", "placename"] + columns = ["longitude", "latitude", "symbol_size", "placename"] data = pd.read_table(filepath_or_buffer=fname, sep="\t", skiprows=3, names=columns) return data diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index af3e0aac0df..db707bcb138 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -85,7 +85,7 @@ def test_hotspots(): assert data.columns.values.tolist() == [ "longitude", "latitude", - "icon_size", + "symbol_size", "placename", ] assert isinstance(data, pd.DataFrame) From c146942300a4e649e453275cf8bc1d189dc4024a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 24 Aug 2021 16:19:55 +0100 Subject: [PATCH 21/23] add load_hotspots to docs --- doc/api/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/index.rst b/doc/api/index.rst index 40ab49ac430..ef3e6d32526 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -159,6 +159,7 @@ and store them in the GMT cache folder. datasets.load_sample_bathymetry datasets.load_usgs_quakes datasets.load_fractures_compilation + datasets.load_hotspots .. automodule:: pygmt.exceptions From 6c47e40164c1dcb4ae5e3a834ee95733dfc3883d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Tue, 24 Aug 2021 20:46:21 +0100 Subject: [PATCH 22/23] rename "placename" to "place_name" --- pygmt/datasets/samples.py | 2 +- pygmt/tests/test_datasets_samples.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 9f39eb5a20c..50953ca0195 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -143,6 +143,6 @@ def load_hotspots(): "placename". """ fname = which("@hotspots.txt", download="c") - columns = ["longitude", "latitude", "symbol_size", "placename"] + columns = ["longitude", "latitude", "symbol_size", "place_name"] data = pd.read_table(filepath_or_buffer=fname, sep="\t", skiprows=3, names=columns) return data diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index db707bcb138..ef598215822 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -86,6 +86,6 @@ def test_hotspots(): "longitude", "latitude", "symbol_size", - "placename", + "place_name", ] assert isinstance(data, pd.DataFrame) From 1639d748b242953273d2b92f3ed4c7d1cb15ca6d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Wed, 25 Aug 2021 19:51:25 +0100 Subject: [PATCH 23/23] Update pygmt/datasets/samples.py Co-authored-by: Meghan Jones --- pygmt/datasets/samples.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 50953ca0195..18ce24f0a30 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -130,7 +130,10 @@ def load_hotspots(): Load a table with the locations, names, and suggested symbol sizes of hotspots. - This is the ``@hotspots.txt`` dataset used in the GMT tutorials. + This is the ``@hotspots.txt`` dataset used in the GMT tutorials, with data + from Mueller, Royer, and Lawver, 1993, Geology, vol. 21, pp. 275-278. The + main 5 hotspots used by Doubrovine et al. [2012] have symbol sizes twice + the size of all other hotspots. The data are downloaded to a cache directory (usually ``~/.gmt/cache``) the first time you invoke this function. Afterwards, it will load the data from