Commit 5999f08a authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Fixed bug: dealing with NULL and all zeros to distribute SERA onto grid

parent adfb204f
...@@ -155,12 +155,22 @@ def get_column_of_hdf5_by_name(target_list, hdf5_subgroup): ...@@ -155,12 +155,22 @@ def get_column_of_hdf5_by_name(target_list, hdf5_subgroup):
def calculate_weights(cells_param_vals, subweights): 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. cells_param_vals= e.g. cell area, cell population (from GPW), satellite area of built-up
subweights= generally conceived to be a flag vector, with 1 in the positions of cells that will be considered for the distribution area in the cell, etc.
and 0 in those that will not be counted (e.g. used to identified the incomplete cells) 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
return aux_array / aux_array.sum() 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): def add_occupancy_broad_cases(bdgs_df, occupancy_definitions):
......
...@@ -320,8 +320,11 @@ def get_cells_and_weights_from_country_occup_adm_id(db_name, db_username, sch_na ...@@ -320,8 +320,11 @@ def get_cells_and_weights_from_country_occup_adm_id(db_name, db_username, sch_na
cell_id_list= [] cell_id_list= []
param_list= [] param_list= []
for i in range(0,len(exec_result)): for i in range(0,len(exec_result)):
cell_id_list.append(exec_result[i][0]) cell_id_list.append(exec_result[i][0])
param_list.append(exec_result[i][1]) 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() conn.close()
return cell_id_list, np.array(param_list) return cell_id_list, np.array(param_list)
...@@ -335,16 +338,16 @@ def define_subweights_of_cells(in_cells_ids, which_cells): ...@@ -335,16 +338,16 @@ def define_subweights_of_cells(in_cells_ids, which_cells):
- 'incomplete'= only incomplete cells will be considered - 'incomplete'= only incomplete cells will be considered
""" """
if which_cells=='all': 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': elif which_cells=='complete':
# TO DO: VECTOR WITH 1 WHERE COMPLETE AND 0 WHERE INCOMPLETE # 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': elif which_cells=='incomplete':
# TO DO: VECTOR WITH 1 WHERE INCOMPLETE AND 0 WHERE COMPLETE # 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: else:
print(' ERROR!! which_cells STRING NOT RECOGNISED!!! SUBWEIGHTS CANNOT BE CALCULATED!!') 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 return out_subweights
......
...@@ -98,6 +98,10 @@ def run_this_file(config_dict): ...@@ -98,6 +98,10 @@ def run_this_file(config_dict):
param_for_weighting_adj= param_for_weighting 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, 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')) 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 for case in level_groups[j]: # Res, Com or Ind
if case not in ignore_occupancy_cases: 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)) 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