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

Nshm organisational refactor #4

Merged
merged 39 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1fff878
package and scriptify nshm db generator
lispandfound May 16, 2024
9daf496
add mypy type-checking
lispandfound May 16, 2024
48a669f
Add fault module
lispandfound May 16, 2024
de2663f
Add nshmdb interface
lispandfound May 16, 2024
60a2a10
add insert parent method
lispandfound May 16, 2024
229f299
add schema creation method
lispandfound May 16, 2024
a374553
fix segment reference
lispandfound May 16, 2024
1290224
begin script refactor
lispandfound May 16, 2024
da52082
s/segment/plane
lispandfound May 16, 2024
215fa67
fix type of tect_type
lispandfound May 16, 2024
f7ccabe
remove unused imports
lispandfound May 16, 2024
76ac658
fix function name
lispandfound May 16, 2024
7d52427
add comment to explain section casting
lispandfound May 16, 2024
f33d385
s/segment/plane & replace reuse sqlite connection
lispandfound May 17, 2024
38eaebb
speed up db gen
lispandfound May 17, 2024
c772631
add rupture plotting functionality
lispandfound May 20, 2024
11f83d6
Fix CLI scripts
lispandfound May 20, 2024
d00556e
update strike and dip_dir calculation for new api
lispandfound May 20, 2024
0f75bac
add docstring for NSHMDB.get_fault
lispandfound May 21, 2024
9b9e692
add docstring for NSHMDB.create
lispandfound May 21, 2024
c12f98d
raise error if db does not exist
lispandfound May 21, 2024
87f7b71
sensitivity tweaks for fault coordinates
lispandfound May 21, 2024
7be42c7
convert to nztm
lispandfound May 21, 2024
9c010a0
fix boundary case fault coord -> global coord bug
lispandfound May 22, 2024
90cf1e4
downgrade used version of python
lispandfound May 22, 2024
139318c
make _corners not private
lispandfound May 28, 2024
55deac6
improve docs
lispandfound May 28, 2024
8be0ace
add fault-level nztm corner calculations
lispandfound May 28, 2024
f1a56e4
write plot to file instead of fig.show
lispandfound May 28, 2024
142afb3
use requirements.txt
lispandfound May 28, 2024
e7190a4
remove unused variable
lispandfound May 28, 2024
23c560f
fix typo
lispandfound May 28, 2024
a6adcca
optimise imports
lispandfound May 28, 2024
aa9f1ff
update names and docstrings
lispandfound May 30, 2024
03d84c0
black
lispandfound May 30, 2024
48ba6a8
use optional
lispandfound May 31, 2024
debf646
update readme
lispandfound May 31, 2024
75ab5ff
spacing
lispandfound May 31, 2024
f2f4fbc
bold text
lispandfound May 31, 2024
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
50 changes: 30 additions & 20 deletions nshmdb/fault.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
from enum import Enum

import numpy as np
import qcore.coordinates
import qcore.geo
from qcore import coordinates, geo


class TectType(Enum):
Expand Down Expand Up @@ -50,7 +49,7 @@ class FaultPlane:
The rake angle of the fault plane.
"""

_corners: np.ndarray
corners_nztm: np.ndarray
lispandfound marked this conversation as resolved.
Show resolved Hide resolved
rake: float

@property
Expand All @@ -62,7 +61,7 @@ def corners(self) -> np.ndarray:
np.ndarray
The corners of the fault plane in (lat, lon, depth) format.
"""
return qcore.coordinates.nztm_to_wgs_depth(self._corners)
return qcore.coordinates.nztm_to_wgs_depth(self.corners_nztm)

@property
def length_m(self) -> float:
Expand All @@ -72,7 +71,7 @@ def length_m(self) -> float:
float
The length of the fault plane (in metres).
"""
return np.linalg.norm(self._corners[1] - self._corners[0])
return np.linalg.norm(self.corners_nztm[1] - self.corners_nztm[0])

@property
def width_m(self) -> float:
Expand All @@ -82,7 +81,7 @@ def width_m(self) -> float:
float
The width of the fault plane (in metres).
"""
return np.linalg.norm(self._corners[-1] - self._corners[0])
return np.linalg.norm(self.corners_nztm[-1] - self.corners_nztm[0])

@property
def bottom_m(self) -> float:
Expand All @@ -92,7 +91,7 @@ def bottom_m(self) -> float:
float
The bottom depth (in metres).
"""
return self._corners[-1, -1]
return self.corners_nztm[-1, -1]

@property
def width(self) -> float:
Expand Down Expand Up @@ -132,7 +131,7 @@ def projected_width(self) -> float:
float
The projected width of the fault plane (in kilometres).
"""
return self.projected_width / _KM_TO_M
return self.projected_width_m / _KM_TO_M

@property
def strike(self) -> float:
Expand All @@ -146,7 +145,7 @@ def strike(self) -> float:

north_direction = np.array([1, 0, 0])
up_direction = np.array([0, 0, 1])
strike_direction = self._corners[1] - self._corners[0]
strike_direction = self.corners_nztm[1] - self.corners_nztm[0]
return qcore.geo.oriented_bearing_wrt_normal(
north_direction, strike_direction, up_direction
)
Expand All @@ -163,7 +162,7 @@ def dip_dir(self) -> float:
return 0 # TODO: Is this right for this case?
north_direction = np.array([1, 0, 0])
up_direction = np.array([0, 0, 1])
dip_direction = self._corners[-1] - self._corners[0]
dip_direction = self.corners_nztm[-1] - self.corners_nztm[0]
dip_direction[-1] = 0
return qcore.geo.oriented_bearing_wrt_normal(
north_direction, dip_direction, up_direction
Expand All @@ -189,8 +188,8 @@ def plane_coordinates_to_global_coordinates(
plane_coordinates : np.ndarray
Plane coordinates to convert. Plane coordinates are
2D coordinates (x, y) given for a fault plane (a plane), where x
lispandfound marked this conversation as resolved.
Show resolved Hide resolved
represents displacement along the length of the fault, and y
displacement along the width of the fault (see diagram below). The
represents displacement along the strike, and y
displacement along the dip (see diagram below). The
origin for plane coordinates is the centre of the fault.

+x
Expand All @@ -209,9 +208,9 @@ def plane_coordinates_to_global_coordinates(
np.ndarray
An 3d-vector of (lat, lon, depth) transformed coordinates.
"""
origin = self._corners[0]
top_right = self._corners[1]
bottom_left = self._corners[-1]
origin = self.corners_nztm[0]
top_right = self.corners_nztm[1]
bottom_left = self.corners_nztm[-1]
frame = np.vstack((top_right - origin, bottom_left - origin))
offset = np.array([1 / 2, 1 / 2])

Expand Down Expand Up @@ -244,9 +243,9 @@ def global_coordinates_to_plane_coordinates(
ValueError
If the given coordinates do not lie in the fault plane.
"""
origin = self._corners[0]
top_right = self._corners[1]
bottom_left = self._corners[-1]
origin = self.corners_nztm[0]
top_right = self.corners_nztm[1]
bottom_left = self.corners_nztm[-1]
frame = np.vstack((top_right - origin, bottom_left - origin))
offset = qcore.coordinates.wgs_depth_to_nztm(global_coordinates) - origin
plane_coordinates, residual, _, _ = np.linalg.lstsq(frame.T, offset, rcond=None)
Expand Down Expand Up @@ -294,7 +293,7 @@ def centroid(self) -> np.ndarray:
"""

return qcore.coordinates.nztm_to_wgs_depth(
np.mean(self._corners, axis=0).reshape((1, -1))
np.mean(self.corners_nztm, axis=0).reshape((1, -1))
).ravel()


Expand Down Expand Up @@ -374,10 +373,21 @@ def corners(self) -> np.ndarray:
Returns
-------
np.ndarray of shape (4n x 3)
The corners of each fault plane in the fault, stacked vertically.
The corners in (lat, lon, depth) format of each fault plane in the
fault, stacked vertically.
"""
return np.vstack([plane.corners for plane in self.planes])

def corners_nztm(self) -> np.ndarray:
"""Get all corners of a fault.

Returns
-------
np.ndarray of shape (4n x 3)
The corners in NZTM format of each fault plane in the fault, stacked vertically.
"""
return np.vstack([plane.corners_nztm for plane in self.planes])

def global_coordinates_to_fault_coordinates(
self, global_coordinates: np.ndarray
) -> np.ndarray:
Expand Down
8 changes: 6 additions & 2 deletions nshmdb/plotting/rupture.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
plot_rupture: Plot ruptures on faults.
"""

from pathlib import Path

import numpy as np
from nshmdb.fault import Fault
from pygmt_helper import plotting


def plot_rupture(title: str, faults: list[Fault]):
def plot_rupture(title: str, faults: list[Fault], output_filepath: Path):
"""Plot faults involved in a rupture scenario using pygmt.

Parameters
Expand All @@ -21,6 +23,8 @@ def plot_rupture(title: str, faults: list[Fault]):
The title of the figure.
faults : list[Fault]
The list of faults involved in the rupture.
output_filepath : Path
The filepath to output the figure to.
"""
corners = np.vstack([fault.corners() for fault in faults])
region = (
Expand All @@ -41,4 +45,4 @@ def plot_rupture(title: str, faults: list[Fault]):
fill="red",
)

fig.show()
fig.savefig(output_filepath)
1 change: 0 additions & 1 deletion nshmdb/scripts/nshm_db_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
app = typer.Typer()


POLYGONS_PATH = Path("ruptures") / "sect_polygons.geojson"
FAULT_INFORMATION_PATH = Path("ruptures") / "fault_sections.geojson"
RUPTURE_FAULT_JOIN_PATH = Path("ruptures") / "fast_indices.csv"

Expand Down
45 changes: 0 additions & 45 deletions pyproject.toml

This file was deleted.

10 changes: 10 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dataclasses
geojson
numpy
pandas
pygments
pygmt
qcore
scipy
tqdm
typer
Loading