Пример #1
0
    def test_xml(self):
        # can read and write a .xml file converted into a Node object
        xmlfile = io.StringIO(u"""\
<root>
<general>
<a>1</a>
<b>2</b>
</general>
<section1 param="xxx" />
<section2 param="yyy" />
</root>
""")
        node = n.node_from_xml(xmlfile)
        outfile = io.BytesIO()
        n.node_to_xml(node, outfile)
        self.assertEqual(
            outfile.getvalue(), b"""\
<?xml version="1.0" encoding="utf-8"?>
<root>
    <general>
        <a>
            1
        </a>
        <b>
            2
        </b>
    </general>
    <section1 param="xxx"/>
    <section2 param="yyy"/>
</root>
""")
Пример #2
0
def _get_shakemap_array(xml_file):
    if hasattr(xml_file, 'read'):
        data = io.BytesIO(xml_file.read())
    else:
        data = open(xml_file)
    grid_node = node_from_xml(data)
    fields = grid_node.getnodes('grid_field')
    lines = grid_node.grid_data.text.strip().splitlines()
    rows = [line.split() for line in lines]

    # the indices start from 1, hence the -1 below
    idx = {
        f['name']: int(f['index']) - 1
        for f in fields if f['name'] in SHAKEMAP_FIELDS
    }
    out = {name: [] for name in idx}
    for name in idx:
        i = idx[name]
        if name in FIELDMAP:
            out[name].append([float(row[i]) for row in rows])
    dt = sorted(
        (imt[1], F32) for key, imt in FIELDMAP.items() if imt[0] == 'val')
    dtlist = [('lon', F32), ('lat', F32), ('vs30', F32), ('val', dt),
              ('std', dt)]
    data = numpy.zeros(len(rows), dtlist)
    for name, field in sorted(FIELDMAP.items()):
        if name not in out:
            continue
        if isinstance(field, tuple):
            # for ('val', IMT) or ('std', IMT)
            data[field[0]][field[1]] = F32(out[name])
        else:
            # for lon, lat, vs30
            data[field] = F32(out[name])
    return data
Пример #3
0
 def read_file(self, identifier, mfd_spacing=0.1, simple_mesh_spacing=1.0,
               complex_mesh_spacing=4.0, area_discretization=10.):
     """
     Reads in the source model in returns an instance of the :class:
     hmtk.sourcs.source_model.mtkSourceModel
     """
     node_set = node_from_xml(self.input_file)[0]
     source_model = mtkSourceModel(identifier,
                                   name=node_set.attrib["name"])
     for node in node_set:
         if "pointSource" in node.tag:
             source_model.sources.append(
                 parse_point_source_node(node, mfd_spacing))
         elif "areaSource" in node.tag:
             source_model.sources.append(
                 parse_area_source_node(node, mfd_spacing))
         elif "simpleFaultSource" in node.tag:
             source_model.sources.append(
                 parse_simple_fault_node(node, mfd_spacing,
                                         simple_mesh_spacing))
         elif "complexFaultSource" in node.tag:
             source_model.sources.append(
                 parse_complex_fault_node(node, mfd_spacing,
                                          complex_mesh_spacing))
         else:
             print("Source typology %s not recognised - skipping!"
                   % node.tag)
     return source_model
Пример #4
0
 def read_file(self,
               identifier,
               mfd_spacing=0.1,
               simple_mesh_spacing=1.0,
               complex_mesh_spacing=4.0,
               area_discretization=10.):
     """
     Reads in the source model in returns an instance of the :class:
     hmtk.sourcs.source_model.mtkSourceModel
     """
     node_set = node_from_xml(self.input_file)[0]
     source_model = mtkSourceModel(identifier, name=node_set.attrib["name"])
     for node in node_set:
         if "pointSource" in node.tag:
             source_model.sources.append(
                 parse_point_source_node(node, mfd_spacing))
         elif "areaSource" in node.tag:
             source_model.sources.append(
                 parse_area_source_node(node, mfd_spacing))
         elif "simpleFaultSource" in node.tag:
             source_model.sources.append(
                 parse_simple_fault_node(node, mfd_spacing,
                                         simple_mesh_spacing))
         elif "complexFaultSource" in node.tag:
             source_model.sources.append(
                 parse_complex_fault_node(node, mfd_spacing,
                                          complex_mesh_spacing))
         else:
             print("Source typology %s not recognised - skipping!" %
                   node.tag)
     return source_model
