|
1 | 1 | """
|
2 |
| -blockmedian - Block average (x,y,z) data tables by median estimation. |
| 2 | +blockm - Block average (x,y,z) data tables by mean or median estimation. |
3 | 3 | """
|
4 | 4 | import pandas as pd
|
5 | 5 | from pygmt.clib import Session
|
|
15 | 15 | )
|
16 | 16 |
|
17 | 17 |
|
| 18 | +def _blockm(block_method, table, outfile, **kwargs): |
| 19 | + r""" |
| 20 | + Block average (x,y,z) data tables by mean or median estimation. |
| 21 | +
|
| 22 | + Reads arbitrarily located (x,y,z) triples [or optionally weighted |
| 23 | + quadruples (x,y,z,w)] from a table and writes to the output a mean or |
| 24 | + median (depending on ``block_method``) position and value for every |
| 25 | + non-empty block in a grid region defined by the ``region`` and ``spacing`` |
| 26 | + parameters. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + block_method : str |
| 31 | + Name of the GMT module to call. Must be "blockmean" or "blockmedian". |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + output : pandas.DataFrame or None |
| 36 | + Return type depends on whether the ``outfile`` parameter is set: |
| 37 | +
|
| 38 | + - :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile`` |
| 39 | + is not set |
| 40 | + - None if ``outfile`` is set (filtered output will be stored in file |
| 41 | + set by ``outfile``) |
| 42 | + """ |
| 43 | + |
| 44 | + kind = data_kind(table) |
| 45 | + with GMTTempFile(suffix=".csv") as tmpfile: |
| 46 | + with Session() as lib: |
| 47 | + if kind == "matrix": |
| 48 | + if not hasattr(table, "values"): |
| 49 | + raise GMTInvalidInput(f"Unrecognized data type: {type(table)}") |
| 50 | + file_context = lib.virtualfile_from_matrix(table.values) |
| 51 | + elif kind == "file": |
| 52 | + if outfile is None: |
| 53 | + raise GMTInvalidInput("Please pass in a str to 'outfile'") |
| 54 | + file_context = dummy_context(table) |
| 55 | + else: |
| 56 | + raise GMTInvalidInput(f"Unrecognized data type: {type(table)}") |
| 57 | + |
| 58 | + with file_context as infile: |
| 59 | + if outfile is None: |
| 60 | + outfile = tmpfile.name |
| 61 | + arg_str = " ".join([infile, build_arg_string(kwargs), "->" + outfile]) |
| 62 | + lib.call_module(module=block_method, args=arg_str) |
| 63 | + |
| 64 | + # Read temporary csv output to a pandas table |
| 65 | + if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame |
| 66 | + result = pd.read_csv(tmpfile.name, sep="\t", names=table.columns) |
| 67 | + elif outfile != tmpfile.name: # return None if outfile set, output in outfile |
| 68 | + result = None |
| 69 | + |
| 70 | + return result |
| 71 | + |
| 72 | + |
| 73 | +@fmt_docstring |
| 74 | +@use_alias( |
| 75 | + I="spacing", |
| 76 | + R="region", |
| 77 | + V="verbose", |
| 78 | + a="aspatial", |
| 79 | + f="coltypes", |
| 80 | + r="registration", |
| 81 | +) |
| 82 | +@kwargs_to_strings(R="sequence") |
| 83 | +def blockmean(table, outfile=None, **kwargs): |
| 84 | + r""" |
| 85 | + Block average (x,y,z) data tables by mean estimation. |
| 86 | +
|
| 87 | + Reads arbitrarily located (x,y,z) triples [or optionally weighted |
| 88 | + quadruples (x,y,z,w)] from a table and writes to the output a mean |
| 89 | + position and value for every non-empty block in a grid region defined by |
| 90 | + the ``region`` and ``spacing`` parameters. |
| 91 | +
|
| 92 | + Full option list at :gmt-docs:`blockmean.html` |
| 93 | +
|
| 94 | + {aliases} |
| 95 | +
|
| 96 | + Parameters |
| 97 | + ---------- |
| 98 | + table : pandas.DataFrame or str |
| 99 | + Either a pandas dataframe with (x, y, z) or (longitude, latitude, |
| 100 | + elevation) values in the first three columns, or a file name to an |
| 101 | + ASCII data table. |
| 102 | +
|
| 103 | + spacing : str |
| 104 | + *xinc*\[\ *unit*\][**+e**\|\ **n**] |
| 105 | + [/*yinc*\ [*unit*][**+e**\|\ **n**]]. |
| 106 | + *xinc* [and optionally *yinc*] is the grid spacing. |
| 107 | +
|
| 108 | + region : str or list |
| 109 | + *xmin/xmax/ymin/ymax*\[\ **+r**\][**+u**\ *unit*]. |
| 110 | + Specify the region of interest. |
| 111 | +
|
| 112 | + outfile : str |
| 113 | + Required if ``table`` is a file. The file name for the output ASCII |
| 114 | + file. |
| 115 | +
|
| 116 | + {V} |
| 117 | + {a} |
| 118 | + {f} |
| 119 | + {r} |
| 120 | +
|
| 121 | + Returns |
| 122 | + ------- |
| 123 | + output : pandas.DataFrame or None |
| 124 | + Return type depends on whether the ``outfile`` parameter is set: |
| 125 | +
|
| 126 | + - :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile`` |
| 127 | + is not set |
| 128 | + - None if ``outfile`` is set (filtered output will be stored in file |
| 129 | + set by ``outfile``) |
| 130 | + """ |
| 131 | + return _blockm(block_method="blockmean", table=table, outfile=outfile, **kwargs) |
| 132 | + |
| 133 | + |
18 | 134 | @fmt_docstring
|
19 | 135 | @use_alias(
|
20 | 136 | I="spacing",
|
@@ -73,30 +189,4 @@ def blockmedian(table, outfile=None, **kwargs):
|
73 | 189 | - None if ``outfile`` is set (filtered output will be stored in file
|
74 | 190 | set by ``outfile``)
|
75 | 191 | """
|
76 |
| - kind = data_kind(table) |
77 |
| - with GMTTempFile(suffix=".csv") as tmpfile: |
78 |
| - with Session() as lib: |
79 |
| - if kind == "matrix": |
80 |
| - if not hasattr(table, "values"): |
81 |
| - raise GMTInvalidInput(f"Unrecognized data type: {type(table)}") |
82 |
| - file_context = lib.virtualfile_from_matrix(table.values) |
83 |
| - elif kind == "file": |
84 |
| - if outfile is None: |
85 |
| - raise GMTInvalidInput("Please pass in a str to 'outfile'") |
86 |
| - file_context = dummy_context(table) |
87 |
| - else: |
88 |
| - raise GMTInvalidInput(f"Unrecognized data type: {type(table)}") |
89 |
| - |
90 |
| - with file_context as infile: |
91 |
| - if outfile is None: |
92 |
| - outfile = tmpfile.name |
93 |
| - arg_str = " ".join([infile, build_arg_string(kwargs), "->" + outfile]) |
94 |
| - lib.call_module(module="blockmedian", args=arg_str) |
95 |
| - |
96 |
| - # Read temporary csv output to a pandas table |
97 |
| - if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame |
98 |
| - result = pd.read_csv(tmpfile.name, sep="\t", names=table.columns) |
99 |
| - elif outfile != tmpfile.name: # return None if outfile set, output in outfile |
100 |
| - result = None |
101 |
| - |
102 |
| - return result |
| 192 | + return _blockm(block_method="blockmedian", table=table, outfile=outfile, **kwargs) |
0 commit comments