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 test_parse(self): parser = parsers.SourceModelParser(self.SAMPLE_FILE) exp_src_model = self._expected_source_model() src_model = parser.parse() self.assertTrue(*_utils.deep_eq(exp_src_model, src_model))
def test_probs_sum_to_1(self): # We want to test that distribution probabilities sum to 1. # Example source model with an area and a point source. source_xml = '''\ <?xml version='1.0' encoding='utf-8'?> <nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4"> <sourceModel name="Some Source Model"> <areaSource id="1" name="Quito" tectonicRegion="Active Shallow Crust"> <areaGeometry> <gml:Polygon> <gml:exterior> <gml:LinearRing> <gml:posList> -122.5 37.5 -121.5 37.5 -121.5 38.5 -122.5 38.5 </gml:posList> </gml:LinearRing> </gml:exterior> </gml:Polygon> <upperSeismoDepth>0.0</upperSeismoDepth> <lowerSeismoDepth>10.0</lowerSeismoDepth> </areaGeometry> <magScaleRel>PeerMSR</magScaleRel> <ruptAspectRatio>1.5</ruptAspectRatio> <incrementalMFD minMag="6.55" binWidth="0.1"> <occurRates>0.0010614989 8.8291627E-4 7.3437777E-4 6.108288E-4 5.080653E-4</occurRates> </incrementalMFD> <nodalPlaneDist> <nodalPlane probability="0.1" strike="1.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="2.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="3.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="4.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="5.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="6.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="7.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="8.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="9.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.1" strike="10.0" dip="90.0" rake="0.0" /> </nodalPlaneDist> <hypoDepthDist> <hypoDepth probability="0.3" depth="4.0" /> <hypoDepth probability="0.3" depth="5.0" /> <hypoDepth probability="0.3" depth="6.0" /> <hypoDepth probability="0.1" depth="7.0" /> </hypoDepthDist> </areaSource> <pointSource id="2" name="point" tectonicRegion="Stable Continental Crust"> <pointGeometry> <gml:Point> <gml:pos>-122.0 38.0</gml:pos> </gml:Point> <upperSeismoDepth>0.0</upperSeismoDepth> <lowerSeismoDepth>10.0</lowerSeismoDepth> </pointGeometry> <magScaleRel>WC1994</magScaleRel> <ruptAspectRatio>0.5</ruptAspectRatio> <truncGutenbergRichterMFD aValue="-3.5" bValue="1.0" minMag="5.0" maxMag="6.5" /> <nodalPlaneDist> <nodalPlane probability="0.3" strike="0.0" dip="90.0" rake="0.0" /> <nodalPlane probability="0.7" strike="90.0" dip="45.0" rake="90.0" /> </nodalPlaneDist> <hypoDepthDist> <hypoDepth probability="0.5" depth="4.0" /> <hypoDepth probability="0.5" depth="8.0" /> </hypoDepthDist> </pointSource> </sourceModel> </nrml>''' parser = parsers.SourceModelParser(StringIO.StringIO(source_xml)) src_model = list(parser.parse()) for src in src_model: self.assertEqual(1.0, sum([x.probability for x in src.hypo_depth_dist])) self.assertEqual( 1.0, sum([x.probability for x in src.nodal_plane_dist]))
def test_invalid_schema(self): parser = parsers.SourceModelParser( StringIO.StringIO(self.INVALID_SCHEMA)) self.assertRaises(etree.XMLSyntaxError, parser.parse)
def test_nrml_elem_not_found(self): parser = parsers.SourceModelParser( StringIO.StringIO(self.NO_NRML_ELEM_FIRST)) self.assertRaises(etree.XMLSyntaxError, parser.parse)
def test_wrong_namespace(self): parser = parsers.SourceModelParser( StringIO.StringIO(self.BAD_NAMESPACE)) self.assertRaises(etree.XMLSyntaxError, parser.parse)