Beispiel #1
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <totalDmgDist>
    <damageStates>no_damage slight moderate extensive complete</damageStates>
    <damage ds="no_damage" mean="1.0" stddev="1.6"/>
    <damage ds="slight" mean="34.8" stddev="18.3"/>
    <damage ds="moderate" mean="64.2" stddev="19.8"/>
    <damage ds="extensive" mean="64.3" stddev="19.7"/>
    <damage ds="complete" mean="64.3" stddev="19.7"/>
  </totalDmgDist>
</nrml>''')

        damage_states = 'no_damage slight moderate extensive complete'.split()
        writer = writers.DmgDistTotalXMLWriter(self.filename, damage_states)
        data = [
            (NO_DAMAGE, 1.0, 1.6),
            (SLIGHT, 34.8, 18.3),
            (MODERATE, 64.2, 19.8),
            (EXTENSIVE, 64.3, 19.7),
            (COMPLETE, 64.3, 19.7),
        ]
        writer.serialize(_starmap(DMG_DIST_TOTAL, data))

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #2
0
    def test_serialize_using_hazard_realization_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8"
           sourceModelTreePath="b1|b2" gsimTreePath="b3|b4"
           lossCategory="economic" unit="USD">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" value="15.23"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(
            self.filename,
            investigation_time=10.0, poe=0.80, source_model_tree_path="b1|b2",
            gsim_tree_path="b3|b4", unit="USD", loss_category="economic")

        data = [LOSS_NODE(asset_ref="asset_1",
                          location=Point(1.0, 1.5), value=15.23, std_dev=None)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #3
0
    def test_serialize(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardMap sourceModelTreePath="b1_b2_b4" gsimTreePath="b1_b4_b5" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0" poE="0.1">
    <node lon="-1.0" lat="1.0" iml="0.01"/>
    <node lon="1.0" lat="1.0" iml="0.02"/>
    <node lon="1.0" lat="-1.0" iml="0.03"/>
    <node lon="-1.0" lat="-1.0" iml="0.04"/>
  </hazardMap>
</nrml>
""")

        metadata = dict(investigation_time=50.0,
                        imt='SA',
                        poe=0.1,
                        sa_period=0.025,
                        sa_damping=5.0,
                        smlt_path='b1_b2_b4',
                        gsimlt_path='b1_b4_b5')
        writer = writers.HazardMapXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #4
0
    def test_serialize_statistics_metadata(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml
  xmlns:gml="http://www.opengis.net/gml"
  xmlns="http://openquake.org/xmlns/nrml/0.4">
  <aggregateLossCurve
    investigationTime="10.0"
    statistics="quantile"
    quantileValue="0.5">
    <poEs>1.0 0.5 0.1</poEs>
    <losses>10.0000 20.0000 30.0000</losses>
    <averageLoss>2.0000e+00</averageLoss>
  </aggregateLossCurve>
</nrml>
""")

        writer = writers.AggregateLossCurveXMLWriter(
            self.filename,
            investigation_time=10.0, statistics="quantile",
            quantile_value=0.50)

        data = AGGREGATE_LOSS_CURVE(
            poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0], average_loss=2.)

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #5
0
    def test_serialize_optional_metadata_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8" statistics="quantile"
        quantileValue="0.5" lossCategory="economic" unit="USD">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" mean="15.23" stdDev="2"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(
            self.filename,
            investigation_time=10.0, poe=0.80, statistics="quantile",
            quantile_value=0.50, unit="USD", loss_category="economic"
        )

        data = [LOSS_NODE(
                asset_ref="asset_1", location=Point(1.0, 1.5), value=15.23,
                std_dev=2)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #6
0
    def test_serialize_statistics_metadata(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossCurves investigationTime="10.0"
              statistics="quantile" quantileValue="0.5">
    <lossCurve assetRef="asset_1">
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.5 0.1</poEs>
      <losses>10.0 20.0 30.0</losses>
      <lossRatios>0.4 0.6 1.8</lossRatios>
      <averageLoss>0.0000e+00</averageLoss>
    </lossCurve>
  </lossCurves>
</nrml>
""")

        writer = writers.LossCurveXMLWriter(
            self.filename,
            investigation_time=10.0, statistics="quantile",
            quantile_value=0.50)

        data = [LOSS_CURVE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0],
                loss_ratios=[0.4, 0.6, 1.8], average_loss=0.)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #7
0
    def test_serialize_a_model(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml
  xmlns:gml="http://www.opengis.net/gml"
  xmlns="http://openquake.org/xmlns/nrml/0.4">
  <aggregateLossCurve
    investigationTime="10.0"
    sourceModelTreePath="b1_b2_b3"
    gsimTreePath="b1_b2"
    unit="USD">
    <poEs>1.0 0.5 0.1</poEs>
    <losses>10.0000 20.0000 30.0000</losses>
    <averageLoss>3.0000e+00</averageLoss>
  </aggregateLossCurve>
</nrml>
""")

        writer = writers.AggregateLossCurveXMLWriter(
            self.filename,
            investigation_time=10.0, source_model_tree_path="b1_b2_b3",
            gsim_tree_path="b1_b2", unit="USD")

        data = AGGREGATE_LOSS_CURVE(
            poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0], average_loss=3.)

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #8
0
    def test_serialize_a_model_xml(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossMap investigationTime="10.0" poE="0.8" statistics="mean">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_1" value="15.23"/>
      <loss assetRef="asset_2" value="16.23"/>
    </node>
    <node>
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <loss assetRef="asset_3" value="17.23"/>
    </node>
  </lossMap>
</nrml>
""")

        writer = writers.LossMapXMLWriter(
            self.filename, investigation_time=10.0, poe=0.8,
            statistics="mean"
        )

        writer.serialize(self.data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #9
0
    def test_serialize_using_hazard_realization(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" sourceModelTreePath="b1|b2" gsimTreePath="b1|b2" lossCategory="economic" unit="USD">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_1" ratio="15.23" aalOrig="10.5" aalRetr="20.5"/>
    </node>
  </bcrMap>
</nrml>
""")

        writer = writers.BCRMapXMLWriter(
            self.filename,
            interest_rate=10.0, asset_life_expectancy=50.0,
            source_model_tree_path="b1|b2", gsim_tree_path="b1|b2",
            unit="USD", loss_category="economic")

        data = [BCR_NODE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                bcr=15.23, average_annual_loss_original=10.5,
                average_annual_loss_retrofitted=20.5)]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #10
0
    def test_serialize_quantile(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardMap statistics="quantile" quantileValue="0.85" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0" poE="0.1">
    <node lon="-1.0" lat="1.0" iml="0.01"/>
    <node lon="1.0" lat="1.0" iml="0.02"/>
    <node lon="1.0" lat="-1.0" iml="0.03"/>
    <node lon="-1.0" lat="-1.0" iml="0.04"/>
  </hazardMap>
</nrml>
""")

        _, self.path = tempfile.mkstemp()
        metadata = dict(investigation_time=50.0,
                        imt='SA',
                        poe=0.1,
                        sa_period=0.025,
                        sa_damping=5.0,
                        statistics='quantile',
                        quantile_value=0.85)
        writer = writers.HazardMapXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #11
0
    def test_serialize_a_model(self):
        expected = StringIO.StringIO(
            """\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" statistics="mean">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_1" ratio="15.23" aalOrig="10.5" aalRetr="20.5"/>
      <bcr assetRef="asset_2" ratio="16.23" aalOrig="11.5" aalRetr="40.5"/>
    </node>
    <node>
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_3" ratio="17.23" aalOrig="12.5" aalRetr="10.5"/>
    </node>
  </bcrMap>
</nrml>
"""
        )

        writer = writers.BCRMapXMLWriter(
            self.filename, interest_rate=10.0, asset_life_expectancy=50.0, statistics="mean"
        )

        data = [
            BCR_NODE(
                asset_ref="asset_1",
                location=Point(1.0, 1.5),
                bcr=15.23,
                average_annual_loss_original=10.5,
                average_annual_loss_retrofitted=20.5,
            ),
            BCR_NODE(
                asset_ref="asset_2",
                location=Point(1.0, 1.5),
                bcr=16.23,
                average_annual_loss_original=11.5,
                average_annual_loss_retrofitted=40.5,
            ),
            BCR_NODE(
                asset_ref="asset_3",
                location=Point(2.0, 2.5),
                bcr=17.23,
                average_annual_loss_original=12.5,
                average_annual_loss_retrofitted=10.5,
            ),
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #12
0
    def test_serialize(self):
        try:
            _, path = tempfile.mkstemp()
            writer = writers.UHSXMLWriter(path, **self.metadata)

            writer.serialize(self.data)

            utils.assert_xml_equal(self.expected_xml, path)
        finally:
            os.unlink(path)
Beispiel #13
0
    def test_serialize(self):
        locations = [Location(i * 0.1, i * 0.1) for i in xrange(12)]
        gmf_nodes = [GmfNode(i * 0.2, locations[i])
                     for i in xrange(12)]
        gmfs = [
            Gmf('SA', 0.1, 5.0, gmf_nodes[:2]),
            Gmf('SA', 0.2, 5.0, gmf_nodes[2:4]),
            Gmf('SA', 0.3, 5.0, gmf_nodes[4:6]),
            Gmf('PGA', None, None, gmf_nodes[6:8]),
            Gmf('PGA', None, None, gmf_nodes[8:10]),
            Gmf('PGA', None, None, gmf_nodes[10:]),
        ]

        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <gmfSet>
    <gmf IMT="SA" saPeriod="0.1" saDamping="5.0">
      <node gmv="0.0" lon="0.0" lat="0.0"/>
      <node gmv="0.2" lon="0.1" lat="0.1"/>
    </gmf>
    <gmf IMT="SA" saPeriod="0.2" saDamping="5.0">
      <node gmv="0.4" lon="0.2" lat="0.2"/>
      <node gmv="0.6" lon="0.3" lat="0.3"/>
    </gmf>
    <gmf IMT="SA" saPeriod="0.3" saDamping="5.0">
      <node gmv="0.8" lon="0.4" lat="0.4"/>
      <node gmv="1.0" lon="0.5" lat="0.5"/>
    </gmf>
    <gmf IMT="PGA">
      <node gmv="1.2" lon="0.6" lat="0.6"/>
      <node gmv="1.4" lon="0.7" lat="0.7"/>
    </gmf>
    <gmf IMT="PGA">
      <node gmv="1.6" lon="0.8" lat="0.8"/>
      <node gmv="1.8" lon="0.9" lat="0.9"/>
    </gmf>
    <gmf IMT="PGA">
      <node gmv="2.0" lon="1.0" lat="1.0"/>
      <node gmv="2.2" lon="1.1" lat="1.1"/>
    </gmf>
  </gmfSet>
</nrml>
""")
        try:
            # Make a temp file to save the results to:
            _, path = tempfile.mkstemp()
            writer = writers.ScenarioGMFXMLWriter(path)
            writer.serialize(gmfs)
            utils.assert_xml_equal(expected, path)
            self.assertTrue(utils.validates_against_xml_schema(path))
        finally:
            os.unlink(path)
Beispiel #14
0
    def test_serialize(self):
        locations = [Location(i * 0.1, i * 0.1) for i in xrange(12)]
        gmf_nodes = [GmfNode(i * 0.2, locations[i]) for i in xrange(12)]
        gmfs = [
            Gmf('SA', 0.1, 5.0, gmf_nodes[:2]),
            Gmf('SA', 0.2, 5.0, gmf_nodes[2:4]),
            Gmf('SA', 0.3, 5.0, gmf_nodes[4:6]),
            Gmf('PGA', None, None, gmf_nodes[6:8]),
            Gmf('PGA', None, None, gmf_nodes[8:10]),
            Gmf('PGA', None, None, gmf_nodes[10:]),
        ]

        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <gmfSet>
    <gmf IMT="SA" saPeriod="0.1" saDamping="5.0">
      <node gmv="0.0" lon="0.0" lat="0.0"/>
      <node gmv="0.2" lon="0.1" lat="0.1"/>
    </gmf>
    <gmf IMT="SA" saPeriod="0.2" saDamping="5.0">
      <node gmv="0.4" lon="0.2" lat="0.2"/>
      <node gmv="0.6" lon="0.3" lat="0.3"/>
    </gmf>
    <gmf IMT="SA" saPeriod="0.3" saDamping="5.0">
      <node gmv="0.8" lon="0.4" lat="0.4"/>
      <node gmv="1.0" lon="0.5" lat="0.5"/>
    </gmf>
    <gmf IMT="PGA">
      <node gmv="1.2" lon="0.6" lat="0.6"/>
      <node gmv="1.4" lon="0.7" lat="0.7"/>
    </gmf>
    <gmf IMT="PGA">
      <node gmv="1.6" lon="0.8" lat="0.8"/>
      <node gmv="1.8" lon="0.9" lat="0.9"/>
    </gmf>
    <gmf IMT="PGA">
      <node gmv="2.0" lon="1.0" lat="1.0"/>
      <node gmv="2.2" lon="1.1" lat="1.1"/>
    </gmf>
  </gmfSet>
</nrml>
""")
        try:
            # Make a temp file to save the results to:
            _, path = tempfile.mkstemp()
            writer = writers.ScenarioGMFXMLWriter(path)
            writer.serialize(gmfs)
            utils.assert_xml_equal(expected, path)
            self.assertTrue(utils.validates_against_xml_schema(path))
        finally:
            os.unlink(path)
Beispiel #15
0
    def test_serialize_an_insured_loss_curve(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossCurves  insured="True" investigationTime="10.0"
    sourceModelTreePath="b1_b2_b3" gsimTreePath="b1_b2" unit="USD">
    <lossCurve assetRef="asset_1">
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.5 0.1</poEs>
      <losses>10.0 20.0 30.0</losses>
      <averageLoss>1.0000e+00</averageLoss>
      <stdDevLoss>5.0000e-01</stdDevLoss>
    </lossCurve>
    <lossCurve assetRef="asset_2">
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.3 0.2</poEs>
      <losses>20.0 30.0 40.0</losses>
      <averageLoss>2.0000e+00</averageLoss>
      <stdDevLoss>1.0000e-01</stdDevLoss>
    </lossCurve>
  </lossCurves>
</nrml>
""")

        writer = writers.LossCurveXMLWriter(
            self.filename,
            investigation_time=10.0, source_model_tree_path="b1_b2_b3",
            gsim_tree_path="b1_b2", unit="USD", insured=True)

        data = [
            LOSS_CURVE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0],
                loss_ratios=None, average_loss=1., stddev_loss=0.5),

            LOSS_CURVE(
                asset_ref="asset_2", location=Point(2.0, 2.5),
                poes=[1.0, 0.3, 0.2], losses=[20.0, 30.0, 40.0],
                loss_ratios=None, average_loss=2., stddev_loss=0.1),
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #16
0
    def test_serialize_mean(self):
        del self.metadata['smlt_path']
        del self.metadata['gsimlt_path']
        self.metadata['statistics'] = 'mean'

        try:
            _, path = tempfile.mkstemp()
            writer = writers.UHSXMLWriter(path, **self.metadata)

            writer.serialize(self.data)

            utils.assert_xml_equal(self.expected_mean_xml, path)
        finally:
            os.unlink(path)
Beispiel #17
0
    def test_serialize_a_model(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
  <lossCurves investigationTime="10.0"
              sourceModelTreePath="b1_b2_b3"
              gsimTreePath="b1_b2" unit="USD">
    <lossCurve assetRef="asset_1">
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.5 0.1</poEs>
      <losses>10.0 20.0 30.0</losses>
      <averageLoss>5.0000e+00</averageLoss>
    </lossCurve>
    <lossCurve assetRef="asset_2">
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <poEs>1.0 0.3 0.2</poEs>
      <losses>20.0 30.0 40.0</losses>
      <averageLoss>3.0000e+00</averageLoss>
    </lossCurve>
  </lossCurves>
</nrml>
""")

        writer = writers.LossCurveXMLWriter(
            self.filename, investigation_time=10.0,
            source_model_tree_path="b1_b2_b3",
            gsim_tree_path="b1_b2", unit="USD")

        data = [
            LOSS_CURVE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                poes=[1.0, 0.5, 0.1], losses=[10.0, 20.0, 30.0],
                loss_ratios=None, average_loss=5.),

            LOSS_CURVE(
                asset_ref="asset_2", location=Point(2.0, 2.5),
                poes=[1.0, 0.3, 0.2], losses=[20.0, 30.0, 40.0],
                loss_ratios=None, average_loss=3.),
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #18
0
    def test_chain_parse_serialize(self):
        # Chain a parser together to with a serializer and test that the
        # produced XML is unchanged.
        for example in ('hazard-curves-mean.xml', 'hazard-curves-pga.xml',
                        'hazard-curves-quantile.xml', 'hazard-curves-sa.xml'):
            infile = os.path.join('tests/data', example)
            hcp = parsers.HazardCurveParser(infile)
            parsed_model = hcp.parse()
            _, outfile = tempfile.mkstemp()
            try:
                hcw = writers.HazardCurveXMLWriter(outfile,
                                                   **parsed_model.__dict__)
                hcw.serialize(parsed_model)

                _utils.assert_xml_equal(infile, outfile)
            finally:
                os.unlink(outfile)
Beispiel #19
0
    def test_serialize(self):
        metaelem = (
            '<disaggMatrices sourceModelTreePath="b1_b2_b3" '
            'gsimTreePath="b1_b7_b15" IMT="SA" investigationTime="50.0" '
            'saPeriod="0.1" saDamping="5.0" lon="8.33" lat="47.22" '
            'magBinEdges="5, 6" distBinEdges="0, 20, 40" '
            'lonBinEdges="6, 7, 8, 9, 10" latBinEdges="46, 47, 48, 49, 50" '
            'epsBinEdges="-0.5, 0.5, 1.5, 2.5" '
            'tectonicRegionTypes="active shallow crust, stable continental">')
        self.expected_xml %= dict(metaelem=metaelem)

        writer = writers.DisaggXMLWriter(self.path, **self.metadata)
        writer.serialize(self.data)

        expected = StringIO.StringIO(self.expected_xml)
        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #20
0
    def test_serialize_a_model(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" statistics="mean">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_1" ratio="15.23" aalOrig="10.5" aalRetr="20.5"/>
      <bcr assetRef="asset_2" ratio="16.23" aalOrig="11.5" aalRetr="40.5"/>
    </node>
    <node>
      <gml:Point>
        <gml:pos>2.0 2.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_3" ratio="17.23" aalOrig="12.5" aalRetr="10.5"/>
    </node>
  </bcrMap>
</nrml>
""")

        writer = writers.BCRMapXMLWriter(
            self.filename,
            interest_rate=10.0, asset_life_expectancy=50.0, statistics="mean")

        data = [
            BCR_NODE(
                asset_ref="asset_1", location=Point(1.0, 1.5),
                bcr=15.23, average_annual_loss_original=10.5,
                average_annual_loss_retrofitted=20.5),
            BCR_NODE(
                asset_ref="asset_2", location=Point(1.0, 1.5),
                bcr=16.23, average_annual_loss_original=11.5,
                average_annual_loss_retrofitted=40.5),
            BCR_NODE(
                asset_ref="asset_3", location=Point(2.0, 2.5),
                bcr=17.23, average_annual_loss_original=12.5,
                average_annual_loss_retrofitted=10.5),
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #21
0
    def test_serialize(self):
        metaelem = (
            '<disaggMatrices sourceModelTreePath="b1_b2_b3" '
            'gsimTreePath="b1_b7_b15" IMT="SA" investigationTime="50.0" '
            'saPeriod="0.1" saDamping="5.0" lon="8.33" lat="47.22" '
            'magBinEdges="5, 6" distBinEdges="0, 20, 40" '
            'lonBinEdges="6, 7, 8, 9, 10" latBinEdges="46, 47, 48, 49, 50" '
            'epsBinEdges="-0.5, 0.5, 1.5, 2.5" '
            'tectonicRegionTypes="active shallow crust, stable continental">'
        )
        self.expected_xml %= dict(metaelem=metaelem)

        writer = writers.DisaggXMLWriter(self.path, **self.metadata)
        writer.serialize(self.data)

        expected = StringIO.StringIO(self.expected_xml)
        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #22
0
    def test_chain_parse_serialize(self):
        # Chain a parser together to with a serializer and test that the
        # produced XML is unchanged.
        for example in ('hazard-curves-mean.xml', 'hazard-curves-pga.xml',
                        'hazard-curves-quantile.xml', 'hazard-curves-sa.xml'):
            infile = os.path.join('tests/data', example)
            hcp = parsers.HazardCurveXMLParser(infile)
            parsed_model = hcp.parse()
            _, outfile = tempfile.mkstemp()
            try:
                hcw = writers.HazardCurveXMLWriter(
                    outfile, **parsed_model.metadata
                )
                hcw.serialize(parsed_model)

                _utils.assert_xml_equal(infile, outfile)
            finally:
                os.unlink(outfile)
Beispiel #23
0
    def test_serialize(self):
        expected = StringIO.StringIO(
            """<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <dmgDistPerTaxonomy>
    <damageStates>no_damage slight moderate extensive complete</damageStates>
    <DDNode>
      <taxonomy>RC</taxonomy>
      <damage ds="no_damage" mean="1.0" stddev="1.6"/>
      <damage ds="slight" mean="34.8" stddev="18.3"/>
      <damage ds="moderate" mean="64.2" stddev="19.8"/>
      <damage ds="extensive" mean="64.3" stddev="19.7"/>
      <damage ds="complete" mean="64.3" stddev="19.7"/>
    </DDNode>
    <DDNode>
      <taxonomy>RM</taxonomy>
      <damage ds="no_damage" mean="1.0" stddev="1.6"/>
      <damage ds="slight" mean="34.8" stddev="18.3"/>
      <damage ds="moderate" mean="64.2" stddev="19.8"/>
      <damage ds="extensive" mean="64.3" stddev="19.7"/>
      <damage ds="complete" mean="64.3" stddev="19.7"/>
    </DDNode>
  </dmgDistPerTaxonomy>
</nrml>"""
        )
        dmg_states = "no_damage slight moderate extensive complete".split()
        data = [
            ("RC", NO_DAMAGE, 1.0, 1.6),
            ("RC", SLIGHT, 34.8, 18.3),
            ("RC", MODERATE, 64.2, 19.8),
            ("RC", EXTENSIVE, 64.3, 19.7),
            ("RC", COMPLETE, 64.3, 19.7),
            ("RM", NO_DAMAGE, 1.0, 1.6),
            ("RM", SLIGHT, 34.8, 18.3),
            ("RM", MODERATE, 64.2, 19.8),
            ("RM", EXTENSIVE, 64.3, 19.7),
            ("RM", COMPLETE, 64.3, 19.7),
        ]
        writer = writers.DmgDistPerTaxonomyXMLWriter(self.filename, dmg_states)
        writer.serialize(_starmap(DMG_DIST_PER_TAXONOMY, data))

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #24
0
    def test_serialize_taxonomies(self):
        expected = file(os.path.join(os.path.dirname(__file__), "../../examples/loss-fractions-taxonomies.xml"))

        writers.LossFractionsWriter(
            self.filename,
            "taxonomy",
            loss_unit="EUR",
            loss_category="building",
            hazard_metadata=HazardMetadata(
                investigation_time=50.0, statistics=None, quantile=None, sm_path="b1_b2_b4", gsim_path="b1_b2"
            ),
            poe=0.1,
        ).serialize(
            dict(RC=(400, 0.2), RM=(1600, 0.8)),
            {(0.0, 0.0): dict(RC=(200, 0.5), RM=(200, 0.5)), (1.0, 1.0): dict(RC=(200, 0.25), RM=(1400, 0.75))},
        )

        etree.XMLSchema(etree.parse(nrmllib.nrml_schema_file())).assert_(etree.parse(self.filename))
        _utils.assert_xml_equal(expected, self.filename)
Beispiel #25
0
    def test_serialize(self):
        # Just a basic serialization test.
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardCurves sourceModelTreePath="b1_b2_b4" gsimTreePath="b1_b4_b5" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0">
    <IMLs>0.005 0.007 0.0098</IMLs>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.0 -20.1</gml:pos>
      </gml:Point>
      <poEs>0.1 0.2 0.3</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.1 -20.2</gml:pos>
      </gml:Point>
      <poEs>0.4 0.5 0.6</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.2 -20.3</gml:pos>
      </gml:Point>
      <poEs>0.7 0.8 0.8</poEs>
    </hazardCurve>
  </hazardCurves>
</nrml>
""")

        metadata = dict(investigation_time=self.TIME,
                        imt='SA',
                        imls=self.IMLS,
                        sa_period=0.025,
                        sa_damping=5.0,
                        smlt_path='b1_b2_b4',
                        gsimlt_path='b1_b4_b5')
        writer = writers.HazardCurveXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #26
0
    def test_serialize_optional_metadata(self):
        expected = StringIO.StringIO(
            """\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <bcrMap interestRate="10.0" assetLifeExpectancy="50.0" statistics="quantile" quantileValue="0.5" lossCategory="economic" unit="USD">
    <node>
      <gml:Point>
        <gml:pos>1.0 1.5</gml:pos>
      </gml:Point>
      <bcr assetRef="asset_1" ratio="15.23" aalOrig="10.5" aalRetr="20.5"/>
    </node>
  </bcrMap>
</nrml>
"""
        )

        writer = writers.BCRMapXMLWriter(
            self.filename,
            interest_rate=10.0,
            asset_life_expectancy=50.0,
            statistics="quantile",
            quantile_value=0.50,
            unit="USD",
            loss_category="economic",
        )

        data = [
            BCR_NODE(
                asset_ref="asset_1",
                location=Point(1.0, 1.5),
                bcr=15.23,
                average_annual_loss_original=10.5,
                average_annual_loss_retrofitted=20.5,
            )
        ]

        writer.serialize(data)

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #27
0
    def test_serialize_quantile(self):
        # Test serialization of qunatile curves.
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardCurves statistics="quantile" quantileValue="0.15" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0">
    <IMLs>0.005 0.007 0.0098</IMLs>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.0 -20.1</gml:pos>
      </gml:Point>
      <poEs>0.1 0.2 0.3</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.1 -20.2</gml:pos>
      </gml:Point>
      <poEs>0.4 0.5 0.6</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.2 -20.3</gml:pos>
      </gml:Point>
      <poEs>0.7 0.8 0.8</poEs>
    </hazardCurve>
  </hazardCurves>
</nrml>
""")

        metadata = dict(investigation_time=self.TIME,
                        imt='SA',
                        imls=self.IMLS,
                        sa_period=0.025,
                        sa_damping=5.0,
                        statistics='quantile',
                        quantile_value=0.15)
        writer = writers.HazardCurveXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #28
0
    def test_serialize(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardMap sourceModelTreePath="b1_b2_b4" gsimTreePath="b1_b4_b5" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0" poE="0.1">
    <node lon="-1.0" lat="1.0" iml="0.01"/>
    <node lon="1.0" lat="1.0" iml="0.02"/>
    <node lon="1.0" lat="-1.0" iml="0.03"/>
    <node lon="-1.0" lat="-1.0" iml="0.04"/>
  </hazardMap>
</nrml>
""")

        metadata = dict(
            investigation_time=50.0, imt='SA', poe=0.1, sa_period=0.025,
            sa_damping=5.0, smlt_path='b1_b2_b4', gsimlt_path='b1_b4_b5')
        writer = writers.HazardMapXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #29
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <collapseMap>
    <CMNode>
      <gml:Point>
        <gml:pos>-72.2 18.0</gml:pos>
      </gml:Point>
      <cf assetRef="a1" mean="1.0" stdDev="1.6"/>
      <cf assetRef="a2" mean="34.8" stdDev="18.3"/>
      <cf assetRef="a3" mean="64.2" stdDev="19.8"/>
    </CMNode>
    <CMNode>
      <gml:Point>
        <gml:pos>-72.25 18.0</gml:pos>
      </gml:Point>
      <cf assetRef="a4" mean="64.3" stdDev="19.7"/>
    </CMNode>
  </collapseMap>
</nrml>
''')
        writer = writers.CollapseMapXMLWriter(self.filename)

        point1 = Point(-72.2, 18.)
        point2 = Point(-72.25, 18.)

        e1 = ExposureData('a1', point1)
        e2 = ExposureData('a2', point1)
        e3 = ExposureData('a3', point1)
        e4 = ExposureData('a4', point2)

        data = [
            (e1, 1.0, 1.6),
            (e2, 34.8, 18.3),
            (e3, 64.2, 19.8),
            (e4, 64.3, 19.7),
        ]
        writer.serialize(_starmap(COLLAPSE_MAP, data))
        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #30