Пример #5
0
 def read_file(self, identifier, mfd_spacing=0.1, simple_mesh_spacing=1.0,
               complex_mesh_spacing=4.0, area_discretization=10.):
     """
     Reads in the source model in returns an instance of the :class:
     openquake.hmtk.sourcs.source_model.mtkSourceModel
     """
     sm_node = node_from_xml(self.input_file)[0]
     if sm_node[0].tag.startswith('{http://openquake.org/xmlns/nrml/0.4}'):
         node_sets = [sm_node]
         sm_name = sm_node.get("name", "")
     else:  # format NRML 0.5+
         node_sets = sm_node
         sm_name = sm_node["name"]
     source_model = mtkSourceModel(identifier, name=sm_name)
     for node_set in node_sets:
         for node in node_set:
             if "pointSource" in node.tag:
                 source_model.sources.append(
                     parse_point_source_node(node, mfd_spacing))
             elif "areaSource" in node.tag:
                 source_model.sources.append(
                     parse_area_source_node(node, mfd_spacing))
             elif "simpleFaultSource" in node.tag:
                 source_model.sources.append(
                     parse_simple_fault_node(node, mfd_spacing,
                                             simple_mesh_spacing))
             elif "complexFaultSource" in node.tag:
                 source_model.sources.append(
                     parse_complex_fault_node(node, mfd_spacing,
                                              complex_mesh_spacing))
             # TODO: multiPointSource are not supported
             else:
                 print("Source typology %s not recognised - skipping!"
                       % node.tag)
     return source_model
Пример #6
0
    def test_xml(self):
        # can read and write a .xml file converted into a Node object
        xmlfile = io.StringIO(u"""\
<root>
<general>
<a>1</a>
<b>2</b>
</general>
<section1 param="xxx" />
<section2 param="yyy" />
</root>
""")
        node = n.node_from_xml(xmlfile)
        outfile = io.BytesIO()
        n.node_to_xml(node, outfile)
        self.assertEqual(outfile.getvalue(), b"""\
<?xml version="1.0" encoding="utf-8"?>
<root>
    <general>
        <a>
            1
        </a>
        <b>
            2
        </b>
    </general>
    <section1 param="xxx"/>
    <section2 param="yyy"/>
</root>
""")
Пример #7
0
 def read_file(self, identifier, mfd_spacing=0.1, simple_mesh_spacing=1.0,
               complex_mesh_spacing=4.0, area_discretization=10.):
     """
     Reads in the source model in returns an instance of the :class:
     openquake.hmtk.sourcs.source_model.mtkSourceModel
     """
     sm_node = node_from_xml(self.input_file)[0]
     if sm_node[0].tag.startswith('{http://openquake.org/xmlns/nrml/0.4}'):
         node_sets = [sm_node]
         sm_name = sm_node.get("name", "")
     else:  # format NRML 0.5+
         node_sets = sm_node
         sm_name = sm_node["name"]
     source_model = mtkSourceModel(identifier, name=sm_name)
     for node_set in node_sets:
         for node in node_set:
             if "pointSource" in node.tag:
                 source_model.sources.append(
                     parse_point_source_node(node, mfd_spacing))
             elif "areaSource" in node.tag:
                 source_model.sources.append(
                     parse_area_source_node(node, mfd_spacing))
             elif "simpleFaultSource" in node.tag:
                 source_model.sources.append(
                     parse_simple_fault_node(node, mfd_spacing,
                                             simple_mesh_spacing))
             elif "complexFaultSource" in node.tag:
                 source_model.sources.append(
                     parse_complex_fault_node(node, mfd_spacing,
                                              complex_mesh_spacing))
             # TODO: multiPointSource are not supported
             else:
                 print("Source typology %s not recognised - skipping!"
                       % node.tag)
     return source_model
