示例#1
0
def read(source, chatty=True):
    """
    Convert a NRML file into a validated LiteralNode object. Keeps
    the entire tree in memory.

    :param source:
        a file name or file object open for reading
    """
    nrml = xmlparse(source).getroot()
    assert striptag(nrml.tag) == 'nrml', nrml.tag
    # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5')
    xmlns = nrml.tag.split('}')[0][1:]
    if xmlns != NRML05 and chatty:
        # for the moment NRML04 is still supported, so we hide the warning
        logging.debug('%s is at an outdated version: %s', source, xmlns)
    subnodes = []
    for elem in nrml:
        nodecls = nodefactory[striptag(elem.tag)]
        try:
            subnodes.append(node_from_elem(elem, nodecls))
        except ValueError as exc:
            raise ValueError('%s of %s' % (exc, source))
    return LiteralNode(
        'nrml', {'xmlns': xmlns, 'xmlns:gml': GML_NAMESPACE},
        nodes=subnodes)
示例#2
0
def read(source, chatty=True):
    """
    Convert a NRML file into a validated LiteralNode object. Keeps
    the entire tree in memory.

    :param source:
        a file name or file object open for reading
    """
    nrml = xmlparse(source).getroot()
    assert striptag(nrml.tag) == 'nrml', nrml.tag
    # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5')
    xmlns = nrml.tag.split('}')[0][1:]
    if xmlns != NRML05 and chatty:
        # for the moment NRML04 is still supported, so we hide the warning
        logging.debug('%s is at an outdated version: %s', source, xmlns)
    subnodes = []
    for elem in nrml:
        nodecls = nodefactory[striptag(elem.tag)]
        try:
            subnodes.append(node_from_elem(elem, nodecls))
        except ValueError as exc:
            raise ValueError('%s of %s' % (exc, source))
    return LiteralNode(
        'nrml', {'xmlns': xmlns, 'xmlns:gml': GML_NAMESPACE},
        nodes=subnodes)
示例#3
0
def upgrade_file(path):
    """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=list(map(riskmodels.obj_to_node, vf_dict.values())))
        gml = False
    elif tag == 'fragilityModel':
        node0 = riskmodels.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)]
    with open(path, 'w') as f:
        nrml.write([node0], f, gml=gml)
示例#4
0
def read_lazy(source, lazytags):
    """
    Convert a NRML file into a validated LiteralNode object. The
    tree is lazy, i.e. you access nodes by iterating on them.

    :param source:
        a file name or file object open for reading
    :param lazytags:
       the name of nodes which subnodes must be read lazily
    :returns:
       a list of nodes; some of them will contain lazy subnodes
    """
    nodes = []
    try:
        for _, el in iterparse(source, remove_comments=True):
            tag = striptag(el.tag)
            if tag in nodefactory:  # NRML tag
                nodes.append(node_from_elem(el, nodefactory[tag], lazy=lazytags))
                el.clear()  # save memory
    except:
        etype, exc, tb = sys.exc_info()
        msg = str(exc)
        if str(source) not in msg:
            msg = "%s in %s" % (msg, source)
        raise_(etype, msg, tb)
    return nodes
示例#5
0
def set_params(w, src):
    """
    Set source parameters.
    """
    params = extract_source_params(src)
    # this is done because for characteristic sources geometry is in
    # 'surface' attribute
    params.update(extract_geometry_params(src))

    mfd_pars, rate_pars = extract_mfd_params(src)
    params.update(mfd_pars)
    params.update(rate_pars)

    strikes, dips, rakes, np_weights = extract_source_nodal_planes(src)
    params.update(strikes)
    params.update(dips)
    params.update(rakes)
    params.update(np_weights)

    hds, hdsw = extract_source_hypocentral_depths(src)
    params.update(hds)
    params.update(hdsw)

    pstrikes, pdips = extract_source_planes_strikes_dips(src)
    params.update(pstrikes)
    params.update(pdips)
    params['sourcetype'] = striptag(src.tag)
    w.record(**params)
示例#6
0
def read_lazy(source, lazytags):
    """
    Convert a NRML file into a validated LiteralNode object. The
    tree is lazy, i.e. you access nodes by iterating on them.

    :param source:
        a file name or file object open for reading
    :param lazytags:
       the name of nodes which subnodes must be read lazily
    :returns:
       a list of nodes; some of them will contain lazy subnodes
    """
    nodes = []
    try:
        for _, el in iterparse(source, remove_comments=True):
            tag = striptag(el.tag)
            if tag in nodefactory:  # NRML tag
                nodes.append(
                    node_from_elem(el, nodefactory[tag], lazy=lazytags))
                el.clear()  # save memory
    except:
        etype, exc, tb = sys.exc_info()
        msg = str(exc)
        if str(source) not in msg:
            msg = '%s in %s' % (msg, source)
        raise_(etype, msg, tb)
    return nodes
示例#7
0
def read(source, chatty=True):
    """
    Convert a NRML file into a validated LiteralNode object. Keeps
    the entire tree in memory.

    :param source:
        a file name or file object open for reading
    """
    nrml = parse(source).getroot()
    assert striptag(nrml.tag) == "nrml", nrml.tag
    # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5')
    xmlns = nrml.tag.split("}")[0][1:]
    if xmlns != NRML05 and chatty:
        logging.warn("%s is at an outdated version: %s", source, xmlns)
    subnodes = []
    for elem in nrml:
        nodecls = nodefactory[striptag(elem.tag)]
        subnodes.append(node_from_elem(elem, nodecls))
    return LiteralNode("nrml", {"xmlns": xmlns, "xmlns:gml": GML_NAMESPACE}, nodes=subnodes)
示例#8
0
def read(source):
    """
    Convert a NRML file into a validated LiteralNode object. Keeps
    the entire tree in memory.

    :param source:
        a file name or file object open for reading
    """
    nrml = parse(source).getroot()
    assert striptag(nrml.tag) == 'nrml', nrml.tag
    # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5')
    xmlns = nrml.tag.split('}')[0][1:]
    subnodes = []
    for elem in nrml:
        nodecls = nodefactory[striptag(elem.tag)]
        subnodes.append(node_from_elem(elem, nodecls))
    return LiteralNode(
        'nrml', {'xmlns': xmlns, 'xmlns:gml': GML_NAMESPACE},
        nodes=subnodes)
示例#9
0
    def convert_node(self, node):
        """
        Convert the given node into a hazardlib source, depending
        on the node tag.

        :param node: a node representing a source
        """
        with context(self.fname, node):
            convert_source = getattr(self, 'convert_' + striptag(node.tag))
        return convert_source(node)
示例#10
0
    def convert_node(self, node):
        """
        Convert the given node into a hazardlib source, depending
        on the node tag.

        :param node: a node representing a source
        """
        with context(self.fname, node):
            convert_source = getattr(self, 'convert_' + striptag(node.tag))
        return convert_source(node)
示例#11
0
def upgrade_file(path):
    """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
    if striptag(node0.tag) == 'vulnerabilityModel':
        vf_dict, cat_dict = get_vulnerability_functions_04(path)
        node0 = LiteralNode(
            'vulnerabilityModel', cat_dict,
            nodes=list(map(riskmodels.obj_to_node, list(vf_dict.values()))))
    with open(path, 'w') as f:
        nrml.write([node0], f)
示例#12
0
    def convert_node(self, node):
        """
        Convert the given rupture node into a hazardlib rupture, depending
        on the node tag.

        :param node: a node representing a rupture
        """
        with context(self.fname, node):
            convert_rupture = getattr(self, 'convert_' + striptag(node.tag))
            mag = ~node.magnitude
            rake = ~node.rake
            hypocenter = ~node.hypocenter
        return convert_rupture(node, mag, rake, hypocenter)
示例#13
0
    def convert_node(self, node):
        """
        Convert the given rupture node into a hazardlib rupture, depending
        on the node tag.

        :param node: a node representing a rupture
        """
        with context(self.fname, node):
            convert_rupture = getattr(self, 'convert_' + striptag(node.tag))
            mag = ~node.magnitude
            rake = ~node.rake
            h = node.hypocenter
            hypocenter = geo.Point(h['lon'], h['lat'], h['depth'])
        return convert_rupture(node, mag, rake, hypocenter)
示例#14
0
    def convert_node(self, node):
        """
        Convert the given rupture node into a hazardlib rupture, depending
        on the node tag.

        :param node: a node representing a rupture
        """
        with context(self.fname, node):
            convert_rupture = getattr(self, "convert_" + striptag(node.tag))
            mag = ~node.magnitude
            rake = ~node.rake
            h = node.hypocenter
            hypocenter = geo.Point(h["lon"], h["lat"], h["depth"])
        return convert_rupture(node, mag, rake, hypocenter)
示例#15
0
def read(source, chatty=True):
    """
    Convert a NRML file into a validated LiteralNode object. Keeps
    the entire tree in memory.

    :param source:
        a file name or file object open for reading
    """
    nrml = parse(source).getroot()
    assert striptag(nrml.tag) == 'nrml', nrml.tag
    # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5')
    xmlns = nrml.tag.split('}')[0][1:]
    if xmlns != NRML05 and chatty:
        logging.warn('%s is at an outdated version: %s', source, xmlns)
    subnodes = []
    for elem in nrml:
        nodecls = nodefactory[striptag(elem.tag)]
        subnodes.append(node_from_elem(elem, nodecls))
    return LiteralNode('nrml', {
        'xmlns': xmlns,
        'xmlns:gml': GML_NAMESPACE
    },
                       nodes=subnodes)
