Skip to content

Commit c126a33

Browse files
author
Meghan Jones
authored
Allow x/y/z input for blockmedian and blockmean (#1319)
1 parent a0b26b4 commit c126a33

File tree

3 files changed

+75
-23
lines changed

3 files changed

+75
-23
lines changed

pygmt/src/blockm.py

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
)
1313

1414

15-
def _blockm(block_method, table, outfile, **kwargs):
15+
def _blockm(block_method, table, outfile, x, y, z, **kwargs):
1616
r"""
1717
Block average (x,y,z) data tables by mean or median estimation.
1818
@@ -41,7 +41,9 @@ def _blockm(block_method, table, outfile, **kwargs):
4141
with GMTTempFile(suffix=".csv") as tmpfile:
4242
with Session() as lib:
4343
# Choose how data will be passed into the module
44-
table_context = lib.virtualfile_from_data(check_kind="vector", data=table)
44+
table_context = lib.virtualfile_from_data(
45+
check_kind="vector", data=table, x=x, y=y, z=z
46+
)
4547
# Run blockm* on data table
4648
with table_context as infile:
4749
if outfile is None:
@@ -73,14 +75,18 @@ def _blockm(block_method, table, outfile, **kwargs):
7375
r="registration",
7476
)
7577
@kwargs_to_strings(R="sequence")
76-
def blockmean(table, outfile=None, **kwargs):
78+
def blockmean(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs):
7779
r"""
7880
Block average (x,y,z) data tables by mean estimation.
7981
8082
Reads arbitrarily located (x,y,z) triples [or optionally weighted
81-
quadruples (x,y,z,w)] from a table and writes to the output a mean
82-
position and value for every non-empty block in a grid region defined by
83-
the ``region`` and ``spacing`` parameters.
83+
quadruples (x,y,z,w)] and writes to the output a mean position and value
84+
for every non-empty block in a grid region defined by the ``region`` and
85+
``spacing`` parameters.
86+
87+
Takes a matrix, xyz triplets, or a file name as input.
88+
89+
Must provide either ``table`` or ``x``, ``y``, and ``z``.
8490
8591
Full option list at :gmt-docs:`blockmean.html`
8692
@@ -92,12 +98,12 @@ def blockmean(table, outfile=None, **kwargs):
9298
Pass in (x, y, z) or (longitude, latitude, elevation) values by
9399
providing a file name to an ASCII data table, a 2D
94100
{table-classes}.
101+
x/y/z : 1d arrays
102+
Arrays of x and y coordinates and values z of the data points.
95103
96104
{I}
97105
98-
region : str or list
99-
*xmin/xmax/ymin/ymax*\[\ **+r**\][**+u**\ *unit*].
100-
Specify the region of interest.
106+
{R}
101107
102108
outfile : str
103109
The file name for the output ASCII file.
@@ -114,11 +120,13 @@ def blockmean(table, outfile=None, **kwargs):
114120
Return type depends on whether the ``outfile`` parameter is set:
115121
116122
- :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile``
117-
is not set
123+
is not set.
118124
- None if ``outfile`` is set (filtered output will be stored in file
119-
set by ``outfile``)
125+
set by ``outfile``).
120126
"""
121-
return _blockm(block_method="blockmean", table=table, outfile=outfile, **kwargs)
127+
return _blockm(
128+
block_method="blockmean", table=table, outfile=outfile, x=x, y=y, z=z, **kwargs
129+
)
122130

123131

124132
@fmt_docstring
@@ -132,14 +140,18 @@ def blockmean(table, outfile=None, **kwargs):
132140
r="registration",
133141
)
134142
@kwargs_to_strings(R="sequence")
135-
def blockmedian(table, outfile=None, **kwargs):
143+
def blockmedian(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs):
136144
r"""
137145
Block average (x,y,z) data tables by median estimation.
138146
139147
Reads arbitrarily located (x,y,z) triples [or optionally weighted
140-
quadruples (x,y,z,w)] from a table and writes to the output a median
141-
position and value for every non-empty block in a grid region defined by
142-
the ``region`` and ``spacing`` parameters.
148+
quadruples (x,y,z,w)] and writes to the output a median position and value
149+
for every non-empty block in a grid region defined by the ``region`` and
150+
``spacing`` parameters.
151+
152+
Takes a matrix, xyz triplets, or a file name as input.
153+
154+
Must provide either ``table`` or ``x``, ``y``, and ``z``.
143155
144156
Full option list at :gmt-docs:`blockmedian.html`
145157
@@ -151,12 +163,12 @@ def blockmedian(table, outfile=None, **kwargs):
151163
Pass in (x, y, z) or (longitude, latitude, elevation) values by
152164
providing a file name to an ASCII data table, a 2D
153165
{table-classes}.
166+
x/y/z : 1d arrays
167+
Arrays of x and y coordinates and values z of the data points.
154168
155169
{I}
156170
157-
region : str or list
158-
*xmin/xmax/ymin/ymax*\[\ **+r**\][**+u**\ *unit*].
159-
Specify the region of interest.
171+
{R}
160172
161173
outfile : str
162174
The file name for the output ASCII file.
@@ -173,8 +185,16 @@ def blockmedian(table, outfile=None, **kwargs):
173185
Return type depends on whether the ``outfile`` parameter is set:
174186
175187
- :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile``
176-
is not set
188+
is not set.
177189
- None if ``outfile`` is set (filtered output will be stored in file
178-
set by ``outfile``)
190+
set by ``outfile``).
179191
"""
180-
return _blockm(block_method="blockmedian", table=table, outfile=outfile, **kwargs)
192+
return _blockm(
193+
block_method="blockmedian",
194+
table=table,
195+
outfile=outfile,
196+
x=x,
197+
y=y,
198+
z=z,
199+
**kwargs
200+
)

pygmt/tests/test_blockmean.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ def test_blockmean_input_table_matrix(dataframe):
4343
npt.assert_allclose(output.iloc[0], [245.888877, 29.978707, -384.0])
4444

4545

46+
def test_blockmean_input_xyz(dataframe):
47+
"""
48+
Run blockmean by passing in x/y/z as input.
49+
"""
50+
output = blockmean(
51+
x=dataframe.longitude,
52+
y=dataframe.latitude,
53+
z=dataframe.bathymetry,
54+
spacing="5m",
55+
region=[245, 255, 20, 30],
56+
)
57+
assert isinstance(output, pd.DataFrame)
58+
assert output.shape == (5849, 3)
59+
npt.assert_allclose(output.iloc[0], [245.888877, 29.978707, -384.0])
60+
61+
4662
def test_blockmean_wrong_kind_of_input_table_grid(dataframe):
4763
"""
4864
Run blockmean using table input that is not a pandas.DataFrame or file but

