A real world example#


In the previous tutorial, we introduced the powerful combination of Xarray and CuPy for handling multi-dimensional datasets and leveraging GPU acceleration to significantly improve performance. We explored high-level Xarray functions such as groupby, rolling mean, and weighted mean, and compared their execution times with traditional NumPy-based implementations. In this tutorial, we will dive deeper into the subject with a hands-on approach, utilizing a real-world dataset. This will enable us to better understand the practical applications of Xarray and CuPy and how they can be efficiently utilized for real-life data analysis tasks.

import numpy as np
import xarray as xr
import cupy as cp
import cupy_xarray  # Adds .cupy to Xarray objects

Reading data#

Here we read in a small portion of the data available from the NEX-GDDP-CMIP6 dataset available through the registry of open data on AWS.

import s3fs

fs = s3fs.S3FileSystem(anon=True, default_fill_cache=False)

scenario = "ssp245"
var = "tasmax"
years = list(range(2020, 2022))

file_objs = [
    for year in years
da = xr.open_mfdataset(file_objs, engine="h5netcdf")[var].load()

We can convert the underlying numpy array to cupy array using cupy.as_cupy().

da = da.as_cupy()
# Check if data is cupy Array

As a first step, let’s calculate the mean global temperature

# Calculate the mean global temperature
da_mean = da.mean(dim=["lat", "lon"]).compute()
CPU times: user 389 ms, sys: 1.77 ms, total: 391 ms
Wall time: 391 ms


The groupby function is used to group data based on one or more dimensions. Here, we’ll group our data by the ‘time’ dimension using CuPy:

climo_da = da.groupby("time.month").mean("time").compute()
CPU times: user 44.2 ms, sys: 0 ns, total: 44.2 ms
Wall time: 47 ms

Advanced workflows and automatic parallelization using apply_ufunc#

xr.apply_ufunc() can automate embarrassingly parallel “map” type operations where a function written for processing NumPy arrays, but we want to apply it on our Xarray DataArray.

xr.apply_ufunc() give users capability to run custom-written functions such as parameter calculations in a parallel way.

In the example below, we calculate the saturation vapor pressure by using apply_ufunc() to apply this function to our Dask Array chunk by chunk.

# return saturation vapor pressure
# using Clausius-Clapeyron equation
def sat_p(t):
    return 0.611 * cp.exp(17.67 * (t - 273.15) * ((t - 29.65) ** (-1)))
es = xr.apply_ufunc(sat_p, da, output_dtypes=[float]).rename("saturation_vapor_pressure")
CPU times: user 102 ms, sys: 11.2 ms, total: 113 ms
Wall time: 119 ms

Add Plotting#

We can plot the result, which will involve the data automatically being transferred to the host

<matplotlib.collections.QuadMesh at 0x7f857155a440>