From b8432dc8b25e150a7cdd1dbdd31c0eaccf7666ce Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 20 Dec 2025 17:11:16 +0800 Subject: [PATCH 1/5] pygmt.grdfilter: Let the parameter nans support descriptive arguments --- pygmt/src/grdfilter.py | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/pygmt/src/grdfilter.py b/pygmt/src/grdfilter.py index a1e9393abb3..619b24f43d5 100644 --- a/pygmt/src/grdfilter.py +++ b/pygmt/src/grdfilter.py @@ -13,16 +13,17 @@ @fmt_docstring -@use_alias(D="distance", F="filter", N="nans", f="coltypes") +@use_alias(D="distance", F="filter", f="coltypes") def grdfilter( grid: PathLike | xr.DataArray, outgrid: PathLike | None = None, - toggle: bool = False, spacing: Sequence[float | str] | None = None, + nans: Literal["ignore", "replace", "preserve"] | None = None, + toggle: bool = False, region: Sequence[float | str] | str | None = None, - registration: Literal["gridline", "pixel"] | bool = False, verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"] | bool = False, + registration: Literal["gridline", "pixel"] | bool = False, cores: int | bool = False, **kwargs, ) -> xr.DataArray | None: @@ -43,6 +44,7 @@ def grdfilter( $aliases - I = spacing + - N = nans - R = region - T = toggle - V = verbose @@ -87,21 +89,22 @@ def grdfilter( calculation. - ``"5"``: grid (x,y) in Mercator ``projection="m1"`` img units, *width* in km, Spherical distance calculation. - $spacing - nans : str or float - **i**\|\ **p**\|\ **r**. - Determine how NaN-values in the input grid affect the filtered output. - Use **i** to ignore all NaNs in the calculation of the filtered value - [Default]. **r** is same as **i** except if the input node was NaN then - the output node will be set to NaN (only applies if both grids are - co-registered). **p** will force the filtered value to be NaN if any - grid nodes with NaN-values are found inside the filter circle. - $region + nans + Determine how NaN-values in the input grid affect the filtered output. Choose + one of: + + - ``"ignore"``: Ignore all NaNs in the calculation of filtered value [Default]. + - ``"replace"``: Similar to ``"ignore"`` except if the input node was NaN then + the output node will be set to NaN (only applies if both grids are + co-registered). + - ``"preserve"``: Force the filtered value to be NaN if any grid nodes with + NaN-values are found inside the filter circle. toggle Toggle the node registration for the output grid so as to become the opposite of the input grid [Default gives the same registration as the input grid]. Alternatively, use ``registration`` to set the registration explicitly. + $region $verbose $coltypes $registration @@ -120,8 +123,8 @@ def grdfilter( -------- >>> from pathlib import Path >>> import pygmt - >>> # Apply a filter of 600 km (full width) to the @earth_relief_30m_g file - >>> # and return a filtered field (saved as netCDF) + >>> # Apply a filter of 600 km (full width) to the @earth_relief_30m_g file and + >>> # return a filtered field (saved as netCDF) >>> pygmt.grdfilter( ... grid="@earth_relief_30m_g", ... filter="m600", @@ -131,13 +134,16 @@ def grdfilter( ... outgrid="filtered_pacific.nc", ... ) >>> Path("filtered_pacific.nc").unlink() # Cleanup file - >>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray - >>> # and return a filtered DataArray with the smoothed field + >>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray and return + >>> # a filtered DataArray with the smoothed field >>> grid = pygmt.datasets.load_earth_relief() >>> smooth_field = pygmt.grdfilter(grid=grid, filter="g600", distance="4") """ aliasdict = AliasSystem( I=Alias(spacing, name="spacing", sep="/", size=2), + N=Alias( + nans, name="nans", mapping={"ignore": "i", "replace": "r", "preserve": "p"} + ), T=Alias(toggle, name="toggle"), ).add_common( R=region, From 990bb274cddde322c1d3ecbd36b2d952e5159cbe Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 26 Dec 2025 08:22:01 +0800 Subject: [PATCH 2/5] Update pygmt/src/grdfilter.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/src/grdfilter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grdfilter.py b/pygmt/src/grdfilter.py index a71381580ef..22dd090b01e 100644 --- a/pygmt/src/grdfilter.py +++ b/pygmt/src/grdfilter.py @@ -93,8 +93,8 @@ def grdfilter( *width* in km, Spherical distance calculation. $spacing nans - Determine how NaN-values in the input grid affect the filtered output. Choose - one of: + Determine how NaN-values in the input grid affect the filtered output grid. + Choose one of: - ``"ignore"``: Ignore all NaNs in the calculation of filtered value [Default]. - ``"replace"``: Similar to ``"ignore"`` except if the input node was NaN then From e89d1213d8054c529bab99d810169f5b9f4b9587 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 26 Dec 2025 08:22:14 +0800 Subject: [PATCH 3/5] Update pygmt/src/grdfilter.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/src/grdfilter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grdfilter.py b/pygmt/src/grdfilter.py index 22dd090b01e..32d0b6fb712 100644 --- a/pygmt/src/grdfilter.py +++ b/pygmt/src/grdfilter.py @@ -137,7 +137,7 @@ def grdfilter( ... ) >>> Path("filtered_pacific.nc").unlink() # Cleanup file >>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray and return - >>> # a filtered DataArray with the smoothed field + >>> # a filtered DataArray with the smoothed grid. >>> grid = pygmt.datasets.load_earth_relief() >>> smooth_field = pygmt.grdfilter(grid=grid, filter="g600", distance="4") """ From 66c61fd6131f31c4dca570bda992e81d3fb8de87 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 26 Dec 2025 08:22:34 +0800 Subject: [PATCH 4/5] Update pygmt/src/grdfilter.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/src/grdfilter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/grdfilter.py b/pygmt/src/grdfilter.py index 32d0b6fb712..3b7936b7273 100644 --- a/pygmt/src/grdfilter.py +++ b/pygmt/src/grdfilter.py @@ -98,7 +98,7 @@ def grdfilter( - ``"ignore"``: Ignore all NaNs in the calculation of filtered value [Default]. - ``"replace"``: Similar to ``"ignore"`` except if the input node was NaN then - the output node will be set to NaN (only applies if both grids are + the output node will be set to NaN (only applied if both grids are co-registered). - ``"preserve"``: Force the filtered value to be NaN if any grid nodes with NaN-values are found inside the filter circle. From 64f646df65f7b45a705180633915575bae32cb16 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 26 Dec 2025 08:23:27 +0800 Subject: [PATCH 5/5] Update pygmt/src/grdfilter.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/src/grdfilter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/src/grdfilter.py b/pygmt/src/grdfilter.py index 3b7936b7273..053f29cb040 100644 --- a/pygmt/src/grdfilter.py +++ b/pygmt/src/grdfilter.py @@ -125,8 +125,8 @@ def grdfilter( -------- >>> from pathlib import Path >>> import pygmt - >>> # Apply a filter of 600 km (full width) to the @earth_relief_30m_g file and - >>> # return a filtered field (saved as netCDF) + >>> # Apply a median filter of 600 km (full width) to the @earth_relief_30m_g grid + >>> # and return a filtered grid (saved as netCDF file). >>> pygmt.grdfilter( ... grid="@earth_relief_30m_g", ... filter="m600",