Skip to content

Commit d14734b

Browse files
pygmt.grdfilter: Let the parameter 'nans' support descriptive arguments (#4310)
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
1 parent c5d10d4 commit d14734b

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

pygmt/src/grdfilter.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515

1616

1717
@fmt_docstring
18-
@use_alias(D="distance", F="filter", N="nans", f="coltypes")
18+
@use_alias(D="distance", F="filter", f="coltypes")
1919
def grdfilter(
2020
grid: PathLike | xr.DataArray,
2121
outgrid: PathLike | None = None,
22-
toggle: bool = False,
2322
spacing: Sequence[float | str] | None = None,
23+
nans: Literal["ignore", "replace", "preserve"] | None = None,
24+
toggle: bool = False,
2425
region: Sequence[float | str] | str | None = None,
25-
registration: Literal["gridline", "pixel"] | bool = False,
2626
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
2727
| bool = False,
28+
registration: Literal["gridline", "pixel"] | bool = False,
2829
cores: int | bool = False,
2930
**kwargs,
3031
) -> xr.DataArray | None:
@@ -45,6 +46,7 @@ def grdfilter(
4546
4647
$aliases
4748
- I = spacing
49+
- N = nans
4850
- R = region
4951
- T = toggle
5052
- V = verbose
@@ -89,21 +91,22 @@ def grdfilter(
8991
calculation.
9092
- ``"5"``: grid (x,y) in Mercator ``projection="m1"`` img units,
9193
*width* in km, Spherical distance calculation.
92-
9394
$spacing
94-
nans : str or float
95-
**i**\|\ **p**\|\ **r**.
96-
Determine how NaN-values in the input grid affect the filtered output.
97-
Use **i** to ignore all NaNs in the calculation of the filtered value
98-
[Default]. **r** is same as **i** except if the input node was NaN then
99-
the output node will be set to NaN (only applies if both grids are
100-
co-registered). **p** will force the filtered value to be NaN if any
101-
grid nodes with NaN-values are found inside the filter circle.
102-
$region
95+
nans
96+
Determine how NaN-values in the input grid affect the filtered output grid.
97+
Choose one of:
98+
99+
- ``"ignore"``: Ignore all NaNs in the calculation of filtered value [Default].
100+
- ``"replace"``: Similar to ``"ignore"`` except if the input node was NaN then
101+
the output node will be set to NaN (only applied if both grids are
102+
co-registered).
103+
- ``"preserve"``: Force the filtered value to be NaN if any grid nodes with
104+
NaN-values are found inside the filter circle.
103105
toggle
104106
Toggle the node registration for the output grid so as to become the opposite of
105107
the input grid [Default gives the same registration as the input grid].
106108
Alternatively, use ``registration`` to set the registration explicitly.
109+
$region
107110
$verbose
108111
$coltypes
109112
$registration
@@ -122,8 +125,8 @@ def grdfilter(
122125
--------
123126
>>> from pathlib import Path
124127
>>> import pygmt
125-
>>> # Apply a filter of 600 km (full width) to the @earth_relief_30m_g file
126-
>>> # and return a filtered field (saved as netCDF)
128+
>>> # Apply a median filter of 600 km (full width) to the @earth_relief_30m_g grid
129+
>>> # and return a filtered grid (saved as netCDF file).
127130
>>> pygmt.grdfilter(
128131
... grid="@earth_relief_30m_g",
129132
... filter="m600",
@@ -133,13 +136,16 @@ def grdfilter(
133136
... outgrid="filtered_pacific.nc",
134137
... )
135138
>>> Path("filtered_pacific.nc").unlink() # Cleanup file
136-
>>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray
137-
>>> # and return a filtered DataArray with the smoothed field
139+
>>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray and return
140+
>>> # a filtered DataArray with the smoothed grid.
138141
>>> grid = pygmt.datasets.load_earth_relief()
139142
>>> smooth_field = pygmt.grdfilter(grid=grid, filter="g600", distance="4")
140143
"""
141144
aliasdict = AliasSystem(
142145
I=Alias(spacing, name="spacing", sep="/", size=2),
146+
N=Alias(
147+
nans, name="nans", mapping={"ignore": "i", "replace": "r", "preserve": "p"}
148+
),
143149
T=Alias(toggle, name="toggle"),
144150
).add_common(
145151
R=region,

0 commit comments

Comments
 (0)