Commit 780b670b authored by Tara Evaz Zadeh's avatar Tara Evaz Zadeh
Browse files

Automatically detect ground-motion type and column_num in the gm_file

parent 6282935c
Pipeline #19354 passed with stage
in 1 minute and 33 seconds
geometry,,origin_id,,asset_id,,lon,,lat,,taxonomy,,gm_value,,PoEs,,PoOs,,tot_num_buildings,,structural_no_damage,,structural_slight,,structural_moderate,,structural_extensive,,structural_complete geometry,,origin_id,,asset_id,,lon,,lat,,taxonomy,,gm_value,,PoEs,,PoOs,,num_buildings,,structural_no_damage,,structural_slight,,structural_moderate,,structural_extensive,,structural_complete
"POLYGON ((23.63055555555556 38.0611111111111, 23.63333333333335 38.0611111111111, 23.63333333333335 38.0638888888889, 23.63055555555556 38.0638888888889, 23.63055555555556 38.0611111111111))",,cell_2423204507,,GDE_Com_940019,,23.631944444400002,,38.0625,,CR/LDUAL+CDH/H:1/15.0,,0.12688663132861894,,"[8.474877104881386e-06, 0.0, 0.0, 0.0]",,"[0.9999915251228951, 8.474877104881386e-06, -0.0, -0.0, 0.0]",,28.3,,28.299760160977932,,0.00023983902206814323,,-0.0,,-0.0,,0.0 "POLYGON ((23.63055555555556 38.0611111111111, 23.63333333333335 38.0611111111111, 23.63333333333335 38.0638888888889, 23.63055555555556 38.0638888888889, 23.63055555555556 38.0611111111111))",,cell_2423204507,,GDE_Com_940019,,23.631944444400002,,38.0625,,CR/LDUAL+CDH/H:1/15.0,,0.12688663132861894,,"[8.474877104881386e-06, 0.0, 0.0, 0.0]",,"[0.9999915251228951, 8.474877104881386e-06, -0.0, -0.0, 0.0]",,28.3,,28.299760160977932,,0.00023983902206814323,,-0.0,,-0.0,,0.0
"POLYGON ((23.63055555555556 38.0611111111111, 23.63333333333335 38.0611111111111, 23.63333333333335 38.0638888888889, 23.63055555555556 38.0638888888889, 23.63055555555556 38.0611111111111))",,cell_2423204507,,GDE_Com_940036,,23.631944444400002,,38.0625,,CR/LDUAL+CDM/H:1/8.0,,0.12688663132861894,,"[8.474877104881386e-06, 0.0, 0.0, 0.0]",,"[0.9999915251228951, 8.474877104881386e-06, -0.0, -0.0, 0.0]",,12.5,,12.499894064036189,,0.00010593596381101732,,-0.0,,-0.0,,0.0 "POLYGON ((23.63055555555556 38.0611111111111, 23.63333333333335 38.0611111111111, 23.63333333333335 38.0638888888889, 23.63055555555556 38.0638888888889, 23.63055555555556 38.0611111111111))",,cell_2423204507,,GDE_Com_940036,,23.631944444400002,,38.0625,,CR/LDUAL+CDM/H:1/8.0,,0.12688663132861894,,"[8.474877104881386e-06, 0.0, 0.0, 0.0]",,"[0.9999915251228951, 8.474877104881386e-06, -0.0, -0.0, 0.0]",,12.5,,12.499894064036189,,0.00010593596381101732,,-0.0,,-0.0,,0.0
"POLYGON ((23.63055555555556 38.0611111111111, 23.63333333333335 38.0611111111111, 23.63333333333335 38.0638888888889, 23.63055555555556 38.0638888888889, 23.63055555555556 38.0611111111111))",,cell_2423204507,,GDE_Res_1036159,,23.631944444400002,,38.0625,,CR/LDUAL+CDL/HBET:3-5/SOS/8.0,,0.251555124927799,,"[0.13663294980106422, 0.00180695837185944, 0.0001277873923228429, 1.821617605136137e-05]",,"[0.8633670501989358, 0.1348259914292048, 0.001679170979536597, 0.00010957121627148152, 1.821617605136137e-05]",,36.2,,31.253887217201477,,4.880700889737214,,0.060785989459224817,,0.003966478029027631,,0.0006594255730592816 "POLYGON ((23.63055555555556 38.0611111111111, 23.63333333333335 38.0611111111111, 23.63333333333335 38.0638888888889, 23.63055555555556 38.0638888888889, 23.63055555555556 38.0611111111111))",,cell_2423204507,,GDE_Res_1036159,,23.631944444400002,,38.0625,,CR/LDUAL+CDL/HBET:3-5/SOS/8.0,,0.251555124927799,,"[0.13663294980106422, 0.00180695837185944, 0.0001277873923228429, 1.821617605136137e-05]",,"[0.8633670501989358, 0.1348259914292048, 0.001679170979536597, 0.00010957121627148152, 1.821617605136137e-05]",,36.2,,31.253887217201477,,4.880700889737214,,0.060785989459224817,,0.003966478029027631,,0.0006594255730592816
......
...@@ -30,7 +30,7 @@ def get_exposure_per_tile( ...@@ -30,7 +30,7 @@ def get_exposure_per_tile(
exposure_filepath, exposure_filepath,
fragility_pathname, fragility_pathname,
taxonomy_conversion_filepath, taxonomy_conversion_filepath,
ground_motion_filepath, ground_motion_field_filepath,
geometry_source_filepath, geometry_source_filepath,
cell_id_source_filepath, cell_id_source_filepath,
exposure_type="cell", exposure_type="cell",
...@@ -41,10 +41,24 @@ def get_exposure_per_tile( ...@@ -41,10 +41,24 @@ def get_exposure_per_tile(
exposure = pd.read_csv(exposure_filepath) exposure = pd.read_csv(exposure_filepath)
lons_whole_area = exposure.lon lons_whole_area = exposure.lon
lats_whole_area = exposure.lat lats_whole_area = exposure.lat
ground_motion_field = np.loadtxt(ground_motion_filepath, delimiter=",", skiprows=1) ground_motion_field = np.loadtxt(ground_motion_field_filepath, delimiter=",", skiprows=1)
full_ground_motion_field = losslib.get_full_GMF( full_ground_motion_field = losslib.get_full_GMF(
ground_motion_field, lons_whole_area, lats_whole_area, interpolation_method ground_motion_field, lons_whole_area, lats_whole_area, interpolation_method
) )
# Creating an empty dictionary to later fill with ground-motion types and their column
# numbers (as they appear in the ground-motion-field file) as its key and value.
ground_motion_type_index_map = {}
with open(ground_motion_field_filepath) as gmf:
# Extracting the ground-motion types available in the ground-motion-field file. Note
# the ground-motion-field file format:
# `[lon, lat, gmValueofType1, ..., gmValueofTypeN]`
gm_types = gmf.readline().strip().split(",")[2:]
for i in range(len(gm_types)):
# Appending each `gm_type` and its column number (as it appears in the
# 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 = pd.read_csv(cell_id_source_filepath) cell_ids = pd.read_csv(cell_id_source_filepath)
with open(result_filepath, "a+", newline="") as write_obj: with open(result_filepath, "a+", newline="") as write_obj:
csv_writer = csv.writer(write_obj) csv_writer = csv.writer(write_obj)
...@@ -129,8 +143,9 @@ def get_exposure_per_tile( ...@@ -129,8 +143,9 @@ def get_exposure_per_tile(
fragility_pathname, fragility_pathname,
exposure_per_tile, exposure_per_tile,
taxonomy_conversion_filepath, taxonomy_conversion_filepath,
ground_motion_filepath, ground_motion_field_filepath,
geometry_source_filepath, geometry_source_filepath,
ground_motion_type_index_map,
exposure_type, exposure_type,
interpolation_method, interpolation_method,
) )
...@@ -241,7 +256,7 @@ if __name__ == "__main__": ...@@ -241,7 +256,7 @@ if __name__ == "__main__":
exposure_type = args.exposure_type exposure_type = args.exposure_type
fragility_pathname = args.fragilities fragility_pathname = args.fragilities
taxonomy_conversion_filepath = args.taxonomy_map taxonomy_conversion_filepath = args.taxonomy_map
ground_motion_filepath = args.ground_motion_field ground_motion_field_filepath = args.ground_motion_field
cell_id_source_filepath = args.cell_ids cell_id_source_filepath = args.cell_ids
exposure_filepath = args.exposure exposure_filepath = args.exposure
geometry_source_filepath = args.geometry geometry_source_filepath = args.geometry
...@@ -262,7 +277,7 @@ if __name__ == "__main__": ...@@ -262,7 +277,7 @@ if __name__ == "__main__":
exposure_filepath, exposure_filepath,
fragility_pathname, fragility_pathname,
taxonomy_conversion_filepath, taxonomy_conversion_filepath,
ground_motion_filepath, ground_motion_field_filepath,
geometry_source_filepath, geometry_source_filepath,
cell_id_source_filepath, cell_id_source_filepath,
exposure_type, exposure_type,
......
...@@ -28,8 +28,9 @@ def damageCalculator_TileVersion( ...@@ -28,8 +28,9 @@ def damageCalculator_TileVersion(
fragility_pathname, fragility_pathname,
exposure, exposure,
taxonomy_conversion_path, taxonomy_conversion_path,
ground_motion_filepath, ground_motion_field_filepath,
geometry_source_path, geometry_source_path,
ground_motion_type_index_map,
exposure_type="cell", exposure_type="cell",
method="linear", method="linear",
): ):
...@@ -97,10 +98,10 @@ def damageCalculator_TileVersion( ...@@ -97,10 +98,10 @@ def damageCalculator_TileVersion(
>>> taxonomy_conversion_path >>> taxonomy_conversion_path
"/home/TileCalculations/taxonomy_mapping_Europe.csv" "/home/TileCalculations/taxonomy_mapping_Europe.csv"
- ground_motion_filepath: (str) - ground_motion_field_filepath: (str)
Address to the ground-motion values file. Address to the ground-motion values file.
Example extract: Example extract:
>>> ground_motion_filepath >>> ground_motion_field_filepath
"/home/TileCalculations/shakemap1381_2.csv" "/home/TileCalculations/shakemap1381_2.csv"
- geometry_source_path: (str) - geometry_source_path: (str)
...@@ -157,14 +158,11 @@ def damageCalculator_TileVersion( ...@@ -157,14 +158,11 @@ def damageCalculator_TileVersion(
origin_ids = exposure.origin_id origin_ids = exposure.origin_id
# Begin Computation # Begin Computation
# Define a dictionary with keys as the ground-motion type and value as the column
# number of the ground-motion type in the shakemap file.
gmDict = {"PGA": 2, "SA(0.3)": 3, "SA(0.6)": 4, "SA(1.0)": 5, "SA(1)": 5}
# Calling the function "taxonomy_to_fragility" to get a dictionary with keys as the # 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) # 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. # and the column number of the respective ground-motion type in `ground-motion-field` file.
taxonomy_to_fragility_map = losslib.taxonomy_to_fragility( taxonomy_to_fragility_map = losslib.taxonomy_to_fragility(
gmDict, taxonomy_to_fragility_source, fragility_pathname ground_motion_type_index_map, taxonomy_to_fragility_source, fragility_pathname
) )
# Calling the function "origin_id_to_geometry" to get a dictionary with keys as the # Calling the function "origin_id_to_geometry" to get a dictionary with keys as the
# origin_id and the value as the respective polygon. # origin_id and the value as the respective polygon.
......
...@@ -107,7 +107,9 @@ def get_full_GMF(ground_motion_field, lons, lats, method="linear"): ...@@ -107,7 +107,9 @@ def get_full_GMF(ground_motion_field, lons, lats, method="linear"):
return full_ground_motion_field return full_ground_motion_field
def taxonomy_to_fragility(gmDict, taxonomy_to_fragility_source, fragility_pathname): def taxonomy_to_fragility(
ground_motion_type_index_map, taxonomy_to_fragility_source, fragility_pathname
):
""" """
Creates an extended map of taxonomies to fragility function. Creates an extended map of taxonomies to fragility function.
The input map 'taxonomy_to_fragility_source' contains the mapping for each The input map 'taxonomy_to_fragility_source' contains the mapping for each
...@@ -116,12 +118,12 @@ def taxonomy_to_fragility(gmDict, taxonomy_to_fragility_source, fragility_pathna ...@@ -116,12 +118,12 @@ def taxonomy_to_fragility(gmDict, taxonomy_to_fragility_source, fragility_pathna
Input: Input:
------ ------
- gmDict: (dictionary) - ground_motion_type_index_map: (dictionary)
key: ground-motion type; value: column number in the ground-motion key: ground-motion type; value: column number in the ground-motion
field file field file
Example extract: Example extract:
>>> gmDict >>> ground_motion_type_index_map
{'PGA': 2, 'SA(0.3)': 3, 'SA(0.6)': 4, 'SA(1)': 5} {'gmv_PGA': 2, 'gmv_SA(0.3)': 3, 'gmv_SA(0.6)': 4, 'gmv_SA(1)': 5}
- taxonomy_to_fragility_source: (csv.reader) - taxonomy_to_fragility_source: (csv.reader)
taxonomy to fragility-function file map following the format: taxonomy to fragility-function file map following the format:
...@@ -155,23 +157,32 @@ def taxonomy_to_fragility(gmDict, taxonomy_to_fragility_source, fragility_pathna ...@@ -155,23 +157,32 @@ def taxonomy_to_fragility(gmDict, taxonomy_to_fragility_source, fragility_pathna
# Prepare return variable # Prepare return variable
taxonomy_to_fragility_map = {} taxonomy_to_fragility_map = {}
# Loop through the taxonomy-to-fragility-function map # Loop through the taxonomy-to-fragility-function map
for mapping_item in taxonomy_to_fragility_source: for taxonomy_map_entry in taxonomy_to_fragility_source:
# Open the fragility-function file corresponding to the taxonomy in # Open the fragility-function file corresponding to the taxonomy in
# 'mapping_item[1]' # 'taxonomy_map_entry[1]'
fragilityFunction = list( fragility_function = list(
csv.reader(open(fragility_pathname + "/" + mapping_item[1] + ".csv")) csv.reader(open(fragility_pathname + "/" + taxonomy_map_entry[1] + ".csv"))
) )
# Check if already one fragility function for a given GM type has been # Check if already one fragility function for a given GM type has been
# selected # selected
if mapping_item[0] in taxonomy_to_fragility_map: if taxonomy_map_entry[0] in taxonomy_to_fragility_map:
# Ignore the additional fragility function to keep everything # Ignore the additional fragility function to keep everything
# unambiguous # unambiguous
pass pass
# Create the entry in the extended map with adding the type of ground # Create the entry in the extended map with adding the type of ground
# motion 'fragilityFunction[0][0]' # motion 'fragility_function[0][0]'
taxonomy_to_fragility_map[mapping_item[0]] = [ taxonomy_to_fragility_map[taxonomy_map_entry[0]] = [
mapping_item[1], taxonomy_map_entry[1],
gmDict[fragilityFunction[0][0]], # The header of the ground-motion-field file contains a `gmv_` prefix before
# ground-motion types as an abbrevation for 'ground-motion value', which is not used
# in the `fragility_function` (for example gmv_SA(0.3) in the ground-motion-field
# file and SA(0.3) in the fragility_function). This prefix is kept so that the
# ground-motion-field file has the same format as the `OpenQuake` program to stay
# compatible with. Thus, this prefix needs to be added to
# the ground_motion type in the `fragility_function` to match its respective
# ground-motion type header in the ground_motion_field file. For more information
# please check the example extracts and also the `documentation.md` file.
ground_motion_type_index_map["gmv_" + fragility_function[0][0]],
] ]
return taxonomy_to_fragility_map return taxonomy_to_fragility_map
...@@ -309,9 +320,10 @@ def get_PoEs(fragility_function, gm_value): ...@@ -309,9 +320,10 @@ def get_PoEs(fragility_function, gm_value):
2.152E-02 2.152E-02
- fragility_function: (Numpy nd-array of shape (5,100) in our case; - fragility_function: (Numpy nd-array of shape (5,100) in our case;
An array of 5 records with values of Intensity measure levels, values An array with different levels of a ground_motion type as its first record
of slight, moderate, extensive and complete damage probabilities of and probabilities of exceeding slight, moderate, extensive and complete damage states
exceedance, as the first to 5th records respectively.) corresponding to each level as the 2nd to 5th record.
Example extract: Example extract:
>>> fragility_function >>> fragility_function
array([ array([
......
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