Skip to content

Instantly share code, notes, and snippets.

@rsignell-usgs
Created March 27, 2018 17:21
Show Gist options
  • Save rsignell-usgs/d1c2590a2e8d95d61b6d07cb0d06503c to your computer and use it in GitHub Desktop.
Save rsignell-usgs/d1c2590a2e8d95d61b6d07cb0d06503c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ADCIRC TriMesh data with Datashader\n",
"\n",
"[Datashader](http://datashader.org) support for irregular triangular meshes allows large ADCIRC datasets to be rendered onscreen efficiently. This notebook shows an example of rendering the depth or M2 Amplitude from the [EC2015 tidal database](https://doi.org/10.3390/jmse4040072). Make possible by the [EarthSim Project](https://pyviz.github.io/EarthSim/).\n",
"\n",
"After the data loads (after ~20-30 seconds), click one of the zoom tools to see performant rendering on the fly...."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-03-27T17:15:54.305867Z",
"start_time": "2018-03-27T17:15:46.057395Z"
}
},
"outputs": [],
"source": [
"import datashader as ds\n",
"import pandas as pd\n",
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews.operation.datashader import datashade, rasterize\n",
"import geoviews as gv\n",
"import palettable\n",
"import ipywidgets as ipyw\n",
"from gridgeo.ugrid import ugrid\n",
"import netCDF4\n",
"hv.extension(\"bokeh\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-03-27T17:15:58.500107Z",
"start_time": "2018-03-27T17:15:54.308867Z"
}
},
"outputs": [],
"source": [
"# EC2015 data in netCDF form, accessed via OPeNDAP. \n",
"# Here we use a UGRID-ized version of http://tds.renci.org:8080/thredds/dodsC/DataLayers/Tides/ec2015_tidaldb/f53.nc.html\n",
"url='http://gamone.whoi.edu/thredds/dodsC/usgs/vault0/models/tides/ec2015/f53.ncml'\n",
"nc = netCDF4.Dataset(url)\n",
"# Use gridgeo to get mesh from UGRID compliant dataset\n",
"u = ugrid(nc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-03-27T17:15:58.508107Z",
"start_time": "2018-03-27T17:15:58.503107Z"
}
},
"outputs": [],
"source": [
"vars = ['Depth','M2 Amp','O1 Amp']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-03-27T17:15:58.535109Z",
"start_time": "2018-03-27T17:15:58.511107Z"
}
},
"outputs": [],
"source": [
"dpdown = ipyw.Dropdown(options=vars, value=vars[0])\n",
"display(dpdown)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-03-27T17:15:58.571111Z",
"start_time": "2018-03-27T17:15:58.540109Z"
}
},
"outputs": [],
"source": [
"def update_dpdown(change):\n",
" var = change['new']\n",
" if var is 'M2 Amp':\n",
" z = nc['Amp'][0,:] # M2 amplitude\n",
" elif var is 'O1 Amp':\n",
" z = nc['Amp'][3,:] # O1 amplitude\n",
" else:\n",
" z = -nc['depth'][:]\n",
" \n",
" v = np.vstack((u['nodes']['x'], u['nodes']['y'], z)).T\n",
"\n",
" verts = pd.DataFrame(v, columns=['x','y','z'])\n",
" tris = pd.DataFrame(u['faces'].astype('int'), columns=['v0','v1','v2'])\n",
" %opts Image [colorbar=True clipping_colors={'NaN': (0, 0, 0, 0)}] (cmap=palettable.cubehelix.perceptual_rainbow_16.mpl_colormap)\n",
" %opts WMTS [width=700 height=400]\n",
" tiles = gv.WMTS('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg')\n",
" points = gv.operation.project_points(gv.Points(verts, vdims=['z']))\n",
" tiles * rasterize(hv.TriMesh((tris, points)), aggregator=ds.mean('z'), precompute=True ) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-03-27T17:15:58.583111Z",
"start_time": "2018-03-27T17:15:58.575111Z"
}
},
"outputs": [],
"source": [
"dpdown.observe(update_dpdown, names=['value'])"
]
}
],
"metadata": {
"extensions": {
"jupyter_dashboards": {
"activeView": "report_default",
"version": 1,
"views": {
"grid_default": {
"cellMargin": 10,
"defaultCellHeight": 20,
"maxColumns": 12,
"name": "grid",
"type": "grid"
},
"report_default": {
"name": "report",
"type": "report"
}
}
}
},
"gist": {
"data": {
"description": "datashader/examples/topics/tide_trimesh_experiments",
"public": true
},
"id": ""
},
"kernelspec": {
"display_name": "Python [conda env:ds]",
"language": "python",
"name": "conda-env-ds-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment