Commit 86a51d1e authored by Tara Evaz Zadeh's avatar Tara Evaz Zadeh
Browse files

added files

parent 0263d1d6
#!/usr/bin/env python3
import numpy as np
import pandas as pd
import csv
import datetime
def AddRespectiveCellIdToOBMExposureFiles(exposureFile_path,OSMSource_path,Result_path):
startTime = datetime.datetime.now()
print(startTime)
OSMSource = csv.reader(open(OSMSource_path),delimiter=';')
next(OSMSource, None)
exposureSource=pd.read_csv(exposureFile_path)
originids=exposureSource.origin_id
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)
title=['id','lon','lat','taxonomy','number','structural','night','occupancy','admin_name','admin_ID','origin_id','respectiveCellId']
append_list_as_row(Result_path, title)
OSMIdToCellId = {}
for MappingItem in OSMSource:
if MappingItem[0] in OSMIdToCellId:
# Ignore the additional polygon to keep everything unambiguous
pass
OSMIdToCellId[MappingItem[0]] = MappingItem[2]
for asset in range(exposureSource.shape[0]):
exposureArr= list(exposureSource.loc[asset])
origin_id=originids[asset]
respectiveCellId = OSMIdToCellId[origin_id]
exposureArr.append(respectiveCellId)
append_list_as_row(Result_path, exposureArr)
print('Execution time of the script',(datetime.datetime.now() - startTime))
#!/usr/bin/env python3
import numpy as np
import pandas as pd
import csv
import datetime
import losslib
def damageCalculator_TileVersion(fullGroundMotionField,Result_path,fragilityFileDir,exposures,\
taxonomy_conversion_path,shakemap_path,polygonSource_path,exposureType="cell",method="linear"):
"""
Returns a file "Result_path" including damage results of a scenario earthquake using the ""losslib" functions.
Input:
------
- fullGroundMotionField: (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 groundMotionField 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:
>>> fullGroundMotionField
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_path: (str)
Result file address.
Example extract:
>>> Result_path
"/home/TileCalculations/DamageResult.csv"
- fragilityFileDir: (str)
Address to the directory including all the fragility functions.
Example extract:
>>> fragilityFileDir
"/home/TileCalculations/Fragilities"
- exposures: (pandas.DataFrame(series))
informaion of the assets of the region of interest under hazard.
Example extract:
>>> exposures
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"
- shakemap_path: (str)
Address to the ground-motion values file.
Example extract:
>>> shakemap_path
"/home/TileCalculations/shakemap1381_2.csv"
- polygonSource_path: (str)
Address to the file including the origin-ids and thir respecive polygins (Could be whether each tile oan OSM building).
Example extract:
>>> polygonSource_path
"/home/TileCalculations/M008_exposure_Attica_GDE_visual_v001_sat_27f_by_cell_reOrder.csv"
- exposureType: (string), optional
{‘cell’, ‘OBM’}, optional'. cell by default.
- method: (string), optional
{‘linear’, ‘nearest’, ‘cubic’}, optional'. Linear by default.
Output:
-------
- Result_path: (arrays written to file)
file containing the damage elements for each asset of the exposure.
Example extract of the result file:
polygon,,origin_id,,asset_id,,lon,,lat,,taxonomy,,gmfValue,,PoEs,,PoOs,,tot_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
taxonomyToFragilitySource = csv.reader(open(taxonomy_conversion_path))
# Skip the header
next(taxonomyToFragilitySource, None)
PolygonSource = csv.reader(open(polygonSource_path),delimiter=';')
# Skip the header
next(PolygonSource, None)
# Read each column of the input "exposure"
taxonomies=exposures.taxonomy
tot_num_buildings_row=exposures.number
lons=exposures.lon
lats=exposures.lat
assetids=exposures.id
originids=exposures.origin_id
# 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)
# 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 taxonomy and the values as both the fragility function name (excluding ".csv" part) and column of ground-motion_type in ground-motion_field file.
taxonomyToFragilityMap = losslib.Taxonomy_to_Fragility(gmDict,\
taxonomyToFragilitySource,fragilityFileDir)
# Calling the function "OriginId_to_Polygon" to get a dictionary with keys as the origin_id and the value as the respective polygon.
OriginIdToPolygonMap = losslib.OriginId_to_Polygon(PolygonSource,exposureType)
# Define number of columns that contain the data in the fragiliy function files.
cls = range(1,101)
# Just a trick to have multiple commas between each result element, since we do not want a single comma as the delimiter due to having ploygons as a result element.
a=[0,2,4,6,8]
# Looping through each line of the exposure file to do the computations line by line.
for asset in range(exposures.shape[0]):
taxonomy=taxonomies.iloc[asset]
fragilityFileName = taxonomyToFragilityMap[taxonomy][0] + ".csv"
tot_num_buildings=tot_num_buildings_row.iloc[asset]
lon=lons.iloc[asset]
lat=lats.iloc[asset]
asset_id=assetids.iloc[asset]
origin_id=originids.iloc[asset]
# Since for the OBM exposure files we also need to know the cell-id that the building is located in, referred as "RespectiveCellid" and the polygon of the buiding.
if exposureType == 'OBM':
[polygon,RespectiveCellid] = OriginIdToPolygonMap[origin_id]
else:
polygon = OriginIdToPolygonMap[origin_id]
# Read fragility functions as numpy arrays.
fragility_function = np.loadtxt(fragilityFileDir+"/"+fragilityFileName\
, delimiter=",", usecols=cls)
# Achieve the ground-motion value from the fullGroundMotionField. Please note that the "fullGroundMotionField" contains many identical lines (since so many assets have same locations (same location leads to same ground-motion value)) because we want it to have same number of lines and same orders as the exposure file, so that in this line we read the same line number as the exposure file.
gm_value = fullGroundMotionField[asset,\
taxonomyToFragilityMap[taxonomy][1]]
# Achieve Probabilities of exceedance and occurance.
[PoEs, PoOs] = losslib.get_PoEs(fragility_function, gm_value)
# Compute damage 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_' + RespectiveCellid,'',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('time now',datetime.datetime.now)
print('Execution time of the script',(datetime.datetime.now() - startTime))
#!/usr/bin/env python3
import numpy as np
import pandas as pd
import csv
import losslib
from damageCalculator_TileVersion import damageCalculator_TileVersion
def getExposuresPerTile(
Result_path, exposures_path, fragilityFileDir, taxonomy_conversion_path,
shakemap_path, polygonSource_path, cellIdSource_path, exposureType,method):
"""
Returns exposure file as assets in each tile and also passes all the inputs to the damage calculator.
Input:
------
- Result_path: (str)
Result file address.
Example extract:
>>> Result_path
"/home/TileCalculations/DamageResult.csv"
- exposures_path: (str)
Address to the directory including all the fragility functions.
Example extract:
>>> exposures_path
"/home/TileCalculations/M008_exposure_Attica_GDE_OBMpart_v001_sat_27f_Ind.csv"
- fragilityFileDir: (str)
Address to the directory including all the fragility functions.
Example extract:
>>> fragilityFileDir
"/home/TileCalculations/Fragilities"
- 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"
- shakemap_path: (str)
Address to the ground-motion values file.
Example extract:
>>> shakemap_path
"/home/TileCalculations/shakemap1381_2.csv"
- cellIdSource_path: (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_path
"/home/TileCalculations/exposure_uniq_cellids.csv"
- polygonSource_path: (str)
Address to the file including the origin-ids and thir respecive polygins (Could be whether each tile oan OSM building).
Example extract:
>>> polygonSource_path
"/home/TileCalculations/M008_exposure_Attica_GDE_visual_v001_sat_27f_by_cell_reOrder.csv"
- exposureType: (string), optional
{‘cell’, ‘OBM’}, optional'. cell by default.
- method: (string), optional
{‘linear’, ‘nearest’, ‘cubic’}, optional'. Linear by default.
Output:
-------
- fullGroundMotionField: (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 groundMotionField 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:
>>> fullGroundMotionField
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_path: (str)
Result file address.
Example extract:
>>> Result_path
"/home/TileCalculations/DamageResult.csv"
- fragilityFileDir: (str)
Address to the directory including all the fragility functions.
Example extract:
>>> fragilityFileDir
"/home/TileCalculations/Fragilities"
- exposuresPerTile: (pandas.DataFrame(series))
informaion of the assets of each tile of the region of interest.
Example extract:
>>> exposures
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"
- shakemap_path: (str)
Address to the ground-motion values file.
Example extract:
>>> shakemap_path
"/home/TileCalculations/shakemap1381_2.csv"
- polygonSource_path: (str)
Address to the file including the origin-ids and thir respecive polygins (Could be whether each tile oan OSM building).
Example extract:
>>> polygonSource_path
"/home/TileCalculations/M008_exposure_Attica_GDE_visual_v001_sat_27f_by_cell_reOrder.csv"
- exposureType: (string), optional
{‘cell’, ‘OBM’}, optional'. cell by default.
- method: (string), optional
{‘linear’, ‘nearest’, ‘cubic’}, optional'. Linear by default.
"""
exposures=pd.read_csv(exposures_path)
lons_wholeArea=exposures.lon
lats_wholeArea=exposures.lat
groundMotionField = np.loadtxt(shakemap_path,delimiter=',',skiprows=1)
fullGroundMotionField = losslib.Get_Full_GMF(groundMotionField,lons_wholeArea,lats_wholeArea,method)
allCellIds=np.loadtxt(cellIdSource_path,dtype='str',skiprows=1)
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 = ['polygon', '', 'origin_id', '', 'RespectiveCellid', '', 'asset_id', \
'', 'lon', '', 'lat', '', 'taxonomy', '', 'gmfValue', '', 'PoEs', \
'', 'PoOs', '', 'tot_num_buildings', '', 'structural_No-damage', '', \
'structural_Slight', '', 'structural_Moderate', '', 'structural_Extensive'\
, '', 'structural_Complete']
tileId = 'respectiveCellId'
else:
title = ['polygon', '', 'origin_id', '', 'asset_id', '', 'lon', '',
'lat', '', 'taxonomy', '', 'gmfValue', '', 'PoEs', '', 'PoOs', '',
'tot_num_buildings', '', 'structural_No-damage', '',
'structural_Slight', '', 'structural_Moderate', '',
'structural_Extensive', '', 'structural_Complete']
tileId = 'origin_id'
append_list_as_row(Result_path, title)
for i in range(allCellIds.shape[0]):
cellId = allCellIds[i]
exposuresPerTile = exposures.loc[exposures[tileId] == cellId]
damageCalculator_TileVersion(
fullGroundMotionField, Result_path, fragilityFileDir,
exposuresPerTile, taxonomy_conversion_path, shakemap_path,
polygonSource_path, exposureType, method)
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