In [1]:
from IPython.core.display import HTML
HTML("<iframe src='http://www.ncdc.noaa.gov/oa/satellite/satelliteseye/cyclones/pfctstorm91/pfctstorm.html' width='700' height='600'></iframe>")
Out[1]:
In [2]:
from IPython import parallel
rc = parallel.Client()
view = rc.load_balanced_view()
In [3]:
%%px
import sys
import os
from pylab import *
import netCDF4
import datetime as dt
import numpy.ma as ma
from mpl_toolkits.basemap import Basemap
In [4]:
import sys
import os
from pylab import *
import netCDF4
import datetime as dt
import numpy.ma as ma
from mpl_toolkits.basemap import Basemap
In [5]:
def perdelta(start, end, delta):
curr = start
while curr < end:
yield curr
curr += delta
def plotWave(args):
start = args["date"]
url = args["url"]
box = args["box"]
vname = args["vname"]
pvar = args["pvar"]
uu = args["u"]
vv = args["v"]
directory = args["savepath"]
nc = netCDF4.Dataset(url)
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
u = nc.variables[str(uu)]
v = nc.variables[str(vv)]
time_var = nc.variables[str(pvar)]
dtime = netCDF4.num2date(time_var[:],time_var.units)
istart = netCDF4.date2index(start,time_var,select='nearest')
if box is not None:
bi=(lon>=box[0])&(lon<=box[2])
bj=(lat>=box[1])&(lat<=box[3])
var = nc.variables[vname][istart,bj,bi]
uin = u[istart,bj,bi]
vin = v[istart,bj,bi]
else:
var = nc.variables[vname][istart,:,:]
uin = u[istart,:,:]
vin = v[istart,:,:]
hs = ma.masked_where(np.isnan(var),var)
figure(figsize=(12,12))
m = Basemap(resolution='i', llcrnrlon=-100.0,llcrnrlat=0.0,urcrnrlon=-50.0,urcrnrlat=60.0, \
lat_0=20.,lon_0=-60.,lat_ts=20.)
m.drawcoastlines(color='#6D5F47', linewidth=.4)
m.drawrivers(color='#3366FF', linewidth=.4)
m.fillcontinents(color='#99CC33',lake_color='#85A6D9')
if box is not None:
ff = plt.pcolormesh(lon[bi], lat[bj], hs, vmin=0, vmax=8)
plt.contour(lon[bi], lat[bj], hs, 20, colors='w', linestyles='-', linewidth=0.1)
#plt.barbs(lon[bi], lat[bj], uin, vin, length=5, barbcolor='k', flagcolor='r', linewidth=0.5)
lons, lats = np.meshgrid(lon[bi], lat[bj])
x, y = m(lons, lats)
plt.quiver(x[::5,::5], y[::5,::5], uin[::5,::5], vin[::5,::5], scale=200);
else :
ff = plt.pcolormesh(lon[:], lat[:], hs, vmin=0, vmax=8)
plt.contour(lon[:], lat[:], hs, 10, colors='w', linestyles='-', linewidth=0.1)
lons, lats = np.meshgrid(lon[:], lat[:])
x, y = m(lons, lats)
plt.quiver(x[::5,::5], y[::5,::5], uin[::5,::5], vin[::5,::5], scale=400);
#plt.colorbar()
parallels = np.arange(0.,60,5.)
meridians = np.arange(-100.,-50.,5.)
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10);
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10);
cb = m.colorbar(ff,"bottom", size="4%", pad="3%")
cb.set_label('wave height (m)')
title('The Perfect Storm - %s' % start.strftime('%Y-%m-%d-%H-%M'))
name = str(pvar)+'_0000'+start.strftime('%Y%m%d%H%M')+'.png'
filesave = os.path.join(directory,name)
plt.savefig(filesave)
print 'processed %s' % start
def gen_dates4Now(past,url, box, vname, pvar):
for result in perdelta(dt.datetime.now() - dt.timedelta(days=past), dt.datetime.now(), dt.timedelta(hours=1)):
yield {"date":result,"url":url, "box":box, "vname":vname, "pvar":pvar}
def gen_dates(tstart,tend,url, box, vname, pvar, u, v, tres, savepath):
day = tend.toordinal() - tstart.toordinal()
for result in perdelta(tend - dt.timedelta(days=day), tend, dt.timedelta(hours=tres)):
yield {"date":result,"url":url, "box":box, "vname":vname, "pvar":pvar, "u":u, "v":v, 'savepath':savepath}
In [6]:
#url='http://geoport.whoi.edu/thredds/dodsC/fmrc/NCEP/ww3/cfsr/4m/best'
url='http://geoport.whoi.edu/thredds/dodsC/fmrc/NCEP/ww3/cfsr/10m/best'
box = None
#box = [-71.5, 39.5, -63.0, 46.0]
vname = 'Significant_height_of_combined_wind_waves_and_swell_surface'
u = 'u-component_of_wind_surface'
v = 'v-component_of_wind_surface'
pvar='time'
tres = 1
savepath = '/Users/epi/mp_video_unh'
tstart = dt.datetime(1991,10,24,12)
tend = dt.datetime(1991,11,4,12)
In [7]:
!mkdir '/Users/epi/mp_video_unh'
In [8]:
results = view.map(plotWave, list(gen_dates(tstart, tend, url, box, vname, pvar, u, v, tres, savepath)), ordered=False)
results
Out[8]:
In [9]:
for r in results:
print r
sys.stdout.flush()
In [10]:
results.display_outputs()
In [11]:
cd /Users/epi/mp_video_unh
In [12]:
os.system('cat *.png | ffmpeg -r 24 -y -f image2pipe -c:v png -i - -c:v libx264 -preset ultrafast -qp 0 -movflags +faststart -pix_fmt yuv420p time.mkv')
Out[12]:
In [13]:
from IPython.core.display import HTML
video = open("time.mkv", "rb").read()
video_encoded = video.encode("base64")
video_tag = '<video autoplay loop controls alt="test" src="data:video/x-m4v;base64,{0}">'.format(video_encoded)
HTML(data=video_tag)
Out[13]: