This notebook will take you through the following process:
In [1]:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML, Image, display
import tempfile
import os
import shutil
import pyart
from tint.data_utils import get_nexrad_keys, read_nexrad_key
from tint import Cell_tracks, animate
from tint.visualization import embed_mp4_as_gif
In [2]:
# Let's get some nexrad data from Amazon S3 to use for tracking
# I know of some storm cells that passed over Houston back in July of 2015
keys = get_nexrad_keys('khgx', start='20150710_183000', end='20150710_193000')
In [3]:
# We can visualize this data using pyart and matplotlib
fig = plt.figure(figsize=(10, 7))
def plot_ppi(key):
""" Plots ppi map of nexrad data given S3 key. """
plt.clf()
radar = read_nexrad_key(key)
display = pyart.graph.RadarMapDisplay(radar)
display.plot_ppi_map('reflectivity', resolution='10m',
sweep=3, fig=fig,
lat_lines=np.arange(27.5, 31.6, 1),
lon_lines=np.arange(-97, -92.9, 1),
min_lon=-97, max_lon=-93, min_lat=27.5,
max_lat=31.5,
lon_0=radar.longitude['data'][0],
lat_0=radar.latitude['data'][0])
del display, radar
ppi_anim = FuncAnimation(fig, plot_ppi, frames=keys, interval=1000)
tmp_anim = tempfile.NamedTemporaryFile()
ppi_anim.save(tmp_anim.name + '.mp4', writer='ffmpeg')
embed_mp4_as_gif(tmp_anim.name + '.mp4')
os.remove(tmp_anim.name + '.mp4')
plt.close()
In [4]:
# Looks like there are some interesting cells coming off the gulf.
# Let's make a function that maps these volumes to cartesian grids using pyart.
# Most of the cells seem to be in the bottom right quadrant, so we'll only grid
# that part of the domain to save time.
def get_grid(radar):
""" Returns grid object from radar object. """
grid = pyart.map.grid_from_radars(
radar, grid_shape=(31, 401, 401),
grid_limits=((0, 15000), (-200000, 0), (0, 200000)),
fields=['reflectivity'], gridding_algo='map_gates_to_grid',
h_factor=0., nb=0.6, bsp=1., min_radius=200.)
return grid
# Let's write these grids to a temporary location so that we can reuse them.
# This might take a few minutes
tmp_dir = tempfile.mkdtemp()
print('temporary directory:', tmp_dir)
filenames = []
for num, key in enumerate(keys):
print('saving grid', num)
radar = read_nexrad_key(key)
grid = get_grid(radar)
name = os.path.join(tmp_dir, 'grid_' + str(num).zfill(3) + '.nc')
filenames.append(name)
pyart.io.write_grid(name, grid)
del radar, grid
In [5]:
# Now we can easily instantiate generators of these grids like so
grids = (pyart.io.read_grid(fn) for fn in filenames)
In [6]:
# First, let's instantiate a tracks object and view the default parameters
tracks_obj = Cell_tracks()
tracks_obj.params
Out[6]:
In [7]:
# The cells we're interested in look a bit small. Let's reduce the minimum size threshold.
tracks_obj.params['MIN_SIZE'] = 4
# We'll give the generator of grids we made earlier to the get_tracks method of our tracks object.
tracks_obj.get_tracks(grids)
In [8]:
# Now we can view the 'tracks' attribute of our tracks object to see the results.
tracks_obj.tracks.head(20)
Out[8]:
In [9]:
# We can visualize these tracks to assess the performance of the algorithm.
# First we need to make another generator of the same grids for the animator
grids = (pyart.io.read_grid(fn) for fn in filenames)
In [10]:
# Let's save the animation in our temp directory so that it will be cleaned up later.
animate(tracks_obj, grids, os.path.join(tmp_dir, 'tint_demo_anim'),
lat_lines=np.arange(27.9, 29.5, .5),
lon_lines=np.arange(-95, -92.9, .5),
tracers=True)
In [11]:
# The animator saved an mp4 to our local directory. Now we can embed it as a gif in this notebook.
embed_mp4_as_gif(os.path.join(tmp_dir, 'tint_demo_anim.mp4'))
In [12]:
# Let's find the cells that were tracked for the most frames
tracks_obj.tracks.groupby(level='uid').size().sort_values(ascending=False)[:5]
Out[12]:
In [13]:
# Now we can view these cells from a lagrangian perspective
grids = (pyart.io.read_grid(fn) for fn in filenames) # refresh grid generator
animate(tracks_obj, grids, os.path.join(tmp_dir, 'tint_demo_lagrangian'), style='lagrangian', uid='0', alt=2000)
In [14]:
embed_mp4_as_gif(os.path.join(tmp_dir, 'tint_demo_lagrangian.mp4'))
In [15]:
# We can view the attributes of this cell throughout its lifetime
tracks_obj.tracks.xs('0', level='uid')
Out[15]:
In [16]:
# Now we can delete the directory containing all the data and output from this demo.
shutil.rmtree(tmp_dir)
print('temporary directory removed')