0
    def test_serialize_quantile(self):
        # Test serialization of qunatile curves.
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardCurves statistics="quantile" quantileValue="0.15" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0">
    <IMLs>0.005 0.007 0.0098</IMLs>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.0 -20.1</gml:pos>
      </gml:Point>
      <poEs>0.1 0.2 0.3</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.1 -20.2</gml:pos>
      </gml:Point>
      <poEs>0.4 0.5 0.6</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.2 -20.3</gml:pos>
      </gml:Point>
      <poEs>0.7 0.8 0.8</poEs>
    </hazardCurve>
  </hazardCurves>
</nrml>
""")

        metadata = dict(
            investigation_time=self.TIME, imt='SA', imls=self.IMLS,
            sa_period=0.025, sa_damping=5.0, statistics='quantile',
            quantile_value=0.15
        )
        writer = writers.HazardCurveXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #31
0
    def test_serialize(self):
        # Just a basic serialization test.
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardCurves sourceModelTreePath="b1_b2_b4" gsimTreePath="b1_b4_b5" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0">
    <IMLs>0.005 0.007 0.0098</IMLs>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.0 -20.1</gml:pos>
      </gml:Point>
      <poEs>0.1 0.2 0.3</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.1 -20.2</gml:pos>
      </gml:Point>
      <poEs>0.4 0.5 0.6</poEs>
    </hazardCurve>
    <hazardCurve>
      <gml:Point>
        <gml:pos>38.2 -20.3</gml:pos>
      </gml:Point>
      <poEs>0.7 0.8 0.8</poEs>
    </hazardCurve>
  </hazardCurves>
</nrml>
""")

        metadata = dict(
            investigation_time=self.TIME, imt='SA', imls=self.IMLS,
            sa_period=0.025, sa_damping=5.0, smlt_path='b1_b2_b4',
            gsimlt_path='b1_b4_b5'
        )
        writer = writers.HazardCurveXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #32
