True colour animations with rainfall

Keywords: data used; sentinel-2, data used; rolling GeoMAD, datasets; CHIRPS, climate, rainfall, data used; sentinel-2 geomedian, data used; landsat 8 & 9 geomedian, data used; landsat 8 geomedian, data used; landsat 5 & 7 geomedian


It can be informative to visualise true colour imagery of an area through time alongside a variable which contributes to the land surface state, such as rainfall. Inspecting an animated timeseries of a given area through time alongside cumulative rainfall can show how landscape features (vegetation, water bodies etc.) respond to rain events or lack thereof.

This notebook demonstrates how rainfall and true colour imagery data can be assimilated to produce informative animations.


We can use surface reflectance and CHIRPS rainfall data to produce these animations. Through the DE Africa Sandbox we can integrate these datasets over a time period of interest and align them both temporally and spatially to visualise them synchronously.

The animations are produced in this notebook by following the steps below:

  1. Select an area of interest.

  2. Load surface reflectance (true colour) imagery.

  3. Resample and interpolate imagery to generate a regular timeseries.

  4. Load rainfall data.

  5. Resample rainfall data to generate a regular timeseries.

  6. Define axes and update function for the animation.

  7. Run and interpret the animation.

Getting started

To run this analysis, run all the cells in the notebook, starting with the “Load packages” cell.

Load packages

%matplotlib inline

# Force GeoPandas to use Shapely instead of PyGEOS
# In a future release, GeoPandas will switch to using Shapely by default.
import os
os.environ['USE_PYGEOS'] = '0'

import datacube
import datetime as dt
import numpy as np
import xarray as xr
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.dates as mdates

from IPython.display import HTML
from datacube.utils.geometry import Geometry
from deafrica_tools.plotting import display_map, rgb
from deafrica_tools.areaofinterest import define_area
from deafrica_tools.dask import create_local_dask_cluster
from deafrica_tools.datahandling import load_ard

Connect to the datacube

Connect to the datacube so we can access DE Africa data. The app parameter is a unique name for the analysis which is based on the notebook file name.

dc = datacube.Datacube(app='animation-rainfall')
[ ]:

Analysis parameters

To define the area of interest, there are two methods available:

  1. By specifying the latitude, longitude, and buffer. This method requires you to input the central latitude, central longitude, and the buffer value in square degrees around the center point you want to analyze. For example, lat = 10.338, lon = -1.055, and buffer = 0.1 will select an area with a radius of 0.1 square degrees around the point with coordinates (10.338, -1.055).

  2. By uploading a polygon as a GeoJSON or Esri Shapefile. If you choose this option, you will need to upload the geojson or ESRI shapefile into the Sandbox using Upload Files button c2d5321095a5450ab8d0b6d9ee8fc9f5 in the top left corner of the Jupyter Notebook interface. ESRI shapefiles must be uploaded with all the related files (.cpg, .dbf, .shp, .shx). Once uploaded, you can use the shapefile or geojson to define the area of interest. Remember to update the code to call the file you have uploaded.

To use one of these methods, you can uncomment the relevant line of code and comment out the other one. To comment out a line, add the "#" symbol before the code you want to comment out. By default, the first option which defines the location using latitude, longitude, and buffer is being used.

The default area is Mosiotunya - the smoke that thunders - (Victoria Falls) on the Zambezi River bordering Zimbabwe and Zambia.

# Method 1: Specify the latitude, longitude, and buffer
aoi = define_area(lat=-17.92, lon=25.85, buffer=0.05)

# Method 2: Use a polygon as a GeoJSON or Esri Shapefile.
# aoi = define_area(vector_path='aoi.shp')

