Skip to content

Commit

Permalink
Added command oq renumber_sm [skip CI]
Browse files Browse the repository at this point in the history
  • Loading branch information
micheles committed Jul 7, 2020
1 parent 5692603 commit a52e634
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 92 deletions.
46 changes: 23 additions & 23 deletions demos/risk/ClassicalBCR/source_model.xml
Expand Up @@ -11,7 +11,7 @@ xmlns:gml="http://www.opengis.net/gml"
tectonicRegion="Active Shallow Crust"
>
<areaSource
id="224"
id="1"
name="Source 8.CH.244"
>
<areaGeometry>
Expand Down Expand Up @@ -46,7 +46,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="226"
id="2"
name="Source 8.CH.249"
>
<areaGeometry>
Expand Down Expand Up @@ -81,7 +81,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="227"
id="3"
name="Source 8.CH.250"
>
<areaGeometry>
Expand Down Expand Up @@ -116,7 +116,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="228"
id="4"
name="Source 8.CH.251"
>
<areaGeometry>
Expand Down Expand Up @@ -151,7 +151,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="229"
id="5"
name="Source 8.CH.252"
>
<areaGeometry>
Expand Down Expand Up @@ -186,7 +186,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="230"
id="6"
name="Source 8.CH.253"
>
<areaGeometry>
Expand Down Expand Up @@ -221,7 +221,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="231"
id="7"
name="Source 8.CH.254"
>
<areaGeometry>
Expand Down Expand Up @@ -256,7 +256,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="232"
id="8"
name="Source 8.CH.255"
>
<areaGeometry>
Expand Down Expand Up @@ -291,7 +291,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="233"
id="9"
name="Source 8.CH.256"
>
<areaGeometry>
Expand Down Expand Up @@ -326,7 +326,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="234"
id="10"
name="Source 8.CH.257"
>
<areaGeometry>
Expand Down Expand Up @@ -361,7 +361,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="235"
id="11"
name="Source 8.CH.258"
>
<areaGeometry>
Expand Down Expand Up @@ -396,7 +396,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="236"
id="12"
name="Source 8.CH.259"
>
<areaGeometry>
Expand Down Expand Up @@ -431,7 +431,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="238"
id="13"
name="Source 8.CH.261"
>
<areaGeometry>
Expand Down Expand Up @@ -466,7 +466,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="239"
id="14"
name="Source 8.CH.262"
>
<areaGeometry>
Expand Down Expand Up @@ -501,7 +501,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="240"
id="15"
name="Source 8.CH.263"
>
<areaGeometry>
Expand Down Expand Up @@ -536,7 +536,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="243"
id="16"
name="Source 8.CH.267"
>
<areaGeometry>
Expand Down Expand Up @@ -571,7 +571,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="244"
id="17"
name="Source 8.CH.268"
>
<areaGeometry>
Expand Down Expand Up @@ -606,7 +606,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="272"
id="18"
name="Source 8.CH.300"
>
<areaGeometry>
Expand Down Expand Up @@ -641,7 +641,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="279"
id="19"
name="Source 8.CH.404"
>
<areaGeometry>
Expand Down Expand Up @@ -676,7 +676,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="280"
id="20"
name="Source 8.CH.405"
>
<areaGeometry>
Expand Down Expand Up @@ -711,7 +711,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="374"
id="21"
name="Source 8.CH.248"
>
<areaGeometry>
Expand Down Expand Up @@ -746,7 +746,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="376"
id="22"
name="Source 8.CH.265"
>
<areaGeometry>
Expand Down Expand Up @@ -781,7 +781,7 @@ xmlns:gml="http://www.opengis.net/gml"
</hypoDepthDist>
</areaSource>
<areaSource
id="386"
id="23"
name="Source 8.CH.245"
>
<areaGeometry>
Expand Down
52 changes: 52 additions & 0 deletions openquake/commands/renumber_sm.py
@@ -0,0 +1,52 @@
import logging
import operator
import collections
from openquake.baselib import sap, parallel, general
from openquake.commonlib import logictree
from openquake.hazardlib import nrml


Source = collections.namedtuple('Source', 'id node value')


def read_sm(fname):
srcs = []
root = nrml.read(fname)
if root['xmlns'] == 'http://openquake.org/xmlns/nrml/0.4':
srcs.extend(root[0])
else:
for srcgroup in root[0]:
srcs.extend(srcgroup)
sources = [Source(src['id'], src, src.to_str()) for src in srcs]
return root[0], fname, sources


@sap.Script
def renumber_sm(smlt_file):
"""
Renumber the sources belonging to the same source model, even if split
in multiple files, to avoid duplicated source IDs. NB: it changes the
XML files in place, without making a backup, so be careful.
"""
logging.basicConfig(level=logging.INFO)
smpaths = logictree.collect_info(smlt_file).smpaths
smap = parallel.Starmap(read_sm, [(path,) for path in smpaths])
smodel, srcs = {}, []
for sm, fname, sources in smap:
smodel[fname] = sm
srcs.extend(sources)
dic = general.groupby(srcs, operator.attrgetter('id', 'value'))
n = 1
for sources in dic.values():
for src in sources:
src.node['id'] = str(n)
n += 1
for fname, sm in smodel.items():
with open(fname, 'wb') as f:
nrml.write([sm], f)


renumber_sm.arg('smlt_file', 'source model logic tree file')

if __name__ == '__main__':
renumber_sm.callfunc()
69 changes: 0 additions & 69 deletions utils/renumber_sources

This file was deleted.

0 comments on commit a52e634

Please sign in to comment.