Пример #8
0
def _get_shakemap_array(xml_file):
    if hasattr(xml_file, 'read'):
        data = io.BytesIO(xml_file.read())
    else:
        data = open(xml_file)
    grid_node = node_from_xml(data)
    fields = grid_node.getnodes('grid_field')
    lines = grid_node.grid_data.text.strip().splitlines()
    rows = [line.split() for line in lines]

    # the indices start from 1, hence the -1 below
    idx = {f['name']: int(f['index']) - 1 for f in fields
           if f['name'] in SHAKEMAP_FIELDS}
    out = {name: [] for name in idx}
    for name in idx:
        i = idx[name]
        if name in FIELDMAP:
            out[name].append([float(row[i]) for row in rows])
    dt = sorted((imt[1], F32) for key, imt in FIELDMAP.items()
                if imt[0] == 'val')
    dtlist = [('lon', F32), ('lat', F32), ('vs30', F32),
              ('val', dt), ('std', dt)]
    data = numpy.zeros(len(rows), dtlist)
    for name, field in sorted(FIELDMAP.items()):
        if name not in out:
            continue
        if isinstance(field, tuple):
            # for ('val', IMT) or ('std', IMT)
            data[field[0]][field[1]] = F32(out[name])
        else:
            # for lon, lat, vs30
            data[field] = F32(out[name])
    return data
Пример #9
0
    def test_duplicates_ok(self):
        # sequential duplicate tags
        xmlfile = io.StringIO(u"""\
<nonParametric>
<singlePlaneRupture>1</singlePlaneRupture>
<singlePlaneRupture>3</singlePlaneRupture>
<multiPlanesRupture>2</multiPlanesRupture>
</nonParametric>
""")
        node = n.node_from_xml(xmlfile)
        dic = n.node_to_dict(node)
        self.assertEqual(dic, {
            'nonParametric':
            {'singlePlaneRupture': ['1', '3'], 'multiPlanesRupture': '2'}})
Пример #10
0
    def test_duplicates_bad(self):
        # this is the current behavior: the first rupture is lost
        # we may want to raise an error instead
        xmlfile = io.StringIO(u"""\
<nonParametric>
<singlePlaneRupture>1</singlePlaneRupture>
<multiPlanesRupture>2</multiPlanesRupture>
<singlePlaneRupture>3</singlePlaneRupture>
</nonParametric>
""")
        node = n.node_from_xml(xmlfile)
        dic = n.node_to_dict(node)
        self.assertEqual(dic, {
            'nonParametric':
            {'singlePlaneRupture': '3', 'multiPlanesRupture': '2'}})
Пример #11
0
def fix_source_node(node):
    if node.tag.endswith('complexFaultSource'):
        geom = node.complexFaultGeometry
        top = geom.faultTopEdge
        intermediate = [edge for edge in geom.getnodes('intermediateEdge')]
        bottom = geom.faultBottomEdge
        edges = map(make_edge, [top] + intermediate + [bottom])
        try:
            ComplexFaultSurface.from_fault_data(edges, mesh_spacing=4.)
        except ValueError as excp:
            if AKI_RICH_ERR_MSG in str(excp):
                print(excp)
                print('Reverting edges ...')
                reverse(geom.faultTopEdge)
                reverse(geom.faultBottomEdge)
            elif WRONG_ORDER_ERR_MSG in str(excp):
                print(excp)
                print('reverting bottom edge ...')
                reverse(geom.faultBottomEdge)
            else:
                raise

if __name__ == '__main__':
    fname = sys.argv[1]
    src_model = node_from_xml(fname).sourceModel
    for src_node in src_model:
        fix_source_node(src_node)
    with open(fname, 'wb') as f:
        nrml.write([src_model], f, xmlns=nrml.NAMESPACE)