-
Notifications
You must be signed in to change notification settings - Fork 0
Alpine plots #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Alpine plots #107
Changes from all commits
6d8daef
400afaa
c579ad1
4e28aab
01c6642
6591194
2b61b26
d0adcfb
3df84f3
e76a6c5
e5a98af
a60058c
bf449e0
df27db4
6f6f0c6
89fdcd4
b44957d
ae4bd94
7ba9866
3f9a81b
754bb15
70b0235
0556e00
9dc9a64
21ccb4d
ee57aaf
c86884d
a8c54c1
64ff999
4efc91f
2614613
65689f4
744a842
a60adf3
f1b835f
0c6683d
17126e4
6e2c777
faa404e
037b0dd
cb6fb35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,5 @@ pytest-cov | |
| pytest-xdist | ||
| typer | ||
| tqdm | ||
| rpy2 | ||
| xarray[io] | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,193 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Annotated | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import numpy.typing as npt | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import oq_wrapper as oqw | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import pandas as pd | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import pygmt | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import shapely | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import typer | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import xarray as xr | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| from pygmt_helper import plotting | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from qcore import cli | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from visualisation import realisation, utils | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from workflow.realisations import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| DomainParameters, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Magnitudes, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Rakes, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RupturePropagationConfig, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| SourceConfig, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| app = typer.Typer() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def plot_diff( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| fig: pygmt.Figure, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dset: xr.Dataset, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| gmm_psa_value: pd.Series, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| period: float, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_max: float | None, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_min: float | None, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ticks: int, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| reverse: bool, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| intensity = dset["pSA"].sel(period=period, component="rotd50") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| pgv_value = intensity.to_series() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| diff = np.log(pgv_value) - gmm_psa_value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_min = cmap_min or diff.min() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_max = cmap_max or diff.max() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_limits = utils.range_for(cmap_min, cmap_max, ticks) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| latitude = intensity.latitude.to_series() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| longitude = intensity.longitude.to_series() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| df = pd.DataFrame({"lat": latitude, "lon": longitude, "value": diff}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| grid: xr.DataArray = plotting.create_grid( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| df, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "value", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| grid_spacing="1000e/1000e", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| region=tuple(fig.region), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| set_water_to_nan=True, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| plotting.plot_grid( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| fig, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| grid, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_limits, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ("red", "blue"), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| reverse_cmap=reverse, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| transparency=40, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| plot_contours=False, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def find_region(domain: DomainParameters) -> tuple[float, float, float, float]: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Find an appropriate domain,""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring for
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| nz_region = shapely.box(166.0, -48.0, 178.5, -34.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| region = shapely.union( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| utils.polygon_nztm_to_pygmt(domain.domain.polygon), nz_region | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| (min_x, min_y, max_x, max_y) = shapely.bounds(region) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return (min_x, max_x, min_y, max_y) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def generate_basemap(region: tuple[float, float, float, float]) -> pygmt.Figure: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| fig: pygmt.Figure = plotting.gen_region_fig( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| title=None, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| region=region, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| plot_kwargs=dict( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| plot_kwargs=["af", "xaf+Longitude", "yaf+Latitude"], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| water_color="white", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| topo_cmap_min=-900, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| topo_cmap_max=3100, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| plot_highways=False, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| config_options=dict( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| MAP_FRAME_TYPE="plain", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| FORMAT_GEO_MAP="ddd.xx", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| MAP_FRAME_PEN="thinner,black", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert isinstance(fig, pygmt.Figure) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return fig | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @cli.from_docstring(app) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def main( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| realisation_ffp: Annotated[Path, typer.Argument()], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dataset: Annotated[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Path, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| typer.Argument(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| period: Annotated[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| float, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| typer.Argument(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| output: Annotated[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Path, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| typer.Argument(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap: Annotated[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| str, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| typer.Option(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ] = "polar", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| reverse: Annotated[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| bool, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| typer.Option(is_flag=True), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ] = False, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_min: Annotated[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| float | None, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| typer.Option(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ] = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_max: Annotated[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| float | None, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| typer.Option(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ] = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ticks: Annotated[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| int, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| typer.Option(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ] = 10, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Compare simulation results to predictions from the NSHM2022 logic tree. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| realisation_ffp : Path | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Path to realisation. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dataset : Path | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Path to xarray intensity measure dataset. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| period : float | ||||||||||||||||||||||||||||||||||||||||||||||||||
| pSA period to compare against. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| output : Path | ||||||||||||||||||||||||||||||||||||||||||||||||||
| The path to write the figure out to. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_min : float | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Colourmap minimum | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_max : float | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Colourmap maximum | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ticks : int | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Number of ticks in discrete colourmap. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| output : Path | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Output path. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap : str | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Colourmap to plot log residuals. Should be divering. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| reverse : bool | ||||||||||||||||||||||||||||||||||||||||||||||||||
| If true, reverse the colourmap. Defaults to false. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+149
to
+160
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring for the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| dset = xr.open_dataset(dataset, engine="h5netcdf") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| domain = DomainParameters.read_from_realisation(realisation_ffp) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| source_config = SourceConfig.read_from_realisation(realisation_ffp) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| magnitudes = Magnitudes.read_from_realisation(realisation_ffp) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| rakes = Rakes.read_from_realisation(realisation_ffp) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| rupture_propagation_config = RupturePropagationConfig.read_from_realisation( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| realisation_ffp | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| region = find_region(domain) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| fig = generate_basemap(region) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| gmm_psa_value = utils.get_gmm_prediction( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dset, period, source_config, magnitudes, rakes, rupture_propagation_config | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| plot_diff( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| fig, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dset, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| gmm_psa_value, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| period, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_max, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cmap_min, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ticks, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| reverse, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| realisation.plot_domain(fig, domain, pen="1p,black,-") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| realisation.plot_sources(fig, source_config) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| fig.savefig(output) | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable
pgv_valueis assigned the value ofintensity, which ispSA. This naming is misleading as pSA (Pseudo-Spectral Acceleration) and PGV (Peak Ground Velocity) are distinct intensity measures. Please renamepgv_valuetopsa_valuefor clarity and correctness.