source_modelling.scripts.plot_rakes
Plot a sample of rake values across a multi-segment rupture.
1"""Plot a sample of rake values across a multi-segment rupture.""" 2 3from pathlib import Path 4from typing import Annotated, Optional 5 6import numpy as np 7import typer 8 9from pygmt_helper import plotting 10from source_modelling import srf 11 12app = typer.Typer() 13 14 15@app.command(help="Plot a sample of rake values across a multi-segment rupture.") 16def plot_rakes( 17 srf_ffp: Annotated[ 18 Path, typer.Argument(help="Path to SRF file to plot.", exists=True) 19 ], 20 output_ffp: Annotated[ 21 Path, typer.Argument(help="Output plot image.", dir_okay=False) 22 ], 23 dpi: Annotated[ 24 float, typer.Option(help="Plot output DPI (higher is better).") 25 ] = 300, 26 title: Annotated[Optional[str], typer.Option(help="Plot title to use.")] = None, 27 sample_size: Annotated[ 28 int, typer.Option(help="Number of points to sample for rake.") 29 ] = 200, 30 vector_length: Annotated[ 31 float, typer.Option(help="Length of rake vectors (cm).") 32 ] = 0.2, 33 seed: Annotated[ 34 Optional[int], typer.Option(help="Random seed to sample rakes with") 35 ] = None, 36) -> None: 37 """Plot an SRF file and output a PNG file. 38 39 Parameters 40 ---------- 41 srf_ffp : Path 42 Path to the SRF file. 43 output_ffp : Path 44 Path of the output plot image. 45 dpi : float, default 300 46 Plot output DPI (higher is better). 47 title : Optional[str], default None 48 Plot title to use. 49 sample_size : int, default 200 50 Number of points to sample for rake. 51 vector_length : float, default 0.2cm 52 Length of rake vectors (cm). 53 """ 54 srf_data = srf.read_srf(srf_ffp) 55 region = ( 56 srf_data.points["lon"].min() - 0.5, 57 srf_data.points["lon"].max() + 0.5, 58 srf_data.points["lat"].min() - 0.25, 59 srf_data.points["lat"].max() + 0.25, 60 ) 61 62 fig = plotting.gen_region_fig(title, region=region, map_data=None) 63 i = 0 64 65 np.random.seed(seed) 66 vectors = srf_data.points[["lon", "lat", "rake"]].sample(sample_size) 67 vectors["rake"] = (vectors["rake"] + 90) % 360 68 vectors["length"] = vector_length 69 70 fig.plot( 71 data=vectors.values.tolist(), style="v0.1c+e+a30", pen="0.2p", fill="black" 72 ) 73 for _, segment in srf_data.header.iterrows(): 74 nstk = int(segment["nstk"]) 75 ndip = int(segment["ndip"]) 76 point_count = nstk * ndip 77 segment_points = srf_data.points.iloc[i : i + point_count] 78 corners = segment_points.iloc[[0, nstk - 1, -1, (ndip - 1) * nstk]] 79 fig.plot( 80 x=corners["lon"].iloc[list(range(len(corners))) + [0]].to_list(), 81 y=corners["lat"].iloc[list(range(len(corners))) + [0]].to_list(), 82 pen="0.5p,black,-", 83 ) 84 85 i += point_count 86 87 fig.savefig( 88 output_ffp, 89 dpi=dpi, 90 anti_alias=True, 91 ) 92 93 94if __name__ == "__main__": 95 app()
app =
<typer.main.Typer object>
@app.command(help='Plot a sample of rake values across a multi-segment rupture.')
def
plot_rakes( srf_ffp: Annotated[pathlib.Path, <typer.models.ArgumentInfo object>], output_ffp: Annotated[pathlib.Path, <typer.models.ArgumentInfo object>], dpi: Annotated[float, <typer.models.OptionInfo object>] = 300, title: Annotated[Optional[str], <typer.models.OptionInfo object>] = None, sample_size: Annotated[int, <typer.models.OptionInfo object>] = 200, vector_length: Annotated[float, <typer.models.OptionInfo object>] = 0.2, seed: Annotated[Optional[int], <typer.models.OptionInfo object>] = None) -> None:
16@app.command(help="Plot a sample of rake values across a multi-segment rupture.") 17def plot_rakes( 18 srf_ffp: Annotated[ 19 Path, typer.Argument(help="Path to SRF file to plot.", exists=True) 20 ], 21 output_ffp: Annotated[ 22 Path, typer.Argument(help="Output plot image.", dir_okay=False) 23 ], 24 dpi: Annotated[ 25 float, typer.Option(help="Plot output DPI (higher is better).") 26 ] = 300, 27 title: Annotated[Optional[str], typer.Option(help="Plot title to use.")] = None, 28 sample_size: Annotated[ 29 int, typer.Option(help="Number of points to sample for rake.") 30 ] = 200, 31 vector_length: Annotated[ 32 float, typer.Option(help="Length of rake vectors (cm).") 33 ] = 0.2, 34 seed: Annotated[ 35 Optional[int], typer.Option(help="Random seed to sample rakes with") 36 ] = None, 37) -> None: 38 """Plot an SRF file and output a PNG file. 39 40 Parameters 41 ---------- 42 srf_ffp : Path 43 Path to the SRF file. 44 output_ffp : Path 45 Path of the output plot image. 46 dpi : float, default 300 47 Plot output DPI (higher is better). 48 title : Optional[str], default None 49 Plot title to use. 50 sample_size : int, default 200 51 Number of points to sample for rake. 52 vector_length : float, default 0.2cm 53 Length of rake vectors (cm). 54 """ 55 srf_data = srf.read_srf(srf_ffp) 56 region = ( 57 srf_data.points["lon"].min() - 0.5, 58 srf_data.points["lon"].max() + 0.5, 59 srf_data.points["lat"].min() - 0.25, 60 srf_data.points["lat"].max() + 0.25, 61 ) 62 63 fig = plotting.gen_region_fig(title, region=region, map_data=None) 64 i = 0 65 66 np.random.seed(seed) 67 vectors = srf_data.points[["lon", "lat", "rake"]].sample(sample_size) 68 vectors["rake"] = (vectors["rake"] + 90) % 360 69 vectors["length"] = vector_length 70 71 fig.plot( 72 data=vectors.values.tolist(), style="v0.1c+e+a30", pen="0.2p", fill="black" 73 ) 74 for _, segment in srf_data.header.iterrows(): 75 nstk = int(segment["nstk"]) 76 ndip = int(segment["ndip"]) 77 point_count = nstk * ndip 78 segment_points = srf_data.points.iloc[i : i + point_count] 79 corners = segment_points.iloc[[0, nstk - 1, -1, (ndip - 1) * nstk]] 80 fig.plot( 81 x=corners["lon"].iloc[list(range(len(corners))) + [0]].to_list(), 82 y=corners["lat"].iloc[list(range(len(corners))) + [0]].to_list(), 83 pen="0.5p,black,-", 84 ) 85 86 i += point_count 87 88 fig.savefig( 89 output_ffp, 90 dpi=dpi, 91 anti_alias=True, 92 )
Plot an SRF file and output a PNG file.
Parameters
- srf_ffp (Path): Path to the SRF file.
- output_ffp (Path): Path of the output plot image.
- dpi (float, default 300): Plot output DPI (higher is better).
- title (Optional[str], default None): Plot title to use.
- sample_size (int, default 200): Number of points to sample for rake.
- vector_length (float, default 0.2cm): Length of rake vectors (cm).