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.

xESMF Demo using SEVIER dataset for Machine Learning Preperation

Thomas Martin ThomasMGeo

Sept 2 - 2022

Overview

This is a quick (~20 minute) notebook that will cover how to use xESMF to regrid with xarray on a dataset. This notebooks will heavily borrow from this repo: https://github.com/ai2es/WAF_ML_Tutorial_Part1 & this paper https://proceedings.neurips.cc/paper/2020/file/fa78a16157fed00d7a80515818432169-Paper.pdf . Randy Chase (dopplershift on github) is thanked for dataset prep and previous work!

Prerequisits

Working knowldge of xarray, matplotlib, and numpy is beneficial. This is not designed to be an introduction to any of those packages.

Imports

Watermark is great repo to track versions when sharing work with notebooks. xESMF can be a little tricky to install, highly reccomend to install via conda instread of pip.

matplotlib: 3.11.2
numpy     : 2.5.4
seaborn   : 0.13.2
xarray    : 2026.9.0
xesmf     : 0.9.2

Dataset Load
Loading...

We will be using this single dataset for the entirety of the notebook

<Figure size 900x600 with 2 Axes>

Let’s Zoom in one one patch, to gain some intuition on how satellite data is used in Machine Learning (ML)

<Figure size 1000x900 with 2 Axes>
<Figure size 640x480 with 0 Axes>

Let’s check out a small section of the lightning strike data:

<Figure size 600x500 with 2 Axes>
<Figure size 640x480 with 0 Axes>

After reviewing the dataset, some of the variables have different shapes:

(768, 768, 12)
(48, 48, 12)

Using xESMF to regrid an xarray dataset:

In order to re-grid, let’s set up some coordinates. Lets do some sanity checking to check what the axises needs to be divided by:

16.340425531914892
Loading...

Decreasing visible resoultion to lighting resoultion

Making new datasets, this might not be required.
Loading...
Loading...
xESMF Regridder Regridding algorithm: bilinear Weight filename: bilinear_768x768_48x48.nc Reuse pre-computed weights? False Input grid shape: (768, 768) Output grid shape: (48, 48) Periodic in longitude? False
/home/runner/micromamba/envs/gridding-cookbook-dev/lib/python3.14/site-packages/xesmf/smm.py:131: UserWarning: Input array is not C_CONTIGUOUS. Will affect performance.
  warnings.warn('Input array is not C_CONTIGUOUS. ' 'Will affect performance.')
Loading...

Direct Comparison

Note the difference in x & y axis

<Figure size 800x800 with 2 Axes>

We can also upscale low-resoultion data

Note: This is not a reccomendation to do this for every workflow/dataset! There are five different algorithms that you can use, here is a nice comparison: https://xesmf.readthedocs.io/en/latest/notebooks/Compare_algorithms.html

xESMF Regridder Regridding algorithm: patch Weight filename: patch_48x48_768x768.nc Reuse pre-computed weights? False Input grid shape: (48, 48) Output grid shape: (768, 768) Periodic in longitude? False
/home/runner/micromamba/envs/gridding-cookbook-dev/lib/python3.14/site-packages/xesmf/smm.py:131: UserWarning: Input array is not C_CONTIGUOUS. Will affect performance.
  warnings.warn('Input array is not C_CONTIGUOUS. ' 'Will affect performance.')
/home/runner/micromamba/envs/gridding-cookbook-dev/lib/python3.14/site-packages/xesmf/smm.py:131: UserWarning: Input array is not C_CONTIGUOUS. Will affect performance.
  warnings.warn('Input array is not C_CONTIGUOUS. ' 'Will affect performance.')
<Figure size 1000x400 with 3 Axes>

Merging Xarray Datasets

After you have made a new grid, you will want to combine them into a new xarray dataset for future analysis. Here is an example using combine by coords:

Loading...
Loading...
Loading...

Summary

This has been a quick introduction to useing the xESMF regridding tools! The documentation for xESMF & xarray are very helpful for future learning.