From a52e634edf56cb38a6f6c2bda799e01c184735ce Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Tue, 7 Jul 2020 06:05:19 +0200 Subject: [PATCH] Added command oq renumber_sm [skip CI] --- demos/risk/ClassicalBCR/source_model.xml | 46 ++++++++-------- openquake/commands/renumber_sm.py | 52 ++++++++++++++++++ utils/renumber_sources | 69 ------------------------ 3 files changed, 75 insertions(+), 92 deletions(-) create mode 100644 openquake/commands/renumber_sm.py delete mode 100755 utils/renumber_sources diff --git a/demos/risk/ClassicalBCR/source_model.xml b/demos/risk/ClassicalBCR/source_model.xml index e9c8c674fc14..cff487613c3c 100644 --- a/demos/risk/ClassicalBCR/source_model.xml +++ b/demos/risk/ClassicalBCR/source_model.xml @@ -11,7 +11,7 @@ xmlns:gml="http://www.opengis.net/gml" tectonicRegion="Active Shallow Crust" > @@ -46,7 +46,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -81,7 +81,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -116,7 +116,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -151,7 +151,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -186,7 +186,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -221,7 +221,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -256,7 +256,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -291,7 +291,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -326,7 +326,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -361,7 +361,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -396,7 +396,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -431,7 +431,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -466,7 +466,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -501,7 +501,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -536,7 +536,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -571,7 +571,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -606,7 +606,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -641,7 +641,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -676,7 +676,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -711,7 +711,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -746,7 +746,7 @@ xmlns:gml="http://www.opengis.net/gml" @@ -781,7 +781,7 @@ xmlns:gml="http://www.opengis.net/gml" diff --git a/openquake/commands/renumber_sm.py b/openquake/commands/renumber_sm.py new file mode 100644 index 000000000000..b1c2c1d1cb6b --- /dev/null +++ b/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() diff --git a/utils/renumber_sources b/utils/renumber_sources deleted file mode 100755 index cafd43496b00..000000000000 --- a/utils/renumber_sources +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -# vim: tabstop=4 shiftwidth=4 softtabstop=4 -# -# Copyright (C) 2018-2019 GEM Foundation -# -# OpenQuake is free software: you can redistribute it and/or modify it -# under the terms of the GNU Affero General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# OpenQuake is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with OpenQuake. If not, see . -import logging -from openquake.baselib import sap -from openquake.commonlib import logictree -from openquake.hazardlib import nrml - - -class ObsoleteFormat(Exception): - pass - - -def _renumber(paths): - srcs = [] - roots = [] - number = 1 - for path in paths: - logging.info('Reading %s', path) - root = nrml.read(path) - if root['xmlns'] == 'http://openquake.org/xmlns/nrml/0.4': - raise ObsoleteFormat('Please use oq upgrade_nrml .') - srcs.extend(src['id'] for sgroup in root[0] for src in sgroup) - roots.append(root) - if len(srcs) == len(set(srcs)): - # there are no duplicated source IDs - return - for path, root in zip(paths, roots): - logging.info('Renumbering %s', path) - for sgroup in root[0]: - for src in sgroup: - src['id'] = str(number) - number += 1 - with open(path, 'wb') as f: - nrml.write(root, f) - return number - - -@sap.Script -def renumber_sources(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 - _renumber(smpaths) - - -renumber_sources.arg('smlt_file', 'source model logic tree file') - -if __name__ == '__main__': - renumber_sources.callfunc()