Skip to content

Commit a99533f

Browse files
arleamanMeghan Jonesseisman
authored
Wrap blockmode (#1456)
Co-authored-by: Meghan Jones <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent c81555e commit a99533f

File tree

5 files changed

+103
-10
lines changed

5 files changed

+103
-10
lines changed

doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Operations on tabular data:
8181

8282
blockmean
8383
blockmedian
84+
blockmode
8485
surface
8586

8687
Operations on grids:

pygmt/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from pygmt.src import (
3333
blockmean,
3434
blockmedian,
35+
blockmode,
3536
config,
3637
grd2cpt,
3738
grdclip,

pygmt/src/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# pylint: disable=import-outside-toplevel
55

66
from pygmt.src.basemap import basemap
7-
from pygmt.src.blockm import blockmean, blockmedian
7+
from pygmt.src.blockm import blockmean, blockmedian, blockmode
88
from pygmt.src.coast import coast
99
from pygmt.src.colorbar import colorbar
1010
from pygmt.src.config import config

pygmt/src/blockm.py

+87-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
blockm - Block average (x,y,z) data tables by mean or median estimation.
2+
blockm - Block average (x,y,z) data tables by mean, median, or mode estimation.
33
"""
44
import pandas as pd
55
from pygmt.clib import Session
@@ -14,18 +14,19 @@
1414

1515
def _blockm(block_method, table, outfile, x, y, z, **kwargs):
1616
r"""
17-
Block average (x,y,z) data tables by mean or median estimation.
17+
Block average (x,y,z) data tables by mean, median, or mode estimation.
1818
1919
Reads arbitrarily located (x,y,z) triples [or optionally weighted
20-
quadruples (x,y,z,w)] from a table and writes to the output a mean or
21-
median (depending on ``block_method``) position and value for every
22-
non-empty block in a grid region defined by the ``region`` and ``spacing``
23-
parameters.
20+
quadruples (x,y,z,w)] from a table and writes to the output a mean,
21+
median, or mode (depending on ``block_method``) position and value for
22+
every non-empty block in a grid region defined by the ``region`` and
23+
``spacing`` parameters.
2424
2525
Parameters
2626
----------
2727
block_method : str
28-
Name of the GMT module to call. Must be "blockmean" or "blockmedian".
28+
Name of the GMT module to call. Must be "blockmean", "blockmedian" or
29+
"blockmode".
2930
3031
Returns
3132
-------
@@ -226,3 +227,82 @@ def blockmedian(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs):
226227
z=z,
227228
**kwargs
228229
)
230+
231+
232+
@fmt_docstring
233+
@use_alias(
234+
I="spacing",
235+
R="region",
236+
V="verbose",
237+
a="aspatial",
238+
b="binary",
239+
d="data",
240+
e="find",
241+
f="coltypes",
242+
h="header",
243+
i="incols",
244+
o="outcols",
245+
r="registration",
246+
s="skiprows",
247+
w="wrap",
248+
)
249+
@kwargs_to_strings(R="sequence", i="sequence_comma")
250+
def blockmode(table=None, outfile=None, *, x=None, y=None, z=None, **kwargs):
251+
r"""
252+
Block average (x,y,z) data tables by mode estimation.
253+
254+
Reads arbitrarily located (x,y,z) triples [or optionally weighted
255+
quadruples (x,y,z,w)] and writes to the output a mode position and value
256+
for every non-empty block in a grid region defined by the ``region`` and
257+
``spacing`` parameters.
258+
259+
Takes a matrix, xyz triplets, or a file name as input.
260+
261+
Must provide either ``table`` or ``x``, ``y``, and ``z``.
262+
263+
Full option list at :gmt-docs:`blockmode.html`
264+
265+
{aliases}
266+
267+
Parameters
268+
----------
269+
table : str or {table-like}
270+
Pass in (x, y, z) or (longitude, latitude, elevation) values by
271+
providing a file name to an ASCII data table, a 2D
272+
{table-classes}.
273+
x/y/z : 1d arrays
274+
Arrays of x and y coordinates and values z of the data points.
275+
276+
{I}
277+
278+
{R}
279+
280+
outfile : str
281+
The file name for the output ASCII file.
282+
283+
{V}
284+
{a}
285+
{b}
286+
{d}
287+
{e}
288+
{f}
289+
{h}
290+
{i}
291+
{o}
292+
{r}
293+
{s}
294+
{w}
295+
296+
Returns
297+
-------
298+
output : pandas.DataFrame or None
299+
Return type depends on whether the ``outfile`` parameter is set:
300+
301+
- :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile``
302+
is not set.
303+
- None if ``outfile`` is set (filtered output will be stored in file
304+
set by ``outfile``).
305+
"""
306+
return _blockm(
307+
block_method="blockmode", table=table, outfile=outfile, x=x, y=y, z=z, **kwargs
308+
)

pygmt/tests/test_blockmean.py renamed to pygmt/tests/test_blockm.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
2-
Tests for blockmean.
2+
Tests for blockmean and blockmode.
33
"""
44
import os
55

66
import numpy.testing as npt
77
import pandas as pd
88
import pytest
9-
from pygmt import blockmean
9+
from pygmt import blockmean, blockmode
1010
from pygmt.datasets import load_sample_bathymetry
1111
from pygmt.exceptions import GMTInvalidInput
1212
from pygmt.helpers import GMTTempFile, data_kind
@@ -96,3 +96,14 @@ def test_blockmean_without_outfile_setting():
9696
assert isinstance(output, pd.DataFrame)
9797
assert output.shape == (5849, 3)
9898
npt.assert_allclose(output.iloc[0], [245.888877, 29.978707, -384.0])
99+
100+
101+
def test_blockmode_input_dataframe(dataframe):
102+
"""
103+
Run blockmode by passing in a pandas.DataFrame as input.
104+
"""
105+
output = blockmode(table=dataframe, spacing="5m", region=[245, 255, 20, 30])
106+
assert isinstance(output, pd.DataFrame)
107+
assert all(dataframe.columns == output.columns)
108+
assert output.shape == (5849, 3)
109+
npt.assert_allclose(output.iloc[0], [245.88819, 29.97895, -385.0])

0 commit comments

Comments
 (0)