{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pyvista as pv\n", "import numpy as np\n", "import xarray as xr\n", "\n", "from ipygany import PolyMesh, Scene, IsoColor, WarpByScalar, ColorBar, colormaps\n", "from ipywidgets import VBox, FloatSlider, FileUpload, Dropdown, jslink, FloatRangeSlider, AppLayout\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (lat: 180, lon: 360)\n",
       "Coordinates:\n",
       "  * lon      (lon) float64 0.0 1.0 2.0 3.0 4.0 ... 355.0 356.0 357.0 358.0 359.0\n",
       "  * lat      (lat) float64 -89.5 -88.5 -87.5 -86.5 -85.5 ... 86.5 87.5 88.5 89.5\n",
       "Data variables:\n",
       "    TOPO     (lat, lon) float32 544.3 529.1 505.7 ... -1.231e+03 -1.254e+03\n",
       "Attributes:\n",
       "    CDI:          Climate Data Interface version 1.9.9 (https://mpimet.mpg.de...\n",
       "    Conventions:  COARDS, CF-1.5\n",
       "    GMT_version:  5.4.5 [64-bit]\n",
       "    history:      Thu Feb 04 16:14:31 2021: cdo remapbil,r360x180 paleobathy-...\n",
       "    NCO:          netCDF Operators version 4.9.7 (Homepage = http://nco.sf.ne...\n",
       "    CDO:          Climate Data Operators version 1.9.9 (https://mpimet.mpg.de...
" ], "text/plain": [ "\n", "Dimensions: (lat: 180, lon: 360)\n", "Coordinates:\n", " * lon (lon) float64 0.0 1.0 2.0 3.0 4.0 ... 355.0 356.0 357.0 358.0 359.0\n", " * lat (lat) float64 -89.5 -88.5 -87.5 -86.5 -85.5 ... 86.5 87.5 88.5 89.5\n", "Data variables:\n", " TOPO (lat, lon) float32 ...\n", "Attributes:\n", " CDI: Climate Data Interface version 1.9.9 (https://mpimet.mpg.de...\n", " Conventions: COARDS, CF-1.5\n", " GMT_version: 5.4.5 [64-bit]\n", " history: Thu Feb 04 16:14:31 2021: cdo remapbil,r360x180 paleobathy-...\n", " NCO: netCDF Operators version 4.9.7 (Homepage = http://nco.sf.ne...\n", " CDO: Climate Data Operators version 1.9.9 (https://mpimet.mpg.de..." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = xr.open_dataset('/home/climwork/psepul/BC_CM5A2/DATASET_Straume_et_al_2020/Paleobathymetry-paleotopography/paleobathy-topo_55.00Ma_Straume_et_al_1x1.nc')\n", "ds" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "xx, yy, zz = np.meshgrid(np.radians(ds['lon']), \n", " np.radians(ds['lat']), \n", " [0])\n", "\n", "# Transform to spherical coordinates\n", "radius = 6371.0e6\n", "x = radius * np.cos(yy) * np.cos(xx)\n", "y = radius * np.cos(yy) * np.sin(xx)\n", "z = radius * np.sin(yy)\n", "\n", "grid = pv.StructuredGrid(x, y, z)\n", "\n", "# Add data to mesh\n", "for var in ds.data_vars:\n", " grid[var] = np.array(ds[var]).ravel(order='F')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert pyvista mesh to ipygany mesh" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Turn the PyVista mesh into a PolyMesh\n", "mesh = PolyMesh.from_vtk(grid)\n", "\n", "# Color the mesh\n", "#colored_mesh = IsoColor(mesh, min=ds.TOPO.min(), max=ds.TOPO.max())\n", "### Get min/max values of the variable\n", "topo_min = ds.TOPO.min()\n", "topo_max = ds.TOPO.max()\n", "\n", "# Colorize by height\n", "colored_mesh = IsoColor(mesh, min=topo_min, max=topo_max)\n", "\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8e72274319a742e5bb73293a91a2c8fc", "version_major": 2, "version_minor": 0 }, "text/plain": [ "AppLayout(children=(Scene(children=[WarpByScalar(factor=0.0, input='TOPO', parent=IsoColor(input='TOPO', max=6…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "# setup warping\n", "warped_mesh = WarpByScalar(colored_mesh, input='TOPO', factor=0)\n", "\n", "# Create a slider that will dynamically change the boundaries of the colormap\n", "colormap_slider_range = FloatRangeSlider(value=[topo_min, topo_max], min=topo_min, max=topo_max, step= 100.)\n", "jslink((colored_mesh, 'range'), (colormap_slider_range, 'value'))\n", "\n", "# Link a slider to the warp value\n", "warp_slider = FloatSlider(min=0., max=10., value=0)\n", "\n", "def on_slider_change(change):\n", " warped_mesh.factor = change['new'] * -1e4\n", "\n", "warp_slider.observe(on_slider_change, 'value')\n", "\n", "# Create a colorbar widget\n", "colorbar = ColorBar(colored_mesh)\n", "\n", "# Colormap choice widget\n", "colormap = Dropdown(\n", " options=colormaps,\n", " description='colormap:'\n", ")\n", "\n", "jslink((colored_mesh, 'colormap'), (colormap, 'index'))\n", "\n", "\n", "AppLayout(\n", " left_sidebar=Scene([warped_mesh]),\n", " right_sidebar=VBox((warp_slider,colormap_slider_range, colormap, colorbar)),\n", " pane_widths=[2, 0, 1]\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.6" } }, "nbformat": 4, "nbformat_minor": 2 }