0
    def test_serialize_quantile(self):
        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <hazardMap statistics="quantile" quantileValue="0.85" IMT="SA" investigationTime="50.0" saPeriod="0.025" saDamping="5.0" poE="0.1">
    <node lon="-1.0" lat="1.0" iml="0.01"/>
    <node lon="1.0" lat="1.0" iml="0.02"/>
    <node lon="1.0" lat="-1.0" iml="0.03"/>
    <node lon="-1.0" lat="-1.0" iml="0.04"/>
  </hazardMap>
</nrml>
""")

        _, self.path = tempfile.mkstemp()
        metadata = dict(
            investigation_time=50.0, imt='SA', poe=0.1, sa_period=0.025,
            sa_damping=5.0, statistics='quantile', quantile_value=0.85
        )
        writer = writers.HazardMapXMLWriter(self.path, **metadata)
        writer.serialize(self.data)

        utils.assert_xml_equal(expected, self.path)
        self.assertTrue(utils.validates_against_xml_schema(self.path))
Beispiel #33
0
    def test_serialize_taxonomies(self):
        expected = file(
            os.path.join(
                os.path.dirname(__file__),
                "../../examples/loss-fractions-taxonomies.xml"))

        writers.LossFractionsWriter(
            self.filename, "taxonomy",
            loss_unit="EUR",
            loss_category="building",
            hazard_metadata=HazardMetadata(
                investigation_time=50.,
                statistics=None,
                quantile=None,
                sm_path="b1_b2_b4",
                gsim_path="b1_b2"), poe=0.1).serialize(
                    dict(RC=(400, 0.2), RM=(1600, 0.8)),
                    {(0., 0.): dict(RC=(200, 0.5), RM=(200, 0.5)),
                     (1., 1.): dict(RC=(200, 0.25), RM=(1400, 0.75))})

        etree.XMLSchema(
            etree.parse(nrmllib.nrml_schema_file())).assert_(
                etree.parse(self.filename))
        _utils.assert_xml_equal(expected, self.filename)
Beispiel #34
0
    def test_serialize_complete_lt_ses(self):
        ruptures = [
            SESRupture(1,
                5.5, 1.0, 40.0, 10.0, 'Active Shallow Crust',
                False, False,
                top_left_corner=(1.1, 1.01, 10.0),
                top_right_corner=(2.1, 2.01, 20.0),
                bottom_right_corner=(3.1, 3.01, 30.0),
                bottom_left_corner=(4.1, 4.01, 40.0)),
            SESRupture(2,
                6.5, 0.0, 41.0, 0.0, 'Active Shallow Crust',
                True, False,
                lons=[
                    [5.1, 6.1],
                    [7.1, 8.1],
                ],
                lats=[
                    [5.01, 6.01],
                    [7.01, 8.01],
                ],
                depths=[
                    [10.5, 10.6],
                    [10.7, 10.8],
                ]),
            SESRupture(3,
                5.4, 2.0, 42.0, 12.0, 'Stable Shallow Crust',
                False, False,
                top_left_corner=(1.1, 1.01, 10.0),
                top_right_corner=(2.1, 2.01, 20.0),
                bottom_right_corner=(3.1, 3.01, 30.0),
                bottom_left_corner=(4.1, 4.01, 40.0)),
            SESRupture(4,
                6.4, 3.0, 43.0, 13.0, 'Stable Shallow Crust',
                True, False,
                lons=[
                    [5.2, 6.2],
                    [7.2, 8.2],
                ],
                lats=[
                    [5.02, 6.02],
                    [7.02, 8.02],
                ],
                depths=[
                    [10.1, 10.2],
                    [10.3, 10.4],
                ]),

        ]
        complete_lt_ses = SES(1, 250.0, ruptures)

        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <stochasticEventSet id="1" investigationTime="250.0">
    <rupture id="1" magnitude="5.5" strike="1.0" dip="40.0" rake="10.0" tectonicRegion="Active Shallow Crust">
      <planarSurface>
        <topLeft lon="1.1" lat="1.01" depth="10.0"/>
        <topRight lon="2.1" lat="2.01" depth="20.0"/>
        <bottomLeft lon="4.1" lat="4.01" depth="40.0"/>
        <bottomRight lon="3.1" lat="3.01" depth="30.0"/>
      </planarSurface>
    </rupture>
    <rupture id="2" magnitude="6.5" strike="0.0" dip="41.0" rake="0.0" tectonicRegion="Active Shallow Crust">
      <mesh rows="2" cols="2">
        <node row="0" col="0" lon="5.1" lat="5.01" depth="10.5"/>
        <node row="0" col="1" lon="6.1" lat="6.01" depth="10.6"/>
        <node row="1" col="0" lon="7.1" lat="7.01" depth="10.7"/>
        <node row="1" col="1" lon="8.1" lat="8.01" depth="10.8"/>
      </mesh>
    </rupture>
    <rupture id="3" magnitude="5.4" strike="2.0" dip="42.0" rake="12.0" tectonicRegion="Stable Shallow Crust">
      <planarSurface>
        <topLeft lon="1.1" lat="1.01" depth="10.0"/>
        <topRight lon="2.1" lat="2.01" depth="20.0"/>
        <bottomLeft lon="4.1" lat="4.01" depth="40.0"/>
        <bottomRight lon="3.1" lat="3.01" depth="30.0"/>
      </planarSurface>
    </rupture>
    <rupture id="4" magnitude="6.4" strike="3.0" dip="43.0" rake="13.0" tectonicRegion="Stable Shallow Crust">
      <mesh rows="2" cols="2">
        <node row="0" col="0" lon="5.2" lat="5.02" depth="10.1"/>
        <node row="0" col="1" lon="6.2" lat="6.02" depth="10.2"/>
        <node row="1" col="0" lon="7.2" lat="7.02" depth="10.3"/>
        <node row="1" col="1" lon="8.2" lat="8.02" depth="10.4"/>
      </mesh>
    </rupture>
  </stochasticEventSet>
</nrml>
""")

        try:
            _, path = tempfile.mkstemp()
            writer = writers.SESXMLWriter(path, None, None)
            writer.serialize([complete_lt_ses])

            utils.assert_xml_equal(expected, path)
            self.assertTrue(utils.validates_against_xml_schema(path))
        finally:
            os.unlink(path)
