def setUp(self): self.s1 = models.SourceModel(name='s1') self.a1 = models.AreaSource( name='a1', geometry=models.AreaGeometry(wkt=self.POLY)) self.p1 = models.PointSource( name='p1', geometry=models.PointGeometry(wkt=self.POINT)) self.s1.sources = [self.a1, self.p1] self.s2 = models.SourceModel(name='s1') self.a2 = models.AreaSource( name='a1', geometry=models.AreaGeometry(wkt=self.POLY)) self.p2 = models.PointSource( name='p1', geometry=models.PointGeometry(wkt=self.POINT)) self.s2.sources = [self.a2, self.p2]
def parse(self): """Parse the source XML content and generate a source model in object form. :returns: :class:`openquake.nrmllib.models.SourceModel` instance. """ src_model = models.SourceModel() schema = etree.XMLSchema( etree.parse(openquake.nrmllib.nrml_schema_file())) tree = etree.iterparse(self.source, events=('start', 'end'), schema=schema) 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) return src_model
def optimize_source_model(input_path, area_src_disc, output_path): """ Parse the source model located at ``input_path``, discretize area sources by ``area_src_disc``, and write the optimized model to ``output_path``. :returns: ``output_path`` """ parser = haz_parsers.SourceModelParser(input_path) src_model = parser.parse() def split_area(model): for src in model: if isinstance(src, nrml_models.AreaSource): for pt in area_source_to_point_sources(src, area_src_disc): yield pt else: yield src out_source_model = nrml_models.SourceModel(name=src_model.name, sources=split_area(src_model)) writer = haz_writers.SourceModelXMLWriter(output_path) writer.serialize(out_source_model) return output_path
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 serialise_to_nrml(self, filename, use_defaults=False): ''' Writes the source model to a nrml source model file given by the filename :param str filename: Path to output file :param bool use_defaults: Boolean to indicate whether to use default values (True) or not. If set to False, ValueErrors will be raised when an essential attribute is missing. ''' output_model = models.SourceModel(name=self.name, sources=[]) for source in self.sources: output_model.sources.append( source.create_oqnrml_source(use_defaults)) writer = SourceModelXMLWriter(filename) writer.serialize(output_model)
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