Commit 90827ce0 authored by Tara Evaz Zadeh's avatar Tara Evaz Zadeh
Browse files

Merge branch 'feature/argparsing' into 'master'

Uploaded the wrapper_cell_by_cell.py to check the argparsing section

See merge request !7
parents b60e0060 833c13ac
#!/usr/bin/env python3
import numpy as np
import pandas as pd
import csv
import losslib
import os
import argparse
from damagecalculator_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",
):
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(
description="This program computes the probabilities of occurence of different damage states for a scenario earthquake, given a ground-motion field for the area, an exposure model representing the assets in the region of interest and fragility functions that show the probability of exceeding a set of damage states, given an intensity measure level."
)
parser.add_argument(
"-m",
"--interpolation-method",
required=False,
type=str,
default="linear",
choices=["linear", "cubic", "nearest"],
help="method used to interpolate the input ground-motion values over all the exposure locations (asset locations). Options: [‘linear’, ‘nearest’, ‘cubic’] (default set to linear)",
)
parser.add_argument(
"-x",
"--exposure-type",
required=False,
type=str,
default="cell",
choices=["cell", "building"],
help="exposure type. 'cell' if your assets belong to a tile and 'building' if your assets are buildings. 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. This file should include all the intensity measure types that you have in your fragility functions (Required)",
)
parser.add_argument(
"-c",
"--cellIdSource-filepath",
required=True,
type=str,
help="path to the file that includes all the unique cell-ids you have in your exposure file (same as the origin-ids of the cell parts of the exposure files or simpy cell-ids) (Required)",
)
parser.add_argument(
"-e",
"--exposure-filepath",
required=True,
type=str,
help="path to the file that includes the exposure assets (Required)",
)
parser.add_argument(
"-p",
"--polygonSource-filepath",
required=True,
type=str,
help="path to the file that includes the origin-ids of either your tiles (in case exposure-type = 'cell'. As an example of the origin-id: cell_92258412) or the buildings (in case exposure-type = 'building'. As an example of the origin-id: OSM_529613252) and their respective polygons. (Required)",
)
parser.add_argument(
"-r",
"--result-filepath",
required=False,
type=str,
default="damage_result.gpkg",
help="path to the file that we want to write the results to (default set to damage_result.gpkg)",
)
parser.add_argument(
"-o",
"--overwrite-result-file",
required=False,
action="store_true",
help="to overwrite the existing result file if this option is mentioned in the command line",
)
args = parser.parse_args()
# read arguments from command line
interpolation_method = args.interpolation_method
exposure_type = args.exposure_type
fragility_pathname = args.fragility_pathname
taxonomy_conversion_filepath = args.taxonomy_conversion_filepath
shakemap_filepath = args.shakemap_filepath
cellIdSource_filepath = args.cellIdSource_filepath
exposure_filepath = args.exposure_filepath
polygonSource_filepath = args.polygonSource_filepath
result_filepath = args.result_filepath
overwrite_result_file = args.overwrite_result_file
if os.path.exists(result_filepath):
if overwrite_result_file == False:
raise ValueError(
"result_filepath exists. Choose another name or use --overwrite_result_file set to True if you want to overwrite the results."
)
else:
os.remove(result_filepath)
get_exposure_per_tile(
exposure_filepath,
fragility_pathname,
taxonomy_conversion_filepath,
shakemap_filepath,
polygonSource_filepath,
cellIdSource_filepath,
exposure_type,
interpolation_method,
result_filepath,
)
This diff is collapsed.
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