Data Animation Using Python (Part 1)

Matplotlib has tools for creating animations which are really slick. You can find some good example animations on the matplotlib examples page. Here we shown the sample script to animate the netcdf sea surface temperature files just like SOTO 2D using the ArtistAnimation routine. The sample SST image frame is displayed in figure 1. Feel free to modify the script to your need.
Here is the data_filenames.list file that contains the list of input files, the script will read this file to extract the netcdf filename. This file can be downloaded from here:
https://podaac-tools.jpl.nasa.gov/drive ... files.list
The python animation script is shown below, and the script can be downloaded here:
https://podaac-tools.jpl.nasa.gov/drive ... dataset.py
Here is the data_filenames.list file that contains the list of input files, the script will read this file to extract the netcdf filename. This file can be downloaded from here:
https://podaac-tools.jpl.nasa.gov/drive ... files.list
- Code: Select all
20160101120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160102120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160103120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160104120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160105120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160106120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160107120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160108120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160109120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
20160110120000-CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc
The python animation script is shown below, and the script can be downloaded here:
https://podaac-tools.jpl.nasa.gov/drive ... dataset.py
- Code: Select all
#! /usr/bin/env python
#
# a skeleton script to animate a set of L4 files using python matplotlib library.
#
#
# 2016.09.01 Yibo Jiang, version 0
##################################
# user parameters to be editted: #
##################################
# Caution: This is a Python script, and Python takes indentation seriously.
# DO NOT CHANGE INDENTATION OF ANY LINE BELOW!
from matplotlib import animation
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
# setup figure object
fig, ax = plt.subplots()
# set up map projection
#m = Basemap(projection='nsper',lon_0=-0,lat_0=90)
parallels = np.arange(-90,90+30,30.)
meridians = np.arange(-180,180+60,60)
m = Basemap(projection='cyl', llcrnrlon=min(meridians), llcrnrlat=min(parallels),
urcrnrlon=max(meridians), urcrnrlat=max(parallels))
# draw costal line and lat lon lines
m.drawcoastlines()
m.drawparallels(parallels)
m.drawmeridians(meridians)
# setup contour levels and colarmap
clevs = np.linspace(270, 310, 21)
cmap=plt.get_cmap("jet")
# Open Data Files which list the filesnames to be processed
f = open('data_files.list', 'r')
# ims is a list of lists, each row is a list of artists to draw in the
# current frame; here we are animating two artists, the contour and a annotatons (title) in each frame
ims = []
# read each data file in the list
for line in f:
filename = line.strip()
ncin = Dataset(filename, 'r')
lon = ncin.variables['lon'][:]
lat = ncin.variables['lat'][:]
data = ncin.variables['analysed_sst'][:]
ncin.close()
lons,lats = np.meshgrid(lon,lat)
cs=m.contourf(lons,lats, data[0,:,:].squeeze(), clevs, cmap=cmap)
m.drawcoastlines()
m.fillcontinents(color='#000000',lake_color='#99ffff')
m.drawparallels(parallels,labels=[1,0,0,0])
meri = m.drawmeridians(meridians,labels=[1,0,0,1])
add_arts = cs.collections
te = ax.text(-180, 99, filename, va='center')
cb = m.colorbar(cs, 'right', size='5%', pad='2%')
cb.set_label('SST (K)', fontsize=10)
for i in meri:
try:
meri[i][1][0].set_rotation(45)
except:
pass
ims.append(add_arts + [te])
ani = animation.ArtistAnimation(fig, ims)
# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
#ani.save('python_anim_dataset.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
plt.show()