Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

NaN-aware Gaussian smoothing

Open In Colab

This notebook demonstrates coastal smoothing with gaussian_smooth_masked, which smooths finite ocean pixels without letting land-mask NaNs erase nearby coastal values.

What you’ll learn:

  1. How standard Gaussian smoothing propagates NaNs near a mask.

  2. How normalized convolution keeps finite coastal pixels alive.

  3. How to call gaussian_smooth_masked on an xarray DataArray.

Setup

gaussian_smooth_masked applies a Gaussian filter to both the NaN-filled data and a finite-pixel mask, then divides the two filtered arrays. The result is a Gaussian-weighted average over valid pixels only, while originally masked land pixels remain NaN.

Python implementation: CPython
Python version       : 3.12.3
IPython version      : 9.10.0

numpy     : 2.4.4
xarray    : 2026.4.0
matplotlib: 3.10.8
xrtoolz  : 0.0.6

Compiler    : GCC 13.3.0
OS          : Linux
Release     : 6.17.0-1010-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 4
Architecture: 64bit

Synthetic coastline

We create a smooth SST-like field on the ocean side of a diagonal coastline and use NaN for land. The existing 1-D Gaussian smoother is applied along latitude and longitude to show the standard NaN-propagating behavior.

finite ocean pixels before smoothing: 4693
finite pixels after naive smoothing: 3862
finite pixels after NaN-aware smoothing: 4693

Coastline comparison

The standard smoother loses a band of coastal ocean because NaNs propagate through the Gaussian support. The NaN-aware smoother keeps ocean pixels finite by normalizing by the Gaussian-smoothed valid-data mask.

<Figure size 1800x500 with 6 Axes>

Takeaways

Use gaussian_smooth_masked when NaNs encode a physical mask such as land, sea ice, or missing swaths. The result is interpolation-style smoothing rather than flux-preserving smoothing: values near masks are averaged over the available support, and originally masked pixels stay masked.