-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProbShakemap.py
334 lines (260 loc) · 15 KB
/
ProbShakemap.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import tools
import os
import numpy as np
import argparse
import time
import shutil
import logging
from datetime import datetime
import config
start_time = time.time()
def setup_logging(log_dir):
"""
Sets up the LOG file
"""
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
log_file = os.path.join(log_dir, f"setup_{current_time}.log")
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def run_main():
"""
Runs tools.Main()
"""
# Set the random seed for reproducibility (must be the same as in tools.py)
config_dict = config.load_config('input_file.txt')
seed = config_dict['seed']
np.random.seed(seed)
ProbCalc = tools.Main(args.imt, args.pois_file,
args.numGMPEsRealizations, args.num_processes)
prob_output = ProbCalc.run_prob_analysis()
return prob_output
# Define command-line arguments
parser = argparse.ArgumentParser(description='ProbShakemap Toolbox')
input_params = parser.add_argument_group('input params')
input_params.add_argument('--imt', choices=['PGA', 'PGV', 'SA(0.3)', 'SA(1.0)', 'SA(3.0)'], help='Intensity measure type (IMT)')
input_params.add_argument('--tool', choices=['StationRecords', 'Save_Output', 'QueryHDF5'], help='Tool(s) to use')
input_params.add_argument('--prob_tool', choices=['GetStatistics', 'GetDistributions', 'EnsemblePlot'], nargs='+', help='ProbShakemap Tool(s) to use')
input_params.add_argument('--numGMPEsRealizations', type=int, help='Total number of GMPEs random samples')
input_params.add_argument('--num_processes', type=int, default=1, help='Number of CPU cores for code parallelization')
input_params.add_argument('--imt_min', type=float, help='Minimum value for the selected IMT (for plot only)')
input_params.add_argument('--imt_max', type=float, help='Maximum value for the selected IMT (for plot only)')
input_params.add_argument('--station_file', help='Shakemap .json station file')
input_params.add_argument('--scenario', type=float, help='Scenario number')
input_params.add_argument('--pois_file', help='Filename with latitude and longitude of POIs')
input_params.add_argument('--deg_round', type=float, default=5, help='Rounding precision for latitude and longitude')
input_params.add_argument('--pois_subset', action='store_true', default=False, help='Extract a subset of POIs')
input_params.add_argument('--n_pois', type=int, default=10, help='Number of POIs in the subset')
input_params.add_argument('--max_distance', type=int, default=200, help='Max distance from epicenter of POIs in the subset')
input_params.add_argument('--pois_selection_method', choices=['random', 'azimuth_uniform'], help='Selection method for the POIs of the subset')
input_params.add_argument('--reuse_pois_subset', action='store_true', default=False, help='Reuse the subset of POIs already extracted in POIs.txt')
input_params.add_argument('--vector_npy', action='store_true', default=False, help='Store ground motion distributions at all POIs (vector.npy)')
input_params.add_argument('--fileScenariosWeights', default="", help='File with scenarios weights')
# Parse command-line arguments
args = parser.parse_args()
# Set up log file
outfile_dir = os.path.join(os.getcwd(), "OUTPUT/LOGS/")
if not os.path.exists(outfile_dir):
os.makedirs(outfile_dir)
setup_logging(outfile_dir)
# Get params
params = tools.get_params()
Lat_Event = float(params["Lat_Event"])
Lon_Event = float(params["Lon_Event"])
event_dir = params["event_dir"]
EnsembleSize = params['Ensemble_Size']
# Scenarios file
listscenarios_dir = os.getcwd() + "/INPUT_FILES/ENSEMBLE/"
scenarios_file = [name for name in os.listdir(listscenarios_dir) if name != ".DS_Store"]
###########################
##### STATION RECORDS #####
###########################
if args.tool == 'StationRecords':
if args.imt_max is None:
raise TypeError("Missing required argument 'imt_max'")
if args.imt_min is None:
raise TypeError("Missing required argument 'imt_min'")
if args.station_file is None:
raise TypeError("Station .json file from shakemap not available")
logging.info(f"Tool: {args.tool}")
logging.info(f"Intensity Measure: {args.imt}")
logging.info(f"Min {args.imt}: {args.imt_min}")
logging.info(f"Max {args.imt}: {args.imt_max}")
logging.info(f"Deg round: {args.deg_round}")
logging.info(f"Station file: {args.station_file}")
StationRecords = tools.StationRecords(args.imt, args.imt_min, args.imt_max,
args.deg_round, args.station_file)
StationRecords.plot()
#################################
##### WRITE OUTPUT FILE #########
#################################
elif args.tool == 'Save_Output':
if args.imt is None:
raise TypeError("Missing required argument 'imt'")
if args.pois_file is None:
raise TypeError("Missing required argument 'pois_file'")
if args.numGMPEsRealizations is None:
raise TypeError("Missing required argument 'numGMPEsRealizations'")
logging.info(f"Tool: {args.tool}")
logging.info(f"Intensity Measure: {args.imt}")
logging.info(f"Scenarios Ensemble File: {scenarios_file[0]}")
logging.info(f"POIs File: {args.pois_file}")
logging.info(f"Num GMPEsRealizations: {args.numGMPEsRealizations}")
prob_output = run_main()
SiteGmf = prob_output["SiteGmf"]
keys_scen = prob_output["keys_scen"]
keys_sites = prob_output["keys_sites"]
Save_Output = tools.Write(args.imt, EnsembleSize, keys_scen,
SiteGmf, keys_sites, args.num_processes)
Save_Output.write_output()
#################################
##### QUERY OUTPUT FILE #########
#################################
elif args.tool == 'QueryHDF5':
outfile_dir = os.path.join(os.getcwd(), "OUTPUT/HDF5_FILES/")
outputfile = [name for name in os.listdir(outfile_dir) if name != ".DS_Store"][0]
if not outputfile:
print("WARNING: No output file found --> run 'Save_Output' first!")
else:
if args.scenario is None:
raise TypeError("Missing required argument 'scenario'")
logging.info(f"Tool: {args.tool}")
logging.info(f"Intensity Measure: {args.imt}")
logging.info(f"POIs File: {args.pois_file}")
logging.info(f"POIs Subset: {args.pois_subset}")
if args.pois_subset:
logging.info(f"Scenario: {args.scenario}")
logging.info(f"Num POIs in the Subset: {args.n_pois}")
logging.info(f"Max Distance: {args.max_distance}")
logging.info(f"POIs Selection Methos: {args.pois_selection_method}")
QueryHDF5 = tools.QueryHDF5(args.scenario, args.pois_file,
args.pois_subset, args.n_pois, args.max_distance,
args.pois_selection_method, Lon_Event, Lat_Event)
QueryHDF5.print_info()
#################################
###### PROB SHAKEMAPS TOOLS #####
#################################
if args.prob_tool:
run_main_flag = True # Flag variable to track if Main() has been executed
pois_subset_flag = True # Flag variable to track if the pois subset has already been extracted
if args.reuse_pois_subset:
pois_subset_flag = False # Reuse the subset of POIs already extracted (in POIs.txt)
for tool in args.prob_tool:
if tool == 'GetStatistics':
if args.imt_max is None:
raise TypeError("Missing required argument 'imt_max'")
if args.imt_min is None:
raise TypeError("Missing required argument 'imt_min'")
if args.pois_subset and not args.pois_selection_method:
raise TypeError("Missing required argument 'pois_selection_method'")
if args.pois_selection_method == 'azimuth_uniform':
if args.n_pois % 4 > 0:
raise TypeError("Select a number of POIs divisible by 4")
logging.info(f"Prob Tool: {tool}")
logging.info(f"Intensity Measure: {args.imt}")
logging.info(f"Min {args.imt}: {args.imt_min}")
logging.info(f"Max {args.imt}: {args.imt_max}")
logging.info(f"Scenarios Ensemble File: {scenarios_file[0]}")
logging.info(f"POIs File: {args.pois_file}")
logging.info(f"Num GMPEsRealizations: {args.numGMPEsRealizations}")
logging.info(f"File Scenarios Weights: {args.fileScenariosWeights}")
logging.info(f"Deg round: {args.deg_round}")
logging.info(f"POIs Subset: {args.pois_subset}")
if args.pois_subset:
logging.info(f"Num POIs in the Subset: {args.n_pois}")
logging.info(f"Max Distance: {args.max_distance}")
logging.info(f"POIs Selection Methos: {args.pois_selection_method}")
if run_main_flag:
if args.imt is None:
raise TypeError("Missing required argument 'imt'")
if args.pois_file is None:
raise TypeError("Missing required argument 'pois_file'")
if args.numGMPEsRealizations is None:
raise TypeError("Missing required argument 'numGMPEsRealizations'")
prob_output = run_main()
SiteGmf = prob_output["SiteGmf"]
run_main_flag = False
GetStatistics = tools.GetStatistics(SiteGmf, EnsembleSize, Lon_Event, Lat_Event, args.numGMPEsRealizations, event_dir, args.imt,
args.imt_min, args.imt_max, args.fileScenariosWeights,
args.pois_file, args.pois_subset, args.n_pois, args.max_distance,
args.pois_selection_method, args.deg_round, pois_subset_flag, args.num_processes, args.vector_npy)
GetStatistics.save_statistics()
GetStatistics.plot_statistics()
pois_subset_flag = False
elif tool == 'GetDistributions':
if args.imt_max is None:
raise TypeError("Missing required argument 'imt_max'")
if args.imt_min is None:
raise TypeError("Missing required argument 'imt_min'")
if args.station_file is None:
raise TypeError("Station .json file from shakemap not available")
if args.pois_subset and not args.pois_selection_method:
raise TypeError("Missing required argument 'pois_selection_method'")
if args.pois_selection_method == 'azimuth_uniform':
if args.n_pois % 4 > 0:
raise TypeError("Select a number of POIs divisible by 4")
logging.info(f"Prob Tool: {tool}")
logging.info(f"Intensity Measure: {args.imt}")
logging.info(f"Min {args.imt}: {args.imt_min}")
logging.info(f"Max {args.imt}: {args.imt_max}")
logging.info(f"Scenarios Ensemble File: {scenarios_file[0]}")
logging.info(f"POIs File: {args.pois_file}")
logging.info(f"Num GMPEsRealizations: {args.numGMPEsRealizations}")
logging.info(f"Station file: {args.station_file}")
logging.info(f"File Scenarios Weights: {args.fileScenariosWeights}")
logging.info(f"Deg round: {args.deg_round}")
logging.info(f"POIs Subset: {args.pois_subset}")
if args.pois_subset:
logging.info(f"Num POIs in the Subset: {args.n_pois}")
logging.info(f"Max Distance: {args.max_distance}")
logging.info(f"POIs Selection Methos: {args.pois_selection_method}")
if run_main_flag:
if args.imt is None:
raise TypeError("Missing required argument 'imt'")
if args.pois_file is None:
raise TypeError("Missing required argument 'pois_file'")
if args.numGMPEsRealizations is None:
raise TypeError("Missing required argument 'numGMPEsRealizations'")
prob_output = run_main()
SiteGmf = prob_output["SiteGmf"]
run_main_flag = False
GetDistributions = tools.GetDistributions(SiteGmf, EnsembleSize, Lon_Event, Lat_Event, args.numGMPEsRealizations, event_dir,
args.imt, args.station_file, args.imt_min, args.imt_max, args.fileScenariosWeights,
args.pois_file, args.pois_subset, args.n_pois, args.max_distance,
args.pois_selection_method, args.deg_round, args.num_processes, pois_subset_flag)
GetDistributions.plot_distributions()
pois_subset_flag = False
elif tool == 'EnsemblePlot':
if args.pois_subset and not args.pois_selection_method:
raise TypeError("Missing required argument 'pois_selection_method'")
if args.pois_selection_method == 'azimuth_uniform':
if args.n_pois % 4 > 0:
raise TypeError("Select a number of POIs divisible by 4")
logging.info(f"Prob Tool: {tool}")
logging.info(f"Intensity Measure: {args.imt}")
logging.info(f"Scenarios Ensemble File: {scenarios_file[0]}")
logging.info(f"POIs File: {args.pois_file}")
logging.info(f"Num GMPEsRealizations: {args.numGMPEsRealizations}")
logging.info(f"File Scenarios Weights: {args.fileScenariosWeights}")
logging.info(f"Deg round: {args.deg_round}")
logging.info(f"POIs Subset: {args.pois_subset}")
if args.pois_subset:
logging.info(f"Num POIs in the Subset: {args.n_pois}")
logging.info(f"Max Distance: {args.max_distance}")
logging.info(f"POIs Selection Methos: {args.pois_selection_method}")
if run_main_flag:
if args.pois_file is None:
raise TypeError("Missing required argument 'pois_file'")
if args.imt is None:
raise TypeError("Missing required argument 'imt'")
if args.numGMPEsRealizations is None:
raise TypeError("Missing required argument 'numGMPEsRealizations'")
prob_output = run_main()
SiteGmf = prob_output["SiteGmf"]
run_main_flag = False
EnsemblePlot = tools.EnsemblePlot(SiteGmf, args.imt, Lon_Event, Lat_Event, EnsembleSize, args.numGMPEsRealizations, args.fileScenariosWeights, args.pois_file,
args.pois_subset, args.n_pois, args.max_distance, args.deg_round, args.pois_selection_method, args.num_processes, pois_subset_flag)
EnsemblePlot.plot()
pois_subset_flag = False
else:
print('No tool specified')
print("********* DONE! *******")
print("--- %s seconds ---" % (time.time() - start_time))