velocity_modelling.scripts.view_velocity_model
View a velocity model in 3-dimensions.
1"""View a velocity model in 3-dimensions.""" 2 3from enum import StrEnum 4from pathlib import Path 5from typing import Annotated 6 7import numpy as np 8import pyvista as pv 9import typer 10 11app = typer.Typer() 12 13 14class Slices(StrEnum): 15 "Slicing control options for the velocity model." 16 17 XY = "xy" 18 XZ = "xz" 19 YZ = "yz" 20 21 22@app.command(help="View a velocity model in 3D.") 23def view_velocity_model( 24 velocity_model_ffp: Annotated[ 25 Path, typer.Argument(help="The velocity model file to plot.") 26 ], 27 nx: Annotated[int, typer.Argument(help="The number of x-slices")], 28 ny: Annotated[int, typer.Argument(help="The number of y-slices")], 29 nz: Annotated[int, typer.Argument(help="The number of z-slices")], 30 slices: Annotated[ 31 Slices, typer.Option(help="Add slice controls for the given axes.") 32 ] = Slices.XY, 33) -> None: 34 """View a velocity model in 3 dimensions. 35 36 Parameters 37 ---------- 38 velocity_model_ffp : Path 39 Path to the velocity model file to view. 40 nx : int 41 The number of x-slices. 42 ny : int 43 The number of y-slices. 44 nz : int 45 The number of z-slices. 46 slices : Slices 47 The slicing controls to add, defaults to x and y axis slicing. 48 """ 49 data_vs = np.fromfile(velocity_model_ffp, "<f4") 50 51 # reshape the data 52 data_vs = data_vs.reshape([ny, nz, nx]) 53 data_vs = np.swapaxes(data_vs, 0, 2) 54 data_vs = np.swapaxes(data_vs, 1, 2) 55 data_vs = np.flip(data_vs, 2) # reverse z axis 56 57 vol = pv.ImageData() 58 vol.dimensions = tuple(np.array(data_vs.shape) + 1) 59 vol.cell_data["values"] = data_vs.flatten(order="F") 60 61 p = pv.Plotter() 62 63 p.add_mesh_clip_plane(vol, assign_to_axis=slices[0]) 64 p.add_mesh_clip_plane(vol, assign_to_axis=slices[1], invert=(slices[1] == "z")) 65 66 p.show()
app =
<typer.main.Typer object>
class
Slices(enum.StrEnum):
15class Slices(StrEnum): 16 "Slicing control options for the velocity model." 17 18 XY = "xy" 19 XZ = "xz" 20 YZ = "yz"
Slicing control options for the velocity model.
XY =
<Slices.XY: 'xy'>
XZ =
<Slices.XZ: 'xz'>
YZ =
<Slices.YZ: 'yz'>
@app.command(help='View a velocity model in 3D.')
def
view_velocity_model( velocity_model_ffp: Annotated[pathlib.Path, <typer.models.ArgumentInfo object>], nx: Annotated[int, <typer.models.ArgumentInfo object>], ny: Annotated[int, <typer.models.ArgumentInfo object>], nz: Annotated[int, <typer.models.ArgumentInfo object>], slices: Annotated[Slices, <typer.models.OptionInfo object>] = <Slices.XY: 'xy'>) -> None:
23@app.command(help="View a velocity model in 3D.") 24def view_velocity_model( 25 velocity_model_ffp: Annotated[ 26 Path, typer.Argument(help="The velocity model file to plot.") 27 ], 28 nx: Annotated[int, typer.Argument(help="The number of x-slices")], 29 ny: Annotated[int, typer.Argument(help="The number of y-slices")], 30 nz: Annotated[int, typer.Argument(help="The number of z-slices")], 31 slices: Annotated[ 32 Slices, typer.Option(help="Add slice controls for the given axes.") 33 ] = Slices.XY, 34) -> None: 35 """View a velocity model in 3 dimensions. 36 37 Parameters 38 ---------- 39 velocity_model_ffp : Path 40 Path to the velocity model file to view. 41 nx : int 42 The number of x-slices. 43 ny : int 44 The number of y-slices. 45 nz : int 46 The number of z-slices. 47 slices : Slices 48 The slicing controls to add, defaults to x and y axis slicing. 49 """ 50 data_vs = np.fromfile(velocity_model_ffp, "<f4") 51 52 # reshape the data 53 data_vs = data_vs.reshape([ny, nz, nx]) 54 data_vs = np.swapaxes(data_vs, 0, 2) 55 data_vs = np.swapaxes(data_vs, 1, 2) 56 data_vs = np.flip(data_vs, 2) # reverse z axis 57 58 vol = pv.ImageData() 59 vol.dimensions = tuple(np.array(data_vs.shape) + 1) 60 vol.cell_data["values"] = data_vs.flatten(order="F") 61 62 p = pv.Plotter() 63 64 p.add_mesh_clip_plane(vol, assign_to_axis=slices[0]) 65 p.add_mesh_clip_plane(vol, assign_to_axis=slices[1], invert=(slices[1] == "z")) 66 67 p.show()
View a velocity model in 3 dimensions.
Parameters
- velocity_model_ffp (Path): Path to the velocity model file to view.
- nx (int): The number of x-slices.
- ny (int): The number of y-slices.
- nz (int): The number of z-slices.
- slices (Slices): The slicing controls to add, defaults to x and y axis slicing.