forked from gem/oq-engine
/
oqscript.py
executable file
·545 lines (456 loc) · 17.9 KB
/
oqscript.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
"""OpenQuake: software for seismic hazard and risk assessment
It receives its inputs through a configuration file plus input data in .xml
format and stores the results in .xml format.
Available Hazard Analysis
Classical PSHA
Input Source Model Logic Tree
GMPE Logic Tree
Output Hazard maps
Hazard curves
Event-Based PSHA
Input Source Model Logic Tree
GMPE Logic Tree
Output Ground Motion fields
Scenario SHA
Input Rupture Model
Output Ground Motion fields
Available Risk Analysis
Classical PSHA-based
Input Exposure (a value per asset)
Vulnerability curves (a list of vulnerability functions)
Seismic hazard input: hazard curves
Output A grid of loss-ratio curves
A grid of loss curves
A map of losses at each interval
Probabilistic event-based
Input Exposure (a value per asset)
Vulnerability curves (a list of vulnerability functions)
Seismic hazard input: sets of ground motion fields
Output A grid of loss-ratio curves
A grid of loss curves
A map of losses at each interval
An aggregated loss curve
"""
import argparse
import getpass
import os
import sys
from os.path import abspath
from os.path import dirname
from os.path import expanduser
from os.path import join
from django.core.exceptions import ObjectDoesNotExist
# just in the case that are you using oq-engine from sources
# with the rest of oq libraries installed into the system (or a
# virtual environment) you must set this environment variable
if os.environ.get("OQ_ENGINE_USE_SRCDIR") is not None:
sys.modules['openquake'].__dict__["__path__"].insert(
0, join(dirname(dirname(__file__)), "openquake")
)
from openquake.engine.utils import config
config.abort_if_no_config_available()
try:
import celeryconfig
except ImportError:
sys.path.append('/usr/openquake/engine')
try:
import oqpath
oqpath.set_oq_path()
except ImportError:
pass
from openquake.engine.db.models import set_django_settings_module
set_django_settings_module()
import openquake.engine
from openquake.engine import __version__
from openquake.engine import engine
from openquake.engine.db import models
from openquake.engine.export import hazard as hazard_export
from openquake.engine.export import risk as risk_export
from openquake.engine.input import source
from openquake.engine.tools.import_gmf_scenario import import_gmf_scenario
from openquake.engine.tools.import_hazard_curves import import_hazard_curves
from openquake.engine.tools import dump_hazards, restore_hazards
HAZARD_OUTPUT_ARG = "--hazard-output-id"
HAZARD_CALCULATION_ARG = "--hazard-calculation-id"
MISSING_HAZARD_MSG = ("Please specify the ID of the hazard output (or "
"calculation) to be used by using '%s (or %s) <id>'" %
(HAZARD_OUTPUT_ARG, HAZARD_CALCULATION_ARG))
def set_up_arg_parser():
"""Set up and return an :class:`argparse.ArgumentParser` with all of the
OpenQuake command line options."""
parser = argparse.ArgumentParser(
description='OpenQuake Seismic Hazard and Risk Analysis Engine')
general_grp = parser.add_argument_group('General')
general_grp.add_argument(
'--version', action='store_true', help='Display version information')
general_grp.add_argument(
'--log-file', '-L',
help=('Location to store log messages; if not specified, log messages'
' will be printed to the console (to stderr)'),
required=False, metavar='LOG_FILE')
general_grp.add_argument(
'--log-level', '-l',
help='Defaults to "progress"', required=False,
choices=['debug', 'info', 'progress', 'warn', 'error', 'critical'],
default='progress')
general_grp.add_argument(
'--no-distribute', '--nd', action='store_true',
help=('Disable calculation task distribution and run the computation'
' in a single process. This is intended for use in debugging '
' and profiling.')
)
general_grp.add_argument(
'--list-inputs', '--li',
help='List inputs of a specific input type',
metavar="INPUT_TYPE")
general_grp.add_argument(
'--yes', '-y', action='store_true',
help='Automatically answer "yes" when asked to confirm an action'
)
general_grp.add_argument(
'--config-file',
help='Custom openquake.cfg file, to override default configurations',
metavar='CONFIG_FILE'
)
hazard_grp = parser.add_argument_group('Hazard')
hazard_grp.add_argument(
'--run-hazard',
'--rh',
help='Run a hazard job with the specified config file',
metavar='CONFIG_FILE')
hazard_grp.add_argument(
'--list-hazard-calculations',
'--lhc',
help='List hazard calculation information',
action='store_true')
hazard_grp.add_argument(
'--list-hazard-outputs',
'--lho',
help='List outputs for the specified hazard calculation',
metavar='HAZARD_CALCULATION_ID')
hazard_grp.add_argument(
'--export-hazard',
'--eh',
help='Export the desired output to the specified directory',
nargs=2, metavar=('OUTPUT_ID', 'TARGET_DIR'))
hazard_grp.add_argument(
'--delete-hazard-calculation',
'--dhc',
help='Delete a hazard calculation and all associated outputs',
metavar='HAZARD_CALCULATION_ID')
hazard_grp.add_argument(
'--delete-uncompleted-calculations',
'--duc',
action='store_true',
help='Delete all the uncompleted calculations')
risk_grp = parser.add_argument_group('Risk')
risk_grp.add_argument(
'--run-risk',
'--rr',
help='Run a risk job with the specified config file',
metavar='CONFIG_FILE')
risk_grp.add_argument(
HAZARD_OUTPUT_ARG,
'--ho',
help='Use the desired hazard output as input for the risk job',
metavar='HAZARD_OUTPUT')
risk_grp.add_argument(
HAZARD_CALCULATION_ARG,
'--hc',
help='Use the desired hazard calculation as input for the risk job',
metavar='HAZARD_CALCULATION_ID')
risk_grp.add_argument(
'--list-risk-calculations',
'--lrc',
help='List risk calculation information',
action='store_true')
risk_grp.add_argument(
'--list-risk-outputs',
'--lro',
help='List outputs for the specified risk calculation',
metavar='RISK_CALCULATION_ID')
risk_grp.add_argument(
'--export-risk',
'--er',
help='Export the desired risk output to the specified directory',
nargs=2,
metavar=('OUTPUT_ID', 'TARGET_DIR'))
risk_grp.add_argument(
'--delete-risk-calculation',
'--drc',
help='Delete a risk calculation and all associated outputs',
metavar='RISK_CALCULATION_ID')
export_grp = parser.add_argument_group('Export')
export_grp.add_argument(
'--exports', choices=['xml'], default=[], action="append",
help=(
'Use with --run-hazard and --run-risk to automatically export '
'all calculation results to the specified format. Only "xml" is '
'supported currently. This is optional. If not specified, '
'nothing will be exported; results will only be stored in the '
'database.'
)
)
export_grp.add_argument(
'--export-type', '--et',
choices=['xml', 'geojson'],
default='xml',
action='store',
help=('Use with --export-hazard or --export-risk, specify the '
'desired output format. Defaults to "xml".')
)
dump_restore_grp = parser.add_argument_group('Dump/Restore')
dump_restore_grp.add_argument(
'--dump-hazard-calculation',
help=('Dump a hazard calculation to a new created directory.'),
nargs=2, metavar=('HAZARD_CALCULATION_ID', 'DUMP_DIR'))
dump_restore_grp.add_argument(
'--restore-hazard-calculation',
help=("Restore a hazard calculation from a dump. "
"Only SES outputs currently supported"),
metavar=('DUMP_DIR'))
import_grp = parser.add_argument_group('Import')
import_grp.add_argument(
'--load-gmf',
help=('Load gmf from a file. Only single-source gmf are supported '
'currently. The file can be xml or tab-separated.'),
metavar='GMF_FILE',
)
import_grp.add_argument(
'--load-curve',
help=('Load hazard curves from an XML file.'),
metavar='CURVE_FILE',
)
import_grp.add_argument(
'--list-imported-outputs', action='store_true',
help=('List outputs which were imported from a file, not calculated '
'from a job'))
pre_proc_grp = parser.add_argument_group('Pre-processing')
pre_proc_grp.add_argument(
'--optimize-source-model',
'--osm',
help=('Optimize a source model by discretizating an area source into'
' point sources, given a discretization step (in km)'),
nargs=3,
metavar=('INPUT_FILE', 'AREA_DISCRETIZATION', 'OUTPUT_FILE')
)
return parser
def list_inputs(input_type):
"""
Print a list of available input models
"""
if input_type == "exposure":
model = models.ExposureModel
else:
engine.complain_and_exit(
"Wrong input type. Available input types: exposure")
inputs = model.objects.all()
if not inputs.count():
print "No inputs found of type %s" % input_type
return
print ('model id | name')
for inp in inputs:
print "%9d|%s" % (inp.id, inp.name)
def list_calculations(calc_manager):
"""
Print a summary of past calculations.
:param calc_manager:
a django manager (e.g.
:class:`openquake.engine.db.models.RiskCalculation.objects`)
which provides calculation instances
"""
# FIXME(lp). As it might happen to have a calculation instance
# without a OqJob instance (e.g. when the user imports outputs
# directly from files) we filter out the calculation without the
# corresponding job
calcs = calc_manager.filter(
oqjob__user_name=getpass.getuser(),
oqjob__isnull=False).order_by('oqjob__last_update')
if len(calcs) == 0:
print 'None'
else:
print ('calc_id | status | last_update | '
'description')
for calc in calcs:
latest_job = calc.oqjob
if latest_job.is_running:
status = 'pending'
else:
if latest_job.status == 'complete':
status = 'successful'
else:
status = 'failed'
last_update = latest_job.last_update.strftime(
'%Y-%m-%d %H:%M:%S %Z'
)
print '%s | %s | %s | %s' % (
calc.id, status, last_update, calc.description
)
def list_imported_outputs():
"""
List outputs which were imported from a file, not calculated from a job
"""
outputs = models.Output.objects.filter(oq_job__isnull=True)
engine.print_outputs_summary(outputs)
def export_hazard(haz_output_id, target_dir, export_type):
export(hazard_export.export, haz_output_id, target_dir, export_type)
def export_risk(risk_output_id, target_dir, export_type):
export(risk_export.export, risk_output_id, target_dir, export_type)
def export(fn, output_id, target_dir, export_type):
"""
Simple UI wrapper around
:func:`openquake.engine.export.hazard.export` which prints a summary
of files exported, if any.
"""
queryset = models.Output.objects.filter(pk=output_id)
if not queryset.exists():
print 'No output found for OUTPUT_ID %s' % output_id
return
if queryset.all()[0].oq_job.status != "complete":
print ("Exporting output produced by a job which did not run "
"successfully. Results might be uncomplete")
try:
the_file = fn(output_id, target_dir, export_type)
print 'File Exported:'
print the_file
except NotImplementedError, err:
print err.message
print 'This feature is probably not implemented yet'
def _touch_log_file(log_file):
"""If a log file destination is specified, attempt to open the file in
'append' mode ('a'). If the specified file is not writable, an
:exc:`IOError` will be raised."""
open(abspath(log_file), 'a').close()
def delete_uncompleted_calculations():
for rc in models.RiskCalculation.objects.filter(
oqjob__user_name=getpass.getuser()).exclude(
oqjob__status="successful"):
del_risk_calc(rc.id, True)
for hc in models.HazardCalculation.objects.filter(
oqjob__user_name=getpass.getuser()).exclude(
oqjob__status="successful"):
del_haz_calc(hc.id, True)
def del_haz_calc(hc_id, confirmed=False):
"""
Delete a hazard calculation and all associated outputs.
"""
if confirmed or confirm(
'Are you sure you want to delete this hazard calculation and all '
'associated outputs?\nThis action cannot be undone. (y/n): '):
try:
engine.del_haz_calc(hc_id)
except RuntimeError, err:
print err.message
def del_risk_calc(rc_id, confirmed=False):
"""
Delete a risk calculation and all associated outputs.
"""
if confirmed or confirm(
'Are you sure you want to delete this risk calculation and all '
'associated outputs?\nThis action cannot be undone. (y/n): '):
try:
engine.del_risk_calc(rc_id)
except RuntimeError, err:
print err.message
def confirm(prompt):
"""
Ask for confirmation, given a ``prompt`` and return a boolean value.
"""
while True:
try:
answer = raw_input(prompt)
except KeyboardInterrupt:
# the user presses ctrl+c, just say 'no'
return False
answer = answer.strip().lower()
if answer not in ('y', 'n'):
print 'Please enter y or n'
continue
return answer == 'y'
def optimize_source_model(input_path, area_src_disc, output_path):
"""
Read a source model and generate a new one, converting area sources to
point sources.
"""
area_src_disc = float(area_src_disc)
source.optimize_source_model(input_path, area_src_disc,
output_path)
engine.complain_and_exit(output_path)
def main():
arg_parser = set_up_arg_parser()
args = arg_parser.parse_args()
if args.version:
engine.complain_and_exit(__version__)
if args.config_file:
os.environ[config.OQ_CONFIG_FILE_VAR] = \
abspath(expanduser(args.config_file))
config.refresh()
if args.optimize_source_model:
optimize_source_model(*args.optimize_source_model)
if args.no_distribute:
os.environ[openquake.engine.NO_DISTRIBUTE_VAR] = '1'
if args.list_inputs:
list_inputs(args.list_inputs)
# hazard
elif args.list_hazard_calculations:
list_calculations(models.HazardCalculation.objects)
elif args.list_hazard_outputs is not None:
engine.list_hazard_outputs(args.list_hazard_outputs)
elif args.export_hazard is not None:
output_id, target_dir = args.export_hazard
output_id = int(output_id)
export_hazard(output_id, expanduser(target_dir), args.export_type)
elif args.run_hazard is not None:
log_file = expanduser(args.log_file) \
if args.log_file is not None else None
engine.run_hazard(expanduser(args.run_hazard), args.log_level,
log_file, args.exports)
elif args.delete_hazard_calculation is not None:
del_haz_calc(args.delete_hazard_calculation, args.yes)
# risk
elif args.list_risk_calculations:
list_calculations(models.RiskCalculation.objects)
elif args.list_risk_outputs is not None:
engine.list_risk_outputs(args.list_risk_outputs)
elif args.export_risk is not None:
output_id, target_dir = args.export_risk
export_risk(output_id, expanduser(target_dir), args.export_type)
elif args.run_risk is not None:
if (args.hazard_output_id is None
and args.hazard_calculation_id is None):
engine.complain_and_exit(MISSING_HAZARD_MSG)
log_file = expanduser(args.log_file) \
if args.log_file is not None else None
engine.run_risk(expanduser(args.run_risk), args.log_level, log_file,
args.exports, hazard_output_id=args.hazard_output_id,
hazard_calculation_id=args.hazard_calculation_id)
elif args.delete_risk_calculation is not None:
del_risk_calc(args.delete_risk_calculation, args.yes)
# import
elif args.load_gmf is not None:
with open(args.load_gmf) as f:
out, hc = import_gmf_scenario(f)
print 'Added output id=%d of type %s; hazard_calculation_id=%d'\
% (out.id, out.output_type, hc.id)
elif args.load_curve is not None:
with open(args.load_curve) as f:
out, hc = import_hazard_curves(f)
print 'Added output id=%d of type %s; hazard_calculation_id=%d'\
% (out.id, out.output_type, hc.id)
elif args.list_imported_outputs:
list_imported_outputs()
elif args.delete_uncompleted_calculations:
delete_uncompleted_calculations()
elif args.dump_hazard_calculation:
dump_hazards.main(*args.dump_hazard_calculation)
elif args.restore_hazard_calculation:
hc_ids = restore_hazards.django_restore(
args.restore_hazard_calculation)
print "Restore hazard calculation with IDs: %s" % hc_ids
else:
arg_parser.print_usage()
if __name__ == '__main__':
main()