Beispiel #35
0
    def test_serialize_complete_lt_gmf(self):
        # Test data is:
        # - 1 gmf set
        # for each set:
        # - 2 ground motion fields
        # for each ground motion field:
        # - 2 nodes
        # Total nodes: 12
        locations = [Location(i * 0.1, i * 0.1) for i in xrange(12)]
        gmf_nodes = [GmfNode(i * 0.2, locations[i]) for i in xrange(12)]
        gmfs = [
            Gmf('SA', 0.1, 5.0, gmf_nodes[:2], 1),
            Gmf('SA', 0.2, 5.0, gmf_nodes[2:4], 2),
            Gmf('SA', 0.3, 5.0, gmf_nodes[4:6], 3),
            Gmf('PGA', None, None, gmf_nodes[6:8], 4),
            Gmf('PGA', None, None, gmf_nodes[8:10], 5),
            Gmf('PGA', None, None, gmf_nodes[10:], 6),
        ]
        gmf_set = GmfSet(gmfs, 350.0, 1)

        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <gmfSet investigationTime="350.0" stochasticEventSetId="1">
    <gmf IMT="SA" saPeriod="0.1" saDamping="5.0" ruptureId="1">
      <node gmv="0.0" lon="0.0" lat="0.0"/>
      <node gmv="0.2" lon="0.1" lat="0.1"/>
    </gmf>
    <gmf IMT="SA" saPeriod="0.2" saDamping="5.0" ruptureId="2">
      <node gmv="0.4" lon="0.2" lat="0.2"/>
      <node gmv="0.6" lon="0.3" lat="0.3"/>
    </gmf>
    <gmf IMT="SA" saPeriod="0.3" saDamping="5.0" ruptureId="3">
      <node gmv="0.8" lon="0.4" lat="0.4"/>
      <node gmv="1.0" lon="0.5" lat="0.5"/>
    </gmf>
    <gmf IMT="PGA" ruptureId="4">
      <node gmv="1.2" lon="0.6" lat="0.6"/>
      <node gmv="1.4" lon="0.7" lat="0.7"/>
    </gmf>
    <gmf IMT="PGA" ruptureId="5">
      <node gmv="1.6" lon="0.8" lat="0.8"/>
      <node gmv="1.8" lon="0.9" lat="0.9"/>
    </gmf>
    <gmf IMT="PGA" ruptureId="6">
      <node gmv="2.0" lon="1.0" lat="1.0"/>
      <node gmv="2.2" lon="1.1" lat="1.1"/>
    </gmf>
  </gmfSet>
