From d0e2fed4d74d1cf7ce1d150345ba63f01bf019bb Mon Sep 17 00:00:00 2001 From: FabianHofmann Date: Fri, 1 Nov 2019 13:27:42 +0100 Subject: [PATCH] incorporate suggestions --- scripts/add_electricity.py | 41 ++++++++++++++++++------------------ scripts/build_powerplants.py | 13 +++++++----- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index 9f1bed7a..e963e050 100644 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -345,7 +345,7 @@ def attach_hydro(n, costs, ppl): country = ppl['bus'].map(n.buses.country).rename("country") - inflow_idx = ror.index ^ hydro.index + inflow_idx = ror.index | hydro.index if not inflow_idx.empty: dist_key = ppl.loc[inflow_idx, 'p_nom'].groupby(country).transform(normed) @@ -391,31 +391,30 @@ def attach_hydro(n, costs, ppl): if 'hydro' in carriers and not hydro.empty: hydro_max_hours = c.get('hydro_max_hours') + hydro_stats = pd.read_csv(snakemake.input.hydro_capacities, + comment="#", na_values='-', index_col=0) + e_target = hydro_stats["E_store[TWh]"].clip(lower=0.2) * 1e6 + e_installed = hydro.eval('p_nom * max_hours').groupby(hydro.country).sum() + e_missing = e_target - e_installed + missing_mh_i = hydro.query('max_hours == 0').index + if hydro_max_hours == 'energy_capacity_totals_by_country': - # TODO: check where the statistics come from, IE's p_nom is totally - # underrepresented - e_target = (pd.read_csv(snakemake.input.hydro_capacities, - index_col=0)["E_store[TWh]"].clip(lower=0.2)*1e6) - e_installed = hydro.eval('p_nom * max_hours').groupby(hydro.country).sum() - e_missing = e_target - e_installed - missing_mh_i = hydro.query('max_hours == 0').index + # watch out some p_nom values like IE's are totally underrepresented + max_hours_country = e_missing / \ + hydro.loc[missing_mh_i].groupby('country').p_nom.sum() - max_hours_country = e_missing / hydro.loc[missing_mh_i].groupby('country').p_nom.sum() - hydro_max_hours = hydro.max_hours.where(hydro.max_hours > 0, - hydro.country.map(max_hours_country)) 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_store[TWh]'] * 1e3 / \ - hydro_capacities['p_nom_discharge[GW]'] + max_hours_country = hydro_stats['E_store[TWh]'] * 1e3 / \ + hydro_stats['p_nom_discharge[GW]'] - missing_countries = (pd.Index(hydro['country'].unique()) - .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))) + missing_countries = (pd.Index(hydro['country'].unique()) + .difference(max_hours_country.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.max_hours.where(hydro.max_hours > 0, + hydro.country.map(max_hours_country)).fillna(6) - hydro_max_hours = hydro['country'].map(estim_hydro_max_hours).fillna(6) n.madd('StorageUnit', hydro.index, carrier='hydro', bus=hydro['bus'], diff --git a/scripts/build_powerplants.py b/scripts/build_powerplants.py index 89193e68..4f29b35f 100644 --- a/scripts/build_powerplants.py +++ b/scripts/build_powerplants.py @@ -8,8 +8,8 @@ Relevant Settings .. code:: yaml electricity: - powerplants_filter: - custom_powerplants: + powerplants_filter: + custom_powerplants: .. seealso:: Documentation of the configuration file ``config.yaml`` at @@ -88,10 +88,13 @@ if __name__ == "__main__": cntries_without_ppl = [c for c in countries if c not in ppl.Country.unique()] - substation_i = n.buses.query('substation_lv').index - kdtree = KDTree(n.buses.loc[substation_i, ['x','y']].values) + for c in countries: + substation_i = n.buses.query('substation_lv and country == @c').index + kdtree = KDTree(n.buses.loc[substation_i, ['x','y']].values) + ppl_i = ppl.query('Country == @c').index - ppl['bus'] = substation_i[kdtree.query(ppl[['lon','lat']].values)[1]] + ppl.loc[ppl_i, 'bus'] = substation_i[kdtree.query(ppl.loc[ppl_i, + ['lon','lat']].values)[1]] if cntries_without_ppl: logging.warning(f"No powerplants known in: {', '.join(cntries_without_ppl)}")