Skip to content
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

feat(nshm_db_generator): Use the from_trace method #12

Merged
merged 4 commits into from
Feb 4, 2025
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
11 changes: 9 additions & 2 deletions nshmdb/nshmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

import duckdb
import numpy as np
from qcore import coordinates
from source_modelling.sources import Fault, Plane

from nshmdb import query
from qcore import coordinates
from source_modelling.sources import Fault, Plane


@dataclasses.dataclass
Expand Down Expand Up @@ -94,6 +94,13 @@ class NSHMDB:
db_filepath: Path

def __init__(self, db_filepath: Path):
"""Initialise the NSHMDB instance.

Parameters
----------
db_filepath : Path
Path to the SQLite database file.
"""
self.db_filepath = db_filepath

def create(self):
Expand Down
1 change: 1 addition & 0 deletions nshmdb/plotting/rupture.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path

import numpy as np

from pygmt_helper import plotting
from source_modelling.sources import Fault

Expand Down
78 changes: 41 additions & 37 deletions nshmdb/scripts/nshm_db_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import geojson
import numpy as np
import pandas as pd
import qcore.coordinates
import shapely
import typer
from geojson import FeatureCollection

from nshmdb.nshmdb import NSHMDB
from qcore import coordinates
from source_modelling.sources import Fault, Plane

app = typer.Typer()
Expand Down Expand Up @@ -65,37 +67,34 @@ def extract_faults_from_info(
faults = {}
for i in range(len(fault_info_list.features)):
fault_feature = fault_info_list[i]
fault_trace = list(geojson.utils.coords(fault_feature))
fault_trace = shapely.LineString(
coordinates.wgs_depth_to_nztm(
np.array(list(geojson.utils.coords(fault_feature)))[:, ::-1]
)
)
fault_trace = shapely.simplify(fault_trace, 10)
claudio525 marked this conversation as resolved.
Show resolved Hide resolved
trace_coords = np.array(fault_trace.coords)
name = fault_feature.properties["FaultName"]
bottom = fault_feature.properties["LowDepth"]
dip_dir = fault_feature.properties["DipDir"]
dip = fault_feature.properties["DipDeg"]
bottom = fault_feature.properties["LowDepth"]
if dip == 90:
projected_width = 0
else:
projected_width = bottom / np.tan(np.radians(dip))
planes = []
for i in range(len(fault_trace) - 1):
top_left = qcore.coordinates.wgs_depth_to_nztm(
np.append(fault_trace[i][::-1], 0)
)
top_right = qcore.coordinates.wgs_depth_to_nztm(
np.append(fault_trace[i + 1][::-1], 0)
)
dip_dir_direction = (
np.array(
[
projected_width * np.cos(np.radians(dip_dir)),
projected_width * np.sin(np.radians(dip_dir)),
bottom,
]
)
* 1000
for j in range(len(trace_coords) - 1):
top_left = trace_coords[j]
top_right = trace_coords[j + 1]
planes.append(
Plane.from_nztm_trace(
np.array([top_left, top_right]),
0,
bottom,
dip,
coordinates.great_circle_bearing_to_nztm_bearing(
coordinates.nztm_to_wgs_depth(top_left), 1, dip_dir
)
if dip != 90
else 0,
),
)
bottom_left = top_left + dip_dir_direction
bottom_right = top_right + dip_dir_direction
corners = np.array([top_left, top_right, bottom_right, bottom_left])
planes.append(Plane(corners))
faults[name] = Fault(planes)
return faults

Expand Down Expand Up @@ -127,9 +126,10 @@ def main(
db = NSHMDB(sqlite_db_path)
db.create()

with zipfile.ZipFile(
cru_solutions_zip_path, "r"
) as cru_solutions_zip_file, db.connection() as conn:
with (
zipfile.ZipFile(cru_solutions_zip_path, "r") as cru_solutions_zip_file,
db.connection() as conn,
):
with cru_solutions_zip_file.open(
str(FAULT_INFORMATION_PATH)
) as fault_info_handle:
Expand Down Expand Up @@ -194,13 +194,17 @@ def main(
)

if not skip_rupture_creation:
with cru_solutions_zip_file.open(
str(RUPTURE_FAULT_JOIN_PATH)
) as rupture_fault_join_handle, cru_solutions_zip_file.open(
str(RUPTURE_RATES_PATH)
) as rupture_rates_handle, cru_solutions_zip_file.open(
str(RUPTURE_PROPERTIES_PATH)
) as rupture_properties_path:
with (
cru_solutions_zip_file.open(
str(RUPTURE_FAULT_JOIN_PATH)
) as rupture_fault_join_handle,
cru_solutions_zip_file.open(
str(RUPTURE_RATES_PATH)
) as rupture_rates_handle,
cru_solutions_zip_file.open(
str(RUPTURE_PROPERTIES_PATH)
) as rupture_properties_path,
):
rupture_rates = pd.read_csv(rupture_rates_handle).set_index(
"Rupture Index"
)
Expand Down
47 changes: 47 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,50 @@ nshm_db_generator = "nshmdb.scripts.nshm_db_generator:app"
[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}

[tool.ruff.lint]
extend-select = [
# isort imports
"I",
# Use r'\s+' rather than '\s+'
"W605",
# All the naming errors, like using camel case for function names.
"N",
# Missing docstrings in classes, methods, and functions
"D101",
"D102",
"D103",
"D105",
"D107",
# Use f-string instead of a format call
"UP032",
# Standard library import is deprecated
"UP035",
# Missing function argument type-annotation
"ANN001",
# Using except without specifying an exception type to catch
"BLE001"
]
ignore = ["D104"]

[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.ruff.lint.isort]
known-first-party = [
"source_modelling",
"workflow",
"pygmt_helper",
"qcore",
"empirical",
"nshmdb",
"IM",
"mera"
]

[tool.ruff.lint.per-file-ignores]
# Ignore no docstring in __init__.py
"__init__.py" = ["D104"]
# Ignore docstring errors in tests folder
"tests/**.py" = ["D"]


3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ geojson
numpy<2
pandas
pygmt_helper @ git+https://github.com/ucgmsim/pygmt_helper.git
pytest
qcore @ git+https://github.com/ucgmsim/qcore.git
shapely
source_modelling @ git+https://github.com/ucgmsim/source_modelling.git
typer
pytest
1 change: 1 addition & 0 deletions tests/test_nshmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest

from nshmdb.nshmdb import NSHMDB, FaultInfo, Rupture


Expand Down
1 change: 1 addition & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from nshmdb.query import (
InfixOperator,
Token,
Expand Down