</nrml>
""")

        try:
            # Make a temp file to save the results to:
            _, path = tempfile.mkstemp()
            writer = writers.EventBasedGMFXMLWriter(
                path, None, None)
            writer.serialize([gmf_set])

            utils.assert_xml_equal(expected, path)
            self.assertTrue(utils.validates_against_xml_schema(path))
        finally:
            os.unlink(path)
Beispiel #36
0
    def test_serialize_complete_lt_gmf(self):
        # Test data is:
        # - 1 gmf set
        # for each set:
        # - 2 ground motion fields
        # for each ground motion field:
        # - 2 nodes
        # Total nodes: 12
        locations = [Location(i * 0.1, i * 0.1) for i in xrange(12)]
        gmf_nodes = [GmfNode(i * 0.2, locations[i]) for i in xrange(12)]
        gmfs = [
            Gmf('SA', 0.1, 5.0, gmf_nodes[:2], 1),
            Gmf('SA', 0.2, 5.0, gmf_nodes[2:4], 2),
            Gmf('SA', 0.3, 5.0, gmf_nodes[4:6], 3),
            Gmf('PGA', None, None, gmf_nodes[6:8], 4),
            Gmf('PGA', None, None, gmf_nodes[8:10], 5),
            Gmf('PGA', None, None, gmf_nodes[10:], 6),
        ]
        gmf_set = GmfSet(gmfs, 350.0, 1)

        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <gmfSet investigationTime="350.0" stochasticEventSetId="1">
    <gmf IMT="SA" saPeriod="0.1" saDamping="5.0" ruptureId="1">
      <node gmv="0.0" lon="0.0" lat="0.0"/>
      <node gmv="0.2" lon="0.1" lat="0.1"/>
    </gmf>
    <gmf IMT="SA" saPeriod="0.2" saDamping="5.0" ruptureId="2">
      <node gmv="0.4" lon="0.2" lat="0.2"/>
      <node gmv="0.6" lon="0.3" lat="0.3"/>
    </gmf>
    <gmf IMT="SA" saPeriod="0.3" saDamping="5.0" ruptureId="3">
      <node gmv="0.8" lon="0.4" lat="0.4"/>
      <node gmv="1.0" lon="0.5" lat="0.5"/>
    </gmf>
    <gmf IMT="PGA" ruptureId="4">
      <node gmv="1.2" lon="0.6" lat="0.6"/>
      <node gmv="1.4" lon="0.7" lat="0.7"/>
    </gmf>
    <gmf IMT="PGA" ruptureId="5">
      <node gmv="1.6" lon="0.8" lat="0.8"/>
      <node gmv="1.8" lon="0.9" lat="0.9"/>
    </gmf>
    <gmf IMT="PGA" ruptureId="6">
      <node gmv="2.0" lon="1.0" lat="1.0"/>
      <node gmv="2.2" lon="1.1" lat="1.1"/>
    </gmf>
  </gmfSet>
</nrml>
""")

        try:
            # Make a temp file to save the results to:
            _, path = tempfile.mkstemp()
            writer = writers.EventBasedGMFXMLWriter(path, None, None)
            writer.serialize([gmf_set])

            utils.assert_xml_equal(expected, path)
            self.assertTrue(utils.validates_against_xml_schema(path))
        finally:
            os.unlink(path)
