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

Merge branch 'feature/ind06' into 'master'

Completed module and tools that create industrial cells around input points

See merge request !16
parents 80be8100 2b4eea99
......@@ -1222,6 +1222,7 @@ def generate_country_industrial_cells(
in_crs="EPSG:4326",
consistency_tol_dist=0.05,
consistency_tol_area=0.05,
verbose=False,
):
"""This function reads the input aggregated exposure model of the country (location defined
by aggr_model_pathname and country), identifies the unique points present in this input
......@@ -1316,6 +1317,8 @@ def generate_country_industrial_cells(
range, which is calculated as ([max-min]/mean) of all
cells. Default: 0.05. Only needed if consistency_checks is
True.
verbose (bool): if True, print statements will be executed to describe progress in the
calculations. Default = False.
Returns:
cells_adj_bound_gdf (GeoDataFrame): GeoPandas GeoDataFrame with the cells defined around
......@@ -1394,6 +1397,8 @@ def generate_country_industrial_cells(
# Retrieve unique points in the exposure file, determined with a specific precision
# (points_gdf is a GeoPandas GeoDataFrame, ids_aggr is an array of strings with length
# equal to the number of rows of aggr_mod_df):
if verbose:
print(" Building cells from input points...")
points_gdf, ids_aggr = retrieve_unique_points(
aggr_mod_df, col_lon, col_lat, id_str, precision=precision_points, in_crs=in_crs
)
......@@ -1423,6 +1428,8 @@ def generate_country_industrial_cells(
# Adjust all the coordinates of the corners of the cells (coords_dict) by taking the
# average value of all instances of that coordinate that "should be the same", as identified
# in coords_uq:
if verbose:
print(" Adjusting cells' geometries...")
coords_dict_adj = adjust_coords(coords_dict, coords_uq) # coords_dict_adj is a dictionary
# Generate final output with adjusted cell geometries (cells_adj_gdf is a
......@@ -1436,7 +1443,11 @@ def generate_country_industrial_cells(
big_dist_diff = "Not_Checked"
big_area_diff = "Not_Checked"
else:
if verbose:
print(" Running consistency checks...")
# Consistency check 1: the output geometries should not overlap
if verbose:
print(" Consistency check #1 of 4...")
num_overlaps = 999 # Initialise variable for the while loop to run at least once
while num_overlaps > 0:
intsect_gdf = overlap_by_full_geom_intersection(cells_adj_gdf, "id_1", "id_2")
......@@ -1461,6 +1472,8 @@ def generate_country_industrial_cells(
overlap_found = "False"
# Consistency check 2:
if verbose:
print(" Consistency check #2 of 4...")
gaps_found = True # Initialise variable for the while loop to run at least once
while gaps_found:
# Expand the cells by 25% of their dimensions in all directions:
......@@ -1499,6 +1512,8 @@ def generate_country_industrial_cells(
gap_found = str(gaps_found)
# Consistency check 3: maximum distance between original points and final centroids:
if verbose:
print(" Consistency check #3 of 4...")
max_dist_centr = get_distance_centroids(cells_adj_gdf, col_lon, col_lat)
# Compare the maximum distance against the tolerance:
if max_dist_centr > min(width_EW, width_NS) * consistency_tol_dist:
......@@ -1507,6 +1522,8 @@ def generate_country_industrial_cells(
big_dist_diff = "False"
# Consistency check 4: stability/variability of area of resulting cells:
if verbose:
print(" Consistency check #4 of 4...")
rel_area_range = get_relative_area_range(cells_gdf)
# Compare the relative area range ([max-min]/mean) against the tolerance:
if rel_area_range > consistency_tol_area:
......@@ -1515,6 +1532,8 @@ def generate_country_industrial_cells(
big_area_diff = "False"
# Intersect cells with admnistrative boundary of country:
if verbose:
print(" Intersecting cells with admnistrative boundary of country...")
cells_adj_bound_gdf = gpd.overlay(cells_adj_gdf, bounds_gdf, how="intersection")
# Eliminate columns that are not useful:
if "ID_0" in cells_adj_bound_gdf.columns:
......@@ -1536,6 +1555,9 @@ def generate_country_industrial_cells(
cells_adj_bound_gdf["lat_s"].values[row] = geometry_row.bounds[1]
cells_adj_bound_gdf["lat_n"].values[row] = geometry_row.bounds[3]
if verbose:
print(" generate_country_industrial_cells has finished generating the cells")
return (
cells_adj_bound_gdf,
aggr_mod_df,
......@@ -1545,3 +1567,147 @@ def generate_country_industrial_cells(
big_area_diff,
country_id,
)
def export_modified_aggregated_model_file(aggr_mod_df, out_pathname, out_filename, separ=","):
"""This function writes the aggr_mod_df DataFrame to a CSV file with name out_filename,
under the path out_pathname. If the file already exists, it will overwrite it. If the path
does not exist, it will create it.
Args:
aggr_mod_df (DataFrame): Pandas DataFrame to be exported.
out_pathname (str): Path to where the output CSV will be written.
out_filename (str): File name of the output CSV file.
separ: Separator to use in the CSV file (default=',').
"""
# Create out_pathname if it does not exist:
if not os.path.exists(out_pathname):
os.makedirs(out_pathname)
aggr_mod_df.to_csv(os.path.join(out_pathname, out_filename), sep=separ)
def export_cells_to_geodatafile(
country_name, country_id, cells_gdf, out_pathname, out_filename, out_driver="ESRI Shapefile"
):
"""This function creates a geospatial data file of format given by out_driver, with the IDs
and geometries of cells_gdf. Additional fields are written with the country name and ID. If
the file already exists, it will overwrite it. If the path does not exist, it will create
it.
Args:
country_name (str): Name of country (as in the files of the aggregated model).
country_id (int): ID of the country as per the geospatial data files of the input
aggregated model.
cells_gdf (GeoDataFrame): GeoPandas GeoDataFrame with the cells geometry. This function
assumes that it contains at least the following columns:
- id: ID of the cell, given by id_str and an incremental
integer.
- geometry: (Shapely) polygons of the output cells.
out_pathname (str): Path to where the geospatial data file will be written.
out_filename (str): File name of the geospatial data file.
out_driver (str): "ESRI Shapefile", "GeoJSON", "GPKG", etc. Format of the output
geospatial data file.
"""
# Check that in_gdf has the needed columns and terminate otherwise:
if ("id" not in cells_gdf.columns) or ("geometry" not in cells_gdf.columns):
print("ERROR!! One or more of id, geometry missing as columns of cells_gdf")
return
# Create out_pathname if it does not exist:
if not os.path.exists(out_pathname):
os.makedirs(out_pathname)
# Start a new GeoDataFrame with the desired columns/features of cells_gdf:
shp_gdf = gpd.GeoDataFrame({}, geometry=cells_gdf["geometry"])
shp_gdf["ID_99"] = deepcopy(cells_gdf["id"])
shp_gdf["NAME_99"] = deepcopy(cells_gdf["id"])
# Add values of admin unit level 0:
id_0 = np.array([country_id for i in range(shp_gdf.shape[0])])
name_0 = np.array([country_name for i in range(shp_gdf.shape[0])])
shp_gdf["ID_0"] = id_0
shp_gdf["NAME_0"] = name_0
# 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
......@@ -31,317 +31,536 @@ out (i.e. not every float is tested to check whether it is a float or not).
"""
import os
import configparser # Documentation: https://docs.python.org/3/library/configparser.html
import configparser # Documentation: https://docs.python.org/3/library/configparser.html
def read_config_parameters(config_file_path, section_names_needed):
config = configparser.ConfigParser()
config.read(config_file_path)
out_config= {}
for section_name in section_names_needed:
if section_name not in config:
print('ERROR!! '+section_name+' NOT FOUND IN CONFIGURATION FILE. THE CODE CANNOT RUN!!')
else:
check_parameters(config, section_name)
out_config[section_name]= config[section_name]
return out_config
config = configparser.ConfigParser()
config.read(config_file_path)
out_config = {}
for section_name in section_names_needed:
if section_name not in config:
print(
"ERROR!! "
+ section_name
+ " NOT FOUND IN CONFIGURATION FILE. THE CODE CANNOT RUN!!"
)
else:
check_parameters(config, section_name)
out_config[section_name] = config[section_name]
return out_config
def check_parameters(config, section_name):
"""
section_name: name of the section in the configuration file (name that goes in [])
"""
if section_name=='File Paths':
kinds_of_paths= ['out_path', 'sera_models_path', 'sera_models_OQ_path', 'sera_boundaries_path', 'ghs_path', 'gpw_path', 'sat_path', 'completeness_path']
for path_kind in kinds_of_paths:
if not config.has_option('File Paths', path_kind):
raise IOError('ERROR!! '+path_kind+' PARAMETER MISSING FROM CONFIG FILE!!')
else:
if not os.path.isdir(config['File Paths'][path_kind]):
raise IOError('ERROR!! '+path_kind+': THE PATH SPECIFIED IN THE CONFIG FILE DOES NOT EXIST')
elif section_name=='Available Results':
if not config.has_option('Available Results', 'results_available_for'):
raise IOError('ERROR!! results_available_for PARAMETER MISSING FROM CONFIG FILE!!')
else:
num_results_cases= len(config['Available Results']['results_available_for'].split(', '))
if not config.has_option('Available Results', 'result_paths'):
raise IOError('ERROR!! result_paths PARAMETER MISSING FROM CONFIG FILE!!')
else:
num_result_paths= len(config['Available Results']['result_paths'].split(', '))
if num_results_cases!=num_result_paths:
raise IOError('ERROR!! results_available_for AND result_paths SHOULD HAVE THE SAME NUMBER OF ELEMENTS IN THE CONFIG FILE!!')
elif section_name=='OBM Database':
if not config.has_option('OBM Database', 'db_obm_name'):
raise IOError('ERROR!! db_obm_name PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('OBM Database', 'db_obm_username'):
raise IOError('ERROR!! db_obm_username PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('OBM Database', 'db_obm_schema_name'):
raise IOError('ERROR!! db_obm_schema_name PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('OBM Database', 'db_obm_table_name'):
raise IOError('ERROR!! db_obm_table_name PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='Tiles Database':
if not config.has_option('Tiles Database', 'db_tiles_name'):
raise IOError('ERROR!! db_tiles_name PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('Tiles Database', 'db_tiles_username'):
raise IOError('ERROR!! db_tiles_username PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('Tiles Database', 'db_tiles_schema_name'):
raise IOError('ERROR!! db_tiles_schema_name PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('Tiles Database', 'db_tiles_table_name'):
raise IOError('ERROR!! db_tiles_table_name PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='Admin Units Database':
if not config.has_option('Admin Units Database', 'db_admin_name'):
raise IOError('ERROR!! db_admin_name PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('Admin Units Database', 'db_admin_username'):
raise IOError('ERROR!! db_admin_username PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('Admin Units Database', 'db_admin_schema_name'):
raise IOError('ERROR!! db_admin_schema_name PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('Admin Units Database', 'db_admin_table_name'):
raise IOError('ERROR!! db_admin_table_name PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='Cells to Process':
check_of_parameters_to_define_list_of_cell_IDs(config, section_name) # Check parameters related to defining the way in which the list of cells is defined
elif section_name=='Ocuppancy String Groups':
num_occup_cases= check_of_occupancy_cases_parameter(config, section_name)
if config.has_option('Ocuppancy String Groups', 'occupancy_classifications'):
# It should have it, it is mandatory
num_classif_subgroups= len(config['Ocuppancy String Groups']['occupancy_classifications'].split(', '))
else:
raise IOError('ERROR!! OCCUPANCY CLASSIFICATIONS ARE NEEDED IN THE CONFIG FILE!!')
if (config.has_option('Ocuppancy String Groups', 'occupancy_cases') and config.has_option('Ocuppancy String Groups', 'occupancy_classifications')):
if num_occup_cases!=num_classif_subgroups:
raise IOError('ERROR!! LISTS OF OCCUPANCY STRINGS NEED TO BE PROVIDED IN occupancy_classifications FOR EACH CASE IN occupancy_cases!!')
elif section_name=='OBM_buildings_per_cell':
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
elif section_name=='GDE_gather_SERA_and_OBM':
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
_= check_of_occupancy_cases_parameter(config, section_name)
if not config.has_option('GDE_gather_SERA_and_OBM', 'version_of_SERA_metadata'):
raise IOError('ERROR!! version_of_SERA_metadata PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('GDE_gather_SERA_and_OBM', 'print_screen_during_run'):
raise IOError('ERROR!! print_screen_during_run PARAMETER MISSING FROM CONFIG FILE!!')
else:
check_if_param_is_True_or_False(config['GDE_gather_SERA_and_OBM']['print_screen_during_run'], 'print_screen_during_run')
elif section_name=='GDE_plot_maps':
_= check_of_occupancy_cases_parameter(config, section_name)
if not config.has_option('GDE_plot_maps', 'location_var'):
raise IOError('ERROR!! location_var PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('GDE_plot_maps', 'parameters'):
raise IOError('ERROR!! parameters (e.g., number, num_dwells, night, structural) PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='GDE_check_consistency':
if not config.has_option('GDE_check_consistency', 'location_var'):
raise IOError('ERROR!! location_var PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='OBM_assign_cell_ids_and_adm_ids_to_footprints':
if not config.has_option('OBM_assign_cell_ids_and_adm_ids_to_footprints', 'method_to_use'):
raise IOError('ERROR!! method_to_use PARAMETER MISSING FROM CONFIG FILE!!')
else:
if config['OBM_assign_cell_ids_and_adm_ids_to_footprints']['method_to_use']!='just_centroid':
raise IOError('ERROR!! method_to_use: THE ONLY CURRENTLY SUPPORTED VALUE IS just_centroid. PLEASE CHECK CONFIG FILE!!')
if not config.has_option('OBM_assign_cell_ids_and_adm_ids_to_footprints', 'obm_ids_to_process'):
raise IOError('ERROR!! obm_ids_to_process PARAMETER MISSING FROM CONFIG FILE!!')
else:
# To do: check that obm_ids_to_process is either "None" or a list of OBM IDs
pass
if not config.has_option('OBM_assign_cell_ids_and_adm_ids_to_footprints', 'assign_cell_IDs'):
raise IOError('ERROR!! assign_cell_IDs PARAMETER MISSING FROM CONFIG FILE!!')
else:
check_if_param_is_True_or_False(config['OBM_assign_cell_ids_and_adm_ids_to_footprints']['assign_cell_IDs'], 'assign_cell_IDs')
if not config.has_option('OBM_assign_cell_ids_and_adm_ids_to_footprints', 'assign_admin_IDs'):
raise IOError('ERROR!! assign_admin_IDs PARAMETER MISSING FROM CONFIG FILE!!')
else:
check_if_param_is_True_or_False(config['OBM_assign_cell_ids_and_adm_ids_to_footprints']['assign_admin_IDs'], 'assign_admin_IDs')
if not config.has_option('OBM_assign_cell_ids_and_adm_ids_to_footprints', 'occupancy_case'):
raise IOError('ERROR!! occupancy_case PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('OBM_assign_cell_ids_and_adm_ids_to_footprints', 'adm_level'):
raise IOError('ERROR!! adm_level PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='SERA_create_HDF5_metadata':
if not config.has_option('SERA_create_HDF5_metadata', 'version_of_SERA_metadata'):
raise IOError('ERROR!! version_of_SERA_metadata PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='SERA_create_OQ_input_files':
if not config.has_option('SERA_create_OQ_input_files', 'version_of_SERA_metadata'):
raise IOError('ERROR!! version_of_SERA_metadata PARAMETER MISSING FROM CONFIG FILE!!')
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
_= check_of_occupancy_cases_parameter(config, section_name)
if not config.has_option('SERA_create_OQ_input_files', 'countries'):
raise IOError('ERROR!! countries PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='SERA_create_outputs_QGIS_for_checking':
if not config.has_option('SERA_create_outputs_QGIS_for_checking', 'country'):
raise IOError('ERROR!! country PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='SERA_create_visual_output_of_grid_model_full_files':
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
_= check_of_occupancy_cases_parameter(config, section_name)
if not config.has_option('SERA_create_visual_output_of_grid_model_full_files', 'max_num_cells_per_stage'):
raise IOError('ERROR!! max_num_cells_per_stage PARAMETER MISSING FROM CONFIG FILE!!')
else:
if not config[section_name]['max_num_cells_per_stage'].isnumeric():
raise IOError('ERROR!! max_num_cells_per_stage NEEDS TO BE A POSITIVE INTEGER!!')
elif section_name=='SERA_distributing_exposure_to_cells':
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
if not config.has_option('SERA_distributing_exposure_to_cells', 'countries'):
raise IOError('ERROR!! countries PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('SERA_distributing_exposure_to_cells', 'columns_to_distribute'):
raise IOError('ERROR!! columns_to_distribute PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('SERA_distributing_exposure_to_cells', 'write_hdf5_bdg_classes_param'):
raise IOError('ERROR!! write_hdf5_bdg_classes_param PARAMETER MISSING FROM CONFIG FILE!!')
else:
check_if_param_is_True_or_False(config['SERA_distributing_exposure_to_cells']['write_hdf5_bdg_classes_param'], 'write_hdf5_bdg_classes_param')
elif section_name=='SERA_exploration_investigate_full_CSV_files':
if not config.has_option('SERA_exploration_investigate_full_CSV_files', 'tolerance_ratio'):
raise IOError('ERROR!! tolerance_ratio PARAMETER MISSING FROM CONFIG FILE!!')
else:
try:
float(config[section_name]['tolerance_ratio'])
except:
raise IOError('ERROR!! tolerance_ratio PARAMETER NEEDS TO BE A REAL NUMBER')
if not config.has_option('SERA_exploration_investigate_full_CSV_files', 'tolerance_diff'):
raise IOError('ERROR!! tolerance_diff PARAMETER MISSING FROM CONFIG FILE!!')
else:
try:
float(config[section_name]['tolerance_diff'])
except:
raise IOError('ERROR!! tolerance_diff PARAMETER NEEDS TO BE A REAL NUMBER')
elif section_name=='SERA_exploration_investigate_macro_taxonomies':
if not config.has_option('SERA_exploration_investigate_macro_taxonomies', 'country'):
raise IOError('ERROR!! country PARAMETER MISSING FROM CONFIG FILE!!')
_= check_of_occupancy_cases_parameter(config, section_name)
elif section_name=='SERA_exploration_summarise_columns_in_full_CSV_files':
_= check_of_occupancy_cases_parameter(config, section_name)
elif section_name=='SERA_mapping_admin_units_to_cells':
if not config.has_option('SERA_mapping_admin_units_to_cells', 'countries'):
raise IOError('ERROR!! countries PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='SERA_mapping_admin_units_to_cells_add_GHS':
if not config.has_option('SERA_mapping_admin_units_to_cells_add_GHS', 'ghs_input_filename'):
raise IOError('ERROR!! ghs_input_filename PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='SERA_mapping_admin_units_to_cells_add_Sat':
if not config.has_option('SERA_mapping_admin_units_to_cells_add_Sat', 'apply_model'):
raise IOError('ERROR!! apply_model PARAMETER MISSING FROM CONFIG FILE!!')
else:
check_if_param_is_True_or_False(config['SERA_mapping_admin_units_to_cells_add_Sat']['apply_model'], 'apply_model')
elif section_name=='SERA_testing_compare_visual_output_vs_OQ_input_files':
if not config.has_option('SERA_testing_compare_visual_output_vs_OQ_input_files', 'country'):
raise IOError('ERROR!! country PARAMETER MISSING FROM CONFIG FILE!!')
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
if not config.has_option('SERA_testing_compare_visual_output_vs_OQ_input_files', 'occupancy_case'):
raise IOError('ERROR!! occupancy_case PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('SERA_testing_compare_visual_output_vs_OQ_input_files', 'visual_output_filename'):
raise IOError('ERROR!! visual_output_filename PARAMETER MISSING FROM CONFIG FILE!!')
elif section_name=='SERA_testing_mapping_admin_units_to_cells_qualitycontrol':
if not config.has_option('SERA_testing_mapping_admin_units_to_cells_qualitycontrol', 'country'):
raise IOError('ERROR!! country PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('SERA_testing_mapping_admin_units_to_cells_qualitycontrol', 'tolerance'):
raise IOError('ERROR!! tolerance PARAMETER MISSING FROM CONFIG FILE!!')
else:
try:
float(config[section_name]['tolerance'])
except:
raise IOError('ERROR!! tolerance PARAMETER NEEDS TO BE A REAL NUMBER')
if not config.has_option('SERA_testing_mapping_admin_units_to_cells_qualitycontrol', 'number_of_cell_samples'):
raise IOError('ERROR!! number_of_cell_samples PARAMETER MISSING FROM CONFIG FILE!!')
else:
if not config[section_name]['number_of_cell_samples'].isnumeric():
raise IOError('ERROR!! number_of_cell_samples NEEDS TO BE A POSITIVE INTEGER!!')
elif section_name=='SERA_testing_rebuilding_exposure_from_cells_alternative_01' or section_name=='SERA_testing_rebuilding_exposure_from_cells_alternative_02' or section_name=='SERA_testing_rebuilding_exposure_from_cells_alternative_03':
if not config.has_option('SERA_testing_rebuilding_exposure_from_cells_alternative_01', 'countries'):
raise IOError('ERROR!! countries PARAMETER MISSING FROM CONFIG FILE!!')
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
_= check_of_occupancy_cases_parameter(config, section_name)
elif section_name=='GDE_check_tiles_vs_visual_CSVs':
_= check_of_occupancy_cases_parameter(config, section_name)
if not config.has_option('GDE_check_tiles_vs_visual_CSVs', 'path_GDE_tiles'):
raise IOError('ERROR!! path_GDE_tiles PARAMETER MISSING FROM CONFIG FILE!!')
if not config.has_option('GDE_check_tiles_vs_visual_CSVs', 'path_visual_csv'):
raise IOError('ERROR!! path_visual_csv PARAMETER MISSING FROM CONFIG FILE!!')
else:
if config['GDE_check_tiles_vs_visual_CSVs']['path_visual_csv'].split('.')[-1]!='csv':
raise IOError('ERROR!! path_visual_csv IN CONFIG FILE IS MEANT TO BE A .csv FILE!!')
if not config.has_option('GDE_check_tiles_vs_visual_CSVs', 'decimal_places_gral'):
raise IOError('ERROR!! decimal_places_gral PARAMETER MISSING FROM CONFIG FILE!!')
else:
if not config[section_name]['decimal_places_gral'].isnumeric():
raise IOError('ERROR!! decimal_places_gral NEEDS TO BE A POSITIVE INTEGER!!')
if not config.has_option('GDE_check_tiles_vs_visual_CSVs', 'decimal_places_costs'):
raise IOError('ERROR!! decimal_places_costs PARAMETER MISSING FROM CONFIG FILE!!')
else:
if not config[section_name]['decimal_places_costs'].isnumeric():
raise IOError('ERROR!! decimal_places_costs NEEDS TO BE A POSITIVE INTEGER!!')
else:
raise IOError('ERROR IN check_parameters(): '+section_name+' NOT RECOGNISED!!')
"""
section_name: name of the section in the configuration file (name that goes in [])
"""
if section_name == "File Paths":
kinds_of_paths = [
"out_path",
"sera_models_path",
"sera_models_OQ_path",
"sera_boundaries_path",
"ghs_path",
"gpw_path",
"sat_path",
"completeness_path",
]
for path_kind in kinds_of_paths:
if not config.has_option("File Paths", path_kind):
raise IOError("ERROR!! " + path_kind + " PARAMETER MISSING FROM CONFIG FILE!!")
else:
if not os.path.isdir(config["File Paths"][path_kind]):
raise IOError(
"ERROR!! "
+ path_kind
+ ": THE PATH SPECIFIED IN THE CONFIG FILE DOES NOT EXIST"
)
if not config.has_option("File Paths", "boundaries_type"):
raise IOError("ERROR!! boundaries_type PARAMETER MISSING FROM CONFIG FILE!!")
elif section_name == "Available Results":
if not config.has_option("Available Results", "results_available_for"):
raise IOError("ERROR!! results_available_for PARAMETER MISSING FROM CONFIG FILE!!")
else:
num_results_cases = len(
config["Available Results"]["results_available_for"].split(", ")
)
if not config.has_option("Available Results", "result_paths"):
raise IOError("ERROR!! result_paths PARAMETER MISSING FROM CONFIG FILE!!")
else:
num_result_paths = len(config["Available Results"]["result_paths"].split(", "))
if num_results_cases != num_result_paths:
raise IOError(
"ERROR!! results_available_for AND result_paths SHOULD HAVE THE SAME NUMBER OF ELEMENTS IN THE CONFIG FILE!!"
)
elif section_name == "OBM Database":
if not config.has_option("OBM Database", "db_obm_name"):
raise IOError("ERROR!! db_obm_name PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("OBM Database", "db_obm_username"):
raise IOError("ERROR!! db_obm_username PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("OBM Database", "db_obm_schema_name"):
raise IOError("ERROR!! db_obm_schema_name PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("OBM Database", "db_obm_table_name"):
raise IOError("ERROR!! db_obm_table_name PARAMETER MISSING FROM CONFIG FILE!!")
elif section_name == "Tiles Database":
if not config.has_option("Tiles Database", "db_tiles_name"):
raise IOError("ERROR!! db_tiles_name PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("Tiles Database", "db_tiles_username"):
raise IOError("ERROR!! db_tiles_username PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("Tiles Database", "db_tiles_schema_name"):
raise IOError("ERROR!! db_tiles_schema_name PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("Tiles Database", "db_tiles_table_name"):
raise IOError("ERROR!! db_tiles_table_name PARAMETER MISSING FROM CONFIG FILE!!")
elif section_name == "Admin Units Database":
if not config.has_option("Admin Units Database", "db_admin_name"):
raise IOError("ERROR!! db_admin_name PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("Admin Units Database", "db_admin_username"):
raise IOError("ERROR!! db_admin_username PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("Admin Units Database", "db_admin_schema_name"):
raise IOError("ERROR!! db_admin_schema_name PARAMETER MISSING FROM CONFIG FILE!!")
if not config.has_option("Admin Units Database", "db_admin_table_name"):
raise IOError("ERROR!! db_admin_table_name PARAMETER MISSING FROM CONFIG FILE!!")
elif section_name == "Cells to Process":
check_of_parameters_to_define_list_of_cell_IDs(
config, section_name
) # Check parameters related to defining the way in which the list of cells is defined
elif section_name == "Ocuppancy String Groups":
num_occup_cases = check_of_occupancy_cases_parameter(config, section_name)
if config.has_option("Ocuppancy String Groups", "occupancy_classifications"):
# It should have it, it is mandatory
num_classif_subgroups = len(
config["Ocuppancy String Groups"]["occupancy_classifications"].split(", ")
)
else:
raise IOError("ERROR!! OCCUPANCY CLASSIFICATIONS ARE NEEDED IN THE CONFIG FILE!!")
if config.has_option(
"Ocuppancy String Groups", "occupancy_cases"
) and config.has_option("Ocuppancy String Groups", "occupancy_classifications"):
if num_occup_cases != num_classif_subgroups:
raise IOError(
"ERROR!! LISTS OF OCCUPANCY STRINGS NEED TO BE PROVIDED IN occupancy_classifications FOR EACH CASE IN occupancy_cases!!"
)
elif section_name == "OBM_buildings_per_cell":
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
elif section_name == "GDE_gather_SERA_and_OBM":
check_of_sera_disaggregation_to_consider_parameter(config, section_name)
_ = check_of_occupancy_cases_parameter(config, section_name)
if not config.has_option("GDE_gather_SERA_and_OBM", "version_of_SERA_metadata"):
raise IOError(
"ERROR!! version_of_SERA_metadata PARAMETER MISSING FROM CONFIG FILE!!"
)
if not config.has_option("GDE_gather_SERA_and_OBM", "print_screen_during_run"):
raise IOError(