-
Notifications
You must be signed in to change notification settings - Fork 235
Add function to import hotspot dataset #1386
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
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
4d37512
add sphdistance.py and import statements
willschlitzer 5b829b1
add sphdistance to index.rst
willschlitzer 41491db
add if statement for required arguments
willschlitzer 6a58014
add sequence for increment in sphdistance.py
willschlitzer 164d414
add test_sphdistance.py
willschlitzer ae2afcf
add sphdistance docstring to sphdistance.py
willschlitzer 0113d48
add load_hotspot to samples.py
willschlitzer 244b8bb
add docstring to load_hotspots()
willschlitzer 7c77a8e
add test_hotspots() to test_datasets_samples.py
willschlitzer 60e3816
run make format
willschlitzer 1ccc512
rename variable
willschlitzer c4dc675
add additional tests for test_hotspots()
willschlitzer 411b400
format imports in test_datasets_samples.py
willschlitzer 751f832
Update pygmt/datasets/samples.py
willschlitzer 2fa63b0
remove sphdistance
willschlitzer 15f9325
update test_hotspots() in test_datasets_samples.py
willschlitzer b1c14c1
Merge branch 'master' into add-hotspot-dataset
willschlitzer 23f6343
add "@hotspots.txt" to testing.py
willschlitzer e4f8584
Update pygmt/datasets/samples.py
willschlitzer 8089ef9
update load_hotspots() for new format of source file
willschlitzer 9330943
Apply suggestions from code review
willschlitzer c146942
add load_hotspots to docs
willschlitzer 09963fd
Merge branch 'add-hotspot-dataset' of github.com:GenericMappingTools/…
willschlitzer 6c47e40
rename "placename" to "place_name"
willschlitzer 1639d74
Update pygmt/datasets/samples.py
willschlitzer d00988a
Merge branch 'main' into add-hotspot-dataset
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 |
|---|---|---|
|
|
@@ -95,6 +95,7 @@ Operations on grids: | |
| grdlandmask | ||
| grdgradient | ||
| grdtrack | ||
| sphdistance | ||
|
|
||
| Crossover analysis with x2sys: | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| grdtrack, | ||
| info, | ||
| makecpt, | ||
| sphdistance, | ||
| surface, | ||
| which, | ||
| x2sys_cross, | ||
|
|
||
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
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,79 @@ | ||
| """ | ||
| 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 |
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,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) |
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.