Beispiel #37
0
    def test_serialize_complete_lt_ses(self):
        ruptures = [
            SESRupture(1,
                       5.5,
                       1.0,
                       40.0,
                       10.0,
                       'Active Shallow Crust',
                       False,
                       top_left_corner=(1.1, 1.01, 10.0),
                       top_right_corner=(2.1, 2.01, 20.0),
                       bottom_right_corner=(3.1, 3.01, 30.0),
                       bottom_left_corner=(4.1, 4.01, 40.0)),
            SESRupture(2,
                       6.5,
                       0.0,
                       41.0,
                       0.0,
                       'Active Shallow Crust',
                       True,
                       lons=[
                           [5.1, 6.1],
                           [7.1, 8.1],
                       ],
                       lats=[
                           [5.01, 6.01],
                           [7.01, 8.01],
                       ],
                       depths=[
                           [10.5, 10.6],
                           [10.7, 10.8],
                       ]),
            SESRupture(3,
                       5.4,
                       2.0,
                       42.0,
                       12.0,
                       'Stable Shallow Crust',
                       False,
                       top_left_corner=(1.1, 1.01, 10.0),
                       top_right_corner=(2.1, 2.01, 20.0),
                       bottom_right_corner=(3.1, 3.01, 30.0),
                       bottom_left_corner=(4.1, 4.01, 40.0)),
            SESRupture(4,
                       6.4,
                       3.0,
                       43.0,
                       13.0,
                       'Stable Shallow Crust',
                       True,
                       lons=[
                           [5.2, 6.2],
                           [7.2, 8.2],
                       ],
                       lats=[
                           [5.02, 6.02],
                           [7.02, 8.02],
                       ],
                       depths=[
                           [10.1, 10.2],
                           [10.3, 10.4],
                       ]),
        ]
        complete_lt_ses = SES(1, 250.0, ruptures)

        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <stochasticEventSet id="1" investigationTime="250.0">
    <rupture id="1" magnitude="5.5" strike="1.0" dip="40.0" rake="10.0" tectonicRegion="Active Shallow Crust">
      <planarSurface>
        <topLeft lon="1.1" lat="1.01" depth="10.0"/>
        <topRight lon="2.1" lat="2.01" depth="20.0"/>
        <bottomRight lon="3.1" lat="3.01" depth="30.0"/>
        <bottomLeft lon="4.1" lat="4.01" depth="40.0"/>
      </planarSurface>
    </rupture>
    <rupture id="2" magnitude="6.5" strike="0.0" dip="41.0" rake="0.0" tectonicRegion="Active Shallow Crust">
      <mesh rows="2" cols="2">
        <node row="0" col="0" lon="5.1" lat="5.01" depth="10.5"/>
        <node row="0" col="1" lon="6.1" lat="6.01" depth="10.6"/>
        <node row="1" col="0" lon="7.1" lat="7.01" depth="10.7"/>
        <node row="1" col="1" lon="8.1" lat="8.01" depth="10.8"/>
      </mesh>
    </rupture>
    <rupture id="3" magnitude="5.4" strike="2.0" dip="42.0" rake="12.0" tectonicRegion="Stable Shallow Crust">
      <planarSurface>
        <topLeft lon="1.1" lat="1.01" depth="10.0"/>
        <topRight lon="2.1" lat="2.01" depth="20.0"/>
        <bottomRight lon="3.1" lat="3.01" depth="30.0"/>
        <bottomLeft lon="4.1" lat="4.01" depth="40.0"/>
      </planarSurface>
    </rupture>
    <rupture id="4" magnitude="6.4" strike="3.0" dip="43.0" rake="13.0" tectonicRegion="Stable Shallow Crust">
      <mesh rows="2" cols="2">
        <node row="0" col="0" lon="5.2" lat="5.02" depth="10.1"/>
        <node row="0" col="1" lon="6.2" lat="6.02" depth="10.2"/>
        <node row="1" col="0" lon="7.2" lat="7.02" depth="10.3"/>
        <node row="1" col="1" lon="8.2" lat="8.02" depth="10.4"/>
      </mesh>
    </rupture>
  </stochasticEventSet>
