Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ def assert_images_match(
Compares two images using diffimg and asserts the difference is within tolerance.
Provides informative assertion messages.
"""
assert (
generated_path.exists()
), f"Generated image file does not exist: {generated_path}"
assert (
expected_path.exists()
), f"Expected image file does not exist: {expected_path}"
assert generated_path.exists(), (
f"Generated image file does not exist: {generated_path}"
)
assert expected_path.exists(), (
f"Expected image file does not exist: {expected_path}"
)

diff_ratio = diffimg.diff(expected_path, generated_path)

Expand Down
8 changes: 5 additions & 3 deletions visualisation/sources/plot_rise.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pygmt_helper import plotting
from qcore import cli
from source_modelling import srf
from visualisation import utils

app = typer.Typer()

Expand Down Expand Up @@ -53,12 +54,13 @@ def plot_rise(
fig = plotting.gen_region_fig(
title, projection=f"M{width}c", region=region, map_data=None
)

dx = srf_data.header.iloc[0]["len"] / srf_data.header.iloc[0]["nstk"]
grid_scale = min(utils.grid_scale_for_region(region), dx * 1000)
for i, segment_points in enumerate(srf_data.segments):
cur_grid = plotting.create_grid(
segment_points,
"trise",
grid_spacing="5e/5e",
grid_spacing=f"{grid_scale}e/{grid_scale}e",
region=(
segment_points["lon"].min(),
segment_points["lon"].max(),
Expand All @@ -82,7 +84,7 @@ def plot_rise(
time_grid = plotting.create_grid(
segment_points,
"tinit",
grid_spacing="5e/5e",
grid_spacing=f"{grid_scale}e/{grid_scale}e",
region=(
segment_points["lon"].min(),
segment_points["lon"].max(),
Expand Down
5 changes: 3 additions & 2 deletions visualisation/sources/plot_srf.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def show_slip(
)
dx = srf_data.header.iloc[0]["len"] / srf_data.header.iloc[0]["nstk"]
subtitle = f"Slip: {slip_stats}, dx = {dx:.2f} km, {len(srf_data.header)} planes"
grid_scale = min(utils.grid_scale_for_region(region), dx * 1000)
for (_, segment), segment_points in zip(
srf_data.header.iterrows(), srf_data.segments
):
Expand All @@ -129,7 +130,7 @@ def show_slip(
cur_grid = plotting.create_grid(
segment_points,
"slip",
grid_spacing="5e/5e",
grid_spacing=f"{grid_scale}e/{grid_scale}e",
region=(
segment_points["lon"].min(),
segment_points["lon"].max(),
Expand All @@ -156,7 +157,7 @@ def show_slip(
time_grid = plotting.create_grid(
segment_points,
"tinit",
grid_spacing="5e/5e",
grid_spacing=f"{grid_scale}e/{grid_scale}e",
region=(
segment_points["lon"].min(),
segment_points["lon"].max(),
Expand Down
3 changes: 2 additions & 1 deletion visualisation/sources/plot_srf_distribution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
"""Plot SRF distributions."""

from pathlib import Path
from typing import Annotated, Optional

Expand Down Expand Up @@ -46,7 +47,7 @@ def plot_srf_distribution(
ax.set_xlabel("Slip (cm)")
ax.set_title(
title
or f'Slip PDF for {srf_ffp.stem} ({utils.format_description(srf_data.points["slip"], compact=True)})'
or f"Slip PDF for {srf_ffp.stem} ({utils.format_description(srf_data.points['slip'], compact=True)})"
)

plt.savefig(plot_png, dpi=dpi)
2 changes: 1 addition & 1 deletion visualisation/sources/plot_stoch.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def plot_stoch(
ax.text(
k * dx + dx / 2,
(j * dy + dy / 2),
f"{int(slip[j, k])}",
f"{int(slip[j, k])}",
ha="center",
va="center",
color=colour,
Expand Down
24 changes: 24 additions & 0 deletions visualisation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,27 @@ def bounding_region_for(
min_latitude - latitude_pad,
max_latitude + latitude_pad,
)


def grid_scale_for_region(region: tuple[float, float, float, float]) -> int:
"""Compute a suitable grid scale for a pygmt region.

Parameters
----------
region : tuple[float, float, float, float]
The pygmt region you will plot a grid in.

Returns
-------
int
A value (in metres) represent for `plotting.create_grid` to
use when plotting the lat-lon grid. Scale is based on the
maximum extent in the lat or lon direction for the figure in
kilometres. Works out that 10km = 25m, 100km = 250m, with a
minimum resolution of 5m.
"""
min_lon, max_lon, min_lat, max_lat = region
lat_km = (max_lat - min_lat) * 111
lon_km = (max_lon - min_lon) * 111 * np.cos(np.radians((min_lat + max_lat) / 2))
maximum_extent = max(lat_km, lon_km)
return int(round(max(5, 2.5 * maximum_extent)))