-
Notifications
You must be signed in to change notification settings - Fork 1
/
step1_model_setup.py
executable file
·200 lines (172 loc) · 5.91 KB
/
step1_model_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python3
from dynworkflow import (
get_usgs_finite_fault_data,
infer_fault_mesh_size_and_spatial_zoom,
modify_FL33_34_fault_instantaneous_slip,
generate_mesh,
generate_input_seissol_fl33,
prepare_velocity_model_files,
generate_input_seissol_dr,
generate_waveform_config_from_usgs,
)
import argparse
import os
import shutil
import sys
import glob
import subprocess
# Append kinematic_models and external folders to path
# Get the directory of the current script
current_script_dir = os.path.dirname(os.path.abspath(__file__))
relative_paths = [
"dynworkflow/kinematic_models",
"external",
]
for relative_path in relative_paths:
absolute_path = os.path.join(current_script_dir, relative_path)
if absolute_path not in sys.path:
sys.path.append(absolute_path)
import generate_FL33_input_files
import compute_moment_rate_from_finite_fault_file
import generate_fault_output_from_fl33_input_files
import project_fault_tractions_onto_asagi_grid
import vizualizeBoundaryConditions
def run_step1():
# Check if 'pumgen' is available
status = os.system("which pumgen > /dev/null 2>&1")
if status != 0:
print("pumgen is not available.")
sys.exit(1)
parser = argparse.ArgumentParser(
description="automatically setup a dynamic rupture model from a kinematic model"
)
parser.add_argument(
"usgs_id_or_dtgeo_npy",
help="usgs earthquake code or event dictionnary (dtgeo workflow)",
)
parser.add_argument(
"--user_defined_kinematic_model",
nargs=1,
help="input filename of alternative model to usgs (e.g. Slipnear)",
type=str,
)
parser.add_argument(
"--velocity_model",
nargs=1,
help="input 1D velocity model in axitra format",
type=str,
)
args = parser.parse_args()
suffix = ""
if args.user_defined_kinematic_model:
finite_fault_fn = args.user_defined_kinematic_model[0]
suffix, ext = os.path.splitext(os.path.basename(finite_fault_fn))
folder_name = get_usgs_finite_fault_data.get_data(
args.usgs_id_or_dtgeo_npy,
min_magnitude=7,
suffix=suffix,
use_usgs_finite_fault=not args.user_defined_kinematic_model,
)
os.chdir(folder_name)
with open("tmp/projection.txt", "r") as fid:
projection = fid.read()
if args.user_defined_kinematic_model:
if os.path.exists(finite_fault_fn):
# absolute path given
shutil.copy(finite_fault_fn, "tmp")
else:
shutil.copy(f"../{finite_fault_fn}", "tmp")
finite_fault_fn = f"tmp/{finite_fault_fn}"
if args.velocity_model:
vel_model = args.velocity_model[0]
if os.path.exists(vel_model):
# absolute path given
shutil.copy(vel_model, "tmp")
else:
shutil.copy(f"../{vel_model}", "tmp")
vel_model = f"tmp/{vel_model}"
if not args.user_defined_kinematic_model:
finite_fault_fn = f"tmp/basic_inversion.param"
(
spatial_zoom,
fault_mesh_size,
) = infer_fault_mesh_size_and_spatial_zoom.infer_quantities(
finite_fault_fn, projection
)
with open(f"tmp/inferred_spatial_zoom.txt", "w") as f:
f.write(str(spatial_zoom))
with open(f"tmp/inferred_fault_mesh_size.txt", "w") as f:
f.write(str(fault_mesh_size))
generate_FL33_input_files.main(
finite_fault_fn,
"cubic",
spatial_zoom,
projection,
write_paraview=False,
PSRthreshold=0.0,
)
suffix, ext = os.path.splitext(os.path.basename(finite_fault_fn))
if not ext == ".txt":
if args.velocity_model:
print("using user-defined velocity model 1D velocity model")
with open(vel_model, "r") as fid:
file_contents = fid.read()
prepare_velocity_model_files.generate_arbitrary_velocity_files(
file_contents
)
else:
print("using USGS 1D velocity model")
prepare_velocity_model_files.generate_usgs_velocity_files()
else:
print("using slipnear 1D velocity model")
prepare_velocity_model_files.generate_arbitrary_velocity_files()
modify_FL33_34_fault_instantaneous_slip.update_file(
f"yaml_files/FL33_34_fault.yaml"
)
generate_mesh.generate(h_domain=20e3, h_fault=fault_mesh_size, interactive=False)
result = os.system("pumgen -s msh4 tmp/mesh.msh")
if result != 0:
sys.exit(1)
generate_input_seissol_fl33.generate()
compute_moment_rate_from_finite_fault_file.compute(
finite_fault_fn, "yaml_files/material.yaml", projection
)
if not os.path.exists("output"):
os.makedirs("output")
print("step1 completed")
return folder_name
def select_station_and_download_waveforms():
vizualizeBoundaryConditions.generate_boundary_file("tmp/mesh.xdmf", "faults")
# mv to tmp
files = glob.glob("mesh_bc_faults.*")
for file in files:
shutil.move(file, os.path.join("tmp", os.path.basename(file)))
generate_fault_output_from_fl33_input_files.generate(
"tmp/mesh_bc_faults.xdmf",
"yaml_files/FL33_34_fault.yaml",
"output/dyn-kinmod-fault",
"Gaussian",
0.5,
)
generate_waveform_config_from_usgs.generate_waveform_config_file(
ignore_source_files=True
)
command = [
os.path.join(
current_script_dir,
"submodules/seismic-waveform-factory/scripts/select_stations.py",
),
"waveforms_config.ini",
"14",
"7",
]
subprocess.run(command, check=True)
print(
"done selecting stations. If you are not satisfied, change waveforms_config.ini and rerun:"
)
scommand = " ".join(command)
print(f"{scommand}")
if __name__ == "__main__":
folder_name = run_step1()
select_station_and_download_waveforms()
print(f"cd {folder_name}")