Commit 9dd8653e authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Created GDE_TOOLS_aggregated_models.py module

This module will contain functions used to generate aggregated exposure
models.
Function write_admin_unit_to_hdf() created.
parent 4e0ad9e0
"""
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
GDE_TOOLS_aggregated_models
===========================
"""
import os
import numpy as np
import h5py
def write_admin_unit_to_hdf(adm_unit_id, adm_level,
bdg_classes, bdg_proportions,
num_bdgs, num_dwells, num_ppl,
out_filename, out_pathname):
"""write_admin_unit_to_hdf
This function writes an administrative unit to the administrative units HDF5 file.
Args:
adm_unit_id (str): ID of the administrative unit, with format "XX_YYYY", where "XX" is
the ISO 3166-1 alpha 2 country code, and "YYYY" is the ID of the
administrative unit as defined according to a relevant standard.
E.g. “GR_3514604” in Greece.
adm_level (int): administrative level of adm_unit_id according to the relevant standard.
bdg_classes (list of str): all building classes associated with this admin_unit_id.
Length equal to bdg_proportions.
bdg_proportions (array of floats): proportions in which each building class of
bdg_classes is present in adm_unit_id. Its elements
must add up to 1.0. Length equal to bdg_classes.
num_bdgs (float): total number of buildings in adm_unit_id.
num_dwells (float): total number of dwellings in adm_unit_id.
num_ppl (float): total number of people in adm_unit_id.
out_filename (str): name of the output HDF5 file, including extension. Example:
"Europe_admin_units_Res.hdf5".
out_pathname (str): path where the HDF5 file is or should be placed.
"""
# Check length of bdg_classes and bdg_proportions is the same:
if len(bdg_classes)!=bdg_proportions.shape[0]:
print("ERROR!! Lengths of bdg_classes and bdg_proportions do not match!!")
return
# Check the elements of bdg_proportions add up to 1:
if (bdg_proportions.sum()>1.001 or bdg_proportions.sum()<0.999):
print("ERROR!! Elements of bdg_proportions do not sum up to 1.0!!")
return
# Check that bdg_proportions does not contain negative numbers:
if np.any(bdg_proportions<0.0):
print("ERROR!! Some elements of bdg_proportions are negative!!")
return
# Create out_pathname if it does not exist:
if not os.path.exists(out_pathname):
os.makedirs(out_pathname)
# Open in write mode HDF5 file (create if it does not exist):
fle = h5py.File(os.path.join(out_pathname, out_filename), "a")
# Create group for this administrative unit if it does not exist:
if adm_unit_id not in fle:
gr= fle.create_group(adm_unit_id)
# Write attributes of this administrative unit:
fle[adm_unit_id].attrs["Adm_Level"] = adm_level
fle[adm_unit_id].attrs["Num_Bdgs"] = num_bdgs
fle[adm_unit_id].attrs["Num_Dwells"] = num_dwells
fle[adm_unit_id].attrs["Num_Ppl"] = num_ppl
# Write dataset of building classes. It will replace it if it already exists:
if "Bdg_classes" in fle[adm_unit_id]:
del fle[adm_unit_id]["Bdg_classes"]
dt=h5py.special_dtype(vlen=str)
dataset_classes = fle[adm_unit_id].create_dataset("Bdg_classes",
bdg_proportions.shape,
dtype=dt)
dataset_classes[:] = np.array(bdg_classes)
# Write dataset of proportions of buildings. It will replace it if it already
# exists:
if "Vals" in fle[adm_unit_id]:
del fle[adm_unit_id]["Vals"]
dataset_props = fle[adm_unit_id].create_dataset("Vals",
bdg_proportions.shape,
dtype="float64")
dataset_props[:] = bdg_proportions
# Close HDF5 file (important!):
fle.close()
\ No newline at end of file
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