from __future__ import print_function
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
Since we already have the machinery for animating the rainfall and the radar, putting them together is a relatively simple process. Mostly we just reuse our previous code.
First:
Draw the Map
from draw_arizona import draw_arizona, draw_roads
phxmap = draw_arizona( -113.75, -111, 32.25, 35.0)
draw_roads(phxmap)
Then:
Prepare the Rainfall Data
from datetime import datetime
from animate_rainfall import load_station_data, plot_weather_station_locs, normalized_hourly_rainfall, RainfallAnimator
from datetime_range import datetime_range
all_times = datetime_range(datetime(2014, 9, 7, 12, 0), datetime(2014, 9, 9, 12, 1))
station_data = load_station_data()
plot_weather_station_locs(phxmap, station_data)
rainfall = normalized_hourly_rainfall(station_data, all_times)
rainfall_animator = RainfallAnimator(phxmap, station_data, rainfall)
Then:
Prepare the Radar Data
from animate_radar import load_radar_data, RadarAnimator
radar_data = load_radar_data()
radar_animator = RadarAnimator(phxmap, radar_data)
Now that we have all the data in place, we can create an animator by grafting together our two previous animators. Now, the interface of those animators should make more sense since they make it easy to combine them into a composite animator.
from matplotlib import animation
class FrankenMator(object):
def __init__(self, fig, *animators):
self.fig = fig
self.animators = animators
def init(self):
artists = []
for x in self.animators:
artists.extend(x.init())
return artists
def animate(self, ts):
artists = []
for x in self.animators:
artists.extend(x.animate(ts))
return artists
def make_animation(self, times, interval=20):
return animation.FuncAnimation(self.fig, self.animate,
init_func=self.init,
frames=times,
interval=interval,
blit=True)
Then we simply call the animation.save on the FuncAnimation we've created and we're done.
animator = FrankenMator(phxmap.fig, rainfall_animator, radar_animator)
anim = animator.make_animation(all_times)
# anim = animate_rainfall(phxmap, station_data, rainfall, all_times)
# These options work for quicktime on OS X. It's possible that some other options may be required for other machines.
anim.save("September_8th_storm_rainfall_radar.m4v", fps=60, extra_args=['-vcodec', 'libx264', '-pix_fmt','yuv420p'])
# This spits out a lot of dots as well as a still of the first frame of the video.
We can view the video as before.
from IPythonVideo import video
video("September_8th_storm_rainfall_radar.m4v", "x-m4v")