This notebook provides an introduction to CAMS global reanalysis of atmospheric composition (EAC4) and shows you how the variable Dust Aerosol Optical Depth at 550nm
can be used to better understand past dust events.
EAC4 (ECMWF Atmospheric Composition Reanalysis 4) is the fourth generation ECMWF global reanalysis of atmospheric composition. CAMS Reanalysis combines model data with observations from across the world into a globally complete and consistent dataset using a model of the atmosphere based on the laws of physics and chemistry. This principle, called data assimilation, is based on the method used by numerical weather prediction centres and air quality forecasting centres, where every so many hours (12 hours at ECMWF) a previous forecast is combined with newly available observations in an optimal way to produce a new best estimate of the state of the atmosphere, called analysis, from which an updated, improved forecast is issued. Reanalysis works in the same way to allow for the provision of a dataset spanning back more than a decade. Reanalysis does not have the constraint of issuing timely forecasts, so there is more time to collect observations, and when going further back in time, to allow for the ingestion of improved versions of the original observations, which all benefit the quality of the reanalysis product.
The notebook examines the Saharan Dust event which occured over Europe at the beginning of February 2021.
Spatial resolution:
0.75° x 0.75°
Spatial coverage:Global
Temporal resolution:3-hourly
Temporal coverage:2003 to 2022
Data format:GRIB
orzipped NetCDF
CAMS global atmospheric composition reanalysis are available for download via the Copernicus Atmosphere Data Store (ADS). You will need to create an ADS account here.
Data from the ADS can be downloaded in two ways:
manually
via the ADS web interfaceprogrammatically
with a Python package called cdsapi (more information)import xarray as xr
import matplotlib.pyplot as plt
import matplotlib.colors
from matplotlib.cm import get_cmap
from matplotlib import animation
from matplotlib.axes import Axes
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.feature as cfeature
from cartopy.mpl.geoaxes import GeoAxes
GeoAxes._pcolormesh_patched = Axes.pcolormesh
from IPython.display import HTML
import warnings
warnings.simplefilter(action = "ignore", category = RuntimeWarning)
%run ../functions.ipynb
The CDS Application Program Interface (CDS API)
is a Python library which allows you to access data from the ADS programmatically
. In order to use the CDS API, follow the steps below:
######
of the KEY
variable below with your individual CDS API keyNote: You find your CDS API key displayed in the black terminal box under the section Install the CDS API key
. If you do not see a URL or key appear in the black terminal box, please refresh your browser tab.
URL='https://ads.atmosphere.copernicus.eu/api/v2'
KEY='#########################'
The next step is then to request the data with a so called API request
. Via the ADS web interface, you can select the data and at the end of the web interface, you can open the ADS request via Show API request
. Copy the request displayed there in the cell below. Once you execute the cell, the download of the data starts automatically. You will see a new file called 20210204_duaod.nc
appear in the same folder as this notebook. This is for demonstration purposes only.
import cdsapi
c = cdsapi.Client(url=URL, key=KEY)
c.retrieve(
'cams-global-reanalysis-eac4',
{
'format': 'netcdf',
'variable': 'dust_aerosol_optical_depth_550nm',
'date': '2021-02-04/2021-02-08',
'time': [
'00:00', '03:00', '06:00',
'09:00', '12:00', '15:00',
'18:00', '21:00',
],
},
'./20210204_duaod.nc')
CAMS global near-real-time forecast data is available either in GRIB
or netCDF
. The data for the present example has been downloaded as netCDF
.
You can use xarray's function xr.open_dataset()
to open the netCDF file as xarray.Dataset
.
file = xr.open_dataset('../../../eodata/dust/part1/3_model/cams/global_reanalysis/20210204_duaod.nc')
file
<xarray.Dataset> Dimensions: (longitude: 480, latitude: 241, time: 40) Coordinates: * longitude (longitude) float32 0.0 0.75 1.5 2.25 ... 357.0 357.8 358.5 359.2 * latitude (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0 * time (time) datetime64[ns] 2021-02-04 ... 2021-02-08T21:00:00 Data variables: duaod550 (time, latitude, longitude) float32 ... Attributes: Conventions: CF-1.6 history: 2023-01-17 13:21:44 GMT by grib_to_netcdf-2.25.1: /opt/ecmw...
array([ 0. , 0.75, 1.5 , ..., 357.75, 358.5 , 359.25], dtype=float32)
array([ 90. , 89.25, 88.5 , ..., -88.5 , -89.25, -90. ], dtype=float32)
array(['2021-02-04T00:00:00.000000000', '2021-02-04T03:00:00.000000000', '2021-02-04T06:00:00.000000000', '2021-02-04T09:00:00.000000000', '2021-02-04T12:00:00.000000000', '2021-02-04T15:00:00.000000000', '2021-02-04T18:00:00.000000000', '2021-02-04T21:00:00.000000000', '2021-02-05T00:00:00.000000000', '2021-02-05T03:00:00.000000000', '2021-02-05T06:00:00.000000000', '2021-02-05T09:00:00.000000000', '2021-02-05T12:00:00.000000000', '2021-02-05T15:00:00.000000000', '2021-02-05T18:00:00.000000000', '2021-02-05T21:00:00.000000000', '2021-02-06T00:00:00.000000000', '2021-02-06T03:00:00.000000000', '2021-02-06T06:00:00.000000000', '2021-02-06T09:00:00.000000000', '2021-02-06T12:00:00.000000000', '2021-02-06T15:00:00.000000000', '2021-02-06T18:00:00.000000000', '2021-02-06T21:00:00.000000000', '2021-02-07T00:00:00.000000000', '2021-02-07T03:00:00.000000000', '2021-02-07T06:00:00.000000000', '2021-02-07T09:00:00.000000000', '2021-02-07T12:00:00.000000000', '2021-02-07T15:00:00.000000000', '2021-02-07T18:00:00.000000000', '2021-02-07T21:00:00.000000000', '2021-02-08T00:00:00.000000000', '2021-02-08T03:00:00.000000000', '2021-02-08T06:00:00.000000000', '2021-02-08T09:00:00.000000000', '2021-02-08T12:00:00.000000000', '2021-02-08T15:00:00.000000000', '2021-02-08T18:00:00.000000000', '2021-02-08T21:00:00.000000000'], dtype='datetime64[ns]')
[4627200 values with dtype=float32]
The data above has three dimensions (latitude
, longitude
, time
) and one data variable:
duaod550
: Dust Aerosol Optical Depth at 550nmLet us inspect the coordinates of the file more in detail.
Below, you see that the data set consists of 40 time steps, ranging from 4 February 2021 0 UTC to 8 February 2021 21 UTC in a 3-hour timestep.
file.time
<xarray.DataArray 'time' (time: 40)> array(['2021-02-04T00:00:00.000000000', '2021-02-04T03:00:00.000000000', '2021-02-04T06:00:00.000000000', '2021-02-04T09:00:00.000000000', '2021-02-04T12:00:00.000000000', '2021-02-04T15:00:00.000000000', '2021-02-04T18:00:00.000000000', '2021-02-04T21:00:00.000000000', '2021-02-05T00:00:00.000000000', '2021-02-05T03:00:00.000000000', '2021-02-05T06:00:00.000000000', '2021-02-05T09:00:00.000000000', '2021-02-05T12:00:00.000000000', '2021-02-05T15:00:00.000000000', '2021-02-05T18:00:00.000000000', '2021-02-05T21:00:00.000000000', '2021-02-06T00:00:00.000000000', '2021-02-06T03:00:00.000000000', '2021-02-06T06:00:00.000000000', '2021-02-06T09:00:00.000000000', '2021-02-06T12:00:00.000000000', '2021-02-06T15:00:00.000000000', '2021-02-06T18:00:00.000000000', '2021-02-06T21:00:00.000000000', '2021-02-07T00:00:00.000000000', '2021-02-07T03:00:00.000000000', '2021-02-07T06:00:00.000000000', '2021-02-07T09:00:00.000000000', '2021-02-07T12:00:00.000000000', '2021-02-07T15:00:00.000000000', '2021-02-07T18:00:00.000000000', '2021-02-07T21:00:00.000000000', '2021-02-08T00:00:00.000000000', '2021-02-08T03:00:00.000000000', '2021-02-08T06:00:00.000000000', '2021-02-08T09:00:00.000000000', '2021-02-08T12:00:00.000000000', '2021-02-08T15:00:00.000000000', '2021-02-08T18:00:00.000000000', '2021-02-08T21:00:00.000000000'], dtype='datetime64[ns]') Coordinates: * time (time) datetime64[ns] 2021-02-04 ... 2021-02-08T21:00:00 Attributes: long_name: time
array(['2021-02-04T00:00:00.000000000', '2021-02-04T03:00:00.000000000', '2021-02-04T06:00:00.000000000', '2021-02-04T09:00:00.000000000', '2021-02-04T12:00:00.000000000', '2021-02-04T15:00:00.000000000', '2021-02-04T18:00:00.000000000', '2021-02-04T21:00:00.000000000', '2021-02-05T00:00:00.000000000', '2021-02-05T03:00:00.000000000', '2021-02-05T06:00:00.000000000', '2021-02-05T09:00:00.000000000', '2021-02-05T12:00:00.000000000', '2021-02-05T15:00:00.000000000', '2021-02-05T18:00:00.000000000', '2021-02-05T21:00:00.000000000', '2021-02-06T00:00:00.000000000', '2021-02-06T03:00:00.000000000', '2021-02-06T06:00:00.000000000', '2021-02-06T09:00:00.000000000', '2021-02-06T12:00:00.000000000', '2021-02-06T15:00:00.000000000', '2021-02-06T18:00:00.000000000', '2021-02-06T21:00:00.000000000', '2021-02-07T00:00:00.000000000', '2021-02-07T03:00:00.000000000', '2021-02-07T06:00:00.000000000', '2021-02-07T09:00:00.000000000', '2021-02-07T12:00:00.000000000', '2021-02-07T15:00:00.000000000', '2021-02-07T18:00:00.000000000', '2021-02-07T21:00:00.000000000', '2021-02-08T00:00:00.000000000', '2021-02-08T03:00:00.000000000', '2021-02-08T06:00:00.000000000', '2021-02-08T09:00:00.000000000', '2021-02-08T12:00:00.000000000', '2021-02-08T15:00:00.000000000', '2021-02-08T18:00:00.000000000', '2021-02-08T21:00:00.000000000'], dtype='datetime64[ns]')
array(['2021-02-04T00:00:00.000000000', '2021-02-04T03:00:00.000000000', '2021-02-04T06:00:00.000000000', '2021-02-04T09:00:00.000000000', '2021-02-04T12:00:00.000000000', '2021-02-04T15:00:00.000000000', '2021-02-04T18:00:00.000000000', '2021-02-04T21:00:00.000000000', '2021-02-05T00:00:00.000000000', '2021-02-05T03:00:00.000000000', '2021-02-05T06:00:00.000000000', '2021-02-05T09:00:00.000000000', '2021-02-05T12:00:00.000000000', '2021-02-05T15:00:00.000000000', '2021-02-05T18:00:00.000000000', '2021-02-05T21:00:00.000000000', '2021-02-06T00:00:00.000000000', '2021-02-06T03:00:00.000000000', '2021-02-06T06:00:00.000000000', '2021-02-06T09:00:00.000000000', '2021-02-06T12:00:00.000000000', '2021-02-06T15:00:00.000000000', '2021-02-06T18:00:00.000000000', '2021-02-06T21:00:00.000000000', '2021-02-07T00:00:00.000000000', '2021-02-07T03:00:00.000000000', '2021-02-07T06:00:00.000000000', '2021-02-07T09:00:00.000000000', '2021-02-07T12:00:00.000000000', '2021-02-07T15:00:00.000000000', '2021-02-07T18:00:00.000000000', '2021-02-07T21:00:00.000000000', '2021-02-08T00:00:00.000000000', '2021-02-08T03:00:00.000000000', '2021-02-08T06:00:00.000000000', '2021-02-08T09:00:00.000000000', '2021-02-08T12:00:00.000000000', '2021-02-08T15:00:00.000000000', '2021-02-08T18:00:00.000000000', '2021-02-08T21:00:00.000000000'], dtype='datetime64[ns]')
The latitude values have a 0.75 degrees resolution and have a global N-S coverage.
file.latitude
<xarray.DataArray 'latitude' (latitude: 241)> array([ 90. , 89.25, 88.5 , ..., -88.5 , -89.25, -90. ], dtype=float32) Coordinates: * latitude (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0 Attributes: units: degrees_north long_name: latitude
array([ 90. , 89.25, 88.5 , ..., -88.5 , -89.25, -90. ], dtype=float32)
array([ 90. , 89.25, 88.5 , ..., -88.5 , -89.25, -90. ], dtype=float32)
The longitude values have a 0.75 degrees resolution as well, and are disseminated in a [0, 360] grid.
file.longitude
<xarray.DataArray 'longitude' (longitude: 480)> array([ 0. , 0.75, 1.5 , ..., 357.75, 358.5 , 359.25], dtype=float32) Coordinates: * longitude (longitude) float32 0.0 0.75 1.5 2.25 ... 357.0 357.8 358.5 359.2 Attributes: units: degrees_east long_name: longitude
array([ 0. , 0.75, 1.5 , ..., 357.75, 358.5 , 359.25], dtype=float32)
array([ 0. , 0.75, 1.5 , ..., 357.75, 358.5 , 359.25], dtype=float32)
Above, you see that the longitude
variables are in the range of [0, 359.6]
. Per default, ECMWF data are on a [0, 360] grid. Let us bring the longitude coordinates to a [-180, 180]
grid. You can use the xarray function assign_coords()
to assign new values to coordinates of a xarray data arra. The code below shifts your longitude coordinates from [0, 359.6]
to [-180, 179.6]
.
file = file.assign_coords(longitude=(((file.longitude + 180) % 360) - 180)).sortby('longitude')
file
<xarray.Dataset> Dimensions: (longitude: 480, latitude: 241, time: 40) Coordinates: * longitude (longitude) float32 -180.0 -179.2 -178.5 ... 177.8 178.5 179.2 * latitude (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0 * time (time) datetime64[ns] 2021-02-04 ... 2021-02-08T21:00:00 Data variables: duaod550 (time, latitude, longitude) float32 ... Attributes: Conventions: CF-1.6 history: 2023-01-17 13:21:44 GMT by grib_to_netcdf-2.25.1: /opt/ecmw...
array([-180. , -179.25, -178.5 , ..., 177.75, 178.5 , 179.25], dtype=float32)
array([ 90. , 89.25, 88.5 , ..., -88.5 , -89.25, -90. ], dtype=float32)
array(['2021-02-04T00:00:00.000000000', '2021-02-04T03:00:00.000000000', '2021-02-04T06:00:00.000000000', '2021-02-04T09:00:00.000000000', '2021-02-04T12:00:00.000000000', '2021-02-04T15:00:00.000000000', '2021-02-04T18:00:00.000000000', '2021-02-04T21:00:00.000000000', '2021-02-05T00:00:00.000000000', '2021-02-05T03:00:00.000000000', '2021-02-05T06:00:00.000000000', '2021-02-05T09:00:00.000000000', '2021-02-05T12:00:00.000000000', '2021-02-05T15:00:00.000000000', '2021-02-05T18:00:00.000000000', '2021-02-05T21:00:00.000000000', '2021-02-06T00:00:00.000000000', '2021-02-06T03:00:00.000000000', '2021-02-06T06:00:00.000000000', '2021-02-06T09:00:00.000000000', '2021-02-06T12:00:00.000000000', '2021-02-06T15:00:00.000000000', '2021-02-06T18:00:00.000000000', '2021-02-06T21:00:00.000000000', '2021-02-07T00:00:00.000000000', '2021-02-07T03:00:00.000000000', '2021-02-07T06:00:00.000000000', '2021-02-07T09:00:00.000000000', '2021-02-07T12:00:00.000000000', '2021-02-07T15:00:00.000000000', '2021-02-07T18:00:00.000000000', '2021-02-07T21:00:00.000000000', '2021-02-08T00:00:00.000000000', '2021-02-08T03:00:00.000000000', '2021-02-08T06:00:00.000000000', '2021-02-08T09:00:00.000000000', '2021-02-08T12:00:00.000000000', '2021-02-08T15:00:00.000000000', '2021-02-08T18:00:00.000000000', '2021-02-08T21:00:00.000000000'], dtype='datetime64[ns]')
[4627200 values with dtype=float32]
Note that data disseminated in 2022 onwards may be already regridded onto a [-180, 180] grid, so you can skip this step if your data is already regridded.
Let us extract from the dataset above the data variable Dust Aerosol Optical Depth (AOD) at 550nm
as xarray.DataArray
with the name du_aod
. You can load a data array from a xarray dataset by specifying the name of the variable (duaod550
) in square brackets.
du_aod = file['duaod550']
du_aod
<xarray.DataArray 'duaod550' (time: 40, latitude: 241, longitude: 480)> [4627200 values with dtype=float32] Coordinates: * longitude (longitude) float32 -180.0 -179.2 -178.5 ... 177.8 178.5 179.2 * latitude (latitude) float32 90.0 89.25 88.5 87.75 ... -88.5 -89.25 -90.0 * time (time) datetime64[ns] 2021-02-04 ... 2021-02-08T21:00:00 Attributes: units: ~ long_name: Dust Aerosol Optical Depth at 550nm
[4627200 values with dtype=float32]
array([-180. , -179.25, -178.5 , ..., 177.75, 178.5 , 179.25], dtype=float32)
array([ 90. , 89.25, 88.5 , ..., -88.5 , -89.25, -90. ], dtype=float32)
array(['2021-02-04T00:00:00.000000000', '2021-02-04T03:00:00.000000000', '2021-02-04T06:00:00.000000000', '2021-02-04T09:00:00.000000000', '2021-02-04T12:00:00.000000000', '2021-02-04T15:00:00.000000000', '2021-02-04T18:00:00.000000000', '2021-02-04T21:00:00.000000000', '2021-02-05T00:00:00.000000000', '2021-02-05T03:00:00.000000000', '2021-02-05T06:00:00.000000000', '2021-02-05T09:00:00.000000000', '2021-02-05T12:00:00.000000000', '2021-02-05T15:00:00.000000000', '2021-02-05T18:00:00.000000000', '2021-02-05T21:00:00.000000000', '2021-02-06T00:00:00.000000000', '2021-02-06T03:00:00.000000000', '2021-02-06T06:00:00.000000000', '2021-02-06T09:00:00.000000000', '2021-02-06T12:00:00.000000000', '2021-02-06T15:00:00.000000000', '2021-02-06T18:00:00.000000000', '2021-02-06T21:00:00.000000000', '2021-02-07T00:00:00.000000000', '2021-02-07T03:00:00.000000000', '2021-02-07T06:00:00.000000000', '2021-02-07T09:00:00.000000000', '2021-02-07T12:00:00.000000000', '2021-02-07T15:00:00.000000000', '2021-02-07T18:00:00.000000000', '2021-02-07T21:00:00.000000000', '2021-02-08T00:00:00.000000000', '2021-02-08T03:00:00.000000000', '2021-02-08T06:00:00.000000000', '2021-02-08T09:00:00.000000000', '2021-02-08T12:00:00.000000000', '2021-02-08T15:00:00.000000000', '2021-02-08T18:00:00.000000000', '2021-02-08T21:00:00.000000000'], dtype='datetime64[ns]')
Above, you see that the variable du_aod
has two attributes, units
and long_name
. Let us define variables for those attributes. The variables can be used later for visualizing the data.
long_name = du_aod.long_name
units = du_aod.units
Let us do the same for the coordinates longitude
and latitude
.
latitude = du_aod.latitude
longitude = du_aod.longitude
The next step is to visualize the dataset. You can use the function visualize_pcolormesh, which makes use of matploblib's function pcolormesh
and the Cartopy library.
With ?visualize_pcolormesh
you can open the function's docstring to see what keyword arguments are needed to prepare your plot.
?visualize_pcolormesh
Signature: visualize_pcolormesh( data_array, longitude, latitude, projection, color_scale, unit, long_name, vmin, vmax, set_global=True, lonmin=-180, lonmax=180, latmin=-90, latmax=90, ) Docstring: Visualizes a xarray.DataArray with matplotlib's pcolormesh function. Parameters: data_array(xarray.DataArray): xarray.DataArray holding the data values longitude(xarray.DataArray): xarray.DataArray holding the longitude values latitude(xarray.DataArray): xarray.DataArray holding the latitude values projection(str): a projection provided by the cartopy library, e.g. ccrs.PlateCarree() color_scale(str): string taken from matplotlib's color ramp reference unit(str): the unit of the parameter, taken from the NetCDF file if possible long_name(str): long name of the parameter, taken from the NetCDF file if possible vmin(int): minimum number on visualisation legend vmax(int): maximum number on visualisation legend set_global(boolean): optional kwarg, default is True lonmin,lonmax,latmin,latmax(float): optional kwarg, set geographic extent is set_global kwarg is set to False File: /tmp/ipykernel_823/2163691773.py Type: function
You can make use of the variables we defined above:
units
long_name
latitude
longitude
Additionally, you can specify the color scale
and minimum (vmin
) and maxium (vmax
) data values.
time_index = 6
visualize_pcolormesh(data_array=du_aod[time_index,:,:],
longitude=longitude,
latitude=latitude,
projection=ccrs.PlateCarree(),
color_scale='hot_r',
unit=units,
long_name=long_name + ' ' + str(du_aod[time_index,:,:].time.data)[0:19],
vmin=0,
vmax=1.5)
(<Figure size 1440x720 with 2 Axes>, <GeoAxesSubplot:title={'center':'Dust Aerosol Optical Depth at 550nm 2021-02-04T18:00:00'}>)
The map above shows Dust Aerosol Optical Depth at 550nm globally. Let us create a geographical subset for Europe, in order to better analyse the Saharan dust event which impacts parts of central and southern Europe.
For geographical subsetting, you can use the function generate_geographical_subset. You can use ?generate_geographical_subset
to open the docstring in order to see the function's keyword arguments.
?generate_geographical_subset
Signature: generate_geographical_subset( xarray, latmin, latmax, lonmin, lonmax, reassign=False, ) Docstring: Generates a geographical subset of a xarray.DataArray and if kwarg reassign=True, shifts the longitude grid from a 0-360 to a -180 to 180 deg grid. Parameters: xarray(xarray.DataArray): a xarray DataArray with latitude and longitude coordinates latmin, latmax, lonmin, lonmax(int): lat/lon boundaries of the geographical subset reassign(boolean): default is False Returns: Geographical subset of a xarray.DataArray. File: /tmp/ipykernel_823/3979307327.py Type: function
Define the bounding box information for Europe
latmin = 28.
latmax = 71.
lonmin = -22.
lonmax = 43
Now, let us apply the function generate_geographical_subset to subset the du_aod
xarray.DataArray. Let us call the new xarray.DataArray
du_aod_subset
.
du_aod_subset = generate_geographical_subset(xarray=du_aod,
latmin=latmin,
latmax=latmax,
lonmin=lonmin,
lonmax=lonmax)
du_aod_subset
<xarray.DataArray 'duaod550' (time: 40, latitude: 57, longitude: 87)> array([[[2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], ..., [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 9.65609550e-02, 1.21821165e-01, 1.50712252e-01], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 4.83577251e-02, 6.50434494e-02, 9.71016884e-02], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.33292580e-02, 3.46076488e-02, 6.21080399e-02]], [[2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], ... [1.62243843e-03, 1.46794319e-03, 1.39069557e-03, ..., 9.34720039e-03, 9.11545753e-03, 8.96096230e-03], [3.24463844e-03, 3.16739082e-03, 3.01289558e-03, ..., 1.08923912e-02, 1.14331245e-02, 1.19738579e-02], [4.24885750e-03, 4.17160988e-03, 4.01711464e-03, ..., 1.45230293e-02, 1.52955055e-02, 1.55272484e-02]], [[2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], ..., [9.27209854e-04, 8.49962234e-04, 7.72714615e-04, ..., 8.88371468e-03, 8.72921944e-03, 8.88371468e-03], [2.39491463e-03, 2.24041939e-03, 2.16317177e-03, ..., 1.04289055e-02, 1.05834007e-02, 1.07378960e-02], [3.78537178e-03, 3.78537178e-03, 3.70812416e-03, ..., 1.28235817e-02, 1.30553246e-02, 1.29780769e-02]]], dtype=float32) Coordinates: * longitude (longitude) float32 -21.75 -21.0 -20.25 ... 41.25 42.0 42.75 * latitude (latitude) float32 70.5 69.75 69.0 68.25 ... 30.0 29.25 28.5 * time (time) datetime64[ns] 2021-02-04 ... 2021-02-08T21:00:00 Attributes: units: ~ long_name: Dust Aerosol Optical Depth at 550nm
array([[[2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], ..., [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 9.65609550e-02, 1.21821165e-01, 1.50712252e-01], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 4.83577251e-02, 6.50434494e-02, 9.71016884e-02], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.33292580e-02, 3.46076488e-02, 6.21080399e-02]], [[2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], ... [1.62243843e-03, 1.46794319e-03, 1.39069557e-03, ..., 9.34720039e-03, 9.11545753e-03, 8.96096230e-03], [3.24463844e-03, 3.16739082e-03, 3.01289558e-03, ..., 1.08923912e-02, 1.14331245e-02, 1.19738579e-02], [4.24885750e-03, 4.17160988e-03, 4.01711464e-03, ..., 1.45230293e-02, 1.52955055e-02, 1.55272484e-02]], [[2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], [2.38418579e-07, 2.38418579e-07, 2.38418579e-07, ..., 2.38418579e-07, 2.38418579e-07, 2.38418579e-07], ..., [9.27209854e-04, 8.49962234e-04, 7.72714615e-04, ..., 8.88371468e-03, 8.72921944e-03, 8.88371468e-03], [2.39491463e-03, 2.24041939e-03, 2.16317177e-03, ..., 1.04289055e-02, 1.05834007e-02, 1.07378960e-02], [3.78537178e-03, 3.78537178e-03, 3.70812416e-03, ..., 1.28235817e-02, 1.30553246e-02, 1.29780769e-02]]], dtype=float32)
array([-21.75, -21. , -20.25, -19.5 , -18.75, -18. , -17.25, -16.5 , -15.75, -15. , -14.25, -13.5 , -12.75, -12. , -11.25, -10.5 , -9.75, -9. , -8.25, -7.5 , -6.75, -6. , -5.25, -4.5 , -3.75, -3. , -2.25, -1.5 , -0.75, 0. , 0.75, 1.5 , 2.25, 3. , 3.75, 4.5 , 5.25, 6. , 6.75, 7.5 , 8.25, 9. , 9.75, 10.5 , 11.25, 12. , 12.75, 13.5 , 14.25, 15. , 15.75, 16.5 , 17.25, 18. , 18.75, 19.5 , 20.25, 21. , 21.75, 22.5 , 23.25, 24. , 24.75, 25.5 , 26.25, 27. , 27.75, 28.5 , 29.25, 30. , 30.75, 31.5 , 32.25, 33. , 33.75, 34.5 , 35.25, 36. , 36.75, 37.5 , 38.25, 39. , 39.75, 40.5 , 41.25, 42. , 42.75], dtype=float32)
array([70.5 , 69.75, 69. , 68.25, 67.5 , 66.75, 66. , 65.25, 64.5 , 63.75, 63. , 62.25, 61.5 , 60.75, 60. , 59.25, 58.5 , 57.75, 57. , 56.25, 55.5 , 54.75, 54. , 53.25, 52.5 , 51.75, 51. , 50.25, 49.5 , 48.75, 48. , 47.25, 46.5 , 45.75, 45. , 44.25, 43.5 , 42.75, 42. , 41.25, 40.5 , 39.75, 39. , 38.25, 37.5 , 36.75, 36. , 35.25, 34.5 , 33.75, 33. , 32.25, 31.5 , 30.75, 30. , 29.25, 28.5 ], dtype=float32)
array(['2021-02-04T00:00:00.000000000', '2021-02-04T03:00:00.000000000', '2021-02-04T06:00:00.000000000', '2021-02-04T09:00:00.000000000', '2021-02-04T12:00:00.000000000', '2021-02-04T15:00:00.000000000', '2021-02-04T18:00:00.000000000', '2021-02-04T21:00:00.000000000', '2021-02-05T00:00:00.000000000', '2021-02-05T03:00:00.000000000', '2021-02-05T06:00:00.000000000', '2021-02-05T09:00:00.000000000', '2021-02-05T12:00:00.000000000', '2021-02-05T15:00:00.000000000', '2021-02-05T18:00:00.000000000', '2021-02-05T21:00:00.000000000', '2021-02-06T00:00:00.000000000', '2021-02-06T03:00:00.000000000', '2021-02-06T06:00:00.000000000', '2021-02-06T09:00:00.000000000', '2021-02-06T12:00:00.000000000', '2021-02-06T15:00:00.000000000', '2021-02-06T18:00:00.000000000', '2021-02-06T21:00:00.000000000', '2021-02-07T00:00:00.000000000', '2021-02-07T03:00:00.000000000', '2021-02-07T06:00:00.000000000', '2021-02-07T09:00:00.000000000', '2021-02-07T12:00:00.000000000', '2021-02-07T15:00:00.000000000', '2021-02-07T18:00:00.000000000', '2021-02-07T21:00:00.000000000', '2021-02-08T00:00:00.000000000', '2021-02-08T03:00:00.000000000', '2021-02-08T06:00:00.000000000', '2021-02-08T09:00:00.000000000', '2021-02-08T12:00:00.000000000', '2021-02-08T15:00:00.000000000', '2021-02-08T18:00:00.000000000', '2021-02-08T21:00:00.000000000'], dtype='datetime64[ns]')
Let us visualize the subsetted xarray.DataArray
again. This time, you set the set_global
kwarg to False
and you specify the longitude and latitude bounds specified above.
Additionally, in order to have the time information as part of the title, we add the string of the datetime information to the long_name
variable: long_name + ' ' + str(du_aod_subset[##,:,:].time.data)[0:19]
.
time_index = 14
visualize_pcolormesh(data_array=du_aod_subset[time_index,:,:],
longitude=du_aod_subset.longitude,
latitude=du_aod_subset.latitude,
projection=ccrs.PlateCarree(),
color_scale='hot_r',
unit=units,
long_name=long_name + ' ' + str(du_aod_subset[time_index,:,:].time.data)[0:19],
vmin=0,
vmax=1,
lonmin=lonmin,
lonmax=lonmax,
latmin=latmin,
latmax=latmax,
set_global=False)
(<Figure size 1440x720 with 2 Axes>, <GeoAxesSubplot:title={'center':'Dust Aerosol Optical Depth at 550nm 2021-02-05T18:00:00'}>)
In the last step, you can animate the Dust Aerosol Optical Depth at 550nm
in order to see how the trace gas develops over a period of 5 days, from 4th to 8th February 2021.
You can do animations with matplotlib's function animation
. Jupyter's function HTML
can then be used to display HTML and video content.
The animation function consists of 4 parts:
Here, you define the general plot your animation shall use to initialise the animation. You can also define the number of frames (time steps) your animation shall have.
An animation consists of three functions: draw()
, init()
and animate()
. draw()
is the function where individual frames are passed on and the figure is returned as image. In this example, the function redraws the plot for each time step. init()
returns the figure you defined for the initial state. animate()
returns the draw()
function and animates the function over the given number of frames (time steps).
animate.FuncAnimation
object: The functions defined before are now combined to build an animate.FuncAnimation
object.
As a final step, you can integrate the animation into the notebook with the HTML
class. You take the generate animation object and convert it to a HTML5 video with the to_html5_video
function
# Setting the initial state:
# 1. Define figure for initial plot
fig, ax = visualize_pcolormesh(data_array=du_aod_subset[0,:,:],
longitude=du_aod_subset.longitude,
latitude=du_aod_subset.latitude,
projection=ccrs.PlateCarree(),
color_scale='hot_r',
unit='-',
long_name=long_name + ' '+ str(du_aod_subset.time[0].data)[0:19],
vmin=0,
vmax=1,
lonmin=lonmin,
lonmax=lonmax,
latmin=latmin,
latmax=latmax,
set_global=False)
frames = 30
def draw(i):
img = plt.pcolormesh(du_aod_subset.longitude,
du_aod_subset.latitude,
du_aod_subset[i,:,:],
cmap='hot_r',
transform=ccrs.PlateCarree(),
vmin=0,
vmax=1,
shading='auto')
ax.set_title(long_name + ' '+ str(du_aod_subset.time[i].data)[0:19], fontsize=20, pad=20.0)
return img
def init():
return fig
def animate(i):
return draw(i)
ani = animation.FuncAnimation(fig, animate, frames, interval=800, blit=False,
init_func=init, repeat=True)
HTML(ani.to_html5_video())
plt.close(fig)
HTML(ani.to_html5_video())
This project is licensed under GNU General Public License v3.0 only and is developed under a Copernicus contract.