Skip to content

Instantly share code, notes, and snippets.

@epifanio
Created January 30, 2013 19:30
Show Gist options
  • Save epifanio/4675997 to your computer and use it in GitHub Desktop.
Save epifanio/4675997 to your computer and use it in GitHub Desktop.
video_parallel
{
"metadata": {
"name": "video_parallel"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "from IPython import parallel\n\nrc = parallel.Client()\nview = rc.load_balanced_view()",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "%%px\n\nimport sys\nimport os\nfrom time import sleep\nfrom multiprocessing import Pool,cpu_count\n\n\nfrom pylab import *\nimport netCDF4\nimport datetime as dt\n\nimport numpy.ma as ma\nfrom datetime import date, datetime, timedelta",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "def perdelta(start, end, delta):\n curr = start\n while curr < end:\n yield curr\n curr += delta\n \ndef plotWave(args):\n start = args[\"date\"]\n url = args[\"url\"]\n box = args[\"box\"]\n vname = args[\"vname\"]\n pvar = args[\"pvar\"]\n nc = netCDF4.Dataset(url)\n lat = nc.variables['lat'][:]\n lon = nc.variables['lon'][:]\n lon = lon - 360.0 \n time_var = nc.variables[str(pvar)]\n dtime = netCDF4.num2date(time_var[:],time_var.units)\n bi=(lon>=box[0])&(lon<=box[2])\n bj=(lat>=box[1])&(lat<=box[3])\n istart = netCDF4.date2index(start,time_var,select='nearest')\n var = nc.variables[vname][istart,bj,bi]\n hs = ma.masked_where(np.isnan(var),var)\n figure(figsize=(12,12)) \n plt.pcolormesh(lon[bi], lat[bj], hs, vmin=0, vmax=3)\n title('Wave Height in the Gulf of Maine %s' % start.strftime('%Y-%m-%d-%H-%M'))\n name = str(pvar)+'_0000'+start.strftime('%Y%m%d%H%M')+'.png'\n print 'processed %' % start\n plt.savefig(name)\n \n\n\ndef gen_dates(past,url, box, vname, pvar):\n #past = arg[0]\n for result in perdelta(datetime.now() - timedelta(days=past), datetime.now(), timedelta(hours=1)):\n yield {\"date\":result,\"url\":url, \"box\":box, \"vname\":vname, \"pvar\":pvar}",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "days=1\nurl='http://thredds.ucar.edu/thredds/dodsC/fmrc/NCEP/WW3/Coastal_US_East_Coast/NCEP-WW3-Coastal_US_East_Coast_best.ncd'\nbox = [-71.5,39.5,-63.,46.]\nvname = 'Significant_height_of_combined_wind_waves_and_swell'\npvar='time'",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "results = view.map(plotWave, list(gen_dates(days,url, box, vname, pvar)), ordered=False)\nresults",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "for r in results:\n print r\n sys.stdout.flush()",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "We can also see the stdout of each case, though in this case it isn't super interesting"
},
{
"cell_type": "code",
"collapsed": false,
"input": "results.display_outputs()",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "!jist -p Plot\\ video_parallel.ipynb",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment