Skip to content

Commit 9e8279e

Browse files
authored
Merge pull request #74 from ucgmsim/no_compress_broadband
Do not compress broadband waveforms
2 parents 9e7c5ae + c6a11ec commit 9e8279e

11 files changed

+78
-31
lines changed

tests/test_realisation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,35 @@ def test_seeds() -> None:
567567
)
568568

569569

570+
def test_seeds_from_random(tmp_path: Path) -> None:
571+
"""Test read from realisation or random"""
572+
realisation_file = tmp_path / "test_realisation.json"
573+
# Create a file with unrelated content
574+
initial_content = {"other_key": "some_value"}
575+
with open(realisation_file, "w") as f:
576+
json.dump(initial_content, f)
577+
578+
seeds = realisations.Seeds.read_from_realisation_or_random(realisation_file)
579+
assert all(
580+
0 <= seed <= 2 ** (struct.Struct("i").size * 8 - 1) - 1
581+
for seed in seeds.to_dict().values()
582+
)
583+
584+
realisation_file = tmp_path / "test_realisation_1.json"
585+
# Create a file with unrelated content
586+
seeds = realisations.Seeds(
587+
nshm_to_realisation_seed=0,
588+
rupture_propagation_seed=0,
589+
genslip_seed=0,
590+
srfgen_seed=0,
591+
hf_seed=0,
592+
)
593+
seeds.write_to_realisation(realisation_file)
594+
595+
seeds_read = realisations.Seeds.read_from_realisation_or_random(realisation_file)
596+
assert seeds == seeds_read
597+
598+
570599
def test_velocity_model_1d(tmp_path: Path) -> None:
571600
velocity_model_1d = realisations.VelocityModel1D(
572601
model=pd.DataFrame(

workflow/realisations.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ class Seeds(RealisationConfiguration):
242242
"""HF seed."""
243243

244244
@classmethod
245-
def read_from_realisation_or_defaults(
246-
cls, realisation_ffp: Path, *args: list[Any]
245+
def read_from_realisation_or_random(
246+
cls, realisation_ffp: Path
247247
) -> Self: # *args is to maintain compat with superclass (remove this and see the error in mypy).
248248
"""Read seeds configuration from a realisation file or generate random seeds if not present.
249249
@@ -256,8 +256,6 @@ def read_from_realisation_or_defaults(
256256
----------
257257
realisation_ffp : Path
258258
The realisation filepath to read from.
259-
*args : list
260-
Ignored arguments.
261259
262260
Returns
263261
-------

workflow/scripts/bb_sim.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ def combine_hf_and_lf(
154154
)
155155
hf = hf.sel(station=common_stations)
156156
lf = lf.sel(station=common_stations)
157-
158157
vs30_df = pd.read_csv(
159-
station_vs30_ffp, sep=r"\s+", header=None, names=["station", "vsite"]
158+
station_vs30_ffp,
159+
sep=r"\s+",
160+
header=None,
161+
names=["station", "vsite"], # type: ignore[invalid-argument-type]
160162
).set_index("station")
161163
vs30_df["vsite"] = vs30_df["vsite"].astype(np.float32)
162164
vs30_df = vs30_df.loc[common_stations]
@@ -218,8 +220,6 @@ def combine_hf_and_lf(
218220
engine="h5netcdf",
219221
encoding={
220222
"waveform": {
221-
"compression": "zlib", # Use zlib compression.
222-
"complevel": 5, # Compress to level 5 (of 9).
223223
"fletcher32": True, # Add Fletcher-32 checksums for long-term storage.
224224
}
225225
},

workflow/scripts/check_srf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ def check_srf(
8383
)
8484
mu = velocity_model["mu"].iloc[indices].values
8585
srf_magnitude = mag_scaling.mom2mag(
86-
(srf_file.points["area"].values * srf_file.points["slip"].values * mu).sum()
86+
(
87+
np.array(srf_file.points["area"].values)
88+
* np.array(srf_file.points["slip"].values)
89+
* mu
90+
).sum()
8791
)
8892
logger = log_utils.get_logger("__name__")
8993

workflow/scripts/gcmt_to_realisation.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,24 @@ def gcmt_to_realisation(
161161
].set_index("PublicID")
162162

163163
if gcmt_event_id in gcmt_solutions.index:
164-
solution = gcmt_solutions.loc[gcmt_event_id]
165-
latitude = solution["Latitude"]
166-
longitude = solution["Longitude"]
167-
centroid_depth = solution["CD"]
168-
magnitude = solution["Mw"]
169-
nodal_plane_1 = NodalPlane(
170-
solution["strike1"], solution["dip1"], solution["rake1"]
171-
)
172-
nodal_plane_2 = NodalPlane(
173-
solution["strike2"], solution["dip2"], solution["rake2"]
174-
)
164+
latitude = gcmt_solutions.at[gcmt_event_id, "Latitude"]
165+
longitude = gcmt_solutions.at[gcmt_event_id, "Longitude"]
166+
centroid_depth = gcmt_solutions.at[gcmt_event_id, "CD"]
167+
magnitude = gcmt_solutions.at[gcmt_event_id, "Mw"]
168+
strike1 = gcmt_solutions.at[gcmt_event_id, "strike1"]
169+
dip1 = gcmt_solutions.at[gcmt_event_id, "dip1"]
170+
rake1 = gcmt_solutions.at[gcmt_event_id, "rake1"]
171+
strike2 = gcmt_solutions.at[gcmt_event_id, "strike2"]
172+
dip2 = gcmt_solutions.at[gcmt_event_id, "dip2"]
173+
rake2 = gcmt_solutions.at[gcmt_event_id, "rake2"]
174+
assert isinstance(strike1, float | int)
175+
assert isinstance(dip1, float | int)
176+
assert isinstance(rake1, float | int)
177+
assert isinstance(strike2, float | int)
178+
assert isinstance(dip2, float | int)
179+
assert isinstance(rake2, float | int)
180+
nodal_plane_1 = NodalPlane(strike1, dip1, rake1)
181+
nodal_plane_2 = NodalPlane(strike2, rake2, dip2)
175182
elif gcmt_event_id in automated_gcmt_solutions:
176183
solution = automated_gcmt_solutions[gcmt_event_id]
177184
latitude = solution["location"]["latitude"]
@@ -203,6 +210,7 @@ def gcmt_to_realisation(
203210
# Calculate dip direction from strike (strike + 90 degrees for right-hand rule)
204211
dip_direction = (selected_nodal_plane.strike + 90) % 360
205212

213+
assert isinstance(magnitude, float)
206214
length, width = magnitude_scaling.magnitude_to_length_width(
207215
scaling_relation, magnitude, rake
208216
)
@@ -217,6 +225,7 @@ def gcmt_to_realisation(
217225
length_m = length_km * 1000 # Convert km to meters
218226
width_m = width_km * 1000 # Convert km to meters
219227

228+
assert isinstance(centroid_depth, float)
220229
source_geometry = sources.Point.from_lat_lon_depth(
221230
point_coordinates=np.array(
222231
[latitude, longitude, centroid_depth * 1000]

workflow/scripts/generate_rupture_propagation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def generate_rupture_propagation(
8181
strategy : RuptureStrategy
8282
The rupture propagation strategy to use. Default is `RuptureStrategy.RANDOM`.
8383
"""
84-
seeds = realisations.Seeds.read_from_realisation_or_defaults(realisation_ffp)
84+
seeds = realisations.Seeds.read_from_realisation_or_random(realisation_ffp)
8585
source_config = realisations.SourceConfig.read_from_realisation(realisation_ffp)
8686
faults = source_config.source_geometries
8787

workflow/scripts/generate_station_coordinates.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ def generate_fd_files(
7575

7676
# retrieve in station names, latitudes and longitudes
7777
stations = pd.read_csv(
78-
stat_file, delimiter=r"\s+", comment="#", names=["lon", "lat", "name"]
78+
stat_file,
79+
delimiter=r"\s+",
80+
comment="#",
81+
names=["lon", "lat", "name"], # type: ignore[invalid-argument-type]
7982
)
8083

8184
x, y = proj(lat=stations["lat"].values, lon=stations["lon"].values).T

workflow/scripts/generate_velocity_model_parameters.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,12 @@ def pgv_target(
387387
for magnitude in magnitudes.magnitudes.values()
388388
)
389389
)
390-
return np.interp(
391-
total_magnitude,
392-
velocity_model_parameters.pgv_interpolants[:, 0],
393-
velocity_model_parameters.pgv_interpolants[:, 1],
390+
return float(
391+
np.interp(
392+
total_magnitude,
393+
velocity_model_parameters.pgv_interpolants[:, 0],
394+
velocity_model_parameters.pgv_interpolants[:, 1],
395+
)
394396
)
395397

396398

workflow/scripts/hf_sim.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def run_hf(
279279
None
280280
The function does not return any value. It writes the HF output directly to `out_file`.
281281
"""
282-
seeds = Seeds.read_from_realisation_or_defaults(realisation_ffp)
282+
seeds = Seeds.read_from_realisation_or_random(realisation_ffp)
283283

284284
domain_parameters = DomainParameters.read_from_realisation(realisation_ffp)
285285
metadata = RealisationMetadata.read_from_realisation(realisation_ffp)
@@ -294,7 +294,7 @@ def run_hf(
294294
station_file,
295295
delimiter=r"\s+",
296296
header=None,
297-
names=["longitude", "latitude", "name"],
297+
names=["longitude", "latitude", "name"], # type: ignore[invalid-argument-type]
298298
).set_index("name")
299299
station_hashes = np.array(
300300
[stable_hash(name) for name in stations.index], dtype=np.int32
@@ -306,7 +306,9 @@ def run_hf(
306306
stations["seed"] = np.int32(seeds.hf_seed) ^ station_hashes
307307
velocity_model_path = work_directory / "velocity_model"
308308
velocity_model.write_velocity_model(velocity_model_path)
309-
nt = int(np.float32(domain_parameters.duration) / np.float32(hf_config.dt)) # Match Fortran's single-precision for consistent nt calculation
309+
nt = int(
310+
np.float32(domain_parameters.duration) / np.float32(hf_config.dt)
311+
) # Match Fortran's single-precision for consistent nt calculation
310312
waveform = np.empty((3, len(stations), nt), dtype=np.float32)
311313

312314
hf_input_template = build_hf_input(

workflow/scripts/nshm2022_to_realisation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def generate_realisation(
329329
)
330330
raise typer.Exit(code=1)
331331
faults_info = db.get_rupture_fault_info(rupture_id)
332-
seeds = Seeds.read_from_realisation_or_defaults(realisation_ffp)
332+
seeds = Seeds.read_from_realisation_or_random(realisation_ffp)
333333
np.random.seed(seed=seeds.nshm_to_realisation_seed)
334334
source_config = SourceConfig(faults)
335335

0 commit comments

Comments
 (0)