Commit 2475017b authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added script to run functions that deal with 30-arcsec ind exposure

parent a0cdefac
"""
Copyright (C) 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/>.
Global Dynamic Exposure Model
Helmholtz Centre Potsdam
GFZ German Research Centre for Geosciences
Section 2.6: Seismic Hazard and Risk Dynamics
SERA_creating_industrial_cells
==============================
This code is used to handle the fact that industrial exposure may be defined in 30-arcsec cells
instead of administrative units in the SERA exposure model. The SERA model only provides the
centroids of those cells with a certain decimal precision. This code makes use of the tools
defined in GDE_TOOLS_create_industrial_cells.py, which create cells around the points given as
input and export these cells to a geodata file. The country/ies to process is defined in the
configuration file. The cells' North-South and East-West widths are given as input too.
"""
import sys
import os
import GDE_TOOLS_create_industrial_cells as gdet_ind
import GDE_TOOLS_general as gdet_gral
import GDE_TOOLS_read_config_file as gdet_conf
import datetime
def run(config_dict):
####################################################
# READ CONFIGURATION PARAMETERS
####################################################
print("Processing configuration parameters...")
# Path for output:
out_path = config_dict["File Paths"]["out_path"]
# SERA models path:
sera_models_path = config_dict["File Paths"]["sera_models_path"]
# SERA boundaries shapefiles path:
sera_shp_path = config_dict["File Paths"]["sera_boundaries_path"]
bound_type = config_dict["File Paths"]["boundaries_type"]
# Countries to process:
countries = config_dict["SERA_creating_industrial_cells"]["countries"].split(", ")
# Name of columns with longitude and latitude in the SERA model:
col_lon = config_dict["SERA_creating_industrial_cells"]["col_lon"]
col_lat = config_dict["SERA_creating_industrial_cells"]["col_lat"]
# Widths of the cells to be generated:
width_EW = float(config_dict["SERA_creating_industrial_cells"]["width_EW"])
width_NS = float(config_dict["SERA_creating_industrial_cells"]["width_NS"])
# Input is given in arcseconds, transform to degrees:
width_EW = width_EW / (60.0 * 60.0)
width_NS = width_NS / (60.0 * 60.0)
# First part of the string used to generate IDs of the individual points (e.g. "IND"):
id_str = config_dict["SERA_creating_industrial_cells"]["id_str"]
# Number of decimal places to be used to determine unique points in the aggregated model:
precision_points = int(config_dict["SERA_creating_industrial_cells"]["precision_points"])
# Run consistency checks (True) or not (False):
consistency_checks_str = config_dict["SERA_creating_industrial_cells"]["consistency_checks"]
if consistency_checks_str.lower() == "true":
consist_checks = True
elif consistency_checks_str.lower() == "false":
consist_checks = False
else:
print(
"ERROR!! IT SHOULD NOT GET TO THIS POINT, "
"AS THE PARAMETER IS CHECKED IN THE CONFIG FILE CHECKS"
)
# Autoadjust potential leftover overlaps and gaps (True) or not (False):
autoadjust_str = config_dict["SERA_creating_industrial_cells"]["autoadjust"]
if autoadjust_str.lower() == "true":
autoadjust = True
elif autoadjust_str.lower() == "false":
autoadjust = False
else:
print(
"ERROR!! IT SHOULD NOT GET TO THIS POINT, "
"AS THE PARAMETER IS CHECKED IN THE CONFIG FILE CHECKS"
)
# Print statemens of progress while running:
verbose_str = config_dict["SERA_creating_industrial_cells"]["verbose"]
if verbose_str.lower() == "true":
verbose = True
elif verbose_str.lower() == "false":
verbose = False
else:
print(
"ERROR!! IT SHOULD NOT GET TO THIS POINT, "
"AS THE PARAMETER IS CHECKED IN THE CONFIG FILE CHECKS"
)
# Tolerances for the consistency checks:
if consist_checks:
# Tolerance for maximum distance between the original points and the centroids of the
# generated cells:
tol_dist = float(config_dict["SERA_creating_industrial_cells"]["consistency_tol_dist"])
# Tolerance for variability of the area of the generated cells:
tol_area = float(config_dict["SERA_creating_industrial_cells"]["consistency_tol_area"])
else: # Dummy values, they will not be used
tol_dist = 0.05
tol_area = 0.05
# CRS of the SERA files:
in_crs = config_dict["SERA_creating_industrial_cells"]["in_crs"]
# File type to export the created cells to (e.g. "shp"=Shapefile, "gpkg"=Geopackage):
export_type = config_dict["SERA_creating_industrial_cells"]["export_type"]
####################################################
# START
####################################################
# Auxiliary dictionary definining the precision to use to convert coordinates into strings:
dec_precision_EW = int("{:E}".format(width_EW).split("-")[1])
dec_precision_NS = int("{:E}".format(width_NS).split("-")[1])
precision_cells = {}
precision_cells["lon_w"] = "{:.%sf}" % (dec_precision_EW)
precision_cells["lat_s"] = "{:.%sf}" % (dec_precision_NS)
precision_cells["lon_e"] = "{:.%sf}" % (dec_precision_EW)
precision_cells["lat_n"] = "{:.%sf}" % (dec_precision_NS)
# Run country by country
if consist_checks:
log = ["country,overlap_found,gap_found,big_distance_found,big_area_variability_found"]
processing_times = ["country,processing_time"]
for k, country_name in enumerate(countries):
start_time = datetime.datetime.now()
print("\n")
print(
"Working on %s, country %s of %s\n"
% (country_name, str(k + 1), str(len(countries)))
)
# Generate cells for this country:
country_iso2 = gdet_gral.get_ISO2_code_for_country(country_name)
id_str_country = "%s_%s" % (country_iso2, id_str)
results = gdet_ind.generate_country_industrial_cells(
country_name,
col_lon,
col_lat,
width_EW,
width_NS,
id_str_country,
precision_points,
precision_cells,
sera_models_path,
sera_shp_path,
boundaries_type=bound_type,
consistency_checks=consist_checks,
autoadjust_overlap_gap=autoadjust,
in_crs=in_crs,
consistency_tol_dist=tol_dist,
consistency_tol_area=tol_area,
verbose=verbose,
)
(
cells,
aggr_model,
overlap_found,
gap_found,
big_dist_diff,
big_area_diff,
country_id,
) = results
# Print to screen and a log file the results of the consistency check if requested:
if consist_checks:
print("\n")
print(" Results for consistency checks:")
if overlap_found == "True":
overlap_str = "Problem found with cells overlapping. ERROR."
else:
overlap_str = "Cells do not overlap. OK."
if gap_found == "True":
gap_str = "Problem found with gaps in between cells. ERROR."
else:
gap_str = "Cells do not have gaps in between. OK."
if big_dist_diff == "True":
big_dist_str = "Distances between centroids and points too large. ERROR."
else:
big_dist_str = "Distances between centroids and points are OK."
if big_area_diff == "True":
big_area_str = "Variability of resulting cell areas too large. ERROR."
else:
big_area_str = "Variability of resulting cell areas is OK."
print(" %s" % (overlap_str))
print(" %s" % (gap_str))
print(" %s" % (big_dist_str))
print(" %s" % (big_area_str))
out_str = [country_name, overlap_found, gap_found, big_dist_diff, big_area_diff]
log.append(",".join(out_str))
# Export SERA aggregated exposure model with additional columns referring to cells:
if verbose:
print("\n")
print(" Updating aggregated exposure file...")
aggr_filename = "Exposure_Ind_%s.csv" % (country_name)
gdet_ind.export_modified_aggregated_model_file(
aggr_model, os.path.join(out_path, "Ind_Cells"), aggr_filename, separ=","
)
# Export geodata file of cells:
if verbose:
print("\n")
print(" Exporting cells geometry to file...")
cells_filename = "Adm99_%s.%s" % (country_name, export_type)
gdet_ind.export_cells_to_geodatafile(
country_name, country_id, cells, os.path.join(out_path, "Ind_Cells"), cells_filename
)
# Time it took to process this country:
end_time = datetime.datetime.now()
duration = (end_time - start_time).total_seconds()
processing_times.append("%s,%s" % (country_name, str(duration)))
# Write output log of consistency checks:
if consist_checks:
gdet_gral.write_log_file(
log, os.path.join(out_path, "Ind_Cells", "log_consistency_checks.csv")
)
# Write output log of processing times:
gdet_gral.write_log_file(
processing_times, os.path.join(out_path, "Ind_Cells", "log_processing_times.csv")
)
print("\n")
print("Done!")
if __name__ == "__main__":
# This code needs to be run from the command line as python3 namefile.py configfile.ini.
# sys.argv retrieves all the commands entered in the command line; position [0] is this
# code, position [1] will be the config file name.
config_filename = sys.argv[1]
section_names_to_validate = ["File Paths", os.path.basename(__file__).split(".")[0]]
config_dict = gdet_conf.read_config_parameters(
os.path.join(os.getcwd(), config_filename), section_names_to_validate
)
run(config_dict)
Supports Markdown
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