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

Deleted unusefull functions

parent dccb4e51
Pipeline #20779 passed with stage
in 1 minute and 28 seconds
#!/usr/bin/env python3
# Copyright (c) 2020-2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
import pandas as pd
import csv
import datetime
def append_respective_cell_id_to_building_exposure(
exposure_filepath, building_to_cell_id_filepath, result_filepath
):
startTime = datetime.datetime.now()
print(startTime)
building_to_cell_id_source = csv.reader(open(building_to_cell_id_filepath), delimiter=";")
next(building_to_cell_id_source, None)
exposure = pd.read_csv(exposure_filepath)
origin_ids = exposure.origin_id
with open(result_filepath, "a+", newline="") as write_obj:
csv_writer = csv.writer(write_obj)
title = [
"id",
"lon",
"lat",
"taxonomy",
"number",
"structural",
"night",
"occupancy",
"admin_name",
"admin_ID",
"origin_id",
"respective_cell_id",
]
csv_writer.writerow(title)
building_to_cell_id_map = {}
for mapping_item in building_to_cell_id_source:
if mapping_item[0] in building_to_cell_id_map:
# Ignore the additional geometry to keep everything unambiguous
pass
building_to_cell_id_map[mapping_item[0]] = mapping_item[2]
for asset_from_model in range(exposure.shape[0]):
asset_for_calculation = list(exposure.loc[asset_from_model])
origin_id = origin_ids[asset_from_model]
respective_cell_id = building_to_cell_id_map[origin_id]
asset_for_calculation.append(respective_cell_id)
csv_writer.writerow(asset_for_calculation)
print("Execution time of the script", (datetime.datetime.now() - startTime))
#!/usr/bin/env python3
# Copyright (c) 2020-2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
import numpy as np
import csv
import datetime
import losslib
def damageCalculator_TileVersion(
full_ground_motion_field,
result_filepath,
fragility_pathname,
exposure,
taxonomy_conversion_path,
ground_motion_field_filepath,
ground_motion_type_index_map,
interpolation_method="linear",
):
"""
Returns a file "result_filepath" including damage results of a scenario earthquake using the
"losslib" functions.
Input:
------
- full_ground_motion_field: (Numpy nd-array of shape(m,n); m records with long,
lat, and n-2 gm-types)
contains locations and the ground-motion values of each location
in the format of lon, lat, gmValueofType1, gmValueofType2, ...,
gmValueofTypeN.
Please note that if the ground_motion_field contains values for
more than one ground-motion type, This function will do
interpolation multiple times and each time using one of the
gmValueTypes with the format as below:
[lon, lat, gmValueofType1, gmValueofType2, ..., gmValueofTypeN]
As an example:
[23.6875,38.3402777778,'PGA','SA(0.3)','SA(0.6)','SA(1.0)']
Example extract:
>>> full_ground_motion_field
array([[
[23.687 , 38.340, 0.179, 0.362 , 0.252,
0.152],
...,
[23.690, 38.340, 0.177, 0.359, 0.25,
0.151],
...,
[23.693, 38.340, 0.176, 0.356, 0.248,
0.149]
]])
- result_filepath: (str)
Result file address.
Example extract:
>>> result_filepath
"/home/TileCalculations/DamageResult.csv"
- fragility_pathname: (str)
Address to the directory including all the fragility functions.
Example extract:
>>> fragility_pathname
"/home/TileCalculations/Fragilities"
- exposure: (pandas.DataFrame(series))
informaion of the assets of the region of interest under hazard.
Example extract:
>>> exposure
id lon lat ... admin_name admin_ID origin_id
0 GDE_Ind_0 23.687500 38.340278 ... Oropos GR_3514913 cell_2410244527
1 GDE_Ind_1 23.687500 38.340278 ... Oropos GR_3514913 cell_2410244527
...
8 GDE_Ind_8 23.690278 38.340278 ... Oropos GR_3514913 cell_2410244528
9 GDE_Ind_9 23.690278 38.340278 ... Oropos GR_3514913 cell_2410244528
...
22 GDE_Ind_22 23.693056 38.340278 ... Oropos GR_3514913 cell_2410244529
23 GDE_Ind_23 23.693056 38.340278 ... Oropos GR_3514913 cell_2410244529
- taxonomy_conversion_path: (str)
Address of the file including old and new taxonomy names.
Example extract:
>>> taxonomy_conversion_path
"/home/TileCalculations/taxonomy_mapping_Europe.csv"
- ground_motion_field_filepath: (str)
Address to the ground-motion values file.
Example extract:
>>> ground_motion_field_filepath
"/home/TileCalculations/shakemap1381_2.csv"
- geometry_source_path: (str)
Address to the file including the origin-ids and their respecive polygons
(Could be whether each tile oan OSM building).
Example extract:
>>> geometry_source_path
"/home/TileCalculations
/M008_exposure_Attica_GDE_visual_v001_sat_27f_by_cell_reOrder.csv"
- exposure_type: (string), optional
{‘cell’, ‘building’}. Cell by default.
- method: (string), optional
{‘linear’, ‘nearest’, ‘cubic’}, optional'. Linear by default.
Output:
-------
- result_filepath: (arrays written to file)
file containing the damage elements for each asset of the exposure.
Example extract of the result file:
geometry,,origin_id,,asset_id,,lon,,lat,,taxonomy,,gmfValue,,PoEs,,PoOs,,
num_buildings,,structural_No-damage,,structural_Slight,,structural_Moderate,,
structural_Extensive,,structural_Complete
"POLYGON ((23.68611111111113 38.3388888888889, 23.6888888888889 38.3388888888889,
23.6888888888889 38.34166666666667, 23.68611111111113 38.34166666666667,
23.68611111111113 38.3388888888889))",,cell_2410244527,,GDE_Ind_0,,23.6875,,
38.340277777800004,,CR/LFINF+CDM/H:2,,0.3623185006574049,,"[0.2495074860984066,
0.008729386056721225, 0.0010441231376296645, 0.00021103727864033706]",,
"[0.7504925139015934, 0.24077810004168537, 0.007685262919091561, 0.0008330858589893275,
0.00021103727864033706]",,0.000629094517209,,0.0004721307257018916,,
0.00015147218260022435,,4.834756765710135e-06,,5.24089746254536e-07,,
1.3276239491934405e-07
"""
# Show the time the script begins running
startTime = datetime.datetime.now()
print(startTime)
# Read inputs as Numpy arrays or Pandas data-frames
taxonomy_to_fragility_source = csv.reader(open(taxonomy_conversion_path))
# Skip the header
next(taxonomy_to_fragility_source, None)
# Read each column of the exposure model
taxonomies = exposure.taxonomy
tot_num_buildings = exposure.number
lons = exposure.lon
lats = exposure.lat
asset_ids = exposure.id
tile_geometries = exposure.tile_geometry
tile_ids = exposure.tile_id
building_geometries = exposure.building_geometry
building_ids = exposure.building_id
# Begin Computation
# 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
)
# Define number of columns that contain the data in the fragility function files.
cls = range(1, 101)
# Looping through each line of the exposure file to run the computations asset by asset.
with open(result_filepath, "a+", newline="") as write_obj:
csv_writer = csv.writer(write_obj, delimiter=";")
for asset in range(exposure.shape[0]):
taxonomy = taxonomies.iloc[asset]
fragilityfunction_filename = taxonomy_to_fragility_map[taxonomy][0] + ".csv"
num_buildings = tot_num_buildings.iloc[asset]
lon = lons.iloc[asset]
lat = lats.iloc[asset]
asset_id = asset_ids.iloc[asset]
tile_geometry = tile_geometries.iloc[asset]
tile_id = tile_ids.iloc[asset]
building_geometry = building_geometries.iloc[asset]
building_id = building_ids.iloc[asset]
# Read fragility functions as numpy arrays.
fragility_function = np.loadtxt(
fragility_pathname + "/" + fragilityfunction_filename,
delimiter=",",
usecols=cls,
)
# Computing the ground-motion values from `full_ground_motion_field`. Please
# note that `full_ground_motion_field` contains many identical lines (because
# many of the assets have same locations and same location leads to the same
# ground-motion value). This way, `full_ground_motion_field` has the same number
# and order of assets as in the exposure model for simpler processing.
gm_value = full_ground_motion_field[asset, taxonomy_to_fragility_map[taxonomy][1]]
# Computing probabilities of exceedance and occurrence.
[PoEs, PoOs] = losslib.get_PoEs(fragility_function, gm_value)
# Compute damage by assets
damage_by_asset = [i * num_buildings for i in PoOs]
# Append results
result_asset_info = [
asset_id,
lon,
lat,
taxonomy,
gm_value,
PoEs,
PoOs,
num_buildings,
tile_id,
tile_geometry,
building_id,
building_geometry,
]
for i in range(len(damage_by_asset)):
result_asset_info.append(damage_by_asset[i])
csv_writer.writerow(result_asset_info)
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