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)
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)
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)
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 """ return getattr(self, 'convert_' + striptag(node.tag))(node)
def convert_node(self, node): """ Convert the given source node into a Row object """ trt = node.attrib.get('tectonicRegion') if trt and trt in self.discard_trts: return return getattr(self, 'convert_' + striptag(node.tag))(node)
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 """ convert = getattr(self, 'convert_' + striptag(node.tag)) return convert(node)
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)
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 """ obj = getattr(self, 'convert_' + striptag(node.tag))(node) source_id = getattr(obj, 'source_id', '') if self.source_id and source_id and source_id not in self.source_id: return return obj
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)
def convert_node(self, node): """ Convert the given source node into a hazardlib source, depending on the node tag. :param node: a node representing a source or a SourceGroup """ trt = node.attrib.get('tectonicRegion') if trt and trt in self.discard_trts: return obj = getattr(self, 'convert_' + striptag(node.tag))(node) source_id = getattr(obj, 'source_id', '') if self.source_id and source_id and source_id not in self.source_id: return return obj
def read(source, 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) if striptag(nrml.tag) != 'nrml': raise ValueError('%s: expected a node of kind nrml, got %s' % (source, nrml.tag)) # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5') xmlns = nrml.tag.split('}')[0][1:] nrml['xmlns'] = xmlns nrml['xmlns:gml'] = GML_NAMESPACE return nrml
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) if striptag(nrml.tag) != 'nrml': raise ValueError('%s: expected a node of kind nrml, got %s' % (source, 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 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) if striptag(nrml.tag) != 'nrml': raise ValueError('%s: expected a node of kind nrml, got %s' % (source, 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]) 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)
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]