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

Merge branch 'feature/ind07' into 'master'

Modified scripts further down the processing line to work well with the new definition of industrial cells

See merge request !17
parents e0348fbc c2d041a6
......@@ -155,12 +155,22 @@ def get_column_of_hdf5_by_name(target_list, hdf5_subgroup):
def calculate_weights(cells_param_vals, subweights):
"""
cells_param_vals= e.g. cell area, cell population (from GPW), satellite area of built-up area in the cell, etc.
subweights= generally conceived to be a flag vector, with 1 in the positions of cells that will be considered for the distribution
and 0 in those that will not be counted (e.g. used to identified the incomplete cells)
cells_param_vals= e.g. cell area, cell population (from GPW), satellite area of built-up
area in the cell, etc.
subweights= generally conceived to be a flag vector, with 1 in the positions of cells that
will be considered for the distribution and 0 in those that will not be counted
(e.g. used to identified the incomplete cells)
If cells_param_vals contains NaNs then the output will contain NaNs too.
"""
aux_array= cells_param_vals * subweights
aux_array = cells_param_vals * subweights
if aux_array.sum() > 1E-15 or np.isnan(aux_array.sum()):
return aux_array / aux_array.sum()
else:
# If the summation of the array is equal to zero, return uniform distribution, filtered
# by subweights:
return subweights / subweights.sum()
def add_occupancy_broad_cases(bdgs_df, occupancy_definitions):
......
......@@ -321,6 +321,9 @@ def get_cells_and_weights_from_country_occup_adm_id(db_name, db_username, sch_na
param_list= []
for i in range(0,len(exec_result)):
cell_id_list.append(exec_result[i][0])
if exec_result[i][1] is None: # this happens when the field in the database is NULL
param_list.append(np.nan)
else:
param_list.append(exec_result[i][1])
conn.close()
return cell_id_list, np.array(param_list)
......@@ -335,16 +338,16 @@ def define_subweights_of_cells(in_cells_ids, which_cells):
- 'incomplete'= only incomplete cells will be considered
"""
if which_cells=='all':
out_subweights= np.ones([len(in_cells_ids)])
out_subweights= np.ones([len(in_cells_ids)], dtype=float)
elif which_cells=='complete':
# TO DO: VECTOR WITH 1 WHERE COMPLETE AND 0 WHERE INCOMPLETE
out_subweights= np.ones([len(in_cells_ids)])
out_subweights= np.ones([len(in_cells_ids)], dtype=float)
elif which_cells=='incomplete':
# TO DO: VECTOR WITH 1 WHERE INCOMPLETE AND 0 WHERE COMPLETE
out_subweights= np.ones([len(in_cells_ids)])
out_subweights= np.ones([len(in_cells_ids)], dtype=float)
else:
print(' ERROR!! which_cells STRING NOT RECOGNISED!!! SUBWEIGHTS CANNOT BE CALCULATED!!')
out_subweights= -999.9 * np.ones([len(in_cells_ids)])
out_subweights= -999.9 * np.ones([len(in_cells_ids)], dtype=float)
return out_subweights
......
......@@ -61,7 +61,7 @@ def get_admin_level_definition(country_name, path, output_dfs=False, full_files=
NOTE: It is not possible to break the for i in range(1,max_possible_level+1) loop because there
are files for which intermediate levels are missing but more detailed levels are available.
"""
max_possible_level= 5
max_possible_level= 99
exposure_cases= ['Res','Com','Ind']
actual_levels_list= [0,0,0]
out_dataframes= {}
......
......@@ -126,7 +126,11 @@ def run_this_file(config_dict):
out_list= [country_admin_id]
if len(country_admin_id_split)>1: # to get rid of "GR"
for occu in list(dfs_dict.keys()):
if len(country_admin_id_split)==2:
which= np.where(dfs_dict[occu]['id_'+str(admin_levels[occu])].values==int(country_admin_id_split[1]))[0]
else: # e.g. industrial 30-arcsec cells
country_admin_id_split_reconstruct = '_'.join(country_admin_id_split[1:])
which= np.where(dfs_dict[occu]['id_'+str(admin_levels[occu])].values==country_admin_id_split_reconstruct)[0]
sera_num_bdgs_j= dfs_dict[occu]['buildings'].values[which].sum()
sera_num_dwells_j= dfs_dict[occu]['dwellings'].values[which].sum()
sera_num_ppl_j= dfs_dict[occu]['occupants_per_asset'].values[which].sum()
......
......@@ -98,6 +98,10 @@ def run_this_file(config_dict):
param_for_weighting_adj= param_for_weighting
cells_ids, cells_param_vals= gdet_psql.get_cells_and_weights_from_country_occup_adm_id(DB_name_grid, DB_username_grid, DB_schema_name_grid, DB_table_name_grid, country_name, level_groups[j][0], admin_id, param_for_weighting_adj)
cells_ids_weights= gdet_gral.calculate_weights(cells_param_vals, gdet_psql.define_subweights_of_cells(cells_ids, 'all'))
if np.any(np.isnan(cells_ids_weights)):
# param_for_weighting_adj is not defined for at list one of the cells. Default to using "area":
cells_ids, cells_param_vals= gdet_psql.get_cells_and_weights_from_country_occup_adm_id(DB_name_grid, DB_username_grid, DB_schema_name_grid, DB_table_name_grid, country_name, level_groups[j][0], admin_id, "area")
cells_ids_weights= gdet_gral.calculate_weights(cells_param_vals, gdet_psql.define_subweights_of_cells(cells_ids, 'all'))
for case in level_groups[j]: # Res, Com or Ind
if case not in ignore_occupancy_cases:
print(' '+country_name+', level group '+str(j+1)+' of '+str(len(level_groups))+', '+case+' ('+str(len(adm_ids_list))+' associated admin IDs)'+': Working on admin ID '+str(admin_id))
......
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