1. Making a movie using python multiprocessing


In [1]:
import sys
import os
import glob
from multiprocessing import Pool,cpu_count

from pylab import *
import netCDF4
import datetime as dt
import numpy.ma as ma

from datetime import date, datetime, timedelta
from visvis import movieWrite

from PIL import Image

In [2]:
NUM_PROCESSES = 6

In [3]:
def perdelta(start, end, delta):
	curr = start
	while curr < end:
		yield curr
		curr += delta

In [4]:
def plotWave(args):
    url = args['url']
    box = args['box']
    name = args['title']
    start = args['date']
    vname = args['vname']
    imname = 'input_0000'+start.strftime('%Y%m%d%H%M')+'.png'
    nc = netCDF4.Dataset(url)
    lat = nc.variables['lat'][:]
    lon = nc.variables['lon'][:]
    time_var = nc.variables['time']
    dtime = netCDF4.num2date(time_var[:],time_var.units)
    bi=(lon>=box[0])&(lon<=box[2])
    bj=(lat>=box[1])&(lat<=box[3])
    istart = netCDF4.date2index(start,time_var,select='nearest')
    var = nc.variables[vname][istart,bj,bi]
    hs = ma.masked_invalid(var)
    plt.figure(figsize=(8,8)) 
    plt.pcolormesh(lon[bi], lat[bj], hs, vmin=0, vmax=3)
    #print imname
    plt.title('%s %s' %(name, start.strftime('%Y/%m/%d %H:%M')) )
    plt.savefig(imname)

In [5]:
def gen_dates(nc,vname,past,box,title):
	#print url
	for result in perdelta(datetime.utcnow() - timedelta(days=past), datetime.utcnow(), timedelta(hours=1)):
#		print "gen_dates:",type(result),result
		yield {'url':url, 'vname':vname, 'date':result, 'box':box, 'title':title}

In [6]:
def makeGOMmovie(url,vname,days,box,title=""):
    print url
    pool = Pool(processes=4)
    results = pool.imap_unordered(plotWave, gen_dates(url,vname,days,box,title), 100)
    pool.close()
    pool.join()

In [13]:
cd /usgs/data2/notebook/frames


/usgs/data2/notebook/frames

In [14]:
url='http://thredds.ucar.edu/thredds/dodsC/grib/NCEP/WW3/Coastal_US_East_Coast/best'
box = [-71.5,39.5,-63.,46.]
days = 1
vname = 'Significant_height_of_combined_wind_waves_and_swell_surface'
title = 'Wave Height in the Gulf of Maine'

In [15]:
makeGOMmovie(url,vname,days,box,title)


http://thredds.ucar.edu/thredds/dodsC/grib/NCEP/WW3/Coastal_US_East_Coast/best

In [17]:
IMGS = []
imgs = glob.glob("*.png")
print imgs
for im in imgs:
    IMGS.append(Image.open(im))
movieWrite("/var/www/images/latest.gif",IMGS)


['input_0000201305202009.png', 'input_0000201305202109.png', 'input_0000201305202209.png', 'input_0000201305202309.png', 'input_0000201305210009.png', 'input_0000201305210109.png', 'input_0000201305210209.png', 'input_0000201305210309.png', 'input_0000201305210409.png', 'input_0000201305210509.png', 'input_0000201305210609.png', 'input_0000201305210709.png', 'input_0000201305210809.png', 'input_0000201305210909.png', 'input_0000201305211009.png', 'input_0000201305211109.png', 'input_0000201305211209.png', 'input_0000201305211309.png', 'input_0000201305211409.png', 'input_0000201305211509.png', 'input_0000201305211609.png', 'input_0000201305211709.png', 'input_0000201305211809.png', 'input_0000201305211909.png', 'input_0000201305212009.png']
Wrote 25 frames to GIF in 2.09 seconds (83 ms/frame)

In [18]:
from IPython.core.display import HTML
HTML('<img src="http://geoport.whoi.edu/images/latest.gif" />')


Out[18]:

2. Making a movie using Ipython parallel


In [21]:
from IPython import parallel

In [22]:
rc = parallel.Client()
rc.block=True
rc.ids


Out[22]:
[0, 1, 2, 3]

In [24]:
view=rc.load_balanced_view()
myArgs=gen_dates(url, vname,days,box,title="")

In [ ]:
view.map(plotWave, myArgs)

In [ ]:


In [ ]: