import pyvista as pv
import numpy as np
import xarray as xr
from ipygany import PolyMesh, Scene, IsoColor, WarpByScalar, ColorBar, colormaps
from ipywidgets import VBox, FloatSlider, FileUpload, Dropdown, jslink, FloatRangeSlider, AppLayout
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')
ds
<xarray.Dataset> Dimensions: (lat: 180, lon: 360) Coordinates: * lon (lon) float64 0.0 1.0 2.0 3.0 4.0 ... 355.0 356.0 357.0 358.0 359.0 * lat (lat) float64 -89.5 -88.5 -87.5 -86.5 -85.5 ... 86.5 87.5 88.5 89.5 Data variables: TOPO (lat, lon) float32 544.3 529.1 505.7 ... -1.231e+03 -1.254e+03 Attributes: CDI: Climate Data Interface version 1.9.9 (https://mpimet.mpg.de... Conventions: COARDS, CF-1.5 GMT_version: 5.4.5 [64-bit] history: Thu Feb 04 16:14:31 2021: cdo remapbil,r360x180 paleobathy-... NCO: netCDF Operators version 4.9.7 (Homepage = http://nco.sf.ne... CDO: Climate Data Operators version 1.9.9 (https://mpimet.mpg.de...
xarray.Dataset
- lat: 180
- lon: 360
- lon(lon)float640.0 1.0 2.0 ... 357.0 358.0 359.0
- standard_name :
- longitude
- long_name :
- longitude
- units :
- degrees_east
- axis :
- X
array([ 0., 1., 2., ..., 357., 358., 359.])
- lat(lat)float64-89.5 -88.5 -87.5 ... 88.5 89.5
- standard_name :
- latitude
- long_name :
- latitude
- units :
- degrees_north
- axis :
- Y
array([-89.5, -88.5, -87.5, -86.5, -85.5, -84.5, -83.5, -82.5, -81.5, -80.5, -79.5, -78.5, -77.5, -76.5, -75.5, -74.5, -73.5, -72.5, -71.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -64.5, -63.5, -62.5, -61.5, -60.5, -59.5, -58.5, -57.5, -56.5, -55.5, -54.5, -53.5, -52.5, -51.5, -50.5, -49.5, -48.5, -47.5, -46.5, -45.5, -44.5, -43.5, -42.5, -41.5, -40.5, -39.5, -38.5, -37.5, -36.5, -35.5, -34.5, -33.5, -32.5, -31.5, -30.5, -29.5, -28.5, -27.5, -26.5, -25.5, -24.5, -23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5, 24.5, 25.5, 26.5, 27.5, 28.5, 29.5, 30.5, 31.5, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.5, 44.5, 45.5, 46.5, 47.5, 48.5, 49.5, 50.5, 51.5, 52.5, 53.5, 54.5, 55.5, 56.5, 57.5, 58.5, 59.5, 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5, 79.5, 80.5, 81.5, 82.5, 83.5, 84.5, 85.5, 86.5, 87.5, 88.5, 89.5])
- TOPO(lat, lon)float32...
- long_name :
- z
- actual_range :
- [-6200. 6690.57519531]
array([[ 544.33844, 529.1336 , 505.7425 , ..., 529.88007, 544.7237 , 548.46295], [ 646.48083, 628.5785 , 610.77954, ..., 670.039 , 662.1315 , 654.1082 ], [ 880.4769 , 906.01886, 911.0824 , ..., 885.28925, 891.17224, 889.92816], ..., [-4719.797 , -4718.3755 , -4717.9883 , ..., -4719.708 , -4719.596 , -4719.3984 ], [-4180.9995 , -4132.8135 , -3559.212 , ..., -4186.6777 , -4168.1567 , -4168.214 ], [-1267.2255 , -1256.2627 , -1238.0942 , ..., -1215.5485 , -1230.6118 , -1253.6396 ]], dtype=float32)
- CDI :
- Climate Data Interface version 1.9.9 (https://mpimet.mpg.de/cdi)
- Conventions :
- COARDS, CF-1.5
- GMT_version :
- 5.4.5 [64-bit]
- history :
- Thu Feb 04 16:14:31 2021: cdo remapbil,r360x180 paleobathy-topo_55.00Ma_Straume_et_al.nc paleobathy-topo_55.00Ma_Straume_et_al_1x1.nc Thu Feb 4 16:14:05 2021: ncrename -v z,TOPO paleobathy-topo_55.00Ma_Straume_et_al.nc grdsample -R-180/180/-90/90 -I0.1 ../output/bathy_may19_55.00.nc -fg -V -Gpaleobathy-topo_55.00Ma_Straume_et_al.nc
- NCO :
- netCDF Operators version 4.9.7 (Homepage = http://nco.sf.net, Code = http://github.com/nco/nco)
- CDO :
- Climate Data Operators version 1.9.9 (https://mpimet.mpg.de/cdo)
xx, yy, zz = np.meshgrid(np.radians(ds['lon']),
np.radians(ds['lat']),
[0])
# Transform to spherical coordinates
radius = 6371.0e6
x = radius * np.cos(yy) * np.cos(xx)
y = radius * np.cos(yy) * np.sin(xx)
z = radius * np.sin(yy)
grid = pv.StructuredGrid(x, y, z)
# Add data to mesh
for var in ds.data_vars:
grid[var] = np.array(ds[var]).ravel(order='F')
Convert pyvista mesh to ipygany mesh¶
# Turn the PyVista mesh into a PolyMesh
mesh = PolyMesh.from_vtk(grid)
# Color the mesh
#colored_mesh = IsoColor(mesh, min=ds.TOPO.min(), max=ds.TOPO.max())
### Get min/max values of the variable
topo_min = ds.TOPO.min()
topo_max = ds.TOPO.max()
# Colorize by height
colored_mesh = IsoColor(mesh, min=topo_min, max=topo_max)
# setup warping
warped_mesh = WarpByScalar(colored_mesh, input='TOPO', factor=0)
# Create a slider that will dynamically change the boundaries of the colormap
colormap_slider_range = FloatRangeSlider(value=[topo_min, topo_max], min=topo_min, max=topo_max, step= 100.)
jslink((colored_mesh, 'range'), (colormap_slider_range, 'value'))
# Link a slider to the warp value
warp_slider = FloatSlider(min=0., max=10., value=0)
def on_slider_change(change):
warped_mesh.factor = change['new'] * -1e4
warp_slider.observe(on_slider_change, 'value')
# Create a colorbar widget
colorbar = ColorBar(colored_mesh)
# Colormap choice widget
colormap = Dropdown(
options=colormaps,
description='colormap:'
)
jslink((colored_mesh, 'colormap'), (colormap, 'index'))
AppLayout(
left_sidebar=Scene([warped_mesh]),
right_sidebar=VBox((warp_slider,colormap_slider_range, colormap, colorbar)),
pane_widths=[2, 0, 1]
)