{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Benchmarking conservative regridding\n", "\n", "In this notebook we compare the results of three conservative regridding methods:\n", "- The CDO (Climate Data Operators) command line program.\n", "- The xESMF xarray extension (based on the Earth System Modelling Framework).\n", "- a custom method, wrapped as an accessor by `xarray-regrid`. This method is based on [a post](https://discourse.pangeo.io/t/conservative-region-aggregation-with-xarray-geopandas-and-sparse/2715/3) by Stephan Hoyer.\n", "\n", "The data to resample is the ERA5 monthly dataset, in this case the total precipitation.\n", "Conservative regridding is more important for variables such as precipitation.\n", "\n", "We start with importing the required modules, starting up Dask and loading in the data:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from time import time\n", "import dask.distributed\n", "import xarray_regrid # Importing this will make Dataset.regrid accessible.\n", "import xarray as xr\n", "from xarray_regrid import Grid\n", "import xesmf as xe\n", "import warnings\n", "\n", "warnings.filterwarnings('ignore')\n", "\n", "client = dask.distributed.Client()\n", "\n", "ds = xr.open_dataset(\n", " \"data/era5_total_precipitation_2020_monthly.nc\",\n", " chunks={\"longitude\": 400, \"latitude\": 400},\n", ").persist()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both `xarray-regrid` and `xESMF` require a dataset to resample to. For this you can also use an already existing dataset with the desired grid.\n", "\n", "Here we use the `Grid` dataclass from `xarray-regrid` to create the dataset." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset> Size: 2kB\n",
"Dimensions: (latitude: 82, longitude: 164)\n",
"Coordinates:\n",
" * latitude (latitude) float64 656B -90.0 -87.8 -85.6 ... 83.8 86.0 88.2\n",
" * longitude (longitude) float64 1kB 0.0 2.2 4.4 6.6 ... 354.2 356.4 358.6\n",
"Data variables:\n",
" *empty*