</nrml>
""")

        try:
            _, path = tempfile.mkstemp()
            writer = writers.SESXMLWriter(path, None, None)
            writer.serialize([complete_lt_ses])

            utils.assert_xml_equal(expected, path)
            self.assertTrue(utils.validates_against_xml_schema(path))
        finally:
            os.unlink(path)
Beispiel #38
0
    def test_serialize(self):
        expected = StringIO.StringIO('''<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <dmgDistPerAsset>
    <damageStates>no_damage slight moderate extensive complete</damageStates>
    <DDNode>
      <gml:Point>
        <gml:pos>-116.0 41.0</gml:pos>
      </gml:Point>
      <asset assetRef="asset_1">
        <damage ds="no_damage" mean="1.0" stddev="1.6"/>
        <damage ds="slight" mean="34.8" stddev="18.3"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
    </DDNode>
    <DDNode>
      <gml:Point>
        <gml:pos>-117.0 42.0</gml:pos>
      </gml:Point>
      <asset assetRef="asset_2">
        <damage ds="no_damage" mean="1.0" stddev="1.6"/>
        <damage ds="slight" mean="34.8" stddev="18.3"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
      <asset assetRef="asset_3">
        <damage ds="no_damage" mean="1.1" stddev="1.7"/>
        <damage ds="slight" mean="34.9" stddev="18.4"/>
        <damage ds="moderate" mean="64.2" stddev="19.8"/>
        <damage ds="extensive" mean="64.3" stddev="19.7"/>
        <damage ds="complete" mean="64.3" stddev="19.7"/>
      </asset>
    </DDNode>
  </dmgDistPerAsset>
</nrml>''')
        dmg_states = 'no_damage slight moderate extensive complete'.split()
        point1 = Point(-116., 41.)
        point2 = Point(-117., 42.)

        e1 = ExposureData('asset_1', point1)
        e2 = ExposureData('asset_2', point2)
        e3 = ExposureData('asset_3', point2)

        data = [
            (e1, NO_DAMAGE, 1.0, 1.6),
            (e1, SLIGHT, 34.8, 18.3),
            (e1, MODERATE, 64.2, 19.8),
            (e1, EXTENSIVE, 64.3, 19.7),
            (e1, COMPLETE, 64.3, 19.7),

            (e2, NO_DAMAGE, 1.0, 1.6),
            (e2, SLIGHT, 34.8, 18.3),
            (e2, MODERATE, 64.2, 19.8),
            (e2, EXTENSIVE, 64.3, 19.7),
            (e2, COMPLETE, 64.3, 19.7),

            (e3, NO_DAMAGE, 1.1, 1.7),
            (e3, SLIGHT, 34.9, 18.4),
            (e3, MODERATE, 64.2, 19.8),
            (e3, EXTENSIVE, 64.3, 19.7),
            (e3, COMPLETE, 64.3, 19.7),
        ]
        writer = writers.DmgDistPerAssetXMLWriter(self.filename, dmg_states)
        writer.serialize(_starmap(DMG_DIST_PER_ASSET, data))

        _utils.assert_xml_equal(expected, self.filename)
        self.assertTrue(_utils.validates_against_xml_schema(self.filename))
Beispiel #39
0
    def test_serialize(self):
        # Test data is:
        # - 1 gmf collection
        # - 3 gmf sets
        # for each set:
        # - 2 ground motion fields
        # for each ground motion field:
        # - 2 nodes
        # Total nodes: 12
        locations = [Location(i * 0.1, i * 0.1) for i in xrange(12)]
        gmf_nodes = [GmfNode(i * 0.2, locations[i]) for i in xrange(12)]
        gmfs = [
            Gmf('SA', 0.1, 5.0, gmf_nodes[:2]),
            Gmf('SA', 0.2, 5.0, gmf_nodes[2:4]),
            Gmf('SA', 0.3, 5.0, gmf_nodes[4:6]),
            Gmf('PGA', None, None, gmf_nodes[6:8]),
            Gmf('PGA', None, None, gmf_nodes[8:10]),
            Gmf('PGA', None, None, gmf_nodes[10:]),
        ]
        gmf_sets = [
            GmfSet(gmfs[:2], 50.0),
            GmfSet(gmfs[2:4], 40.0),
            GmfSet(gmfs[4:], 30.0),
        ]
        gmf_collection = GmfCollection(gmf_sets)

        expected = StringIO.StringIO("""\
