build_renewable_profiles: Pass all globals with init_globals

This commit is contained in:
Jonas Hoersch 2019-02-14 09:27:03 +01:00
parent 7f3f096ba6
commit c858ed0b0d

View File

@ -20,22 +20,24 @@ import progressbar as pgb
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
bounds = dx = dy = gebco = clc = natura = None bounds = dx = dy = config = paths = gebco = clc = natura = None
def init_globals(n_bounds, n_dx, n_dy): def init_globals(n_bounds, n_dx, n_dy, n_config, n_paths):
# global in each process of the multiprocessing.Pool # global in each process of the multiprocessing.Pool
global bounds, dx, dy, gebco, clc, natura global bounds, dx, dy, config, paths, gebco, clc, natura
bounds = n_bounds bounds = n_bounds
dx = n_dx dx = n_dx
dy = n_dy dy = n_dy
config = n_config
paths = n_paths
gebco = gk.raster.loadRaster(snakemake.input.gebco) gebco = gk.raster.loadRaster(paths["gebco"])
gebco.SetProjection(gk.srs.loadSRS(4326).ExportToWkt()) gebco.SetProjection(gk.srs.loadSRS(4326).ExportToWkt())
clc = gk.raster.loadRaster(snakemake.input.corine) clc = gk.raster.loadRaster(paths["corine"])
clc.SetProjection(gk.srs.loadSRS(3035).ExportToWkt()) clc.SetProjection(gk.srs.loadSRS(3035).ExportToWkt())
natura = gk.raster.loadRaster(snakemake.input.natura) natura = gk.raster.loadRaster(paths["natura"])
def downsample_to_coarse_grid(bounds, dx, dy, mask, data): def downsample_to_coarse_grid(bounds, dx, dy, mask, data):
# The GDAL warp function with the 'average' resample algorithm needs a band of zero values of at least # The GDAL warp function with the 'average' resample algorithm needs a band of zero values of at least
@ -49,7 +51,7 @@ def downsample_to_coarse_grid(bounds, dx, dy, mask, data):
return average return average
def calculate_potential(gid): def calculate_potential(gid):
feature = gk.vector.extractFeature(snakemake.input.regions, where=gid) feature = gk.vector.extractFeature(paths["regions"], where=gid)
ec = gl.ExclusionCalculator(feature.geom) ec = gl.ExclusionCalculator(feature.geom)
corine = config.get("corine", {}) corine = config.get("corine", {})
@ -67,9 +69,9 @@ def calculate_potential(gid):
# TODO compute a distance field as a raster beforehand # TODO compute a distance field as a raster beforehand
if 'max_shore_distance' in config: if 'max_shore_distance' in config:
ec.excludeVectorType(snakemake.input.country_shapes, buffer=config['max_shore_distance'], invert=True) ec.excludeVectorType(paths["country_shapes"], buffer=config['max_shore_distance'], invert=True)
if 'min_shore_distance' in config: if 'min_shore_distance' in config:
ec.excludeVectorType(snakemake.input.country_shapes, buffer=config['min_shore_distance']) ec.excludeVectorType(paths["country_shapes"], buffer=config['min_shore_distance'])
availability = downsample_to_coarse_grid(bounds, dx, dy, ec.region, np.where(ec.region.mask, ec._availability, 0)) availability = downsample_to_coarse_grid(bounds, dx, dy, ec.region, np.where(ec.region.mask, ec._availability, 0))
@ -96,10 +98,11 @@ if __name__ == '__main__':
miny - dy/2., maxy + dy/2.)) miny - dy/2., maxy + dy/2.))
# Use GLAES to compute available potentials and the transition matrix # Use GLAES to compute available potentials and the transition matrix
paths = dict(snakemake.input)
with Pool(initializer=init_globals, initargs=(bounds, dx, dy), with Pool(initializer=init_globals, initargs=(bounds, dx, dy, config, paths),
maxtasksperchild=20, processes=snakemake.config['atlite'].get('nprocesses', 2)) as pool: maxtasksperchild=20, processes=snakemake.config['atlite'].get('nprocesses', 2)) as pool:
regions = gk.vector.extractFeatures(snakemake.input.regions, onlyAttr=True) regions = gk.vector.extractFeatures(paths["regions"], onlyAttr=True)
buses = pd.Index(regions['name'], name="bus") buses = pd.Index(regions['name'], name="bus")
widgets = [ widgets = [
pgb.widgets.Percentage(), pgb.widgets.Percentage(),