def exposure(job, exposure_model_input): """ Load exposure assets and write them to database. :param exposure_model_input: the pathname to an exposure file """ return ExposureDBWriter(job).serialize( parsers.ExposureModelParser(exposure_model_input))
def test_parsing(self): exposure = """\ <?xml version='1.0' encoding='utf-8'?> <nrml xmlns="http://openquake.org/xmlns/nrml/0.4"> <exposureModel id="ep1" category="buildings" taxonomySource="Pavia buildings"> <conversions> <area type="per_asset" unit="GBP"/> <costTypes> <costType name="contents" type="per_area" unit="CHF"/> <costType name="structural" type="aggregated" unit="USD" retrofittedType="aggregated" retrofittedUnit="EUR"/> <costType name="nonStructural" type="aggregated" unit="USD"/> </costTypes> </conversions> <description>Buildings in Pavia</description> <assets> <asset id="asset_01" area="120" number="7" taxonomy="RC/DMRF-D/LR"> <location lon="9.15000" lat="45.16667"/> <costs> <cost type="contents" value="12.95" /> <cost type="structural" value="150000" deductible="55" insuranceLimit="999"/> <cost type="nonStructural" value="25000" /> </costs> </asset> <asset id="asset_02" area="119" number="6" taxonomy="RC/DMRF-D/HR"> <location lon="9.15333" lat="45.12200"/> <costs> <cost type="contents" value="21.95"/> <cost type="structural" value="250000" insuranceLimit="1999" deductible="66"/> </costs> <occupancies> <occupancy period="day" occupants="12"/> <occupancy period="night" occupants="50"/> </occupancies> </asset> </assets> </exposureModel> </nrml> """ expected_result = [ ([9.15000, 45.16667], [], { "area": 120.0, "areaType": "per_asset", "areaUnit": "GBP", "category": "buildings", "id": "asset_01", "description": "Buildings in Pavia", "exposureID": "ep1", "number": 7.0, "taxonomy": "RC/DMRF-D/LR", "taxonomySource": "Pavia buildings", }, [ parsers.Cost("contents", 12.95, None, None, None), parsers.Cost("structural", 150000.0, None, 55.0, 999.0), parsers.Cost("nonStructural", 25000.0, None, None, None) ], { "contents": ("per_area", "CHF", None, None), "structural": ("aggregated", "USD", "aggregated", "EUR"), "nonStructural": ("aggregated", "USD", None, None), }), ([9.15333, 45.12200], [parsers.Occupancy(12, "day"), parsers.Occupancy(50, "night")], { "area": 119.0, "areaType": "per_asset", "areaUnit": "GBP", "category": "buildings", "id": "asset_02", "description": "Buildings in Pavia", "exposureID": "ep1", "number": 6.0, "taxonomy": "RC/DMRF-D/HR", "taxonomySource": "Pavia buildings", }, [ parsers.Cost("contents", 21.95, None, None, None), parsers.Cost("structural", 250000.0, None, 66.0, 1999.0) ], { "contents": ("per_area", "CHF", None, None), "structural": ("aggregated", "USD", "aggregated", "EUR"), "nonStructural": ("aggregated", "USD", None, None), }), ] parser = parsers.ExposureModelParser(StringIO.StringIO(exposure)) i = None for ctr, (exposure_point, occupancy_data, exposure_data, costs, conversions) in enumerate(parser): self.assertEqual(expected_result[ctr][0], exposure_point) self.assertEqual(expected_result[ctr][1], occupancy_data) self.assertEqual(expected_result[ctr][2], exposure_data) self.assertEqual(expected_result[ctr][3], costs) self.assertEqual(expected_result[ctr][4], conversions) i = ctr self.assertEqual(2, i + 1)
def parse_risk_models(self): """ If any risk model is given in the hazard calculation, the computation will be driven by risk data. In this case the locations will be extracted from the exposure file (if there is one) and the imt (and levels) will be extracted from the vulnerability model (if there is one) """ hc = self.hc if hc.vulnerability_models: logs.LOG.progress("parsing risk models") hc.intensity_measure_types_and_levels = dict() hc.intensity_measure_types = list() for vf in hc.vulnerability_models: intensity_measure_types_and_levels = dict( (record['IMT'], record['IML']) for record in parsers.VulnerabilityModelParser(vf)) for imt, levels in \ intensity_measure_types_and_levels.items(): if (imt in hc.intensity_measure_types_and_levels and (set(hc.intensity_measure_types_and_levels[imt]) - set(levels))): logs.LOG.warning("The same IMT %s is associated with " "different levels" % imt) else: hc.intensity_measure_types_and_levels[imt] = levels hc.intensity_measure_types.extend( intensity_measure_types_and_levels) # remove possible duplicates if hc.intensity_measure_types is not None: hc.intensity_measure_types = list( set(hc.intensity_measure_types)) hc.save() logs.LOG.info("Got IMT and levels " "from vulnerability models: %s - %s" % (hc.intensity_measure_types_and_levels, hc.intensity_measure_types)) if 'fragility' in hc.inputs: hc.intensity_measure_types_and_levels = dict() hc.intensity_measure_types = list() parser = iter(parsers.FragilityModelParser(hc.inputs['fragility'])) hc = self.hc fragility_format, _limit_states = parser.next() if (fragility_format == "continuous" and hc.calculation_mode != "scenario"): raise NotImplementedError( "Getting IMT and levels from " "a continuous fragility model is not yet supported") hc.intensity_measure_types_and_levels = dict( (iml['IMT'], iml['imls']) for _taxonomy, iml, _params, _no_damage_limit in parser) hc.intensity_measure_types.extend( hc.intensity_measure_types_and_levels) hc.save() if 'exposure' in hc.inputs: with logs.tracing('storing exposure'): exposure.ExposureDBWriter(self.job).serialize( parsers.ExposureModelParser(hc.inputs['exposure']))
def test_parsing(self): exposure = """\ <?xml version='1.0' encoding='utf-8'?> <nrml xmlns:gml="http://www.opengis.net/gml" xmlns="http://openquake.org/xmlns/nrml/0.4"> <exposureModel gml:id="ep1"> <config/> <exposureList gml:id="PAV01" areaType="per_asset" areaUnit="GBP" assetCategory="buildings" cocoType="per_area" cocoUnit="CHF" recoType="aggregated" recoUnit="EUR" stcoType="aggregated" stcoUnit="USD"> <gml:description>Buildings in Pavia</gml:description> <taxonomySource>Pavia taxonomy</taxonomySource> <assetDefinition gml:id="asset_01"> <site> <gml:Point srsName="epsg:4326"> <gml:pos>9.15000 45.16667</gml:pos> </gml:Point> </site> <area>120</area> <coco>12.95</coco> <deductible>55</deductible> <limit>999</limit> <number>7</number> <reco>109876</reco> <stco>150000</stco> <taxonomy>RC/DMRF-D/LR</taxonomy> </assetDefinition> <assetDefinition gml:id="asset_02"> <site> <gml:Point srsName="epsg:4326"> <gml:pos>9.15333 45.12200</gml:pos> </gml:Point> </site> <area>119</area> <coco>21.95</coco> <deductible>66</deductible> <limit>1999</limit> <number>6</number> <occupants description="day">12</occupants> <occupants description="night">50</occupants> <reco>205432</reco> <stco>250000</stco> <taxonomy>RC/DMRF-D/HR</taxonomy> </assetDefinition> </exposureList> </exposureModel> </nrml> """ expected_result = [ ([9.15000, 45.16667], [], { "area": 120.0, "areaType": "per_asset", "areaUnit": "GBP", "assetCategory": "buildings", "assetID": "asset_01", "coco": 12.95, "cocoType": "per_area", "cocoUnit": "CHF", "deductible": 55.0, "limit": 999.0, "listDescription": "Buildings in Pavia", "listID": "PAV01", "number": 7.0, "reco": 109876.0, "recoType": "aggregated", "recoUnit": "EUR", "stco": 150000.0, "stcoType": "aggregated", "stcoUnit": "USD", "taxonomy": "RC/DMRF-D/LR", "taxonomySource": "Pavia taxonomy", }), ([9.15333, 45.12200], [parsers.OCCUPANCY(12, "day"), parsers.OCCUPANCY(50, "night")], { "area": 119.0, "areaType": "per_asset", "areaUnit": "GBP", "assetCategory": "buildings", "assetID": "asset_02", "coco": 21.95, "cocoType": "per_area", "cocoUnit": "CHF", "deductible": 66.0, "limit": 1999.0, "listDescription": "Buildings in Pavia", "listID": "PAV01", "number": 6.0, "reco": 205432.0, "recoType": "aggregated", "recoUnit": "EUR", "stco": 250000.0, "stcoType": "aggregated", "stcoUnit": "USD", "taxonomy": "RC/DMRF-D/HR", "taxonomySource": "Pavia taxonomy", }), ] parser = parsers.ExposureModelParser(StringIO.StringIO(exposure)) for ctr, (exposure_point, occupancy_data, exposure_data) \ in enumerate(parser): self.assertEqual(expected_result[ctr][0], exposure_point) self.assertEqual(expected_result[ctr][1], occupancy_data) self.assertEqual(expected_result[ctr][2], exposure_data)
def test_parsing(self): exposure = """\ <?xml version='1.0' encoding='utf-8'?> <nrml xmlns="http://openquake.org/xmlns/nrml/0.4"> <exposureModel id="ep1" category="buildings" taxonomySource="source"> <conversions> <area type="per_asset" unit="GBP"/> <costTypes> <costType name="contents" type="per_area" unit="CHF"/> <costType name="structural" type="aggregated" unit="USD" retrofittedType="aggregated" retrofittedUnit="EUR"/> <costType name="nonStructural" type="aggregated" unit="USD"/> </costTypes> <deductible isAbsolute="false"/> <insuranceLimit isAbsolute="true"/> </conversions> <description>Buildings in Pavia</description> <assets> <asset id="asset_01" area="120" number="7" taxonomy="RC/DMRF-D/LR"> <location lon="9.15000" lat="45.16667"/> <costs> <cost type="contents" value="12.95" /> <cost type="structural" value="150000" deductible="55" insuranceLimit="999"/> <cost type="nonStructural" value="25000" /> </costs> </asset> <asset id="asset_02" area="119" number="6" taxonomy="RC/DMRF-D/HR"> <location lon="9.15333" lat="45.12200"/> <costs> <cost type="contents" value="21.95"/> <cost type="structural" value="250000" insuranceLimit="1999" deductible="66"/> </costs> <occupancies> <occupancy period="day" occupants="12"/> <occupancy period="night" occupants="50"/> </occupancies> </asset> </assets> </exposureModel> </nrml> """ parser = parsers.ExposureModelParser(StringIO.StringIO(exposure)) for i, asset_data in enumerate(parser): self.assertEqual( parsers.ExposureMetadata( "ep1", "source", "buildings", "Buildings in Pavia", parsers.Conversions([ parsers.CostType("contents", "per_area", "CHF", None, None), parsers.CostType("structural", "aggregated", "USD", "aggregated", "EUR"), parsers.CostType("nonStructural", "aggregated", "USD", None, None) ], "per_asset", "GBP", False, True)), asset_data.exposure_metadata) self.assertEqual([ parsers.Site(9.15000, 45.16667), parsers.Site(9.15333, 45.12200) ][i], asset_data.site) self.assertEqual(["asset_01", "asset_02"][i], asset_data.asset_ref) self.assertEqual(["RC/DMRF-D/LR", "RC/DMRF-D/HR"][i], asset_data.taxonomy) self.assertEqual([120, 119][i], asset_data.area) self.assertEqual([7, 6][i], asset_data.number) self.assertEqual([[ parsers.Cost("contents", 12.95, None, None, None), parsers.Cost("structural", 150000, None, 55, 999), parsers.Cost("nonStructural", 25000, None, None, None) ], [ parsers.Cost("contents", 21.95, None, None, None), parsers.Cost("structural", 250000.0, None, 66, 1999) ]][i], asset_data.costs) self.assertEqual([[], [ parsers.Occupancy(12, "day"), parsers.Occupancy(50, "night") ]][i], asset_data.occupancy)