diff --git a/tests/test_plots.py b/tests/test_plots.py index 1b98960..d4fcf39 100644 --- a/tests/test_plots.py +++ b/tests/test_plots.py @@ -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) diff --git a/visualisation/sources/plot_rise.py b/visualisation/sources/plot_rise.py index 0d98b27..b9153f4 100644 --- a/visualisation/sources/plot_rise.py +++ b/visualisation/sources/plot_rise.py @@ -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() @@ -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(), @@ -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(), diff --git a/visualisation/sources/plot_srf.py b/visualisation/sources/plot_srf.py index 3205c4a..54d75a8 100644 --- a/visualisation/sources/plot_srf.py +++ b/visualisation/sources/plot_srf.py @@ -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 ): @@ -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(), @@ -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(), diff --git a/visualisation/sources/plot_srf_distribution.py b/visualisation/sources/plot_srf_distribution.py index a82dd3a..ddedcd9 100644 --- a/visualisation/sources/plot_srf_distribution.py +++ b/visualisation/sources/plot_srf_distribution.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """Plot SRF distributions.""" + from pathlib import Path from typing import Annotated, Optional @@ -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) diff --git a/visualisation/sources/plot_stoch.py b/visualisation/sources/plot_stoch.py index 15fe88e..d305965 100644 --- a/visualisation/sources/plot_stoch.py +++ b/visualisation/sources/plot_stoch.py @@ -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, diff --git a/visualisation/utils.py b/visualisation/utils.py index 6eeb860..7a2694a 100644 --- a/visualisation/utils.py +++ b/visualisation/utils.py @@ -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)))