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 grd2xyz #1284

Merged
merged 66 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
17a86be
add grd2xyz import/doc link
willschlitzer May 22, 2021
7e7a7fa
format fix
willschlitzer May 22, 2021
6525ff3
create grd2xyz.py
willschlitzer May 22, 2021
13e309c
create test_grd2xyz.py
willschlitzer May 22, 2021
c08d717
spelling fix; change docstring
willschlitzer May 22, 2021
cd26ff2
Merge branch 'master' into wrap-grd2xyz
willschlitzer May 24, 2021
ccd27c0
Merge branch 'master' into wrap-grd2xyz
willschlitzer May 25, 2021
bf58dc2
allow specifiying format for returned xyz data
willschlitzer May 25, 2021
7f100d5
add format tests in test_grd2xyz.py
willschlitzer May 25, 2021
0701d62
change format parameter to xyz_format
willschlitzer May 25, 2021
789ac47
Merge branch 'master' into wrap-grd2xyz
willschlitzer May 25, 2021
8787892
Merge branch 'master' into wrap-grd2xyz
willschlitzer May 26, 2021
11f1782
Merge branch 'master' into wrap-grd2xyz
willschlitzer May 26, 2021
6ac279c
Update pygmt/src/grd2xyz.py
willschlitzer Jul 6, 2021
a226f34
change xyz_format to output_type
willschlitzer Jul 6, 2021
0b52451
change default output to DataFrame
willschlitzer Jul 6, 2021
37d366d
update info return options
willschlitzer Jul 6, 2021
b27699a
add grd2xyz import/doc link
willschlitzer May 22, 2021
0361381
format fix
willschlitzer May 22, 2021
a9fef07
create grd2xyz.py
willschlitzer May 22, 2021
957d8fe
create test_grd2xyz.py
willschlitzer May 22, 2021
5a4baa5
spelling fix; change docstring
willschlitzer May 22, 2021
1cf9e56
allow specifiying format for returned xyz data
willschlitzer May 25, 2021
948ca73
add format tests in test_grd2xyz.py
willschlitzer May 25, 2021
b050cd8
change format parameter to xyz_format
willschlitzer May 25, 2021
9ab9701
Update pygmt/src/grd2xyz.py
willschlitzer Jul 6, 2021
891ff6c
change xyz_format to output_type
willschlitzer Jul 6, 2021
02cd02f
change default output to DataFrame
willschlitzer Jul 6, 2021
00bece0
update info return options
willschlitzer Jul 6, 2021
3b6fc93
Merge branch 'wrap-grd2xyz' of github.com:GenericMappingTools/pygmt i…
willschlitzer Jul 16, 2021
01613ff
change output name to "data" from "info"
willschlitzer Jul 16, 2021
3da6dda
Apply suggestions from code review
willschlitzer Jul 16, 2021
2dcf859
remove numpy import
willschlitzer Jul 16, 2021
283d515
update test_grd2xyz.py
willschlitzer Jul 16, 2021
77315ba
Merge branch 'master' into wrap-grd2xyz
willschlitzer Jul 16, 2021
29c8be6
Apply suggestions from code review
willschlitzer Aug 25, 2021
c4549e4
Apply suggestions from code review
willschlitzer Aug 30, 2021
66761a8
Apply suggestions from code review
willschlitzer Aug 30, 2021
0a525b6
add test_grd2xyz_file_output()
willschlitzer Aug 30, 2021
8de23a8
assign column names in dataframe
willschlitzer Aug 31, 2021
938532a
remove unused import
willschlitzer Aug 31, 2021
161c94b
adding comments
willschlitzer Aug 31, 2021
317a3dd
adding test_grd2xyz_outfile_incorrect_output_type
willschlitzer Sep 1, 2021
41051a9
style fix
willschlitzer Sep 1, 2021
9eebee3
Apply suggestions from code review
willschlitzer Sep 2, 2021
3ac3642
Apply suggestions from code review
willschlitzer Sep 14, 2021
81b0511
Update pygmt/src/grd2xyz.py
willschlitzer Sep 15, 2021
af6aac9
update test
willschlitzer Sep 16, 2021
e1c6555
Apply suggestions from code review
willschlitzer Sep 16, 2021
1927795
add line for grd2xyz region
willschlitzer Sep 16, 2021
95c7731
Merge remote-tracking branch 'origin/wrap-grd2xyz' into wrap-grd2xyz
willschlitzer Sep 16, 2021
76190ab
update region docstring
willschlitzer Sep 16, 2021
31388eb
add if statement
willschlitzer Sep 16, 2021
924a3e0
Apply suggestions from code review
willschlitzer Sep 19, 2021
9a3c377
update comment for o if statement
willschlitzer Sep 19, 2021
4b1214c
change if statement for o
willschlitzer Sep 20, 2021
59bae5c
add test for setting o and output_type to pandas
willschlitzer Sep 20, 2021
2a11880
run make format
willschlitzer Sep 20, 2021
ff45946
typo fix
willschlitzer Sep 20, 2021
290cf04
Merge branch 'main' into wrap-grd2xyz
willschlitzer Sep 22, 2021
e3336c4
Apply suggestions from code review
willschlitzer Sep 22, 2021
552bf3f
Apply suggestions from code review
willschlitzer Sep 22, 2021
6731814
Update pygmt/src/grd2xyz.py
willschlitzer Sep 22, 2021
f49dac0
fix sentence in docstring
willschlitzer Sep 22, 2021
708e3c4
run make format
willschlitzer Sep 22, 2021
f94df58
Merge branch 'main' into wrap-grd2xyz
willschlitzer Sep 22, 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 @@ -88,6 +88,7 @@ Operations on grids:
.. autosummary::
:toctree: generated

