Example #1
0
    def test_serialize_using_hazard_realization(self):
        expected = io.BytesIO(b"""\
<?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" lossType="structural">
    <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",
                                         loss_type="structural")

        data = [
            BCR_NODE(asset_ref="asset_1",
                     location=writers.Site(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)
Example #2
0
    def test_empty_model_not_supported(self):
        writer = writers.BCRMapXMLWriter(
            self.filename,
            interest_rate=10.0, asset_life_expectancy=0.5, statistics="mean",
            loss_type="structural")

        self.assertRaises(ValueError, writer.serialize, [])
        self.assertRaises(ValueError, writer.serialize, None)
Example #3
0
    def test_serialize_a_model(self):
        expected = io.BytesIO(b"""\
<?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" lossType="structural">
    <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",
                                         loss_type="structural")

        data = [
            BCR_NODE(asset_ref="asset_1",
                     location=writers.Site(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=writers.Site(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=writers.Site(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)
Example #4
0
def export_bcr_distribution_xml(key, output, target):
    """
    Export `output` to `target` by using a nrml bcr distribution
    serializer
    """
    risk_calculation = output.oq_job.get_oqparam()
    args = _export_common(output, output.bcr_distribution.loss_type)

    dest = _get_result_export_dest(target, output)
    args.update(
        dict(interest_rate=risk_calculation.interest_rate,
             asset_life_expectancy=risk_calculation.asset_life_expectancy))
    del args['investigation_time']

    risk_writers.BCRMapXMLWriter(dest, **args).serialize(
        output.bcr_distribution.bcrdistributiondata_set.all().order_by(
            'asset_ref'))
    return dest