-
Notifications
You must be signed in to change notification settings - Fork 235
Wrap xyz2grd #636
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
willschlitzer
merged 48 commits into
GenericMappingTools:main
from
carocamargo:newfeature_xyz2grd
Aug 9, 2021
Merged
Wrap xyz2grd #636
Changes from 46 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
884bde7
added gridfilter function
carocamargo c2a7d3e
added to call gridfilter from grdops.py
carocamargo 2cb715d
added grdfilter caller
carocamargo 6139c17
corrected indentation in grdfilter docstring, and made each function …
carocamargo d2e0aee
added usage example to grdfilter
carocamargo db2cdc7
Update pygmt/gridops.py
carocamargo 8b84039
Update pygmt/gridops.py
carocamargo a8dd55c
added more optional aliases
carocamargo 452d4f7
Merge remote-tracking branch 'upstream/master' into newfeature
carocamargo 269ce62
Merge branch 'newfeature' of https://github.com/carocamargo/pygmt int…
carocamargo 9f47f01
added more optional aliases
carocamargo 874bca4
Update gridops.py
carocamargo 0373c32
Update pygmt/gridops.py
carocamargo fd64a4e
Update pygmt/gridops.py
carocamargo df53508
Update pygmt/gridops.py
carocamargo 6fae103
formated code with black
carocamargo e653fa5
added xyz2grd function to gridding.py
carocamargo 3cfff68
modified __init__.py to import xyz2grd from gridding.py
carocamargo b4742fe
Merge branch 'master' into newfeature_xyz2grd
weiji14 e6201e5
Merge branch 'master' into newfeature_xyz2grd
weiji14 162a3d7
Move xyz2grd under pygmt/src and initialize test file
weiji14 fa85878
Merge branch 'master' into newfeature_xyz2grd
willschlitzer 4c5c860
Merge branch 'master' into newfeature_xyz2grd
208c376
change GMT doc link
willschlitzer 72d2e76
Merge branch 'master' into newfeature_xyz2grd
willschlitzer 74ca8a7
rename Z and i parameters
willschlitzer 67efb66
change grid to table
willschlitzer 6b1e49d
add pytest fixture
willschlitzer 549e1e1
add xyz2grd to index.rst
willschlitzer 63e2332
reorganize import in test_xyz2grd.py
willschlitzer 711ac4a
update docstring
willschlitzer b150564
remove "Optional Arguments" sub-header
willschlitzer 456eb1b
add single-line docstring
willschlitzer 34c3d1e
update docstring for xyz2grd.py
willschlitzer 5cd1a0f
change file suffic for GMTTempFile
willschlitzer 3b241a6
add parameter name in test_xyz2grd.py
willschlitzer 6d62c8f
updating test_xyz2grd.py
willschlitzer 2c37450
update xyz2grd.py and test_xyz2grd.py to accept dataframe
willschlitzer 97ece78
add outgrid set test to test_xyz2grd.py
willschlitzer 3e5f7df
remove kind check in xyz2grd.py
willschlitzer d92ec3c
remove optional parameters
willschlitzer 99d81f6
fix doc error
willschlitzer 567d821
remove unused imports
willschlitzer e475165
remove print statement
willschlitzer 38adbb2
shorten docstring
willschlitzer cae8e04
Apply suggestions from code review
willschlitzer 10f2474
Update pygmt/src/xyz2grd.py
weiji14 55e5c07
Merge branch 'main' into newfeature_xyz2grd
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,6 +95,7 @@ Operations on grids: | |
| grdlandmask | ||
| grdgradient | ||
| grdtrack | ||
| xyz2grd | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ | |
| which, | ||
| x2sys_cross, | ||
| x2sys_init, | ||
| xyz2grd, | ||
| ) | ||
|
|
||
| # Get semantic version through setuptools-scm | ||
|
|
||
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,74 @@ | ||
| """ | ||
| xyz2grd - Convert data table to a grid. | ||
| """ | ||
| import xarray as xr | ||
| from pygmt.clib import Session | ||
| from pygmt.helpers import ( | ||
| GMTTempFile, | ||
| build_arg_string, | ||
| fmt_docstring, | ||
| kwargs_to_strings, | ||
| use_alias, | ||
| ) | ||
|
|
||
|
|
||
| @fmt_docstring | ||
| @use_alias( | ||
| G="outgrid", | ||
| I="spacing", | ||
| R="region", | ||
| V="verbose", | ||
| ) | ||
| @kwargs_to_strings(R="sequence") | ||
| def xyz2grd(table, **kwargs): | ||
| """ | ||
| Create a grid file from table data. | ||
|
|
||
| xyz2grd reads one or more z or xyz tables and creates a binary grid file. | ||
| xyz2grd will report if some of the nodes are not filled in with data. Such | ||
| unconstrained nodes are set to a value specified by the user [Default is | ||
| NaN]. Nodes with more than one value will be set to the mean value. | ||
|
|
||
| Full option list at :gmt-docs:`xyz2grd.html` | ||
|
|
||
| Parameters | ||
| ---------- | ||
| table : str or {table-like} | ||
| Pass in either a file name to an ASCII data table, a 1D/2D | ||
| {table-classes}. | ||
|
|
||
| outgrid : str or None | ||
| Optional. 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 ``outfile`` parameter is set: | ||
|
|
||
| - :class:`xarray.DataArray`: if ``outfile`` is not set | ||
| - None if ``outfile`` is set (grid output will be stored in file set by | ||
| ``outfile``)``` | ||
| """ | ||
| with GMTTempFile(suffix=".nc") as tmpfile: | ||
| with Session() as lib: | ||
| 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}) | ||
| outgrid = kwargs["G"] | ||
| arg_str = build_arg_string(kwargs) | ||
| arg_str = " ".join([infile, arg_str]) | ||
| lib.call_module("xyz2grd", 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """ | ||
| Tests for xyz2grd. | ||
| """ | ||
| import os | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import xarray as xr | ||
| from pygmt import grdinfo, xyz2grd | ||
| from pygmt.datasets import load_sample_bathymetry | ||
| from pygmt.helpers import GMTTempFile | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module", name="ship_data") | ||
| def fixture_ship_data(): | ||
| """ | ||
| Load the data from the sample bathymetry dataset. | ||
| """ | ||
| return load_sample_bathymetry() | ||
|
|
||
|
|
||
| def test_xyz2grd_input_file(): | ||
| """ | ||
| Run xyz2grd by passing in a filename. | ||
| """ | ||
| output = xyz2grd(table="@tut_ship.xyz", spacing=5, region=[245, 255, 20, 30]) | ||
| assert isinstance(output, xr.DataArray) | ||
| assert output.gmt.registration == 0 # Gridline registration | ||
| assert output.gmt.gtype == 0 # Cartesian type | ||
| return output | ||
|
|
||
|
|
||
| def test_xyz2grd_input_array(ship_data): | ||
| """ | ||
| Run xyz2grd by passing in a numpy array. | ||
| """ | ||
| output = xyz2grd(table=np.array(ship_data), spacing=5, region=[245, 255, 20, 30]) | ||
| assert isinstance(output, xr.DataArray) | ||
| assert output.gmt.registration == 0 # Gridline registration | ||
| assert output.gmt.gtype == 0 # Cartesian type | ||
| return output | ||
|
|
||
|
|
||
| def test_xyz2grd_input_df(ship_data): | ||
| """ | ||
| Run xyz2grd by passing in a data frame. | ||
| """ | ||
| output = xyz2grd(table=ship_data, spacing=5, region=[245, 255, 20, 30]) | ||
| assert isinstance(output, xr.DataArray) | ||
| assert output.gmt.registration == 0 # Gridline registration | ||
| assert output.gmt.gtype == 0 # Cartesian type | ||
| return output | ||
|
|
||
|
|
||
| def test_xyz2grd_input_array_file_out(ship_data): | ||
| """ | ||
| Run xyz2grd by passing in a numpy array and set an outgrid file. | ||
| """ | ||
| with GMTTempFile(suffix=".nc") as tmpfile: | ||
| result = xyz2grd( | ||
| table=np.array(ship_data), | ||
| spacing=5, | ||
| region=[245, 255, 20, 30], | ||
| outgrid=tmpfile.name, | ||
| ) | ||
| assert result is None # return value is None | ||
| assert os.path.exists(path=tmpfile.name) | ||
| result = grdinfo(tmpfile.name, per_column=True).strip() | ||
| assert result == "245 255 20 30 -3651.06079102 -352.379486084 5 5 3 3 0 0" |
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.