grd2xyz
grdclip
grdcut
grdfill
Expand Down
1 change: 1 addition & 0 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
blockmedian,
config,
grd2cpt,
grd2xyz,
grdclip,
grdcut,
grdfill,
Expand Down
1 change: 1 addition & 0 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pygmt.src.config import config
from pygmt.src.contour import contour
from pygmt.src.grd2cpt import grd2cpt
from pygmt.src.grd2xyz import grd2xyz
from pygmt.src.grdclip import grdclip
from pygmt.src.grdcontour import grdcontour
from pygmt.src.grdcut import grdcut
Expand Down
53 changes: 53 additions & 0 deletions pygmt/src/grd2xyz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
grd2xyz - Convert grid to data table
"""
from pygmt.clib import Session
from pygmt.helpers import (
GMTTempFile,
build_arg_string,
fmt_docstring,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@use_alias(
R="region",
V="verbose",
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved
)
@kwargs_to_strings(R="sequence")
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved
def grd2xyz(grid, **kwargs):
r"""
Create xyz tables from grid files.
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved

Read a binary 2-D grid files and write out
xyz-triplets in ASCII [or binary] format to a standard output.
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved

Full option list at :gmt-docs:`grd2xyz.html`

{aliases}

Parameters
----------
grid : str or xarray.DataArray
The file name of the input grid or the grid loaded as a DataArray.
This is the only required parameter.
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved
{R}
seisman marked this conversation as resolved.
Show resolved Hide resolved
{V}
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
info : str
A string with information about the grid.
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved
"""
with GMTTempFile() as outfile:
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as infile:
arg_str = " ".join(
[infile, build_arg_string(kwargs), "->" + outfile.name]
)
lib.call_module("grd2xyz", arg_str)
result = outfile.read()
return result
27 changes: 27 additions & 0 deletions pygmt/tests/test_grd2xyz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Tests for grd2xyz.
"""
import pytest
from pygmt import grd2xyz
from pygmt.datasets import load_earth_relief


@pytest.fixture(scope="module", name="grid")
def fixture_grid():
"""
Load the grid data from the sample earth_relief file.
"""
return load_earth_relief(resolution="01d", region=[-1, 1, -1, 1])
willschlitzer marked this conversation as resolved.
Show resolved Hide resolved


def test_grd2xyz(grid):
"""
Make sure grd2xyz works as expected.
"""
xyz_data = grd2xyz(grid=grid)
assert xyz_data.strip().split("\n") == [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick comment: I don't think a string-type output is useful. The function should either return numpy 1d/2d arrays or pandas.DataFrame.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seisman could you look at what I just added? As far as I can tell it is working correctly.

Copy link
Member

@weiji14 weiji14 May 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to open an issue to discuss the output format. Currently, the blockm*/grdtrack modules simply outputs pandas.DataFrame/filenames (see #1099) automatically depending on the input type (2 options). The implementation @willschlitzer made at bf58dc2 and 68fab7c returns either numpy/pandas/str depending on the user input (3 options). Best to be consistent across PyGMT on the types of outputs users can expect.

"-0.5 0.5 -4967",
"0.5 0.5 -4852",
"-0.5 -0.5 -4917",
"0.5 -0.5 -4747.5",
]