Commit 3383ae51 authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Fixed logger in SERA_mapping_admin_units_to_cells.py

parent d718d49b
......@@ -43,6 +43,10 @@ import GDE_TOOLS_psql as gdet_psql
import GDE_TOOLS_read_config_file as gdet_conf
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def run_this_file(config_dict):
####################################################
# READ CONFIGURATION PARAMETERS
......@@ -63,7 +67,7 @@ def run_this_file(config_dict):
countries = \
config_dict['SERA_mapping_admin_units_to_cells']['countries'].split(', ')
####################################################
# SET UP LOGGER
# SET UP LOGGER FILE
####################################################
now = datetime.datetime.now()
now_str = '{:s}_{:s}_{:s}_{:s}_{:s}_{:s}'.format(str(now.year),
......@@ -74,7 +78,7 @@ def run_this_file(config_dict):
str(now.second).zfill(2))
log_fn = 'log_file_mapping_adm_units_%s.log' % (now_str)
fh = logging.FileHandler(os.path.join(out_path, log_fn))
logger = logging.getLogger(__name__)
logger.addHandler(fh)
####################################################
# START
####################################################
......@@ -160,10 +164,8 @@ def process_country(country_str, country_pos, number_countries,
continue
# Identify row of this administrative unit in adm_boundary DF:
print(' %s, level group %i of %i, %s (%i associated admin IDs): Working on admin ID %i (position %i)'
% (country_str, j+1, len(level_groups), case, len(unique_admins), admin_id, m+1))
# logger.info(' %s, level group %i of %i, %s (%i associated admin IDs): Working on admin ID %i (position %i)'
# % (country_str, j+1, len(level_groups), case, len(unique_admins), admin_id, m+1))
logger.info('\r %s, level group %i of %i, %s (%i associated admin IDs): Working on admin ID %i (position %i)'
% (country_str, j+1, len(level_groups), case, len(unique_admins), admin_id, m+1))
which_row = np.where(
adm_boundary['ID_%i' % (level_groups_levels[j])].values == admin_id)[0]
if len(which_row) != 1:
......@@ -174,16 +176,15 @@ def process_country(country_str, country_pos, number_countries,
# Pass on adm_boundary and the row
# to process all cells associated with this admin unit:
for results_adm in process_admin_unit(adm_boundary,
which_row, logger):
which_row):
cell_id, area_intersect, geom_intersect = results_adm
yield cell_id, level_groups_levels[j], admin_id, \
area_intersect, geom_intersect, level_groups[j], \
shp_not_found
break
print('\n')
def process_admin_unit(adm_boundary_gdf, which_row_of_gdf, logger=None):
def process_admin_unit(adm_boundary_gdf, which_row_of_gdf):
"""
adm_boundary_gdf = GeoPandas DataFrame with admin boundaries
which_row_of_gdf = row of adm_boundary_gdf to be processed
......@@ -202,8 +203,6 @@ def process_admin_unit(adm_boundary_gdf, which_row_of_gdf, logger=None):
for i in range(gdf_intersect.shape[0]):
print('\r Intersection of cell '+str(gdf_intersect['cell_id'].values[i]),
end='')
# logger.info('\r Intersection of cell %s'
# % (gdf_intersect['cell_id'].values[i]))
geom_cell = gdf_cells['geometry'].values[gdf_intersect['index_left'].values[i]]
geom_adm_unit = adm_boundary_gdf['geometry'].values[which_row_of_gdf[0]]
geom_intersect = geom_adm_unit.intersection(geom_cell)
......@@ -214,9 +213,6 @@ def process_admin_unit(adm_boundary_gdf, which_row_of_gdf, logger=None):
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
# This code needs to be run from the command line as
# python3 namefile.py configfile.ini
# sys.argv retrieves all the commands entered in the command line;
......
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