Commit dccb4e51 authored by Tara Evaz Zadeh's avatar Tara Evaz Zadeh
Browse files

Modified the calculator to work asset-by-asset instead of tile-by-tile

parent fac4c77d
Pipeline #20497 passed with stage
in 1 minute and 42 seconds
......@@ -23,10 +23,10 @@ import csv
import numpy as np
import pandas as pd
import losslib
from damage_calculator_tile_version import damageCalculator_TileVersion
import datetime
def get_exposure_per_tile(
def damage_calculator(
exposure_filepath,
fragility_pathname,
taxonomy_conversion_filepath,
......@@ -35,12 +35,24 @@ def get_exposure_per_tile(
result_filepath="damage_result.csv",
):
exposure = pd.read_csv(exposure_filepath)
lons_whole_area = exposure.lon
lats_whole_area = exposure.lat
"""
This function computes the probabilities of occurrence (PoE) of damage states for a scenario
earthquakes, given a ground-motion field, an exposure model representing the assets in the
region of interest and fragility functions that model the probability of exceeding defined
damage states given a set of ground-motion values of a specific type.
Please read the manual for more information on the inputs.
"""
# Read inputs.
taxonomy_to_fragility_source = csv.reader(open(taxonomy_conversion_filepath))
# Skipping the header.
next(taxonomy_to_fragility_source)
ground_motion_field = np.loadtxt(ground_motion_field_filepath, delimiter=",", skiprows=1)
exposure = pd.read_csv(exposure_filepath)
# Interpolate the ground-motion values for all the assets of the exposure model.
full_ground_motion_field = losslib.get_full_GMF(
ground_motion_field, lons_whole_area, lats_whole_area, interpolation_method
ground_motion_field, exposure.lon, exposure.lat, interpolation_method
)
# Creating an empty dictionary to later fill with ground-motion types and their column
......@@ -56,51 +68,60 @@ def get_exposure_per_tile(
# ground-motion-field file) to the `ground_motion_type_index_map` as the dictionary
# key and value, respectively.
ground_motion_type_index_map[gm_types[i]] = i + 2
cell_ids = exposure.tile_id.unique()
with open(result_filepath, mode="w") as write_obj:
field_names = [
"asset_id",
"lon",
"lat",
"taxonomy",
"number",
"structural",
"night",
"occupancy",
"admin_name",
"admin_ID",
"tile_id",
"tile_geometry",
"building_id",
"building_geometry",
"gm_value",
"gm_type",
"PoEs",
"PoOs",
"structural_no_damage",
"structural_slight",
"structural_moderate",
"structural_extensive",
"structural_complete",
]
writer = csv.DictWriter(write_obj, fieldnames=field_names, delimiter=";")
writer.writeheader()
for i in range(len(cell_ids)):
exposure_per_tile = exposure.loc[exposure["tile_id"] == cell_ids[i]]
damageCalculator_TileVersion(
full_ground_motion_field,
result_filepath,
fragility_pathname,
exposure_per_tile,
taxonomy_conversion_filepath,
ground_motion_field_filepath,
ground_motion_type_index_map,
interpolation_method,
# Calling the function "taxonomy_to_fragility" to get a dictionary with keys as the
# taxonomy and the values as both the fragility function name (excluding the ".csv" part)
# and the column number of the respective ground-motion type in `ground-motion-field` file.
taxonomy_to_fragility_map = losslib.taxonomy_to_fragility(
ground_motion_type_index_map, taxonomy_to_fragility_source, fragility_pathname
)
gm_values = []
all_PoEs = []
all_PoOs = []
damage_by_assets = []
# Going through each asset to do the damage calculation.
for asset in range(exposure.shape[0]):
taxonomy = exposure["taxonomy"][asset]
fragilityfunction_filename = taxonomy_to_fragility_map[taxonomy][0] + ".csv"
num_buildings = exposure["number"][asset]
# Read fragility functions as numpy arrays.
fragility_function = np.loadtxt(
fragility_pathname + "/" + fragilityfunction_filename,
delimiter=",",
usecols=range(1, 101),
)
# Computing the ground motions for each asset (also for duplicate locations).
gm_value = full_ground_motion_field[asset, taxonomy_to_fragility_map[taxonomy][1]]
gm_values.append(gm_value)
# Computing probabilities of exceedance and occurrence.
[PoEs, PoOs] = losslib.get_PoEs(fragility_function, gm_value)
all_PoEs.append(PoEs)
all_PoOs.append(PoOs)
# Compute damage by assets
damage_by_asset = [i * num_buildings for i in PoOs]
damage_by_assets.append(damage_by_asset)
# Append results
exposure["gm_values"] = gm_values
exposure["PoES"] = [all_PoEs for i in exposure.index][i]
exposure["PoOS"] = [all_PoOs for i in exposure.index][i]
exposure["structural_no_damage"] = [row[0] for row in damage_by_assets]
exposure["structural_slight"] = [row[1] for row in damage_by_assets]
exposure["structural_moderate"] = [row[2] for row in damage_by_assets]
exposure["structural_extensive"] = [row[3] for row in damage_by_assets]
exposure["structural_complete"] = [row[4] for row in damage_by_assets]
exposure.to_csv(result_filepath, index=False)
if __name__ == "__main__":
......@@ -168,7 +189,7 @@ if __name__ == "__main__":
required=False,
action="store_true",
help="Result file exists. Choose another name or set --overwrite"
+ "to overwrite the existing file.",
+ "to overwrite the existing result file.",
)
args = parser.parse_args()
......@@ -184,14 +205,16 @@ if __name__ == "__main__":
if os.path.exists(result_filepath):
if not overwrite_result_file:
raise ValueError(
"result_filepath exists. Choose another name or use "
+ "--overwrite set to True if you want to "
+ "overwrite the results."
"result_filepath exists. Choose another name or set "
+ "--overwrite to overwrite existing result file."
)
else:
os.remove(result_filepath)
get_exposure_per_tile(
startTime = datetime.datetime.now()
print(startTime)
damage_calculator(
exposure_filepath,
fragility_pathname,
taxonomy_conversion_filepath,
......@@ -199,3 +222,4 @@ if __name__ == "__main__":
interpolation_method,
result_filepath,
)
print("Execution time of the script", (datetime.datetime.now() - startTime))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment