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
8 changes: 3 additions & 5 deletions pygmt_helper/examples/ds_sources.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from pathlib import Path

import pygmt
import pandas as pd
import numpy as np
import pandas as pd
import pygmt

from pygmt_helper import plotting


# Config
ds_ffp = Path(__file__).parent / "resources" / "NZ_DSmodel_2015.txt"
map_data_ffp = Path("path to qcore/qcore/data")
output_dir = Path("path to the output directory")


Expand All @@ -26,7 +24,7 @@
ds_df["lon"] = split_ids[:, 1]

# Plotting data
map_data = plotting.NZMapData.load(map_data_ffp) if map_data_ffp is not None else None
map_data = plotting.NZMapData.load()

if incl_rec_prob:
sum_df = (
Expand Down
15 changes: 4 additions & 11 deletions pygmt_helper/examples/event_stations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
inset_region = [172.60, 172.69, -43.545, -43.495]
# Output file name
output_ffp = Path("path to the output file")
# Path to the map data
# Is part of the qcore package, once installed it
# can be found under qcore/qcore/data
# Set to None for lower quality map, but much faster plotting time
map_data_ffp = None
# If true, then use New Zealand specific map data
use_map_data = True
# If true, then use the high resolution topography
# This will further increase plot time, and only has an
# effect if map_data_ffp is set
# effect if use_map_data is set
use_high_res_topo = True
# List of events to highlight
events_to_highlight = None
Expand Down Expand Up @@ -66,11 +63,7 @@


# Load map data
map_data = (
None
if map_data_ffp is None
else plotting.NZMapData.load(map_data_ffp, high_res_topo=use_high_res_topo)
)
map_data = plotting.NZMapData.load(high_res_topo=use_high_res_topo) if use_map_data else None

# Generate the figure
fig = plotting.gen_region_fig(
Expand Down
10 changes: 4 additions & 6 deletions pygmt_helper/examples/fault_traces.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from pathlib import Path

import pandas as pd
import numpy as np

import pandas as pd
from qcore import nhm
from pygmt_helper import plots
from pygmt_helper import plotting

from pygmt_helper import plots, plotting

# Config
nhm_ffp = Path("path to nhm file")
map_data_ffp = Path("path to qcore/qcore/data")
output_dir = Path("path to the output directory")

# If true, then for each CS fault
Expand All @@ -20,7 +18,7 @@
nhm_data = nhm.load_nhm(str(nhm_ffp))

# Load plotting data
map_data = plotting.NZMapData.load(map_data_ffp)
map_data = plotting.NZMapData.load()

# Load the source information
cybershake_df = pd.read_csv(
Expand Down
6 changes: 2 additions & 4 deletions pygmt_helper/examples/ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

from pathlib import Path

import pandas as pd
import numpy as np
import pandas as pd

from pygmt_helper import plotting

site_ffp = Path(__file__).parent / "resources"

# Config
# map_data_ffp = Path("path to qcore/qcore/data")
map_data_ffp = Path("/Users/claudy/dev/work/code/qcore/qcore/data")
site_locations = (
Path(__file__).parent
/ "resources"
Expand All @@ -30,7 +28,7 @@
)

# Load plotting data
map_data = plotting.NZMapData.load(map_data_ffp)
map_data = plotting.NZMapData.load()

# Add some fake data
df["ratio"] = np.random.uniform(-0.5, 0.5, df.shape[0])
Expand Down
5 changes: 2 additions & 3 deletions pygmt_helper/examples/srf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

import numpy as np
import pandas as pd
from qcore import srf

from pygmt_helper import plotting
from qcore import srf

map_data_ffp = Path("/path/to/qcore/qcore/data")
output_dir = Path("/path/to/output_dir")

srf_ffp = Path(__file__).parent / "resources" / "Akatarawa.srf"
Expand All @@ -23,7 +22,7 @@
slip_values = srf.srf2llv_py(str(srf_ffp), value="slip")

# Set map_data to None for faster plotting without topography
map_data = plotting.NZMapData.load(map_data_ffp, high_res_topo=False)
map_data = plotting.NZMapData.load(high_res_topo=False)
# map_data = None

# Compute colormap limits
Expand Down
9 changes: 3 additions & 6 deletions pygmt_helper/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def im_plots(
output_dir: Path,
rel_name: Optional[str] = None,
nhm_ffp: Optional[Path] = None,
qcore_data_dir: Optional[Path] = None,
nz_map_data: Optional[plotting.NZMapData] = None,
cb_limits_dict: Optional[dict[str, tuple[float, float]]] = None,
n_procs: int = 1,
) -> None:
Expand All @@ -233,8 +233,8 @@ def im_plots(
If provided, the function will generate plots for a specific realization (run) identified by this name.
nhm_ffp : Path, optional
Path to the NHM file, which can provide fault trace information.
qcore_data_dir : Path, optional
Path to the directory containing QCore data used for NZ map generation.
nz_map_data: Optional[NZMapData], default=None
The NZMapData object to supply topographic data.
cb_limits_dict : dict, optional
A dictionary specifying the color bar limits for each intensity measure (IM). If None, limits will be automatically computed.
n_procs : int, optional
Expand All @@ -245,9 +245,6 @@ def im_plots(
None
This function saves the generated plots to the specified output directory and does not return any values.
"""
nz_map_data = (
None if qcore_data_dir is None else plotting.NZMapData.load(qcore_data_dir)
)

# Load the trace if possible
fault_trace = (
Expand Down
23 changes: 11 additions & 12 deletions pygmt_helper/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pandas as pd
import pygmt
import xarray as xr
from qcore import gmt
from scipy import interpolate
from shapely import geometry

Expand All @@ -25,13 +26,11 @@ class NZMapData(NamedTuple):
topo_shading_grid: xr.DataArray = None

@classmethod
def load(cls, qcore_data_dir: Path, high_res_topo: bool = False) -> Self:
def load(cls, high_res_topo: bool = False) -> Self:
"""Load NZMapData from qcore resources.

Parameters
----------
qcore_data_dir : Path
Path to qcore data directory.
high_res_topo : bool
If True, load high resolution topographic data.

Expand All @@ -40,17 +39,17 @@ def load(cls, qcore_data_dir: Path, high_res_topo: bool = False) -> Self:
NZMapData
A map data object containing the paths to the map data.
"""
road_ffp = qcore_data_dir / "Paths/road/NZ.gmt"
highway_ffp = qcore_data_dir / "Paths/highway/NZ.gmt"
coastline_ffp = qcore_data_dir / "Paths/coastline/NZ.gmt"
water_ffp = qcore_data_dir / "Paths/water/NZ.gmt"
road_ffp = gmt.GMT_DATA.fetch("data/Paths/road/NZ.gmt")
highway_ffp = gmt.GMT_DATA.fetch("data/Paths/highway/NZ.gmt")
coastline_ffp = gmt.GMT_DATA.fetch("data/Paths/coastline/NZ.gmt")
water_ffp = gmt.GMT_DATA.fetch("data/Paths/water/NZ.gmt")

if high_res_topo:
topo_ffp = qcore_data_dir / "Topo/srtm_NZ_1s.grd"
topo_shading_ffp = qcore_data_dir / "Topo/srtm_NZ_1s_i5.grd"
topo_ffp = gmt.GMT_DATA.fetch("data/Topo/srtm_NZ_1s.grd")
topo_shading_ffp = gmt.GMT_DATA.fetch("data/Topo/srtm_NZ_1s_i5.grd")
else:
topo_ffp = qcore_data_dir / "Topo/srtm_NZ.grd"
topo_shading_ffp = qcore_data_dir / "Topo/srtm_NZ_i5.grd"
topo_ffp = gmt.GMT_DATA.fetch("data/Topo/srtm_NZ.grd")
topo_shading_ffp = gmt.GMT_DATA.fetch("data/Topo/srtm_NZ_i5.grd")

return cls(
road_df=geopandas.read_file(road_ffp),
Expand Down Expand Up @@ -372,7 +371,7 @@ def plot_grid(
phase = f"+{cmap_limits[0]}" if cmap_limits[0] > 0 else f"+{cmap_limits[1]}"
cb_frame = [f"a+{cmap_limits[2] * 2}{phase}f+{cmap_limits[2]}"]
if cb_label is not None:
cb_frame.append(f'x+l"{cb_label}"')
cb_frame.append(f"x+l{cb_label.replace(' ', r'\040')}")
fig.colorbar(
cmap=cpt_ffp,
frame=cb_frame,
Expand Down