pygmt/tests/test_blockmedian.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_blockmedian_input_dataframe(dataframe):
3131
npt.assert_allclose(output.iloc[0], [245.88819, 29.97895, -385.0])
3232

3333

34-
def test_blockmedian_wrong_kind_of_input_table_matrix(dataframe):
34+
def test_blockmedian_input_table_matrix(dataframe):
3535
"""
3636
Run blockmedian using table input that is not a pandas.DataFrame but still
3737
a matrix.
@@ -43,6 +43,22 @@ def test_blockmedian_wrong_kind_of_input_table_matrix(dataframe):
4343
npt.assert_allclose(output.iloc[0], [245.88819, 29.97895, -385.0])
4444

4545

46+
def test_blockmedian_input_xyz(dataframe):
47+
"""
48+
Run blockmedian by passing in x/y/z as input.
49+
"""
50+
output = blockmedian(
51+
x=dataframe.longitude,
52+
y=dataframe.latitude,
53+
z=dataframe.bathymetry,
54+
spacing="5m",
55+
region=[245, 255, 20, 30],
56+
)
57+
assert isinstance(output, pd.DataFrame)
58+
assert output.shape == (5849, 3)
59+
npt.assert_allclose(output.iloc[0], [245.88819, 29.97895, -385.0])
60+
61+
4662
def test_blockmedian_wrong_kind_of_input_table_grid(dataframe):
4763
"""
4864
Run blockmedian using table input that is not a pandas.DataFrame or file

0 commit comments

Comments
 (0)