<jupyter_start><jupyter_text>Index >>  3.4.2  Ozone hole case study - CAMS animation This notebook gives an example how `Copernicus Atmosphere Monitoring Service (CAMS)` Near-Realtime forecast data can be animated.The example makes use of matplotlib's function `animation` and Jupyter notebooks function `HTML` to display HTML and video content within the notebook.  Load required libraries<jupyter_code>import xarray as xr
import matplotlib.pyplot as plt
from matplotlib import animation

from IPython.display import HTML
import cartopy
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

from matplotlib.axes import Axes
from cartopy.mpl.geoaxes import GeoAxes<jupyter_output><empty_output><jupyter_text>Load helper functions<jupyter_code>from ipynb.fs.full.ltpy_functions import visualize_s5p_pcolormesh<jupyter_output><empty_output><jupyter_text>Load CAMS Near-real time for 10 September 2019 You can load the `CAMS Near-Real Time forecast` data with xarray's function `xr.open_dataset`. The example data has three dimensions, `latitude`, `longitude` and `time`. You can also see that the forecast data has 6 time steps, the initial run and 5 forecast days respectively.<jupyter_code>cams_fc = './eodata/cams/nrt/o3/2019/09/10/20190910_o3_fc.nc'
cams_fc_xr = xr.open_dataset(cams_fc)
cams_fc_xr<jupyter_output><empty_output><jupyter_text>Load the ozone data variable as `DataArray` `gtco3` is the variable name you would like to load as `Data Array`. You see that the data is disseminated in `kg m**-2` as unit.<jupyter_code>cams_o3 = cams_fc_xr['gtco3']
cams_o3<jupyter_output><empty_output><jupyter_text>Convert from `kg m**-2` to `Dobson Unit` Ozone data are often represented in `Dobson Unit`. Thus, with the factor `2.1415*1e-5`, you can easily convert from `kg m**-2` to `Dobson Unit`.<jupyter_code>cams_o3_du = cams_o3 / (2.1415*1e-5)
cams_o3_du<jupyter_output><empty_output><jupyter_text>Animate the total column ozone over the 5 day forecast The animation function consists of 4 parts:- **Setting the initial state:** 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  - **Functions to animate:** 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).  - **Create a `animate.FuncAnimation` object:**  The functions defined before are now combined to build an `animate.FuncAnimation` object.  - **Play the animation as video:** 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<jupyter_code># Setting the initial state:
# 1. Define figure for initial plot
fig, ax = visualize_s5p_pcolormesh(cams_o3_du[0,:,:],cams_o3_du.longitude, cams_o3_du.latitude,ccrs.Orthographic(180,-90), 
                        'jet', 'DU',cams_o3.long_name + ' '+ str(cams_o3_du.time[0].data), 100,600, -180, 179.6, -90. , 90., log=False,set_global=True)

frames = 6

def draw(i):
    img = plt.pcolormesh(cams_o3_du.longitude, cams_o3_du.latitude, cams_o3_du[i,:,:], 
                        cmap='jet', transform=ccrs.PlateCarree(),
                        vmin=100,
                        vmax=600)
    ax.set_title(cams_o3.long_name + ' '+ str(cams_o3_du.time[i].data), 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)<jupyter_output><empty_output><jupyter_text>Play the animation as HTML5 video<jupyter_code>HTML(ani.to_html5_video())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09482758620689655, 'fraction_numerical': 0.02681992337164751, 'mean_word_length': 4.841958041958042, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '72076', 'license': 'MIT', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1340, 'n_words': 442, 'path': '342_ltpy_Ozone_hole_case_study_cams_animation.ipynb', 'repo_name': 'federicofierli1/LTPy_Notebooks_satellite_copernicus', 'size': '4176'}