2018-01-29 21:28:33 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2018-08-03 09:54:28 +00:00
|
|
|
import os
|
2018-01-29 21:28:33 +00:00
|
|
|
import atlite
|
|
|
|
import numpy as np
|
|
|
|
import xarray as xr
|
|
|
|
import pandas as pd
|
|
|
|
import geopandas as gpd
|
|
|
|
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
2018-02-19 09:16:29 +00:00
|
|
|
logging.basicConfig(level=snakemake.config['logging_level'])
|
2018-01-29 21:28:33 +00:00
|
|
|
|
|
|
|
config = snakemake.config['renewable'][snakemake.wildcards.technology]
|
|
|
|
|
|
|
|
time = pd.date_range(freq='m', **snakemake.config['snapshots'])
|
|
|
|
params = dict(years=slice(*time.year[[0, -1]]), months=slice(*time.month[[0, -1]]))
|
|
|
|
|
|
|
|
regions = gpd.read_file(snakemake.input.regions).set_index('name')
|
|
|
|
regions.index.name = 'bus'
|
|
|
|
|
2018-08-03 09:54:28 +00:00
|
|
|
cutout = atlite.Cutout(config['cutout'],
|
|
|
|
cutout_dir=os.path.dirname(snakemake.input.cutout),
|
|
|
|
**params)
|
2018-01-29 21:28:33 +00:00
|
|
|
|
|
|
|
# Potentials
|
|
|
|
potentials = xr.open_dataarray(snakemake.input.potentials)
|
|
|
|
|
|
|
|
# Indicatormatrix
|
|
|
|
indicatormatrix = cutout.indicatormatrix(regions.geometry)
|
|
|
|
|
|
|
|
resource = config['resource']
|
|
|
|
func = getattr(cutout, resource.pop('method'))
|
2018-02-19 08:56:15 +00:00
|
|
|
correction_factor = config.get('correction_factor', 1.)
|
|
|
|
if correction_factor != 1.:
|
|
|
|
logger.warning('correction_factor is set as {}'.format(correction_factor))
|
|
|
|
capacity_factor = correction_factor * func(capacity_factor=True, **resource)
|
2018-01-29 21:28:33 +00:00
|
|
|
layout = capacity_factor * potentials
|
|
|
|
|
|
|
|
profile, capacities = func(matrix=indicatormatrix, index=regions.index,
|
|
|
|
layout=layout, per_unit=True, return_capacity=True,
|
|
|
|
**resource)
|
|
|
|
|
|
|
|
relativepotentials = (potentials / layout).stack(spatial=('y', 'x')).values
|
|
|
|
p_nom_max = xr.DataArray([np.nanmin(relativepotentials[row.nonzero()[1]])
|
|
|
|
if row.getnnz() > 0 else 0
|
|
|
|
for row in indicatormatrix.tocsr()],
|
|
|
|
[capacities.coords['bus']]) * capacities
|
|
|
|
|
2018-02-19 08:56:15 +00:00
|
|
|
ds = xr.merge([(correction_factor * profile).rename('profile'),
|
2018-01-29 21:28:33 +00:00
|
|
|
capacities.rename('weight'),
|
2018-07-10 14:33:22 +00:00
|
|
|
p_nom_max.rename('p_nom_max'),
|
|
|
|
layout.rename('potential')])
|
2018-01-29 21:28:33 +00:00
|
|
|
(ds.sel(bus=ds['profile'].mean('time') > config.get('min_p_max_pu', 0.))
|
|
|
|
.to_netcdf(snakemake.output.profile))
|