Exemplo n.º 1
0
def upgrade_file(path, multipoint):
    """Upgrade to the latest NRML version"""
    node0 = nrml.read(path)[0]
    shutil.copy(path, path + '.bak')  # make a backup of the original file
    tag = striptag(node0.tag)
    gml = True
    if tag == 'vulnerabilityModel':
        vf_dict, cat_dict = get_vulnerability_functions_04(path)
        # below I am converting into a NRML 0.5 vulnerabilityModel
        node0 = Node('vulnerabilityModel',
                     cat_dict,
                     nodes=[obj_to_node(val) for val in vf_dict.values()])
        gml = False
    elif tag == 'fragilityModel':
        node0 = read_nrml.convert_fragility_model_04(nrml.read(path)[0], path)
        gml = False
    elif tag == 'sourceModel':
        node0 = nrml.read(path)[0]
        dic = groupby(node0.nodes, operator.itemgetter('tectonicRegion'))
        node0.nodes = [
            Node('sourceGroup',
                 dict(tectonicRegion=trt, name="group %s" % i),
                 nodes=srcs) for i, (trt, srcs) in enumerate(dic.items(), 1)
        ]
        if multipoint:
            sourceconverter.update_source_model(node0, path + '.bak')
    with open(path, 'wb') as f:
        nrml.write([node0], f, gml=gml)
Exemplo n.º 2
0
def upgrade_file(path, multipoint):
    """Upgrade to the latest NRML version"""
    node0 = nrml.read(path, chatty=False)[0]
    shutil.copy(path, path + '.bak')  # make a backup of the original file
    tag = striptag(node0.tag)
    gml = True
    if tag == 'vulnerabilityModel':
        vf_dict, cat_dict = get_vulnerability_functions_04(path)
        # below I am converting into a NRML 0.5 vulnerabilityModel
        node0 = Node(
            'vulnerabilityModel', cat_dict,
            nodes=[obj_to_node(val) for val in vf_dict.values()])
        gml = False
    elif tag == 'fragilityModel':
        node0 = read_nrml.convert_fragility_model_04(
            nrml.read(path)[0], path)
        gml = False
    elif tag == 'sourceModel':
        node0 = nrml.read(path)[0]
        dic = groupby(node0.nodes, operator.itemgetter('tectonicRegion'))
        node0.nodes = [Node('sourceGroup',
                            dict(tectonicRegion=trt, name="group %s" % i),
                            nodes=srcs)
                       for i, (trt, srcs) in enumerate(dic.items(), 1)]
        if multipoint:
            sourceconverter.update_source_model(node0, path + '.bak')
    with open(path, 'wb') as f:
        nrml.write([node0], f, gml=gml)
Exemplo n.º 3
0
 def test_simple(self):
     testfile = os.path.join(testdir, 'two-point-sources.xml')
     sm = nrml.read(testfile).sourceModel
     update_source_model(sm, testfile)
     with io.BytesIO() as f:
         nrml.write(sm, f)
         got = f.getvalue().decode('utf-8')
         self.assertEqual(got, expected)
Exemplo n.º 4
0
 def test_simple(self):
     testfile = os.path.join(testdir, 'two-point-sources.xml')
     sm = nrml.read(testfile).sourceModel
     update_source_model(sm, testfile)
     with io.BytesIO() as f:
         nrml.write(sm, f)
         got = f.getvalue().decode('utf-8')
         self.assertEqual(got, expected)
Exemplo n.º 5
0
 def test_complex(self):
     testfile = os.path.normpath(os.path.join(
         testdir, '../../../qa_tests_data/classical/case_30/ssm/shallow/'
         'gridded_seismicity_source_4.xml'))
     sm = nrml.read(testfile).sourceModel
     update_source_model(sm, testfile)
     with io.BytesIO() as f:
         nrml.write(sm, f)
         got = f.getvalue().decode('utf-8')
         self.assertEqual(got, multipoint)
Exemplo n.º 6
0
 def test_complex(self):
     testfile = os.path.normpath(os.path.join(
         testdir, '../../../qa_tests_data/classical/case_30/ssm/shallow/'
         'gridded_seismicity_source_4.xml'))
     sm = nrml.read(testfile).sourceModel
     update_source_model(sm, testfile)
     with io.BytesIO() as f:
         nrml.write(sm, f)
         got = f.getvalue().decode('utf-8')
         self.assertEqual(got, multipoint)
Exemplo n.º 7
0
def main(directory, dry_run=False, multipoint=False):
    """
    Upgrade all the NRML files contained in the given directory to the latest
    NRML version. Works by walking all subdirectories.
    WARNING: there is no downgrade!
    """
    for cwd, dirs, files in os.walk(directory):
        for f in files:
            path = os.path.join(cwd, f)
            if f.endswith('.xml'):
                ip = iterparse(path, events=('start', ))
                next(ip)  # read node zero
                try:
                    fulltag = next(ip)[1].tag  # tag of the first node
                    xmlns, tag = fulltag.split('}')
                except Exception:  # not a NRML file
                    xmlns, tag = '', ''
                if xmlns[1:] == NRML05:  # already upgraded
                    if 'sourceModel' in tag and multipoint:
                        print('upgrading to multiPointSources', path)
                        node0 = nrml.read(path)[0]
                        sourceconverter.update_source_model(node0, path)
                        with open(path, 'wb') as f:
                            nrml.write([node0], f, gml=True)
                elif 'nrml/0.4' in xmlns and ('vulnerability' in tag
                                              or 'fragility' in tag
                                              or 'sourceModel' in tag):
                    if not dry_run:
                        print('Upgrading', path)
                        try:
                            upgrade_file(path, multipoint)
                        except Exception as exc:
                            raise
                            print(exc)
                    else:
                        print('Not upgrading', path)
Exemplo n.º 8
0
def upgrade_nrml(directory, dry_run, multipoint):
    """
    Upgrade all the NRML files contained in the given directory to the latest
    NRML version. Works by walking all subdirectories.
    WARNING: there is no downgrade!
    """
    for cwd, dirs, files in os.walk(directory):
        for f in files:
            path = os.path.join(cwd, f)
            if f.endswith('.xml'):
                ip = iterparse(path, events=('start',))
                next(ip)  # read node zero
                try:
                    fulltag = next(ip)[1].tag  # tag of the first node
                    xmlns, tag = fulltag.split('}')
                except Exception:  # not a NRML file
                    xmlns, tag = '', ''
                if xmlns[1:] == NRML05:  # already upgraded
                    if 'sourceModel' in tag and multipoint:
                        print('upgrading to multiPointSources', path)
                        node0 = nrml.read(path)[0]
                        sourceconverter.update_source_model(node0, path)
                        with open(path, 'wb') as f:
                            nrml.write([node0], f, gml=True)
                elif 'nrml/0.4' in xmlns and (
                        'vulnerability' in tag or 'fragility' in tag or
                        'sourceModel' in tag):
                    if not dry_run:
                        print('Upgrading', path)
                        try:
                            upgrade_file(path, multipoint)
                        except Exception as exc:
                            raise
                            print(exc)
                    else:
                        print('Not upgrading', path)