-
Notifications
You must be signed in to change notification settings - Fork 245
Wrap grdtrack #308
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
weiji14
merged 17 commits into
GenericMappingTools:master
from
weiji14:sampling/grdtrack
Mar 26, 2020
Merged
Wrap grdtrack #308
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a6cef02
Wrap grdtrack
weiji14 97c2b4a
Enable netcdf file input for grdtrack
weiji14 bcfab7f
Modify grdtrack parameter names according to code review
weiji14 028b55a
Remove east_pacific_rise_grid, add test for load_ocean_ridge_points
weiji14 3776214
Add grdtrack and load_ocean_ridge_points tutorial data to docs
weiji14 a37fb0a
Enable ascii file input for grdtrack
weiji14 b1e7b75
Add gallery example 'sampling along tracks' for grdtrack
weiji14 1f94896
Finalize grdtrack wrapper, use numpy assertion, change grid bg to gray
weiji14 421e10d
Merge branch 'master' into sampling/grdtrack
weiji14 e9c8c3d
Merge branch 'master' into sampling/grdtrack
weiji14 7590d4a
Merge branch 'master' into sampling/grdtrack
weiji14 b737684
Merge branch 'master' into sampling/grdtrack
weiji14 afb4382
Fix bathymetry values in test_grdtrack
weiji14 4581a3e
Merge branch 'master' into sampling/grdtrack
weiji14 fb8bb46
Merge branch 'master' into sampling/grdtrack
weiji14 0ed8eb4
Fix test values and truncate docstrings to 79 char
weiji14 f0b6de3
Minor formatting fix
weiji14 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
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,78 @@ | ||
| """ | ||
| GMT modules for Sampling of 1-D and 2-D Data | ||
| """ | ||
| import pandas as pd | ||
| import xarray as xr | ||
|
|
||
| from .clib import Session | ||
| from .helpers import ( | ||
| build_arg_string, | ||
| fmt_docstring, | ||
| GMTTempFile, | ||
| data_kind, | ||
| dummy_context, | ||
| ) | ||
| from .exceptions import GMTInvalidInput | ||
|
|
||
|
|
||
| @fmt_docstring | ||
| def grdtrack(table: pd.DataFrame, grid: xr.DataArray, newcolname: str = "z_", **kwargs): | ||
| """ | ||
| Sample grids at specified (x,y) locations. | ||
|
|
||
| Grdtrack reads one or more grid files and a table with (x,y) [or (lon,lat)] | ||
| positions in the first two columns (more columns may be present). It interpolates | ||
| the grid(s) at the positions in the table and writes out the table with the | ||
| interpolated values added as (one or more) new columns. A bicubic [Default], | ||
| bilinear, B-spline or nearest-neighbor (see -n) interpolation is used, requiring | ||
| boundary conditions at the limits of the region. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| table: pandas.DataFrame | ||
|
weiji14 marked this conversation as resolved.
Outdated
|
||
| Table with (x, y) or (lon, lat) values in the first two columns. More columns | ||
| may be present. | ||
|
|
||
| grid: xarray.DataArray or file (netcdf) | ||
| Gridded array from which to sample values from. | ||
|
|
||
| newcolname: str | ||
|
weiji14 marked this conversation as resolved.
|
||
| Name for the new column in the table where the sampled values will be placed. | ||
| Defaults to "z_". | ||
|
|
||
| Returns | ||
| ------- | ||
| ret: pandas.DataFrame | ||
|
weiji14 marked this conversation as resolved.
Outdated
|
||
| Table with (x, y, ..., z_) or (lon, lat, ..., z_) values. | ||
|
|
||
| """ | ||
| with GMTTempFile(suffix=".csv") as tmpfile: | ||
| with Session() as lib: | ||
| # Store the pandas.DataFrame table in virtualfile | ||
| if data_kind(table) == "matrix": | ||
| table_context = lib.virtualfile_from_matrix(table.values) | ||
| else: | ||
| raise GMTInvalidInput(f"Unrecognized data type {type(table)}") | ||
|
|
||
| # Store the xarray.DataArray grid in virtualfile | ||
| if data_kind(grid) == "grid": | ||
| grid_context = lib.virtualfile_from_grid(grid) | ||
| elif data_kind(grid) == "file": | ||
| grid_context = dummy_context(grid) | ||
| else: | ||
| raise GMTInvalidInput(f"Unrecognized data type {type(grid)}") | ||
|
|
||
| # Run grdtrack on the temporary (csv) table and (netcdf) grid virtualfiles | ||
| with table_context as csvfile: | ||
| with grid_context as grdfile: | ||
| kwargs.update({"G": grdfile}) | ||
| arg_str = " ".join( | ||
| [csvfile, build_arg_string(kwargs), "->" + tmpfile.name] | ||
| ) | ||
| lib.call_module(module="grdtrack", args=arg_str) | ||
|
|
||
| # Read temporary csv output to a pandas table | ||
| column_names = table.columns.to_list() + [newcolname] | ||
| result = pd.read_csv(tmpfile.name, sep="\t", names=column_names) | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| """ | ||
| Tests for grdtrack | ||
| """ | ||
|
|
||
| import pandas as pd | ||
|
weiji14 marked this conversation as resolved.
|
||
| import pytest | ||
|
|
||
| from .. import grdtrack | ||
| from .. import which | ||
| from ..datasets import load_east_pacific_rise_grid, load_ocean_ridge_points | ||
| from ..exceptions import GMTInvalidInput | ||
| from ..helpers import data_kind | ||
|
|
||
|
|
||
| def test_grdtrack_input_dataframe_and_dataarray(): | ||
| """ | ||
| Run grdtrack by passing in a pandas.DataFrame and xarray.DataArray as inputs | ||
| """ | ||
| dataframe = load_ocean_ridge_points() | ||
| dataarray = load_east_pacific_rise_grid() | ||
|
|
||
| output = grdtrack(table=dataframe, grid=dataarray) | ||
| assert isinstance(output, pd.DataFrame) | ||
| assert output.columns.to_list() == ["longitude", "latitude", "z_"] | ||
| assert output.iloc[0].to_list() == [-110.9536, -42.2489, -2950.49576833] | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| def test_grdtrack_input_dataframe_and_ncfile(): | ||
| """ | ||
| Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs | ||
| """ | ||
| dataframe = load_ocean_ridge_points() | ||
| ncfile = which("@spac_33.nc", download="c") | ||
|
|
||
| output = grdtrack(table=dataframe, grid=ncfile) | ||
| assert isinstance(output, pd.DataFrame) | ||
| assert output.columns.to_list() == ["longitude", "latitude", "z_"] | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| def test_grdtrack_input_wrong_kind_of_table(): | ||
| """ | ||
| Run grdtrack using table input that is not a pandas.DataFrame (matrix) | ||
| """ | ||
| dataframe = load_ocean_ridge_points() | ||
| invalid_table = dataframe.longitude.to_xarray() | ||
| dataarray = load_east_pacific_rise_grid() | ||
|
|
||
| assert data_kind(invalid_table) == "grid" | ||
| with pytest.raises(GMTInvalidInput): | ||
| grdtrack(table=invalid_table, grid=dataarray) | ||
|
|
||
|
|
||
| def test_grdtrack_input_wrong_kind_of_grid(): | ||
| """ | ||
| Run grdtrack using grid input that is not as xarray.DataArray (grid) or file | ||
| """ | ||
| dataframe = load_ocean_ridge_points() | ||
| dataarray = load_east_pacific_rise_grid() | ||
| invalid_grid = dataarray.to_dataset() | ||
|
|
||
| assert data_kind(invalid_grid) == "matrix" | ||
| with pytest.raises(GMTInvalidInput): | ||
| grdtrack(table=dataframe, grid=invalid_grid) | ||
|
|
||
|
|
||
| def test_grdtrack_newcolname_setting(): | ||
| """ | ||
| Run grdtrack by passing in a non-default newcolname parameter setting | ||
| """ | ||
| dataframe = load_ocean_ridge_points() | ||
| dataarray = load_east_pacific_rise_grid() | ||
|
|
||
| output = grdtrack(table=dataframe, grid=dataarray, newcolname="bathymetry") | ||
| assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"] | ||
| assert output.iloc[0].to_list() == [-110.9536, -42.2489, -2950.49576833] | ||
|
|
||
| return output | ||
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.