示例#16
0
 def from_node(cls, node):
     """
     Return a specialized Converter instance
     """
     tag = striptag(node.tag)
     name = tag[0].upper() + tag[1:]
     clsname = name[:-5] if name.endswith('Model') else name
     if 'format' in node.attrib:  # for fragility functions
         clsname += node['format'].capitalize()
     if clsname == 'GmfSet':
         clsname = 'GmfCollection'
     convertertype = globals()[clsname]
     tset = record.TableSet(convertertype)
     tset.insert_all(convertertype.node_to_records(node))
     return convertertype(tset)
示例#17
0
def upgrade_file(path):
    """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)
    if tag == 'vulnerabilityModel':
        vf_dict, cat_dict = get_vulnerability_functions_04(path)
        # below I am converting into a NRML 0.5 vulnerabilityModel
        node0 = LiteralNode(
            'vulnerabilityModel', cat_dict,
            nodes=list(map(riskmodels.obj_to_node, list(vf_dict.values()))))
    elif tag == 'fragilityModel':
        node0 = riskmodels.convert_fragility_model_04(
            nrml.read(path)[0], path)
    with open(path, 'w') as f:
        nrml.write([node0], f)
示例#18
0
def read(source, chatty=True, stop=None):
    """
    Convert a NRML file into a validated Node object. Keeps
    the entire tree in memory.

    :param source:
        a file name or file object open for reading
    """
    vparser = ValidatingXmlParser(validators, stop)
    nrml = vparser.parse_file(source)
    assert striptag(nrml.tag) == 'nrml', nrml.tag
    # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5')
    xmlns = nrml.tag.split('}')[0][1:]
    if xmlns != NRML05 and chatty:
        # for the moment NRML04 is still supported, so we hide the warning
        logging.debug('%s is at an outdated version: %s', source, xmlns)
    nrml['xmlns'] = xmlns
    nrml['xmlns:gml'] = GML_NAMESPACE
    return nrml
def parse_planar_surf(element):
    """
    Parse NRML 0.4 'planarSurface' and return class PlanarSurface.
    """
    tag_set = [striptag(node.tag) for node in element.nodes]
    (top_left, top_right, bottom_left, bottom_right) = tuple(
        [element.nodes[tag_set.index(tag)] for tag in PLANAR_TAGS])
#    for node in element.nodes:
#        if "topLeft" in node.tag:
#            top_left = copy(node)
#        elif "topRight" in node.tag:
#            top_right = copy(node)
#        elif "bottomRight" in node.tag:
#          
#
#    top_left = element.find('%stopLeft' % NRML)
#    top_right = element.find('%stopRight' % NRML)
#    bottom_left = element.find('%sbottomLeft' % NRML)
#    bottom_right = element.find('%sbottomRight' % NRML)

    corners_lons = numpy.array(
        [float(top_left.attrib['lon']), float(top_right.attrib['lon']),
        float(bottom_right.attrib['lon']), float(bottom_left.attrib['lon'])]
    )

    corners_lats = numpy.array(
        [float(top_left.attrib['lat']), float(top_right.attrib['lat']),
        float(bottom_right.attrib['lat']), float(bottom_left.attrib['lat'])]
    )

    corners_depths = numpy.array(
        [float(top_left.attrib['depth']), float(top_right.attrib['depth']),
        float(bottom_right.attrib['depth']), float(bottom_left.attrib['depth'])]
    )

    return PlanarSurface(corners_lons, corners_lats, corners_depths)
示例#20
0
def parse_planar_surf(element):
    """
    Parse NRML 0.4 'planarSurface' and return class PlanarSurface.
    """
    tag_set = [striptag(node.tag) for node in element.nodes]
    (top_left, top_right, bottom_left, bottom_right) = tuple(
        [element.nodes[tag_set.index(tag)] for tag in PLANAR_TAGS])

    corners_lons = numpy.array(
        [float(top_left.attrib['lon']), float(top_right.attrib['lon']),
         float(bottom_right.attrib['lon']), float(bottom_left.attrib['lon'])]
    )

    corners_lats = numpy.array(
        [float(top_left.attrib['lat']), float(top_right.attrib['lat']),
         float(bottom_right.attrib['lat']), float(bottom_left.attrib['lat'])]
    )

    corners_depths = numpy.array(
        [float(top_left.attrib['depth']), float(top_right.attrib['depth']),
         float(bottom_right.attrib['depth']), float(bottom_left.attrib['depth'])]
    )

    return PlanarSurface(corners_lons, corners_lats, corners_depths)
示例#21
0
def get_taglist(node):
    """
    Return a list of tags (with NRML namespace removed) representing the
    order of the nodes within a node
    """
    return [striptag(subnode.tag) for subnode in node]