#Create a geopolygon and geodataframe of the area of interest
geopolygon = Geometry(aoi["features"][0]["geometry"], crs="epsg:4326")
geopolygon_gdf = gpd.GeoDataFrame(geometry=[geopolygon],

# Get the latitude and longitude range of the geopolygon
lat_range = (geopolygon_gdf.total_bounds[1], geopolygon_gdf.total_bounds[3])
lon_range = (geopolygon_gdf.total_bounds[0], geopolygon_gdf.total_bounds[2])

display_map(lon_range, lat_range)
Make this Notebook Trusted to load map: File -> Trust Notebook

Load satellite data from datacube

Initially, we will load the rolling geomedian true colour (red, green, blue) bands to visualise changes in the landscape within a recent year. We load a month prior and following the period of interest as it assists in generating a regular timeseries within the year of interest.

# Create a reusable query
query = {
    'x': lon_range,
    'y': lat_range,
    'time': ('2022-01-01', '2022-12-31'),
    'resolution': (-20, 20)

# Load available data
ds = dc.load(product=['gm_s2_rolling'],
             measurements=['red', 'green', 'blue'],

# Print output data
Dimensions:      (time: 14, y: 609, x: 483)
  * time         (time) datetime64[ns] 2021-12-16T23:59:59.999999 ... 2023-01...
  * y            (y) float64 -2.244e+06 -2.244e+06 ... -2.256e+06 -2.256e+06
  * x            (x) float64 2.489e+06 2.489e+06 ... 2.499e+06 2.499e+06
    spatial_ref  int32 6933
Data variables:
    red          (time, y, x) uint16 1257 1299 1223 1177 ... 411 370 357 368
    green        (time, y, x) uint16 1015 1021 964 911 980 ... 665 638 609 611
    blue         (time, y, x) uint16 727 755 684 660 736 ... 397 384 364 359 362
    crs:           epsg:6933
    grid_mapping:  spatial_ref

Interpolation of timeseries

We want to generate an animation for a single calendar year, so we need a regular timeseries with time-steps that match rainfall data. This is generally not available from an analysis-ready dataset as some images will be omitted due to cloud cover, especially in wetter seasons.

To regulate the timeseries, we downsample the temporal resolution using interpolation. This means that we are generating new data, or gap-filling through time, based on the existing data. It is important that this is disclosed to viewers and users because the interpolated data is not observational and is therefore subject to uncertainty.

There are several methods available for interpolation: “linear”, “nearest”, “zero”, “slinear”, “quadratic”, “cubic”, and “polynomial”. The method determines the flexibility or ‘wiggliness’ allowed in the temporal gap-filling. You can try a few different methods below. Further information can be found in the scipy documentation.

In this case, we generate a timeseries with an image for every two days of the year, giving a dataset with 182 time-steps (approx. = 365/2). This gives a nice smooth animation without placing excessive demand on computation and memory.

ds = ds.resample(time="2D").interpolate("linear").sel(
    time=slice('2022-01-01', '2022-12-31'))

Dimensions:      (y: 609, x: 483, time: 183)
  * y            (y) float64 -2.244e+06 -2.244e+06 ... -2.256e+06 -2.256e+06
  * x            (x) float64 2.489e+06 2.489e+06 ... 2.499e+06 2.499e+06
    spatial_ref  int32 6933
  * time         (time) datetime64[ns] 2022-01-01 2022-01-03 ... 2022-12-31
Data variables:
    red          (time, y, x) float64 1.082e+03 1.139e+03 ... 443.4 443.0
    green        (time, y, x) float64 967.4 978.1 937.1 ... 726.4 682.4 676.7
    blue         (time, y, x) float64 667.5 705.3 644.7 ... 447.3 433.0 429.8
    crs:           epsg:6933
    grid_mapping:  spatial_ref

Load rainfall data

We will load daily rainfall data from CHIRPS and then resample it to sum rainfall every two days, so the temporal resolution matches that of the true-colour imagery. We will also accumulate rainfall across the year using the cumsum() function. This enables us to visualise total rainfall, which can be more informative than viewing sporadic rainfall events. However, there are numerous other ways rainfall can be visualised through time which you may like to explore.

A static chart of cumulative annual rainfall is generated below. For the default location of Mosiotunya (Victoria Falls), a distinct dry season through the middle part of the year is evident.

ds_rf_daily = dc.load(product='rainfall_chirps_daily',
                y = lat_range,
                x = lon_range,
                resolution=(-5000, 5000),

ds_rf_daily['rainfall'].resample(time='2D').sum().mean(['y', 'x']).cumsum().plot(figsize=(16, 4))
plt.ylabel('%s (%s)' % ('Total Precipitation', ds_rf_daily['rainfall'].attrs['units']));

Define and run animation

The below cell defines and runs the animation through the following steps: 1. Setting up two sublots. 2. Defining axis parameters. 3. Defining an update function. 4. Running and plotting the animation.

You can optionally save the animation to a .gif by removing the # before the command. Note that this closes the plot so you will have to add the # back in and run the cell again to visualise the animation in dynamic form.

For the default region, we can see that the vegetation is green and waterfall flowing in early months. As the dry period begins, the vegetation senesces, though the flow of the waterfall takes longer to slow.

# create a figure and axes
fig = plt.figure(figsize=(10,5))
ax1 = plt.subplot(122)
ax2 = plt.subplot(121)

ax1.set_title("Cumulative Rainfall")
ax1.set_ylabel("Total Precipitation (mm)")

bands=['red', 'green', 'blue']

cax = (ds[bands].isel(time=0).to_array().transpose('y','x','variable').squeeze()/np.max(
    ).transpose('y','x','variable').squeeze())).plot.imshow(rgb='variable', animated = True, robust=True)

def update(num, x, y, line):
    ax2.set_title("Time = " + str(ds[bands].coords['time'].values[(num)])[:12])
    line.set_data(x[:num], y[:num])
    return line,

x = ds_rf_daily['rainfall'].resample(time='2D').sum().mean(['y', 'x']).cumsum().time
y = ds_rf_daily['rainfall'].resample(time='2D').sum().mean(['y', 'x']).cumsum()

line, = ax1.plot(x, y)


ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line],
                              interval=200, blit=True)'rgb_rain_anim.gif')