<?xml version='1.0' encoding='UTF-8'?>
<nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4">
  <gmfCollection sourceModelTreePath="b1_b2_b3" gsimTreePath="b1_b7_b15">
    <gmfSet investigationTime="50.0">
      <gmf IMT="SA" saPeriod="0.1" saDamping="5.0">
        <node iml="0.0" lon="0.0" lat="0.0"/>
        <node iml="0.2" lon="0.1" lat="0.1"/>
      </gmf>
      <gmf IMT="SA" saPeriod="0.2" saDamping="5.0">
        <node iml="0.4" lon="0.2" lat="0.2"/>
        <node iml="0.6" lon="0.3" lat="0.3"/>
      </gmf>
    </gmfSet>
    <gmfSet investigationTime="40.0">
      <gmf IMT="SA" saPeriod="0.3" saDamping="5.0">
        <node iml="0.8" lon="0.4" lat="0.4"/>
        <node iml="1.0" lon="0.5" lat="0.5"/>
      </gmf>
      <gmf IMT="PGA">
        <node iml="1.2" lon="0.6" lat="0.6"/>
        <node iml="1.4" lon="0.7" lat="0.7"/>
      </gmf>
    </gmfSet>
    <gmfSet investigationTime="30.0">
      <gmf IMT="PGA">
        <node iml="1.6" lon="0.8" lat="0.8"/>
        <node iml="1.8" lon="0.9" lat="0.9"/>
      </gmf>
      <gmf IMT="PGA">
        <node iml="2.0" lon="1.0" lat="1.0"/>
        <node iml="2.2" lon="1.1" lat="1.1"/>
      </gmf>
    </gmfSet>
  </gmfCollection>
</nrml>
""")

        sm_lt_path = 'b1_b2_b3'
        gsim_lt_path = 'b1_b7_b15'

        try:
            # Make a temp file to save the results to:
            _, path = tempfile.mkstemp()
            writer = writers.EventBasedGMFXMLWriter(
                path, sm_lt_path, gsim_lt_path)
            writer.serialize(gmf_collection)

            utils.assert_xml_equal(expected, path)
            self.assertTrue(utils.validates_against_xml_schema(path))
        finally:
            os.unlink(path)