Skip to content

Commit 86ad4ee

Browse files
authored
Merge pull request #10410 from gem/mmi_values
Storing `mmi_values` in pre_execute
2 parents 242ab76 + 2df3f7f commit 86ad4ee

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

openquake/calculators/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ def pre_execute(self):
728728
with self.monitor('importing inputs', measuremem=True):
729729
self.read_inputs()
730730
self.save_crmodel()
731+
if oq.impact and 'mmi' in oq.inputs:
732+
logging.info('Computing MMI-aggregated values')
733+
if mmi_values := self.assetcol.get_mmi_values(
734+
oq.aggregate_by, oq.inputs['mmi']):
735+
self.datastore['mmi_values'] = mmi_values
731736

732737
def pre_execute_from_parent(self):
733738
"""

openquake/commonlib/oqvalidation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,7 @@ def ruptures_hdf5(self):
18861886
@property
18871887
def impact(self):
18881888
"""
1889-
Return True if we are in Aristotle mode, i.e. there is an HDF5
1889+
Return True if we are in OQImpact mode, i.e. there is an HDF5
18901890
exposure with a known structure
18911891
"""
18921892
exposures = self.inputs.get('exposure', [])
@@ -1895,7 +1895,8 @@ def impact(self):
18951895
if not self.quantiles:
18961896
self.quantiles = [0.05, 0.95]
18971897
if not self.aggregate_by:
1898-
self.aggregate_by = [['ID_1'], ['OCCUPANCY']]
1898+
# self.aggregate_by = [['ID_1'], ['OCCUPANCY']]
1899+
self.aggregate_by = [['ID_1']]
18991900
return yes
19001901

19011902
@property

openquake/engine/tests/impact_test.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import pathlib
2121
import unittest
2222
import pytest
23-
import fiona
24-
from shapely.geometry import shape
2523
from openquake.calculators.checkers import check
2624
from openquake.calculators.export import export
2725

@@ -56,17 +54,9 @@ def test_impact(n):
5654

5755

5856
def test_impact5():
59-
# NB: expecting exposure in oq-engine and not in mosaic_dir!
57+
# this is a case where there are no assets inside the MMI multipolygons
6058
if not os.path.exists(expo := cd.parent.parent.parent / 'exposure.hdf5'):
6159
raise unittest.SkipTest(f'Missing {expo}')
6260

6361
# importing the exposure around Nepal and aggregating it
64-
calc = check(cd / 'impact5/job.ini')
65-
agg_values = calc.assetcol.get_agg_values
66-
67-
# this is a case where there are no assets inside the MMI multipolygons
68-
shapes = calc.oqparam.inputs['mmi']
69-
with fiona.open(f'zip://{shapes}!mi.shp') as f:
70-
for feat in f:
71-
values = agg_values([['ID_1']], shape(feat.geometry))
72-
assert values['number'].sum() == 0
62+
check(cd / 'impact5/job.ini')

openquake/risklib/asset.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
import numpy
2626
import pandas
27-
from shapely import contains_xy
28-
27+
import fiona
28+
from shapely import geometry, contains_xy
2929

3030
from openquake.baselib import hdf5, general, config
3131
from openquake.baselib.node import Node, context
@@ -48,6 +48,19 @@
4848
'business_interruption'}
4949

5050

51+
def to_mmi(value, MMIs=('I', 'II', 'III', 'IV', 'V', 'VI', 'VII',
52+
'VIII', 'IX', 'X')):
53+
"""
54+
:param value: float in the range 1..10
55+
:returns: string "I" .. "X" representing a MMI
56+
"""
57+
if value >= 10.5:
58+
raise ValueError(f'{value} is too large to be an MMI')
59+
elif value < 0.5:
60+
raise ValueError(f'{value} is too small to be an MMI')
61+
return MMIs[round(value) - 1]
62+
63+
5164
def add_dupl_fields(df, oqfields):
5265
"""
5366
Add duplicated fields to the DataFrame, if any.
@@ -476,6 +489,25 @@ def get_agg_values(self, aggregate_by, geometry=None):
476489
agg_values[K] = tuple(dataf[vfields].sum())
477490
return agg_values
478491

492+
def get_mmi_values(self, aggregate_by, mmi_file):
493+
"""
494+
:param aggregate_by:
495+
a list of lists of tag names (i.e. [['NAME_1']])
496+
:param mmi_file:
497+
shapefile containing MMI geometries and values
498+
:returns:
499+
a dictionary MMI -> array with the value fields
500+
"""
501+
out = {}
502+
with fiona.open(f'zip://{mmi_file}!mi.shp') as f:
503+
for feat in f:
504+
geom = geometry.shape(feat.geometry)
505+
mmi = to_mmi(feat.properties['PARAMVALUE'])
506+
values = self.get_agg_values(aggregate_by, geom)
507+
if values['number'].any():
508+
out[mmi] = values
509+
return out
510+
479511
def build_aggids(self, aggregate_by):
480512
"""
481513
:param aggregate_by: list of Ag lists of strings

0 commit comments

Comments
 (0)