def setUp(self): """ """ self.npd_as_list = [ models.NodalPlane(0.5, 0., 90., 0.), models.NodalPlane(0.5, 90., 90., 180.) ] self.npd_as_pmf = PMF([(0.5, NodalPlane(0., 90., 0.)), (0.5, NodalPlane(90., 90., 180.))]) self.npd_as_pmf_bad = PMF([(0.5, None), (0.5, NodalPlane(90., 90., 180.))])
def test_raises_useful_error(self): # Test that the source id and name are included with conversion errors, # to help the users deal with problems in their source models. area_geom = nrml_models.AreaGeometry( wkt='POLYGON((0.0 0.0, 1.0 0.0, 0.0 0.0 ))', upper_seismo_depth=0.0, lower_seismo_depth=10.0, ) area_mfd = nrml_models.IncrementalMFD( min_mag=6.55, bin_width=0.1, occur_rates=[ 0.0010614989, 8.8291627E-4, 7.3437777E-4, 6.108288E-4, 5.080653E-4 ], ) area_npd = [ nrml_models.NodalPlane(probability=decimal.Decimal("0.3"), strike=0.0, dip=90.0, rake=0.0), nrml_models.NodalPlane(probability=decimal.Decimal("0.7"), strike=90.0, dip=45.0, rake=90.0), ] area_hdd = [ nrml_models.HypocentralDepth(probability=decimal.Decimal("0.5"), depth=4.0), nrml_models.HypocentralDepth(probability=decimal.Decimal("0.5"), depth=8.0), ] area_src = nrml_models.AreaSource( id='1', name='Quito', trt='Active Shallow Crust', geometry=area_geom, mag_scale_rel='PeerMSR', rupt_aspect_ratio=1.5, mfd=area_mfd, nodal_plane_dist=area_npd, hypo_depth_dist=area_hdd, ) with self.assertRaises(Exception) as ar: source_input.nrml_to_hazardlib(area_src, MESH_SPACING, BIN_WIDTH, AREA_SRC_DISC) expected_error = ( "The following error has occurred with source id='1', " "name='Quito': Could not create geometry because of errors while " "reading input.") self.assertEqual(expected_error, ar.exception.message)
def render_npd(nodal_plane_dist, use_default=False): ''' Checks to see if nodal plane distribution exists and renders to an instances of the class openquake.nrmllib.models.NodalPlane :param nodal_plane_dist: Nodal plane distribution of a source as either an instance of :class: openquake.nrmllib.models.NodalPlane or :class: openquake.hazardlib.pmf.PMF :param bool use_default: Boolean flag to opt to use defaults (True) or not. If set to False then ValueError will be raised if information is missing :returns: List of instances of openquake.nrmllib.models.NodalPlane ''' if isinstance(nodal_plane_dist, list): for npd in nodal_plane_dist: assert isinstance(npd, models.NodalPlane) return nodal_plane_dist elif isinstance(nodal_plane_dist, PMF): # Create an instance of openquak.nrmllib.models.NodalPlane prob_sum = Decimal('0.0') output_npd = [] for (prob, value) in nodal_plane_dist.data: prob_sum += Decimal(str(prob)) if not isinstance(value, NodalPlane): raise ValueError('Nodal Planes incorrectly formatted!') output_npd.append( models.NodalPlane(Decimal(str(prob)), strike=value.strike, dip=value.dip, rake=value.rake)) if not prob_sum == Decimal('1.0'): raise ValueError('Nodal Plane probabilities do not sum to 1.0') return output_npd else: if use_default: # Use a default nodal plane with strike 0, dip 90 and rake 0. return [ models.NodalPlane(Decimal('1.0'), strike=0., dip=90., rake=0.) ] else: raise ValueError('Nodal Plane distribution not defined')
def write_to_nrml(self, output_filename, mmin, upper_depth=0., lower_depth=50., source_model_name="POINT SOURCE MODEL", trt=DEFAULT_TRT, msr=DEFAULT_MSR, aspect=ASPECT_RATIO, hdd=None): """ Converts the smoothed seismicity data to a set of oq-nrml point sources and writes to the output file """ writer = SourceModelXMLWriter(output_filename) source_model = models.SourceModel(source_model_name) source_model.sources = [] print 'Building source model ...' for iloc, row in enumerate(self.data): # Geometry #trt =(row[18]) geom = models.PointGeometry( "POINT (%9.4f %9.4f)" % (row[0], row[1]), upper_depth, lower_depth) if hdd: src_hdd = [] #each tuple with probality and depth for d in hdd: src_hdd.append(models.HypocentralDepth(d[1], d[0])) else: src_hdd = [models.HypocentralDepth(Decimal("1.0"), row[2])] npd = [models.NodalPlane(1, 0, 90, 0)] #if row[5]==1: # npd = [models.NodalPlane(row[6], row[7], row[9], row[8])] #elif row[5] ==2: # npd = [models.NodalPlane(row[6], row[7], row[9], row[8]), models.NodalPlane(row[10], row[11], row[13], row[12])] #else: # npd = [models.NodalPlane(row[6], row[7], row[9], row[8]), models.NodalPlane(row[10], row[11], row[13], row[12]), models.NodalPlane(row[14], row[15], row[17], row[16])] source_model.sources.append( models.PointSource(str(iloc), "PNT_%s" % str(iloc), trt, geom, msr, aspect, self.get_mfd(iloc, row, mmin), npd, src_hdd)) print 'done!' print 'Writing to file ...' writer.serialize(source_model) print 'done!'
def setUp(self): self.area_source_attrib = dict( id='1', name='source A', trt='Active Shallow Crust', geometry=nrml_models.AreaGeometry( upper_seismo_depth=10, lower_seismo_depth=20, wkt=('POLYGON((1.0 1.0, 1.0 -1.0, -1.0 -1.0, -1.0 1.0, ' '1.0 1.0))'), ), mag_scale_rel='WC1994', rupt_aspect_ratio=1.0, mfd=None, nodal_plane_dist=[ nrml_models.NodalPlane(probability=1.0, strike=0.0, dip=90.0, rake=0.0) ], hypo_depth_dist=[ nrml_models.HypocentralDepth(probability=1.0, depth=10.0) ], ) self.expected = [] lons = [ -0.100677001712, 0.798645996576, -0.100591068089, 0.798817863822 ] lats = [ 0.100830691185, 0.100830691185, -0.798490914733, -0.798490914733 ] for i, (lon, lat) in enumerate(zip(lons, lats)): point_attrib = self.area_source_attrib.copy() del point_attrib['geometry'] point_attrib['id'] = '1-%s' % i point_attrib['name'] = 'source A-%s' % i pt_source = nrml_models.PointSource(**point_attrib) pt_source.geometry = nrml_models.PointGeometry( upper_seismo_depth=10, lower_seismo_depth=20, wkt='POINT(%s %s)' % (lon, lat), ) self.expected.append(pt_source)
def _parse_nodal_plane_dist(cls, src_elem): """ :param src_elem: :class:`lxml.etree._Element` instance representing a source. :returns: `list` of :class:`openquake.nrmllib.models.NodalPlane` objects. """ npd = [] for elem in _xpath(src_elem, './/nrml:nodalPlane'): nplane = models.NodalPlane() nplane.probability = decimal.Decimal(elem.get('probability')) nplane.strike = float(elem.get('strike')) nplane.dip = float(elem.get('dip')) nplane.rake = float(elem.get('rake')) npd.append(nplane) return npd
def _parse_nodal_plane_dist(cls, src_elem): """ :param src_elem: :class:`lxml.etree._Element` instance representing a source. :returns: `list` of :class:`openquake.nrmllib.models.NodalPlane` objects. """ npd = [] value_set = False for elem in _xpath(src_elem, './/nrml:nodalPlane'): nplane = models.NodalPlane() if elem.get('probability'): nplane.probability = decimal.Decimal(elem.get('probability')) for attribute in ['strike', 'dip', 'rake']: if elem.get(attribute): setattr(nplane, attribute, float(elem.get(attribute))) value_set = True npd.append(nplane) if value_set: return npd else: return None
""" import argparse import numpy as np from decimal import Decimal from openquake.nrmllib import models from openquake.nrmllib.hazard.writers import SourceModelXMLWriter DEFAULT_MSR = "WC1994" DEFAULT_TRT = "Active Shallow Crust" ASPECT_RATIO = 1 MIN_MAGNITUDE = 4 DEFAULT_RAKE = 0. #Probability, Strike, Dip, Rake NODAL_PLANES = [ models.NodalPlane(Decimal("0.25"), 0., 90., DEFAULT_RAKE), models.NodalPlane(Decimal("0.25"), 45., 90., DEFAULT_RAKE), models.NodalPlane(Decimal("0.25"), 90., 90., DEFAULT_RAKE), models.NodalPlane(Decimal("0.25"), 135., 90., DEFAULT_RAKE) ] class SmoothedSeismicity2Nrml(object): """ Converts hmtk smoothed seismicity output to OQ Point Source Model """ def __init__(self, input_filename, bvalue=None, mmax=None): """ """ self.data = np.genfromtxt(input_filename, delimiter=',',
def _expected_source_model(cls): # Area: area_geom = models.AreaGeometry( wkt=('POLYGON((-122.5 37.5, -121.5 37.5, -121.5 38.5, -122.5 38.5,' ' -122.5 37.5))'), upper_seismo_depth=0.0, lower_seismo_depth=10.0, ) area_mfd = models.IncrementalMFD( min_mag=6.55, bin_width=0.1, occur_rates=[ 0.0010614989, 8.8291627E-4, 7.3437777E-4, 6.108288E-4, 5.080653E-4 ], ) area_npd = [ models.NodalPlane(probability=decimal.Decimal("0.3"), strike=0.0, dip=90.0, rake=0.0), models.NodalPlane(probability=decimal.Decimal("0.7"), strike=90.0, dip=45.0, rake=90.0), ] area_hdd = [ models.HypocentralDepth(probability=decimal.Decimal("0.5"), depth=4.0), models.HypocentralDepth(probability=decimal.Decimal("0.5"), depth=8.0), ] area_src = models.AreaSource( id='1', name='Quito', trt='Active Shallow Crust', geometry=area_geom, mag_scale_rel='PeerMSR', rupt_aspect_ratio=1.5, mfd=area_mfd, nodal_plane_dist=area_npd, hypo_depth_dist=area_hdd, ) # Point: point_geom = models.PointGeometry( wkt='POINT(-122.0 38.0)', upper_seismo_depth=0.0, lower_seismo_depth=10.0, ) point_mfd = models.TGRMFD( a_val=-3.5, b_val=1.0, min_mag=5.0, max_mag=6.5, ) point_npd = [ models.NodalPlane(probability=decimal.Decimal("0.3"), strike=0.0, dip=90.0, rake=0.0), models.NodalPlane(probability=decimal.Decimal("0.7"), strike=90.0, dip=45.0, rake=90.0), ] point_hdd = [ models.HypocentralDepth(probability=decimal.Decimal("0.5"), depth=4.0), models.HypocentralDepth(probability=decimal.Decimal("0.5"), depth=8.0), ] point_src = models.PointSource( id='2', name='point', trt='Stable Continental Crust', geometry=point_geom, mag_scale_rel='WC1994', rupt_aspect_ratio=0.5, mfd=point_mfd, nodal_plane_dist=point_npd, hypo_depth_dist=point_hdd, ) # Simple: simple_geom = models.SimpleFaultGeometry( wkt='LINESTRING(-121.82290 37.73010, -122.03880 37.87710)', dip=45.0, upper_seismo_depth=10.0, lower_seismo_depth=20.0, ) simple_mfd = models.IncrementalMFD( min_mag=5.0, bin_width=0.1, occur_rates=[ 0.0010614989, 8.8291627E-4, 7.3437777E-4, 6.108288E-4, 5.080653E-4 ], ) simple_src = models.SimpleFaultSource( id='3', name='Mount Diablo Thrust', trt='Active Shallow Crust', geometry=simple_geom, mag_scale_rel='WC1994', rupt_aspect_ratio=1.5, mfd=simple_mfd, rake=30.0, ) # Complex: complex_geom = models.ComplexFaultGeometry( top_edge_wkt=('LINESTRING(-124.704 40.363 0.5493260E+01, ' '-124.977 41.214 0.4988560E+01, ' '-125.140 42.096 0.4897340E+01)'), bottom_edge_wkt=('LINESTRING(-123.829 40.347 0.2038490E+02, ' '-124.137 41.218 0.1741390E+02, ' '-124.252 42.115 0.1752740E+02)'), int_edges=[ ('LINESTRING(-124.704 40.363 0.5593260E+01, ' '-124.977 41.214 0.5088560E+01, ' '-125.140 42.096 0.4997340E+01)'), ('LINESTRING(-124.704 40.363 0.5693260E+01, ' '-124.977 41.214 0.5188560E+01, ' '-125.140 42.096 0.5097340E+01)'), ]) complex_mfd = models.TGRMFD(a_val=-3.5, b_val=1.0, min_mag=5.0, max_mag=6.5) complex_src = models.ComplexFaultSource( id='4', name='Cascadia Megathrust', trt='Subduction Interface', geometry=complex_geom, mag_scale_rel='WC1994', rupt_aspect_ratio=2.0, mfd=complex_mfd, rake=30.0, ) # 3 Characteristic Sources: char_src_simple = models.CharacteristicSource( id='5', name='characteristic source, simple fault', trt='Volcanic', mfd=models.TGRMFD(a_val=-3.5, b_val=1.0, min_mag=5.0, max_mag=6.5), rake=30.0, surface=simple_geom) char_src_complex = models.CharacteristicSource( id='6', name='characteristic source, complex fault', trt='Volcanic', mfd=models.IncrementalMFD( min_mag=5.0, bin_width=0.1, occur_rates=[ 0.0010614989, 8.8291627E-4, 7.3437777E-4, 6.108288E-4, 5.080653E-4 ], ), rake=60.0, surface=complex_geom) char_src_multi = models.CharacteristicSource( id='7', name='characteristic source, multi surface', trt='Volcanic', mfd=models.TGRMFD(a_val=-3.6, b_val=1.0, min_mag=5.2, max_mag=6.4), rake=90.0) psurface_1 = models.PlanarSurface( strike=0.0, dip=90.0, top_left=models.Point(longitude=-1.0, latitude=1.0, depth=21.0), top_right=models.Point(longitude=1.0, latitude=1.0, depth=21.0), bottom_left=models.Point(longitude=-1.0, latitude=-1.0, depth=59.0), bottom_right=models.Point(longitude=1.0, latitude=-1.0, depth=59.0), ) psurface_2 = models.PlanarSurface( strike=20.0, dip=45.0, top_left=models.Point(longitude=1.0, latitude=1.0, depth=20.0), top_right=models.Point(longitude=3.0, latitude=1.0, depth=20.0), bottom_left=models.Point(longitude=1.0, latitude=-1.0, depth=80.0), bottom_right=models.Point(longitude=3.0, latitude=-1.0, depth=80.0), ) char_src_multi.surface = [psurface_1, psurface_2] source_model = models.SourceModel() source_model.name = 'Some Source Model' # Generator: source_model.sources = (x for x in [ area_src, point_src, simple_src, complex_src, char_src_simple, char_src_complex, char_src_multi ]) return source_model
trt = "stable crust", geometry = geo.Polygon([geo.Point(-58.73, -10.33), geo.Point(-58.74, -13.54), geo.Point(-55.94, -13.59), geo.Point(-56.58, -10.31), geo.Point(-58.73, -10.33)]), upper_depth = "0", lower_depth = "30", 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) #