Skip to content
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

Wrap xyz2grd #636

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
884bde7
added gridfilter function
carocamargo Sep 17, 2020
c2a7d3e
added to call gridfilter from grdops.py
carocamargo Sep 17, 2020
2cb715d
added grdfilter caller
carocamargo Sep 18, 2020
6139c17
corrected indentation in grdfilter docstring, and made each function …
carocamargo Sep 18, 2020
d2e0aee
added usage example to grdfilter
carocamargo Sep 18, 2020
db2cdc7
Update pygmt/gridops.py
carocamargo Sep 18, 2020
8b84039
Update pygmt/gridops.py
carocamargo Sep 18, 2020
a8dd55c
added more optional aliases
carocamargo Sep 28, 2020
452d4f7
Merge remote-tracking branch 'upstream/master' into newfeature
carocamargo Sep 28, 2020
269ce62
Merge branch 'newfeature' of https://github.com/carocamargo/pygmt int…
carocamargo Sep 28, 2020
9f47f01
added more optional aliases
carocamargo Sep 28, 2020
874bca4
Update gridops.py
carocamargo Sep 28, 2020
0373c32
Update pygmt/gridops.py
carocamargo Sep 29, 2020
fd64a4e
Update pygmt/gridops.py
carocamargo Sep 29, 2020
df53508
Update pygmt/gridops.py
carocamargo Sep 29, 2020
6fae103
formated code with black
carocamargo Sep 29, 2020
e653fa5
added xyz2grd function to gridding.py
carocamargo Sep 29, 2020
3cfff68
modified __init__.py to import xyz2grd from gridding.py
carocamargo Sep 29, 2020
b4742fe
Merge branch 'master' into newfeature_xyz2grd
weiji14 Oct 24, 2020
e6201e5
Merge branch 'master' into newfeature_xyz2grd
weiji14 Feb 17, 2021
162a3d7
Move xyz2grd under pygmt/src and initialize test file
weiji14 Feb 17, 2021
fa85878
Merge branch 'master' into newfeature_xyz2grd
willschlitzer Jun 28, 2021
4c5c860
Merge branch 'master' into newfeature_xyz2grd
maxrjones Jul 12, 2021
208c376
change GMT doc link
willschlitzer Jul 24, 2021
72d2e76
Merge branch 'master' into newfeature_xyz2grd
willschlitzer Jul 24, 2021
74ca8a7
rename Z and i parameters
willschlitzer Jul 24, 2021
67efb66
change grid to table
willschlitzer Jul 24, 2021
6b1e49d
add pytest fixture
willschlitzer Jul 24, 2021
549e1e1
add xyz2grd to index.rst
willschlitzer Jul 24, 2021
63e2332
reorganize import in test_xyz2grd.py
willschlitzer Jul 24, 2021
711ac4a
update docstring
willschlitzer Jul 24, 2021
b150564
remove "Optional Arguments" sub-header
willschlitzer Jul 24, 2021
456eb1b
add single-line docstring
willschlitzer Jul 27, 2021
34c3d1e
update docstring for xyz2grd.py
willschlitzer Jul 31, 2021
5cd1a0f
change file suffic for GMTTempFile
willschlitzer Jul 31, 2021
3b241a6
add parameter name in test_xyz2grd.py
willschlitzer Jul 31, 2021
6d62c8f
updating test_xyz2grd.py
willschlitzer Aug 5, 2021
2c37450
update xyz2grd.py and test_xyz2grd.py to accept dataframe
willschlitzer Aug 5, 2021
97ece78
add outgrid set test to test_xyz2grd.py
willschlitzer Aug 5, 2021
3e5f7df
remove kind check in xyz2grd.py
willschlitzer Aug 5, 2021
d92ec3c
remove optional parameters
willschlitzer Aug 5, 2021
99d81f6
fix doc error
willschlitzer Aug 5, 2021
567d821
remove unused imports
willschlitzer Aug 5, 2021
e475165
remove print statement
willschlitzer Aug 7, 2021
38adbb2
shorten docstring
willschlitzer Aug 7, 2021
cae8e04
Apply suggestions from code review
willschlitzer Aug 8, 2021
10f2474
Update pygmt/src/xyz2grd.py
weiji14 Aug 8, 2021
55e5c07
Merge branch 'main' into newfeature_xyz2grd
weiji14 Aug 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Operations on grids:
grdlandmask
grdgradient
grdtrack
xyz2grd

Crossover analysis with x2sys:

Expand Down
1 change: 1 addition & 0 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
which,
x2sys_cross,
x2sys_init,
xyz2grd,
)

# Get semantic version through setuptools-scm
Expand Down
1 change: 1 addition & 0 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@
from pygmt.src.wiggle import wiggle
from pygmt.src.x2sys_cross import x2sys_cross
from pygmt.src.x2sys_init import x2sys_init
from pygmt.src.xyz2grd import xyz2grd
74 changes: 74 additions & 0 deletions pygmt/src/xyz2grd.py
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 ``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``)```
"""
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
69 changes: 69 additions & 0 deletions pygmt/tests/test_xyz2grd.py
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"