Skip to content

Commit 671d1b6

Browse files
authored
AliasSystem: Migrate the 'outcols' parameter to the new alias system (#4211)
1 parent bcad4c1 commit 671d1b6

File tree

6 files changed

+52
-43
lines changed

6 files changed

+52
-43
lines changed

pygmt/alias.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ def add_common(self, **kwargs): # noqa: PLR0912
337337
name="registration",
338338
mapping={"gridline": "g", "pixel": "p"},
339339
)
340+
case "o":
341+
alias = Alias(value, name="outcols", sep=",")
340342
case "p":
341343
alias = Alias(value, name="perspective", sep="/", size={2, 3})
342344
case "t":

pygmt/helpers/decorators.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -204,28 +204,35 @@
204204
Prepend **i** to the *nodata* value for input columns only. Prepend
205205
**o** to the *nodata* value for output columns only.""",
206206
"outcols": r"""
207-
outcols : str or 1-D array
207+
outcols
208208
*cols*\ [,...][,\ **t**\ [*word*]].
209-
Specify data columns for primary output in arbitrary order. Columns
210-
can be repeated and columns not listed will be skipped [Default
211-
writes all columns in order, starting with the first (i.e., column
212-
0)].
213-
214-
- For *1-D array*: specify individual columns in output order (e.g.,
215-
``outcols=[1,0]`` for the 2nd column followed by the 1st column).
216-
- For :py:class:`str`: specify individual columns or column
217-
ranges in the format *start*\ [:*inc*]:*stop*, where *inc*
218-
defaults to 1 if not specified, with columns and/or column ranges
219-
separated by commas (e.g., ``outcols="0:2,4"`` to output the
220-
first three columns followed by the 5th column).
221-
To write from a given column until the end of the record, leave
222-
off *stop* when specifying the column range. To write trailing
223-
text, add the column **t**. Append the word number to **t** to
224-
write only a single word from the trailing text. Instead of
225-
specifying columns, use ``outcols="n"`` to simply read numerical
226-
input and skip trailing text. **Note**: If ``incols`` is also
227-
used then the columns given to ``outcols`` correspond to the
228-
order after the ``incols`` selection has taken place.""",
209+
210+
Specify data columns for primary output in arbitrary order. Columns can be
211+
repeated and columns not listed will be skipped [Default writes all columns
212+
in order, starting with the first (i.e., column 0)].
213+
214+
- For a sequence: specify individual columns in output order (e.g.,
215+
``outcols=(1, 0)`` for the 2nd column followed by the 1st column).
216+
- For a string: specify individual columns or column ranges in the format
217+
*start*\ [:*inc*]:*stop*, where *inc* defaults to 1 if not specified, with
218+
columns and/or column ranges separated by commas (e.g.,
219+
``outcols="0:2,4"`` to output the first three columns followed by the 5th
220+
column). To write from a given column until the end of the record, leave
221+
off *stop* when specifying the column range. To write trailing text, add
222+
the column **t**. Append the word number to **t** to write only a single
223+
word from the trailing text. Instead of specifying columns, use
224+
``outcols="n"`` to simply read numerical input and skip trailing text.
225+
**Note**: If ``incols`` is also used then the columns given to ``outcols``
226+
correspond to the order after the ``incols`` selection has taken place.
227+
228+
Optionally, append one of the following modifiers to any column or column
229+
range to transform the output columns:
230+
231+
- **+l** to take the *log10* of the input values.
232+
- **+d** to divide the output values by the factor *divisor* [Default is
233+
1].
234+
- **+s** to multiply the output values by the factor *scale* [Default is 1].
235+
- **+o** to add the given *offset* to the output values [Default is 0].""",
229236
"outfile": """
230237
outfile
231238
File name for saving the result data. Required if ``output_type="file"``.

pygmt/src/blockm.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from pygmt.helpers import (
1414
build_arg_list,
1515
fmt_docstring,
16-
kwargs_to_strings,
1716
use_alias,
1817
validate_output_table_type,
1918
)
@@ -80,10 +79,8 @@ def _blockm(
8079
e="find",
8180
f="coltypes",
8281
h="header",
83-
o="outcols",
8482
w="wrap",
8583
)
86-
@kwargs_to_strings(o="sequence_comma")
8784
def blockmean( # noqa: PLR0913
8885
data: PathLike | TableLike | None = None,
8986
x=None,
@@ -97,6 +94,7 @@ def blockmean( # noqa: PLR0913
9794
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
9895
| bool = False,
9996
incols: int | str | Sequence[int | str] | None = None,
97+
outcols: int | str | Sequence[int | str] | None = None,
10098
**kwargs,
10199
) -> pd.DataFrame | np.ndarray | None:
102100
r"""
@@ -118,6 +116,7 @@ def blockmean( # noqa: PLR0913
118116
- R = region
119117
- V = verbose
120118
- i = incols
119+
- o = outcols
121120
- r = registration
122121
123122
Parameters
@@ -176,6 +175,7 @@ def blockmean( # noqa: PLR0913
176175
R=region,
177176
V=verbose,
178177
i=incols,
178+
o=outcols,
179179
r=registration,
180180
)
181181
aliasdict.merge(kwargs)
@@ -194,16 +194,8 @@ def blockmean( # noqa: PLR0913
194194

195195
@fmt_docstring
196196
@use_alias(
197-
a="aspatial",
198-
b="binary",
199-
d="nodata",
200-
e="find",
201-
f="coltypes",
202-
h="header",
203-
o="outcols",
204-
w="wrap",
197+
a="aspatial", b="binary", d="nodata", e="find", f="coltypes", h="header", w="wrap"
205198
)
206-
@kwargs_to_strings(o="sequence_comma")
207199
def blockmedian( # noqa: PLR0913
208200
data: PathLike | TableLike | None = None,
209201
x=None,
@@ -217,6 +209,7 @@ def blockmedian( # noqa: PLR0913
217209
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
218210
| bool = False,
219211
incols: int | str | Sequence[int | str] | None = None,
212+
outcols: int | str | Sequence[int | str] | None = None,
220213
**kwargs,
221214
) -> pd.DataFrame | np.ndarray | None:
222215
r"""
@@ -238,6 +231,7 @@ def blockmedian( # noqa: PLR0913
238231
- R = region
239232
- V = verbose
240233
- i = incols
234+
- o = outcols
241235
- r = registration
242236
243237
Parameters
@@ -290,6 +284,7 @@ def blockmedian( # noqa: PLR0913
290284
R=region,
291285
V=verbose,
292286
i=incols,
287+
o=outcols,
293288
r=registration,
294289
)
295290
aliasdict.merge(kwargs)
@@ -314,10 +309,8 @@ def blockmedian( # noqa: PLR0913
314309
e="find",
315310
f="coltypes",
316311
h="header",
317-
o="outcols",
318312
w="wrap",
319313
)
320-
@kwargs_to_strings(o="sequence_comma")
321314
def blockmode( # noqa: PLR0913
322315
data: PathLike | TableLike | None = None,
323316
x=None,
@@ -331,6 +324,7 @@ def blockmode( # noqa: PLR0913
331324
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
332325
| bool = False,
333326
incols: int | str | Sequence[int | str] | None = None,
327+
outcols: int | str | Sequence[int | str] | None = None,
334328
**kwargs,
335329
) -> pd.DataFrame | np.ndarray | None:
336330
r"""
@@ -352,6 +346,7 @@ def blockmode( # noqa: PLR0913
352346
- R = region
353347
- V = verbose
354348
- i = incols
349+
- o = outcols
355350
- r = registration
356351
357352
Parameters
@@ -402,6 +397,7 @@ def blockmode( # noqa: PLR0913
402397
R=region,
403398
V=verbose,
404399
i=incols,
400+
o=outcols,
405401
r=registration,
406402
)
407403
aliasdict.merge(kwargs)

pygmt/src/grd2xyz.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from pygmt.helpers import (
1616
build_arg_list,
1717
fmt_docstring,
18-
kwargs_to_strings,
1918
use_alias,
2019
validate_output_table_type,
2120
)
@@ -32,17 +31,16 @@
3231
d="nodata",
3332
f="coltypes",
3433
h="header",
35-
o="outcols",
3634
s="skiprows",
3735
)
38-
@kwargs_to_strings(o="sequence_comma")
3936
def grd2xyz(
4037
grid: PathLike | xr.DataArray,
4138
output_type: Literal["pandas", "numpy", "file"] = "pandas",
4239
outfile: PathLike | None = None,
4340
region: Sequence[float | str] | str | None = None,
4441
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
4542
| bool = False,
43+
outcols: int | str | Sequence[int | str] | None = None,
4644
**kwargs,
4745
) -> pd.DataFrame | np.ndarray | None:
4846
r"""
@@ -56,6 +54,7 @@ def grd2xyz(
5654
{aliases}
5755
- R = region
5856
- V = verbose
57+
- o = outcols
5958
6059
Parameters
6160
----------
@@ -149,7 +148,7 @@ def grd2xyz(
149148
"""
150149
output_type = validate_output_table_type(output_type, outfile=outfile)
151150

152-
if kwargs.get("o") is not None and output_type == "pandas":
151+
if kwargs.get("o", outcols) is not None and output_type == "pandas":
153152
raise GMTValueError(
154153
output_type,
155154
description="value for parameter 'output_type'",
@@ -165,6 +164,7 @@ def grd2xyz(
165164
aliasdict = AliasSystem().add_common(
166165
R=region,
167166
V=verbose,
167+
o=outcols,
168168
)
169169
aliasdict.merge(kwargs)
170170

pygmt/src/grdtrack.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@
4343
h="header",
4444
j="distcalc",
4545
n="interpolation",
46-
o="outcols",
4746
s="skiprows",
4847
w="wrap",
4948
)
50-
@kwargs_to_strings(S="sequence", o="sequence_comma")
49+
@kwargs_to_strings(S="sequence")
5150
def grdtrack(
5251
grid: PathLike | xr.DataArray,
5352
points: PathLike | TableLike | None = None,
@@ -58,6 +57,7 @@ def grdtrack(
5857
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
5958
| bool = False,
6059
incols: int | str | Sequence[int | str] | None = None,
60+
outcols: int | str | Sequence[int | str] | None = None,
6161
**kwargs,
6262
) -> pd.DataFrame | np.ndarray | None:
6363
r"""
@@ -83,6 +83,7 @@ def grdtrack(
8383
- R = region
8484
- V = verbose
8585
- i = incols
86+
- o = outcols
8687
8788
Parameters
8889
----------
@@ -319,6 +320,7 @@ def grdtrack(
319320
R=region,
320321
V=verbose,
321322
i=incols,
323+
o=outcols,
322324
)
323325
aliasdict.merge(kwargs)
324326

pygmt/src/select.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@
3737
f="coltypes",
3838
g="gap",
3939
h="header",
40-
o="outcols",
4140
s="skiprows",
4241
w="wrap",
4342
)
44-
@kwargs_to_strings(N="sequence", o="sequence_comma")
43+
@kwargs_to_strings(N="sequence")
4544
def select(
4645
data: PathLike | TableLike | None = None,
4746
output_type: Literal["pandas", "numpy", "file"] = "pandas",
@@ -54,6 +53,7 @@ def select(
5453
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
5554
| bool = False,
5655
incols: int | str | Sequence[int | str] | None = None,
56+
outcols: int | str | Sequence[int | str] | None = None,
5757
**kwargs,
5858
) -> pd.DataFrame | np.ndarray | None:
5959
r"""
@@ -83,6 +83,7 @@ def select(
8383
- R = region
8484
- V = verbose
8585
- i = incols
86+
- o = outcols
8687
8788
Parameters
8889
----------
@@ -239,6 +240,7 @@ def select(
239240
R=region,
240241
V=verbose,
241242
i=incols,
243+
o=outcols,
242244
)
243245
aliasdict.merge(kwargs)
244246

0 commit comments

Comments
 (0)