Commit a0cdefac authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added function which_countries_on_a_grid to GDE_TOOLS_create_ind...

parent 1ebb7357
......@@ -1632,3 +1632,81 @@ def export_cells_to_geodatafile(
# Write to ShapeFile or other format (as per out_driver):
shp_gdf.to_file(os.path.join(out_pathname, out_filename), driver=out_driver)
def which_countries_on_a_grid(
metadata_filepath,
sheet_name="IND",
col_name="Variables",
var_name="Resolution",
target="30 arc seconds",
export=False,
out_pathname="",
out_filename="",
):
"""This function retrieves the list of countries for which the industrial exposure is
defined on a 30-arcsec grid in the SERA exposure model. This information is provided in the
EFEHR repository within an Excel spreadsheet called
"European_Exposure_Model_Data_Assumptions.xlsx". The default values of all input parameters
refer to the structure of this file.
Args:
metadata_filepath (str): Full path to the source metadata file, including file name and
extension. It needs to be an Excel spreadsheet (.xls or .xlsx).
sheet_name (str): Name of the sheet of metadata_filepath to read. Default: "IND".
col_name (str): Name of the column of sheet_name that contains the parameter names.
Default: "Variables".
var_name (str): Name of the parameter to read. Default: "Resolution".
target (str): Target value of var_name (i.e. the code will seek for cases in which the
value of var_name is target. Default: "30 arc seconds".
export (bool): If True, the resulting list of countries will be exported to a CSV file
under the path out_pathname and the file name out_filename.
Default: False.
out_pathname (str): Path where to export the output CSV to. Only needed if export is
True. Default: "".
out_filename (str): Name of the file where to write the list of countries. Only needed
if export is True. Default: "".
Returns:
countries (list of str): List of countries for which the var_name takes the value given
by target (the countries for which the industrial exposure is
defined on a 30-arcsec grid in the SERA exposure model.
"""
# Check metadata_filepath exists and points at an Excel spreadsheet:
if not os.path.isfile(metadata_filepath):
print("ERROR in which_countries_on_a_grid: input file not found")
return []
if "xls" not in metadata_filepath.split(".")[-1]:
print("ERROR in which_countries_on_a_grid: input file is not an Excel spreadsheet")
return []
# Read the SERA metadata file:
metadata = pd.read_excel(metadata_filepath, sheet_name=sheet_name)
# Identify the row in which the variable var_name is given:
which_row = np.where(metadata[col_name].values == var_name)[0]
if len(which_row) != 1: # This should not occur
print("ERROR READING %s: ROW NOT FOUND." % (metadata_filepath))
return []
# Retrieve the row in which the variable var_name is given:
whole_row = metadata.iloc[which_row[0], :]
# Columns in which var_name takes the value target:
which_cols = np.where(whole_row == target)[0]
# The names of those columns are the countries for which var_name takes the value target:
countries = list(whole_row.index[which_cols])
# Write a csv file with the list of countries:
if export:
# Create out_pathname if it does not exist:
if not os.path.exists(out_pathname):
os.makedirs(out_pathname)
# Write to file:
out_filepath = os.path.join(out_pathname, out_filename)
f = open(out_filepath, "w")
f.write(",".join(countries))
f.close()
return countries
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