| 1 |
""" |
| 2 |
.. _spherical_example: |
| 3 |
|
| 4 |
Plot data in spherical coordinates |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Generate and visualize meshes from data in longitude-latitude coordinates |
| 8 |
using :func:`~pyvista.grid_from_sph_coords`. |
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pyvista as pv |
| 13 |
|
| 14 |
|
| 15 |
def _cell_bounds(points, bound_position=0.5): |
| 16 |
""" |
| 17 |
Calculate coordinate cell boundaries. |
| 18 |
|
| 19 |
Parameters |
| 20 |
---------- |
| 21 |
points: numpy.ndarray |
| 22 |
One-dimensional array of uniformly spaced values of shape (M,). |
| 23 |
|
| 24 |
bound_position: bool, optional |
| 25 |
The desired position of the bounds relative to the position |
| 26 |
of the points. |
| 27 |
|
| 28 |
Returns |
| 29 |
------- |
| 30 |
bounds: numpy.ndarray |
| 31 |
Array of shape (M+1,) |
| 32 |
|
| 33 |
Examples |
| 34 |
-------- |
| 35 |
>>> a = np.arange(-1, 2.5, 0.5) |
| 36 |
>>> a |
| 37 |
array([-1. , -0.5, 0. , 0.5, 1. , 1.5, 2. ]) |
| 38 |
>>> _cell_bounds(a) |
| 39 |
array([-1.25, -0.75, -0.25, 0.25, 0.75, 1.25, 1.75, 2.25]) |
| 40 |
|
| 41 |
""" |
| 42 |
if points.ndim != 1: |
| 43 |
msg = 'Only 1D points are allowed.' |
| 44 |
raise ValueError(msg) |
| 45 |
diffs = np.diff(points) |
| 46 |
delta = diffs[0] * bound_position |
| 47 |
return np.concatenate([[points[0] - delta], points + delta]) |
| 48 |
|
| 49 |
|
| 50 |
# Seed random number generator for reproducible plots |
| 51 |
rng = np.random.default_rng(seed=0) |
| 52 |
|
| 53 |
# First, create some dummy data |
| 54 |
|
| 55 |
# Approximate radius of the Earth |
| 56 |
RADIUS = 6371.0 |
| 57 |
|
| 58 |
# Longitudes and latitudes |
| 59 |
x = np.arange(0, 360, 5) |
| 60 |
y = np.arange(-90, 91, 10) |
| 61 |
y_polar = 90.0 - y # grid_from_sph_coords() expects polar angle |
| 62 |
|
| 63 |
xx, yy = np.meshgrid(x, y) |
| 64 |
|
| 65 |
|
| 66 |
# x- and y-components of the wind vector |
| 67 |
u_vec = np.cos(np.radians(xx)) # zonal |
| 68 |
v_vec = np.sin(np.radians(yy)) # meridional |
| 69 |
|
| 70 |
# Scalar data |
| 71 |
scalar = u_vec**2 + v_vec**2 |
| 72 |
|
| 73 |
# Create arrays of grid cell boundaries, which have shape of (x.shape[0] + 1) |
| 74 |
xx_bounds = _cell_bounds(x) |
| 75 |
yy_bounds = _cell_bounds(y_polar) |
| 76 |
# Vertical levels |
| 77 |
# in this case a single level slightly above the surface of a sphere |
| 78 |
levels = [RADIUS * 1.01] |
| 79 |
|
| 80 |
# %% |
| 81 |
# Create a structured grid |
| 82 |
grid_scalar = pv.grid_from_sph_coords(xx_bounds, yy_bounds, levels) |
| 83 |
|
| 84 |
# And fill its cell arrays with the scalar data |
| 85 |
grid_scalar.cell_data['example'] = np.array(scalar).swapaxes(-2, -1).ravel('C') |
| 86 |
|
| 87 |
# Make a plot |
| 88 |
pl = pv.Plotter() |
| 89 |
pl.add_mesh(pv.Sphere(radius=RADIUS)) |
| 90 |
pl.add_mesh(grid_scalar, clim=[0.1, 2.0], opacity=0.5, cmap='plasma') |
| 91 |
pl.show() |
| 92 |
|
| 93 |
|
| 94 |
# %% |
| 95 |
# Visualize vectors in spherical coordinates |
| 96 |
# Vertical wind |
| 97 |
w_vec = rng.random(u_vec.shape) |
| 98 |
|
| 99 |
wind_level = [RADIUS * 1.2] |
| 100 |
|
| 101 |
# Sequence of axis indices for transpose() |
| 102 |
# (1, 0) for 2D arrays |
| 103 |
# (2, 1, 0) for 3D arrays |
| 104 |
inv_axes = [*range(u_vec.ndim)[::-1]] |
| 105 |
|
| 106 |
# Transform vectors to cartesian coordinates |
| 107 |
vectors = np.stack( |
| 108 |
[ |
| 109 |
i.transpose(inv_axes).swapaxes(-2, -1).ravel('C') |
| 110 |
for i in pv.transform_vectors_sph_to_cart( |
| 111 |
theta=x, |
| 112 |
phi=y_polar, |
| 113 |
r=wind_level, |
| 114 |
u=u_vec.transpose(inv_axes), |
| 115 |
v=-v_vec.transpose( |
| 116 |
inv_axes |
| 117 |
), # Minus sign since y-vector in polar coords is required |
| 118 |
w=w_vec.transpose(inv_axes), |
| 119 |
) |
| 120 |
], |
| 121 |
axis=1, |
| 122 |
) |
| 123 |
|
| 124 |
# Scale vectors to make them visible |
| 125 |
vectors *= RADIUS * 0.1 |
| 126 |
|
| 127 |
# Create a grid for the vectors |
| 128 |
grid_winds = pv.grid_from_sph_coords(x, y_polar, wind_level) |
| 129 |
|
| 130 |
# Add vectors to the grid |
| 131 |
grid_winds.point_data['example'] = vectors |
| 132 |
|
| 133 |
# Show the result |
| 134 |
pl = pv.Plotter() |
| 135 |
pl.add_mesh(pv.Sphere(radius=RADIUS)) |
| 136 |
pl.add_mesh(grid_winds.glyph(orient='example', scale='example', tolerance=0.005)) |
| 137 |
pl.show() |
| 138 |
|
| 139 |
|
| 140 |
# %% |
| 141 |
# Isurfaces of 3D data in spherical coordinates |
| 142 |
|
| 143 |
# Number of vertical levels |
| 144 |
nlev = 10 |
| 145 |
|
| 146 |
# Dummy 3D scalar data |
| 147 |
scalar_3d = ( |
| 148 |
scalar.repeat(nlev).reshape((*scalar.shape, nlev)) |
| 149 |
* np.arange(nlev)[np.newaxis, np.newaxis, :] |
| 150 |
).transpose(2, 0, 1) |
| 151 |
|
| 152 |
|
| 153 |
z_scale = 10 |
| 154 |
z_offset = RADIUS * 1.1 |
| 155 |
|
| 156 |
# Now it's not a single level but an array of levels |
| 157 |
levels = z_scale * (np.arange(scalar_3d.shape[0] + 1)) ** 2 + z_offset |
| 158 |
|
| 159 |
# Create a structured grid by transforming coordinates |
| 160 |
grid_scalar_3d = pv.grid_from_sph_coords(xx_bounds, yy_bounds, levels) |
| 161 |
|
| 162 |
# Add data to the grid |
| 163 |
grid_scalar_3d.cell_data['example'] = np.array(scalar_3d).swapaxes(-2, -1).ravel('C') |
| 164 |
|
| 165 |
# Create a set of isosurfaces |
| 166 |
surfaces = grid_scalar_3d.cell_data_to_point_data().contour(isosurfaces=[1, 5, 10, 15]) |
| 167 |
|
| 168 |
# Show the result |
| 169 |
pl = pv.Plotter() |
| 170 |
pl.add_mesh(pv.Sphere(radius=RADIUS)) |
| 171 |
pl.add_mesh(surfaces) |
| 172 |
pl.show() |
| 173 |
# %% |
| 174 |
# .. tags:: plot |