damage_calculator.py 8.52 KB
Newer Older
1
2
#!/usr/bin/env python3

3
4
# Copyright (c) 2020-2021:
#   Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# 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/.


20
21
22
23
24
import numpy as np
import pandas as pd
import losslib
import os
import argparse
25
from damage_calculator_tile_version import damageCalculator_TileVersion
26
import csv
27
28
29
30
31
32


def get_exposure_per_tile(
    exposure_filepath,
    fragility_pathname,
    taxonomy_conversion_filepath,
33
    ground_motion_filepath,
34
    geometry_source_filepath,
35
    cell_id_source_filepath,
36
37
    exposure_type="cell",
    interpolation_method="linear",
38
    result_filepath="damage_result.csv",
39
40
41
42
43
):

    exposure = pd.read_csv(exposure_filepath)
    lons_whole_area = exposure.lon
    lats_whole_area = exposure.lat
44
45
    ground_motion_field = np.loadtxt(ground_motion_filepath, delimiter=",", skiprows=1)
    full_ground_motion_field = losslib.get_full_GMF(
46
47
        ground_motion_field, lons_whole_area, lats_whole_area, interpolation_method
    )
48
    cell_ids = pd.read_csv(cell_id_source_filepath)
49
50
51
52
53
54
55
56
    with open(result_filepath, "a+", newline="") as write_obj:
        csv_writer = csv.writer(write_obj)
        if exposure_type == "building":
            title = [
                "geometry",
                "",
                "origin_id",
                "",
57
                "respective_cell_id",
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
                "",
                "asset_id",
                "",
                "lon",
                "",
                "lat",
                "",
                "taxonomy",
                "",
                "gm_value",
                "",
                "PoEs",
                "",
                "PoOs",
                "",
                "num_buildings",
                "",
                "structural_no_damage",
                "",
                "structural_slight",
                "",
                "structural_moderate",
                "",
                "structural_extensive",
                "",
                "structural_complete",
            ]
85
            tile_id = "respective_cell_id"
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        else:
            title = [
                "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",
            ]
            tile_id = "origin_id"
119
        csv_writer.writerow(title)
120

121
    for i in range(cell_ids.shape[0]):
122
        cell_id = cell_ids.loc[i][0]
123
124
125
126
127
128
129
130
131

        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,
132
            ground_motion_filepath,
133
            geometry_source_filepath,
134
135
136
137
138
139
140
141
            exposure_type,
            interpolation_method,
        )


if __name__ == "__main__":

    parser = argparse.ArgumentParser(
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
142
143
144
145
146
        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."
147
148
    )
    parser.add_argument(
149
        "-i",
150
151
152
153
154
        "--interpolation-method",
        required=False,
        type=str,
        default="linear",
        choices=["linear", "cubic", "nearest"],
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
155
156
157
        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)",
158
159
160
161
162
163
164
165
    )
    parser.add_argument(
        "-x",
        "--exposure-type",
        required=False,
        type=str,
        default="cell",
        choices=["cell", "building"],
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
166
167
        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)",
168
169
170
    )
    parser.add_argument(
        "-f",
171
        "--fragilities",
172
173
        required=True,
        type=str,
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
174
175
        help="path to the directory that includes all the fragility csv file functions "
        + "(Required)",
176
177
178
    )
    parser.add_argument(
        "-t",
179
        "--taxonomy-map",
180
181
        required=True,
        type=str,
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
182
183
        help="path to the file that includes the taxonomy names and their respective "
        + "fragility function names (Required)",
184
185
186
    )
    parser.add_argument(
        "-g",
187
        "--ground-motion-field",
188
189
        required=True,
        type=str,
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
190
191
        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)",
192
193
194
    )
    parser.add_argument(
        "-c",
195
        "--cell-ids",
196
197
        required=True,
        type=str,
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
198
199
200
        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)",
201
202
203
    )
    parser.add_argument(
        "-e",
204
        "--exposure",
205
206
207
208
209
210
        required=True,
        type=str,
        help="path to the file that includes the exposure assets (Required)",
    )
    parser.add_argument(
        "-p",
211
        "--geometry",
212
213
        required=True,
        type=str,
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
214
215
216
217
218
        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)",
219
220
221
    )
    parser.add_argument(
        "-r",
222
        "--results",
223
224
        required=False,
        type=str,
225
        default="damage_result.csv",
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
226
        help="path to the file that we want to write the results to (default set to"
227
        + "damage_result.csv) ",
228
229
230
    )
    parser.add_argument(
        "-o",
231
        "--overwrite",
232
233
        required=False,
        action="store_true",
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
234
235
        help="to overwrite the existing result file if this option is mentioned in "
        + "the command line",
236
237
238
239
240
241
    )
    args = parser.parse_args()

    # read arguments from command line
    interpolation_method = args.interpolation_method
    exposure_type = args.exposure_type
242
243
    fragility_pathname = args.fragilities
    taxonomy_conversion_filepath = args.taxonomy_map
244
245
    ground_motion_filepath = args.ground_motion_field
    cell_id_source_filepath = args.cell_ids
246
    exposure_filepath = args.exposure
247
    geometry_source_filepath = args.geometry
248
249
    result_filepath = args.results
    overwrite_result_file = args.overwrite
250
251

    if os.path.exists(result_filepath):
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
252
        if not overwrite_result_file:
253
            raise ValueError(
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
254
                "result_filepath exists. Choose another name or use "
255
                + "--overwrite set to True if you want to "
Tara Evaz Zadeh's avatar
Tara Evaz Zadeh committed
256
                + "overwrite the results."
257
258
259
260
261
262
263
264
            )
        else:
            os.remove(result_filepath)

    get_exposure_per_tile(
        exposure_filepath,
        fragility_pathname,
        taxonomy_conversion_filepath,
265
        ground_motion_filepath,
266
        geometry_source_filepath,
267
        cell_id_source_filepath,
268
269
270
271
        exposure_type,
        interpolation_method,
        result_filepath,
    )