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
def build_fault_model(self, collapse=False, rendered_msr=WC1994(), mfd_config=None): ''' Constructs a full fault model with epistemic uncertainty by enumerating all the possible recurrence models of each fault as separate faults, with the recurrence rates multiplied by the corresponding weights. :param bool collapse: Determines whether or not to collapse the branches :param rendered_msr: If the option is taken to collapse the branches then a recurrence model for rendering must be defined :param list/dict mfd_config: Universal list or dictionay of configuration parameters for the magnitude frequency distribution - will overwrite whatever is previously defined for the fault! ''' self.source_model = mtkSourceModel(self.id, self.name) for fault in self.faults: fault.generate_recurrence_models(collapse, config=mfd_config, rendered_msr=rendered_msr) src_model, src_weight = fault.generate_fault_source_model() for iloc, model in enumerate(src_model): new_model = deepcopy(model) new_model.id = str(model.id) + '_%g' % (iloc + 1) new_model.mfd.occurrence_rates = \ (np.array(new_model.mfd.occurrence_rates) * src_weight[iloc]).tolist() self.source_model.sources.append(new_model)
def test_core_instantiation(self): ''' Simple test to ensure the class is correctly instantiated ''' self.source_model = mtkSourceModel('101', 'Model Name') self.assertEqual(self.source_model.id, '101') self.assertEqual(self.source_model.name, 'Model Name') # No sources on input self.assertEqual(self.source_model.get_number_sources(), 0) # Input correctly good_model = [mtkPointSource('101', 'Point 1'), mtkPointSource('102', 'Point 2')] self.source_model = mtkSourceModel('1001', 'Good Model', good_model) self.assertEqual(self.source_model.get_number_sources(), 2) # Input incorrectly - source not as list with self.assertRaises(ValueError) as ver: self.source_model = mtkSourceModel('1002', 'Bad Model', mtkPointSource('103', 'Point 3')) self.assertEqual(ver.exception.message, 'Sources must be input as list!')
def read_file(self, fault_mesh_spacing=1.0, validation=False): """ Parse the source XML content and generate a source model in object form. :param float fault_mesh_spacing: Mesh spacing to use for fault sources (km) :param bool validation: Option to validate against nrml 0.4 schema - should only be set to true when inputting a fully defined source model :returns: :class:`hmtk.sources.source_model.SourceModel` instance. """ self.mesh_spacing = fault_mesh_spacing src_model = mtkSourceModel() if validation: # Validate against nrml schema schema = etree.XMLSchema(etree.parse(nrml.nrml_schema_file())) tree = etree.iterparse(self.source, events=('start', 'end'), schema=schema) else: tree = etree.iterparse(self.source, events=('start', 'end')) for event, element in tree: # Find the <sourceModel> element and get the 'name' attr. if event == 'start': if element.tag == self._SM_TAG: src_model.name = element.get('name') break else: # If we get to here, we didn't find the <sourceModel> element. raise ValueError('<sourceModel> element not found.') src_model.sources = self._source_gen(tree) src_model.sources = list(src_model.sources) return src_model
sources = [] #smooth = np.genfromtxt("../data_output/hmtk_bsb2013_decluster_frankel1995.csv", delimiter=",",skip_header=True) smooth = np.genfromtxt("../data_output/hmtk_bsb2013_pp_decluster_woo_rates.csv", delimiter=",",skip_header=True) for i, line in enumerate(smooth): if not np.isnan(line[2]): #print line p = mtkPointSource(identifier = i, name = "%s"%i, trt='Stable Continental Crust', geometry = geo.point.Point(line[0], line[1]), upper_depth = 0., lower_depth = 40., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(min_mag=m0, max_mag=7.0, a_val=float(line[2]), b_val=1.0), nodal_plane_dist=None, hypo_depth_dist=None) sources.append(p) s = source_model.mtkSourceModel(identifier="03", name = "pshab woo1996 TGRMFD", sources = sources) #s.serialise_to_nrml(filename = "../smoothing/source_model_pshab_decluster_frankel1995.xml", s.serialise_to_nrml(filename = "../woo/source_model_pshab_pp_decluster_woo1996.xml", use_defaults = True)
c = g.strip().split(" ") points.append(geo.Point(float(c[1]), float(c[0]))) points.append(points[0]) print points geom = geo.Polygon(points) print geom a = area_source.mtkAreaSource(identifier = i, name = region, trt = "Stable Continental Crust", geometry = geom, upper_depth = "0", lower_depth = "40", mag_scale_rel = "WC1994", # default rupt_aspect_ratio = 1, mfd = mfd, nodal_plane_dist = None, hypo_depth_dist = None) sources.append(a) f.close() s = source_model.mtkSourceModel(identifier="01", name = "PSHAB", sources = sources) s.serialise_to_nrml(filename = "areas_pshab_dourado.xml", use_defaults = True)
r = [l for l in csv.reader(open(filename), delimiter=',', quotechar='"')] #smooth = np.genfromtxt("../data_output/hmtk_bsb2013_decluster_woo_rates.csv", delimiter=",",skip_header=True) for i, line in enumerate(r[1:]): rates = line[5].split(" ") #print rates p = mtkPointSource(identifier = i, name = "%s"%i, trt='Stable Continental Crust', geometry = geo.point.Point(float(line[0]), float(line[1])), upper_depth = 0., lower_depth = 30., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.IncrementalMFD(min_mag=float(line[3]), bin_width=float(line[4]), occur_rates=rates), nodal_plane_dist=None, hypo_depth_dist=None) #print p sources.append(p) s = source_model.mtkSourceModel(identifier="33", name = "PSHAB-Woo Discrete MFD", sources = sources) s.serialise_to_nrml(filename = "../woo/test_source_model_pshab_woo_incremental.xml", use_defaults = True)
m_min, m_max = 3.0, 7.0 sources = [] smooth = np.genfromtxt("data_output/hmtk_bsb2013_decluster_frankel1995.csv", delimiter=",",skip_header=True) for i, line in enumerate(smooth): if str(line[4]) != "nan": #print line[4] p = mtkPointSource(identifier = i, name = "%s"%i, trt='Stable Continental Crust', geometry = geo.point.Point(line[0], line[1]), upper_depth = 0., lower_depth = 30., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(min_mag=m_min, max_mag=m_max, a_val=float(line[4]), b_val=1.0), nodal_plane_dist=None, hypo_depth_dist=None) sources.append(p) s = source_model.mtkSourceModel(identifier="02", name = "PSHAB-Smoothed Frankel1995 (decluster)", sources = sources) s.serialise_to_nrml(filename = "smoothing/source_model_pshab_decluster_frankel1995.xml", use_defaults = True)
# print max_mag, trt, depth point = Point(lons[j], lats[j], depth) # Openquake geometry Point mfd = TruncatedGRMFD(min_mag, max_mag, 0.1, a_vals[j], b_vals[j]) hypo_depth_dist = PMF([(1.0, depth)]) nodal_plane_dist = PMF([(0.3, NodalPlane(0, 30, 90)), (0.2, NodalPlane(90, 30, 90)), (0.3, NodalPlane(180, 30, 90)), (0.2, NodalPlane(270, 30, 90))]) point_source = mtkPointSource(identifier, name, geometry=point, mfd=mfd, mag_scale_rel = 'Leonard2014_SCR', rupt_aspect_ratio=1.0, upper_depth = 0.1, lower_depth = 20.0, trt = trt, nodal_plane_dist = nodal_plane_dist, hypo_depth_dist = hypo_depth_dist) source_list.append(point_source) source_model = mtkSourceModel(identifier=0, name='Cuthbertson2016', sources = source_list) print 'Writing to NRML' outbase = 'cuthbertson2018' source_model_filename = outbase + '_source_model.xml' source_model.serialise_to_nrml(source_model_filename) source_models.append(source_model) ###################################### # Now write the source model logic tree file ###################################### print 'Writing logic tree file' newxml = '<?xml version="1.0" encoding="UTF-8"?>\n' newxml += '<nrml xmlns:gml="http://www.opengis.net/gml"\n' newxml += ' xmlns="http://openquake.org/xmlns/nrml/0.4">\n\n' newxml += ' <logicTree logicTreeID="lt1">\n' newxml += ' <logicTreeBranchingLevel branchingLevelID="bl1">\n'
p = mtkPointSource( identifier='001', name='A Point Source', trt='Stable Continental Crust', geometry=geo.point.Point(10., 10.), upper_depth=0., lower_depth=30., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(a_val=3., b_val=1.0, min_mag=2.5, max_mag=7.0), nodal_plane_dist=None, hypo_depth_dist=None) s = source_model.mtkSourceModel(identifier="09", name="nick smoothed", sources=[p]) s.serialise_to_nrml(filename="smoothed_model.xml", use_defaults=True) # # # Define a complete source # area_geom = polygon.Polygon([point.Point(10., 10.), # point.Point(12., 10.), # point.Point(12., 8.), # point.Point(10., 8.)]) # # # # # area_source = mtkAreaSource('001',
from hmtk.sources.point_source import mtkPointSource import glob import numpy as np m_min, m_max = 3.0, 7.0 sources = [] smooth = np.genfromtxt("data_output/hmtk_bsb2013_decluster_frankel1995.csv", delimiter=",", skip_header=True) for i, line in enumerate(smooth): if str(line[4]) != "nan": # print line[4] p = mtkPointSource( identifier=i, name="%s" % i, trt="Stable Continental Crust", geometry=geo.point.Point(line[0], line[1]), upper_depth=0.0, lower_depth=30.0, mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(min_mag=m_min, max_mag=m_max, a_val=float(line[4]), b_val=1.0), nodal_plane_dist=None, hypo_depth_dist=None, ) sources.append(p) s = source_model.mtkSourceModel(identifier="02", name="PSHAB-Smoothed Frankel1995 (decluster)", sources=sources) s.serialise_to_nrml(filename="smoothing/source_model_pshab_decluster_frankel1995.xml", use_defaults=True)
mag_scale_rel = "WC1994", # default rupt_aspect_ratio = 1, mfd = models.TGRMFD(min_mag=3.0, max_mag=7.0, b_val=0.847, a_val=0.737), nodal_plane_dist = models.NodalPlane(Decimal('1.0'), strike=0., dip=90., rake=0.), hypo_depth_dist = None) #a.create_oqnrml_source s = source_model.mtkSourceModel(identifier="09", name = "sources model name", sources = [a]) s.serialise_to_nrml(filename = "s03.xml", use_defaults = True) # # # Define a complete source # area_geom = polygon.Polygon([point.Point(10., 10.), # point.Point(12., 10.), # point.Point(12., 8.), # point.Point(10., 8.)]) # #
p = mtkPointSource(identifier = '001', name = 'A Point Source', trt='Stable Continental Crust', geometry = geo.point.Point(10., 10.), upper_depth = 0., lower_depth = 30., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(a_val=3., b_val=1.0, min_mag=2.5, max_mag=7.0), nodal_plane_dist=None, hypo_depth_dist=None) s = source_model.mtkSourceModel(identifier="09", name = "nick smoothed", sources = [p]) s.serialise_to_nrml(filename = "smoothed_model.xml", use_defaults = True) # # # Define a complete source # area_geom = polygon.Polygon([point.Point(10., 10.), # point.Point(12., 10.), # point.Point(12., 8.), # point.Point(10., 8.)]) # #
r = [l for l in csv.reader(open(filename), delimiter=',', quotechar='"')] #smooth = np.genfromtxt("../data_output/hmtk_bsb2013_decluster_woo_rates.csv", delimiter=",",skip_header=True) for i, line in enumerate(r[1:]): rate = line[2].strip #print rates p = mtkPointSource(identifier = i, name = "%s"%i, trt='Stable Continental Crust', geometry = geo.point.Point(float(line[0]), float(line[1])), upper_depth = 0., lower_depth = 30., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(min_mag=m0, max_mag=7.0, a_val=np.log10(float(line[2])*m0) + 1.0*m0, b_val=1.0), nodal_plane_dist=None, hypo_depth_dist=None) #print p sources.append(p) s = source_model.mtkSourceModel(identifier="04", name = "PSHAB-Helmstetter2012", sources = sources) s.serialise_to_nrml(filename = "../helmstetter2012/source_model_pshab_helmstetter2012.xml", use_defaults = True)
#aval = rate aval = np.log10(rate) + bval * config["mmin"] #print rate, aval mfd = TruncatedGRMFD(min_mag, max_mag, 0.1, aval, bval) hypo_depth_dist = PMF([(1.0, 10.0)]) nodal_plane_dist = PMF([(0.25, NodalPlane(0, 30, 90)), (0.25, NodalPlane(90, 30, 90)), (0.25, NodalPlane(180, 30, 90)), (0.25, NodalPlane(270, 30, 90))]) point_source = mtkPointSource(identifier, name, geometry=point, mfd=mfd, mag_scale_rel='WC1994', rupt_aspect_ratio=1.5, upper_depth=0.1, lower_depth=20.0, trt='Non_cratonic', nodal_plane_dist=nodal_plane_dist, hypo_depth_dist=hypo_depth_dist) source_list.append(point_source) # i+=1 # if j==1000: # break source_model = mtkSourceModel(identifier=0, name='Helmstetter_K4', sources=source_list) source_model.serialise_to_nrml( ('source_model_smoothed_adaptive_K%i_0.1.xml' % smoother.config['k']))
skip_header=True) for i, line in enumerate(smooth): if not np.isnan(line[2]): #print line p = mtkPointSource( identifier=i, name="%s" % i, trt='Stable Continental Crust', geometry=geo.point.Point(line[0], line[1]), upper_depth=0., lower_depth=40., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(min_mag=m0, max_mag=7.0, a_val=float(line[2]), b_val=1.0), nodal_plane_dist=None, hypo_depth_dist=None) sources.append(p) s = source_model.mtkSourceModel(identifier="03", name="pshab woo1996 TGRMFD", sources=sources) #s.serialise_to_nrml(filename = "../smoothing/source_model_pshab_decluster_frankel1995.xml", s.serialise_to_nrml( filename="../woo/source_model_pshab_pp_decluster_woo1996.xml", use_defaults=True)
(0.3, NodalPlane(180, 30, 90)), (0.2, NodalPlane(270, 30, 90))]) point_source = mtkPointSource(identifier, name, geometry=point, mfd=mfd, mag_scale_rel='Leonard2014_SCR', rupt_aspect_ratio=1.0, upper_depth=0.1, lower_depth=20.0, trt=trt, nodal_plane_dist=nodal_plane_dist, hypo_depth_dist=hypo_depth_dist) source_list.append(point_source) source_model = mtkSourceModel(identifier=0, name='Hall2007', sources=source_list) print 'Writing to NRML' outbase = 'Hall2007' source_model_filename = outbase + '_source_model.xml' source_model.serialise_to_nrml(source_model_filename) source_models.append(source_model) ###################################### # Now write the source model logic tree file ###################################### print 'Writing logic tree file' newxml = '<?xml version="1.0" encoding="UTF-8"?>\n' newxml += '<nrml xmlns:gml="http://www.opengis.net/gml"\n' newxml += ' xmlns="http://openquake.org/xmlns/nrml/0.4">\n\n' newxml += ' <logicTree logicTreeID="lt1">\n'
geometry=geo.point.Point(line[0], line[1]), upper_depth=0., lower_depth=20., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(min_mag=m_min, max_mag=m_max, a_val=_a, b_val=1.0), nodal_plane_dist=None, hypo_depth_dist=None) sources.append(p) s = source_model.mtkSourceModel(identifier="04", name="PSHAB-Smoothed Helmstetter2012", sources=sources) s.serialise_to_nrml( filename="helmstetter2012/source_model_pshab_helmstetter2012.xml", use_defaults=True) o = np.array(o) x = o[:, 0] y = o[:, 1] r = o[:, 2] from map import rate_map #print len(x), len(y), len(r), sqrt()
m_min, m_max = 3.0, 7.0 sources = [] smooth = np.genfromtxt("data_output/hmtk_bsb2013_decluster_woo1996.csv", delimiter=",",skip_header=True) for i, line in enumerate(smooth): #print line if not line[4] == np.nan: p = mtkPointSource(identifier = i, name = "%s"%i, trt='Stable Continental Crust', geometry = geo.point.Point(line[0], line[1]), upper_depth = 0., lower_depth = 30., mag_scale_rel="WC1994", # default rupt_aspect_ratio=1.0, mfd=models.TGRMFD(min_mag=m_min, max_mag=m_max, a_val=float(line[5]), b_val=1.0), nodal_plane_dist=None, hypo_depth_dist=None) sources.append(p) s = source_model.mtkSourceModel(identifier="03", name = "PSHAB-Smoothed Woo1996 (decluster)", sources = sources) s.serialise_to_nrml(filename = "woo/source_model_pshab_decluster_woo1996.xml", use_defaults = True)