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
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)
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)
In [18]:
from IPython.core.display import HTML
HTML('<img src="http://geoport.whoi.edu/images/latest.gif" />')
Out[18]:
In [21]:
from IPython import parallel
In [22]:
rc = parallel.Client()
rc.block=True
rc.ids
Out[22]:
In [24]:
view=rc.load_balanced_view()
myArgs=gen_dates(url, vname,days,box,title="")
In [ ]:
view.map(plotWave, myArgs)
In [ ]:
In [ ]: