add_electricity: Fix for choices of countries without any offshore or hydro

fixes #22.
This commit is contained in:
Jonas Hoersch 2019-02-22 17:09:52 +01:00
parent 31aefae93c
commit f0a0a2531e

View File

@ -178,6 +178,8 @@ def attach_wind_and_solar(n, costs):
n.add("Carrier", name=tech) n.add("Carrier", name=tech)
with xr.open_dataset(getattr(snakemake.input, 'profile_' + tech)) as ds: with xr.open_dataset(getattr(snakemake.input, 'profile_' + tech)) as ds:
if ds.indexes['bus'].empty: continue
suptech = tech.split('-', 2)[0] suptech = tech.split('-', 2)[0]
if suptech == 'offwind': if suptech == 'offwind':
underwater_fraction = ds['underwater_fraction'].to_pandas() underwater_fraction = ds['underwater_fraction'].to_pandas()
@ -249,83 +251,88 @@ def attach_hydro(n, costs, ppl):
has_pump=ppl.technology.str.contains('Pumped Storage') has_pump=ppl.technology.str.contains('Pumped Storage')
) )
country = ppl['bus'].map(n.buses.country) country = ppl['bus'].map(n.buses.country).rename("country")
# distribute by p_nom in each country
dist_key = ppl.loc[ppl.has_inflow, 'p_nom'].groupby(country).transform(normed)
with xr.open_dataarray(snakemake.input.profile_hydro) as inflow: if ppl.has_inflow.any():
inflow_countries = pd.Index(country.loc[ppl.has_inflow].values) dist_key = ppl.loc[ppl.has_inflow, 'p_nom'].groupby(country).transform(normed)
assert len(inflow_countries.unique().difference(inflow.indexes['countries'])) == 0, \
"'{}' is missing inflow time-series for at least one country: {}".format(snakemake.input.profile_hydro, ", ".join(inflow_countries.unique().difference(inflow.indexes['countries'])))
inflow_t = ( with xr.open_dataarray(snakemake.input.profile_hydro) as inflow:
inflow.sel(countries=inflow_countries) inflow_countries = pd.Index(country.loc[ppl.has_inflow].values)
.rename({'countries': 'name'}) assert len(inflow_countries.unique().difference(inflow.indexes['countries'])) == 0, (
.assign_coords(name=ppl.index[ppl.has_inflow]) "'{}' is missing inflow time-series for at least one country: {}"
.transpose('time', 'name') .format(snakemake.input.profile_hydro, ", ".join(inflow_countries.unique().difference(inflow.indexes['countries'])))
.to_pandas() )
.multiply(dist_key, axis=1)
) inflow_t = (
inflow.sel(countries=inflow_countries)
.rename({'countries': 'name'})
.assign_coords(name=ppl.index[ppl.has_inflow])
.transpose('time', 'name')
.to_pandas()
.multiply(dist_key, axis=1)
)
if 'ror' in carriers: if 'ror' in carriers:
ror = ppl.loc[ppl.has_inflow & ~ ppl.has_store] ror = ppl.loc[ppl.has_inflow & ~ ppl.has_store]
n.madd("Generator", ror.index, if not ror.empty:
carrier='ror', n.madd("Generator", ror.index,
bus=ror['bus'], carrier='ror',
p_nom=ror['p_nom'], bus=ror['bus'],
efficiency=costs.at['ror', 'efficiency'], p_nom=ror['p_nom'],
capital_cost=costs.at['ror', 'capital_cost'], efficiency=costs.at['ror', 'efficiency'],
weight=ror['p_nom'], capital_cost=costs.at['ror', 'capital_cost'],
p_max_pu=(inflow_t.loc[:, ror.index] weight=ror['p_nom'],
.divide(ror['p_nom'], axis=1) p_max_pu=(inflow_t.loc[:, ror.index]
.where(lambda df: df<=1., other=1.))) .divide(ror['p_nom'], axis=1)
.where(lambda df: df<=1., other=1.)))
if 'PHS' in carriers: if 'PHS' in carriers:
phs = ppl.loc[ppl.has_store & ppl.has_pump] phs = ppl.loc[ppl.has_store & ppl.has_pump]
n.madd('StorageUnit', phs.index, if not phs.empty:
carrier='PHS', n.madd('StorageUnit', phs.index,
bus=phs['bus'], carrier='PHS',
p_nom=phs['p_nom'], bus=phs['bus'],
capital_cost=costs.at['PHS', 'capital_cost'], p_nom=phs['p_nom'],
max_hours=c['PHS_max_hours'], capital_cost=costs.at['PHS', 'capital_cost'],
efficiency_store=np.sqrt(costs.at['PHS','efficiency']), max_hours=c['PHS_max_hours'],
efficiency_dispatch=np.sqrt(costs.at['PHS','efficiency']), efficiency_store=np.sqrt(costs.at['PHS','efficiency']),
cyclic_state_of_charge=True, efficiency_dispatch=np.sqrt(costs.at['PHS','efficiency']),
inflow=inflow_t.loc[:, phs.index[phs.has_inflow]]) cyclic_state_of_charge=True,
inflow=inflow_t.loc[:, phs.index[phs.has_inflow]])
if 'hydro' in carriers: if 'hydro' in carriers:
hydro = ppl.loc[ppl.has_store & ~ ppl.has_pump & ppl.has_inflow].join(country.rename('country')) hydro = ppl.loc[ppl.has_store & ~ ppl.has_pump & ppl.has_inflow].join(country)
if not hydro.empty:
hydro_max_hours = c.get('hydro_max_hours')
if hydro_max_hours == 'energy_capacity_totals_by_country':
hydro_e_country = pd.read_csv(snakemake.input.hydro_capacities, index_col=0)["E_store[TWh]"].clip(lower=0.2)*1e6
hydro_max_hours_country = hydro_e_country / hydro.groupby('country').p_nom.sum()
hydro_max_hours = hydro.country.map(hydro_e_country / hydro.groupby('country').p_nom.sum())
elif hydro_max_hours == 'estimate_by_large_installations':
hydro_capacities = pd.read_csv(snakemake.input.hydro_capacities, comment="#", na_values='-', index_col=0)
estim_hydro_max_hours = hydro_capacities.e_stor / hydro_capacities.p_nom_discharge
hydro_max_hours = c.get('hydro_max_hours') missing_countries = (pd.Index(hydro['country'].unique())
if hydro_max_hours == 'energy_capacity_totals_by_country': .difference(estim_hydro_max_hours.dropna().index))
hydro_e_country = pd.read_csv(snakemake.input.hydro_capacities, index_col=0)["E_store[TWh]"].clip(lower=0.2)*1e6 if not missing_countries.empty:
hydro_max_hours_country = hydro_e_country / hydro.groupby('country').p_nom.sum() logger.warning("Assuming max_hours=6 for hydro reservoirs in the countries: {}"
hydro_max_hours = hydro.country.map(hydro_e_country / hydro.groupby('country').p_nom.sum()) .format(", ".join(missing_countries)))
elif hydro_max_hours == 'estimate_by_large_installations':
hydro_capacities = pd.read_csv(snakemake.input.hydro_capacities, comment="#", na_values='-', index_col=0)
estim_hydro_max_hours = hydro_capacities.e_stor / hydro_capacities.p_nom_discharge
missing_countries = (pd.Index(hydro['country'].unique()) hydro_max_hours = hydro['country'].map(estim_hydro_max_hours).fillna(6)
.difference(estim_hydro_max_hours.dropna().index))
if not missing_countries.empty:
logger.warning("Assuming max_hours=6 for hydro reservoirs in the countries: {}"
.format(", ".join(missing_countries)))
hydro_max_hours = hydro['country'].map(estim_hydro_max_hours).fillna(6) n.madd('StorageUnit', hydro.index, carrier='hydro',
bus=hydro['bus'],
n.madd('StorageUnit', hydro.index, carrier='hydro', p_nom=hydro['p_nom'],
bus=hydro['bus'], max_hours=hydro_max_hours,
p_nom=hydro['p_nom'], capital_cost=(costs.at['hydro', 'capital_cost']
max_hours=hydro_max_hours, if c.get('hydro_capital_cost') else 0.),
capital_cost=(costs.at['hydro', 'capital_cost'] marginal_cost=costs.at['hydro', 'marginal_cost'],
if c.get('hydro_capital_cost') else 0.), p_max_pu=1., # dispatch
marginal_cost=costs.at['hydro', 'marginal_cost'], p_min_pu=0., # store
p_max_pu=1., # dispatch efficiency_dispatch=costs.at['hydro', 'efficiency'],
p_min_pu=0., # store efficiency_store=0.,
efficiency_dispatch=costs.at['hydro', 'efficiency'], cyclic_state_of_charge=True,
efficiency_store=0., inflow=inflow_t.loc[:, hydro.index])
cyclic_state_of_charge=True,
inflow=inflow_t.loc[:, hydro.index])
def attach_extendable_generators(n, costs, ppl): def attach_extendable_generators(n, costs, ppl):