Commit 1c3ecd7b authored by Tara Evaz Zadeh's avatar Tara Evaz Zadeh

Deleted wrapper_cell_by_cell.py and loss_calculator.py

parent a086fcaf
Pipeline #18276 passed with stage
in 1 minute and 46 seconds
#!/usr/bin/env python
# Copyright (c) 2020:
# 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 os
import pandas as pd
import csv
import datetime
import losslib
def main(
Result_path,
fragilityFileDir,
exposures_path,
taxonomy_conversion_path,
shakemap_path,
geometry_source_path,
exposureType,
method,
):
# This function prepares the inputs and calls the functions needed.
startTime = datetime.datetime.now()
print(startTime)
if os.path.exists(Result_path):
os.remove(Result_path)
# Read Files as Numpy arrays or Pandas data-frames
exposures = pd.read_csv(exposures_path)
groundMotionField = np.loadtxt(shakemap_path, delimiter=",", skiprows=1)
taxonomyToFragilitySource = csv.reader(open(taxonomy_conversion_path))
next(taxonomyToFragilitySource, None)
geometry_source = csv.reader(open(geometry_source_path), delimiter=";")
next(geometry_source, None)
# Read Columns
taxonomies = exposures.taxonomy
tot_num_buildings_row = exposures.number
lons = exposures.lon
lats = exposures.lat
assetids = exposures.id
originids = exposures.origin_id
# originids=exposures.id_3
# Prepare Result File
def append_list_as_row(file_name, list_of_elem):
with open(file_name, "a+", newline="") as write_obj:
csv_writer = csv.writer(write_obj)
csv_writer.writerow(list_of_elem)
if exposureType == "OBM":
title = [
"geometry",
"",
"origin_id",
"",
"RelativeCellid",
"",
"asset_id",
"",
"lon",
"",
"lat",
"",
"taxonomy",
"",
"gmfValue",
"",
"PoEs",
"",
"PoOs",
"",
"tot_num_buildings",
"",
"structural_No-damage",
"",
"structural_Slight",
"",
"structural_Moderate",
"",
"structural_Extensive",
"",
"structural_Complete",
]
else:
title = [
"geometry",
"",
"origin_id",
"",
"asset_id",
"",
"lon",
"",
"lat",
"",
"taxonomy",
"",
"gmfValue",
"",
"PoEs",
"",
"PoOs",
"",
"tot_num_buildings",
"",
"structural_No-damage",
"",
"structural_Slight",
"",
"structural_Moderate",
"",
"structural_Extensive",
"",
"structural_Complete",
]
append_list_as_row(Result_path, title)
# Computation
fullGroundMotionField = losslib.Get_Full_GMF(groundMotionField, lons, lats, method)
gmDict = {"PGA": 2, "SA(0.3)": 3, "SA(0.6)": 4, "SA(1.0)": 5, "SA(1)": 5}
taxonomyToFragilityMap = losslib.Taxonomy_to_Fragility(
gmDict, taxonomyToFragilitySource, fragilityFileDir
)
origin_id_to_geometry_map = losslib.origin_id_to_geometry(geometry_source, exposureType)
# Number of columns that contain the data in the fragiliy function files
cls = range(1, 101)
a = [0, 2, 4, 6, 8]
for asset in range(exposures.shape[0]):
taxonomy = taxonomies[asset]
fragilityFileName = taxonomyToFragilityMap[taxonomy][0] + ".csv"
tot_num_buildings = tot_num_buildings_row[asset]
lon = lons[asset]
lat = lats[asset]
asset_id = assetids[asset]
origin_id = originids[asset]
if exposureType == "OBM":
[polygon, relativeCellid] = origin_id_to_geometry_map[origin_id]
else:
polygon = origin_id_to_geometry_map[origin_id]
# Find new taxonomy names
fragility_function = np.loadtxt(
fragilityFileDir + "/" + fragilityFileName, delimiter=",", usecols=cls
)
gm_value = fullGroundMotionField[asset, taxonomyToFragilityMap[taxonomy][1]]
[PoEs, PoOs] = losslib.get_PoEs(fragility_function, gm_value)
# Compute damages by assets
dmg_by_asset = [i * tot_num_buildings for i in PoOs]
for h in a:
dmg_by_asset.insert(h, "")
# Append results
if exposureType == "OBM":
arr0 = [
polygon,
"",
origin_id,
"",
"cell_" + relativeCellid,
"",
asset_id,
"",
lon,
"",
lat,
"",
taxonomy,
"",
gm_value,
"",
PoEs,
"",
PoOs,
"",
tot_num_buildings,
]
else:
arr0 = [
polygon,
"",
origin_id,
"",
asset_id,
"",
lon,
"",
lat,
"",
taxonomy,
"",
gm_value,
"",
PoEs,
"",
PoOs,
"",
tot_num_buildings,
]
arr0.extend(dmg_by_asset)
append_list_as_row(Result_path, arr0)
print("Execution time of the script", (datetime.datetime.now() - startTime))
#!/usr/bin/env python3
# Copyright (c) 2020:
# 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 pandas as pd
import csv
import losslib
import os
import argparse
from damage_calculator_tile_version import damageCalculator_TileVersion
def get_exposure_per_tile(
exposure_filepath,
fragility_pathname,
taxonomy_conversion_filepath,
shakemap_filepath,
polygonSource_filepath,
cellIdSource_filepath,
exposure_type="cell",
interpolation_method="linear",
result_filepath="damage_result.gpkg",
):
"""
Returns exposure file as assets in each tile and also passes all the inputs
to the damage calculator.
Input:
------
- result_filepath: (str)
Result file address.
Example extract:
>>> result_filepath
"/home/TileCalculations/DamageResult.csv"
- exposure_filepath: (str)
Address to the directory including all the fragility functions.
Example extract:
>>> exposure_filepath
"/home/TileCalculations/M008_exposure_Attica_GDE_OBMpart_v001_sat_27f_Ind.csv"
Which should include data as below:
id,lon,lat,taxonomy,number,structural,night,occupancy,admin_name,admin_ID,origin_id
GDE_Ind_8344,23.6835236228,38.2808067265,CR/LFINF+CDM/H:2,0.225062972292,
402000.816015,0.0,Ind,Oropos,GR_3514913,OSM_502509057
GDE_Ind_8345,23.6835236228,38.2808067265,CR/LFM+CDM/H:2,0.225062972292,
402000.816015,0.0,Ind,Oropos,GR_3514913,OSM_502509057
...
GDE_Ind_201281,23.8998942292,37.4896629325,W/LWAL+CDL/H:1,0.00944510035419,
15263.838681,0.0,Ind,Lavreotiki,GR_3514905,OSM_506291001
- fragility_pathname: (str)
Address to the directory including all the fragility functions.
Example extract:
>>> fragility_pathname
"/home/TileCalculations/Fragilities"
Each of the fragility functions (csv files) in this directory should include
data as below:
PGA,0.050000 ,0.052179 ,0.054454 ,0.056827 ,... ,3.271841 ,3.414451
slight,0.000000 ,0.000000 ,0.000000 ,0.000000 ,... ,0.873138 ,0.887882
moderate,0.000000 ,0.000000 ,0.000000 ,0.000000 ,... ,0.440287 ,0.469610
extensive,0.000000 ,0.000000 ,0.000000 ,0.000000 ,... ,0.222803 ,0.245484
complete,0.000000 ,0.000000 ,0.000000 ,0.000000 ,... ,0.121331 ,0.136897
Please note that the number of columns of each fragility function is 101 in this
case. You should consider this in a variable called "cls".
There could be any kind of Intensity measure level for the first row such as
PGA, SA(0.3), SA(0.6), SA(1.0) etc.
- taxonomy_conversion_filepath: (str)
Address of the file including old and new taxonomy names.
Example extract:
>>> taxonomy_conversion_filepath
"/home/TileCalculations/taxonomy_mapping_Europe.csv"
This file Includes the taxonomy (as mentioned in the "taxonomy" column of the
xposure files) and its conversion which is the name of each fragility csv file
and should include data as below:
taxonomy,conversion,weight
CR/LDUAL+CDM/HBET:6-/11.0,CR_LDUAL-DUM_H6,1
CR/LDUAL+CDM/HBET:6-/5.0,CR_LDUAL-DUL_H6,1
...
CR/LDUAL+CDM/HBET:6-/SOS/11.0,CR_LDUAL-DUL_H6,1
- shakemap_filepath: (str)
Address to the ground-motion values file.
Example extract:
>>> shakemap_filepath
"/home/TileCalculations/shakemap1381_2.csv"
This file includes the location and the ground-motion values for all the intensity
measure levels mentioned in the fragility functions and should include the data as
below:
lon,lat,gmv_PGA,gmv_SA(0.3),gmv_SA(0.6),gmv_SA(1.0)
23.629170000000002,38.06528,0.1291617,0.2559737,0.1626296,0.08834055
...
23.63472,38.05972,0.1246124,0.2471472,0.1570672,0.08533953
Please note that Since we do only interpolation to find the ground-motion value for all
the locations of the exposure model, you should be careful not to have locations of the
exposure models outside or on the edge of the given ground-motion values.
- cellIdSource_filepath: (str)
Address to the file including the unique ids of the tiles (same as the origin-ids of
the cell exposure files or simpy cell-ids).
Example extract:
>>> polygonSource_filepath
"/home/TileCalculations/exposure_uniq_cellids.csv"
This file contains the unique cell-ids mentioned in the exposure file and should
include the data as below:
uniqueCellIds
cell_2423204507
cell_2423204508
...
cell_2413743765
- polygonSource_filepath: (str)
Address to the file including the origin-ids and thir respecive polygons
(Could be whether each tile oan OSM building).
Example extract:
>>> polygonSource_filepath
"/home/TileCalculations/M008_exposure_Attica_GDE_visual_v001_sat_27f_by_cell_reOrder.csv"
This file contains the polygons for either each 10-arcsec cell (in case
exposure_type = "cell") or each buildings (in case exposure_type = "building")
and the cell-id that includes each building and should contain the data as below:
OSM_ID;geometry;cell_ID
OSM_517924352;POLYGON((2641289.26886243 4582010.12482994,2641299.67723482
4582007.12923406,2641304.17454225 4582019.8887829,2641294.42295486
4582023.47785041,2641291.51751615 4582013.47368108,2641290.54903658
4582013.85519583,2641289.26886243 4582010.12482994));2425278141
...
OSM_585772157;POLYGON((2644395.16 4566329.71,2644395.62 4566343.61,2644412.41
4566343.04,2644411.94 4566329.27,2644406.98 4566329.44,2644406.93 4566328.05,
2644398.08 4566328.24,2644396.22 4566328.38,2644396.26 4566329.68,2644395.16
4566329.71));2430462151
- exposure_type: (string), optional
{‘cell’, ‘building’}, optional'. cell by default.
- interpolation_method: (string), optional
{‘linear’, ‘nearest’, ‘cubic’}, optional'. linear by default.
Output:
-------
- 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. Passed unchaged from input to the output.
Example extract:
>>> result_filepath
"/home/TileCalculations/DamageResult.csv"
- fragility_pathname: (str)
Address to the directory including all the fragility functions. Passed unchaged from
input to the output.
Example extract:
>>> fragility_pathname
"/home/TileCalculations/Fragilities"
- exposure_per_tile: (pandas.DataFrame(series))
informaion of the assets of each tile of the region of interest.
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_filepath: (str)
Address of the file including old and new taxonomy names. Passed unchaged from input
to the output.
Example extract:
>>> taxonomy_conversion_filepath
"/home/TileCalculations/taxonomy_mapping_Europe.csv"
- shakemap_filepath: (str)
Address to the ground-motion values file. Passed unchaged from input to the output.
Example extract:
>>> shakemap_filepath
"/home/TileCalculations/shakemap1381_2.csv"
- polygonSource_filepath: (str)
Address to the file including the origin-ids and thir respecive polygins
(Could be whether each tile oan OSM building). Passed unchaged from input to
the output.
Example extract:
>>> polygonSource_filepath
"/home/TileCalculations/
M008_exposure_Attica_GDE_visual_v001_sat_27f_by_cell_reOrder.csv"
- exposure_type: (string), optional. Passed unchaged from input to the output.
{‘cell’, ‘building’}, optional'. cell by default.
- interpolation_method: (string), optional
{‘linear’, ‘nearest’, ‘cubic’}, optional'. Linear by default. Passed unchaged
from input to the output.
"""
# if len(sys.argv)<6:
# raise ValueError('There are not enough inputs given')
# sys.exit(1)
exposure = pd.read_csv(exposure_filepath)
lons_whole_area = exposure.lon
lats_whole_area = exposure.lat
ground_motion_field = np.loadtxt(shakemap_filepath, delimiter=",", skiprows=1)
full_ground_motion_field = losslib.Get_Full_GMF(
ground_motion_field, lons_whole_area, lats_whole_area, interpolation_method
)
all_cell_ids = np.loadtxt(cellIdSource_filepath, dtype="str", skiprows=1)
def write_asset_to_file(file_name, list_of_elem):
"""This function writes a list (list_of_elem) to a file (file_name)"""
# Open the file to write the lists to. a+ gives the possibility of reading and
# writing if file exists and creating it if it doesn't.
with open(file_name, "a+", newline="") as write_obj:
csv_writer = csv.writer(write_obj)
csv_writer.writerow(list_of_elem)
if exposure_type == "building":
title = [
"geometry",
"",
"origin_id",
"",
"RespectiveCellid",
"",
"asset_id",
"",
"lon",
"",
"lat",
"",
"taxonomy",
"",
"gm_value",
"",
"PoEs",
"",
"PoOs",
"",
"tot_num_buildings",
"",
"structural_no_damage",
"",
"structural_slight",
"",
"structural_moderate",
"",
"structural_extensive",
"",
"structural_complete",
]
tile_id = "respectiveCellId"
else:
title = [
"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",
]
tile_id = "origin_id"
write_asset_to_file(result_filepath, title)
for i in range(all_cell_ids.shape[0]):
cell_id = all_cell_ids[i]
exposure_per_tile = exposure.loc[exposure[tile_id] == cell_id]
damageCalculator_TileVersion(
full_ground_motion_field,
result_filepath,
fragility_pathname,
exposure_per_tile,
taxonomy_conversion_filepath,
shakemap_filepath,
polygonSource_filepath,
exposure_type,
interpolation_method,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-m",
"--interpolation_method",
required=False,
type=str,
default="linear",
help="the method used to interpolate the ground-motion values over the exposure "
+ "locations. Options: [‘linear’, ‘nearest’, ‘cubic’] (default set to linear)",
)
parser.add_argument(
"-x",
"--exposure_type",
required=False,
type=str,
default="cell",
help="exposure type. Options = [‘cell’, ‘building’] (default set to cell)",
)
parser.add_argument(
"-f",
"--fragility_pathname",
required=True,
type=str,
help="path to the directory that includes all the fragility csv file functions "
+ "(Required)",
)
parser.add_argument(
"-t",
"--taxonomy_conversion_filepath",
required=True,
type=str,
help="path to the file that includes the taxonomy names and their respective "
+ "fragility function names (Required)",
)
parser.add_argument(
"-g",
"--shakemap_filepath",
required=True,
type=str,
help="path to the ground-motion values file (Required)",
)
parser.add_argument(
"-c",