build_powerplants: Assign power plants only within the right country

Also update the delivered powerplants snapshots.
This commit is contained in:
Jonas Hörsch 2018-11-12 21:40:05 +01:00
parent 5adf2ccd6c
commit e7d007bc8f
2 changed files with 5181 additions and 5338 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,21 @@
# coding: utf-8 # coding: utf-8
import logging import logging
import numpy as np
import pandas as pd import pandas as pd
from scipy.spatial import cKDTree as KDTree from scipy.spatial import cKDTree as KDTree
import pycountry as pyc
import pypsa import pypsa
import powerplantmatching as ppm import powerplantmatching as ppm
def country_alpha_2(name):
try:
cntry = pyc.countries.get(name=name)
except KeyError:
cntry = pyc.countries.get(official_name=name)
return cntry.alpha_2
if 'snakemake' not in globals(): if 'snakemake' not in globals():
from vresutils.snakemake import MockSnakemake, Dict from vresutils.snakemake import MockSnakemake, Dict
@ -25,16 +34,32 @@ ppl = (ppm.collection.matched_data()
.assign(Fueltype=lambda df: ( .assign(Fueltype=lambda df: (
df.Fueltype.where(df.Fueltype != 'Natural Gas', df.Fueltype.where(df.Fueltype != 'Natural Gas',
df.Technology.replace('Steam Turbine', 'OCGT').fillna('OCGT')))) df.Technology.replace('Steam Turbine', 'OCGT').fillna('OCGT'))))
.pipe(ppm.utils.fill_geoposition, parse=True, only_saved_locs=True) .pipe(ppm.utils.fill_geoposition))
.pipe(ppm.heuristics.fill_missing_duration))
# ppl.loc[(ppl.Fueltype == 'Other') & ppl.Technology.str.contains('CCGT'), 'Fueltype'] = 'CCGT' # ppl.loc[(ppl.Fueltype == 'Other') & ppl.Technology.str.contains('CCGT'), 'Fueltype'] = 'CCGT'
# ppl.loc[(ppl.Fueltype == 'Other') & ppl.Technology.str.contains('Steam Turbine'), 'Fueltype'] = 'CCGT' # ppl.loc[(ppl.Fueltype == 'Other') & ppl.Technology.str.contains('Steam Turbine'), 'Fueltype'] = 'CCGT'
ppl = ppl.loc[ppl.lon.notnull() & ppl.lat.notnull()] ppl = ppl.loc[ppl.lon.notnull() & ppl.lat.notnull()]
substation_lv_i = n.buses.index[n.buses['substation_lv']] ppl_country = ppl.Country.map(country_alpha_2)
kdtree = KDTree(n.buses.loc[substation_lv_i, ['x','y']].values) countries = n.buses.country.unique()
ppl = ppl.assign(bus=substation_lv_i[kdtree.query(ppl[['lon','lat']].values)[1]]) cntries_without_ppl = []
for cntry in countries:
substation_lv_i = n.buses.index[n.buses['substation_lv'] & (n.buses.country == cntry)]
ppl_b = ppl_country == cntry
if not ppl_b.any():
cntries_without_ppl.append(cntry)
continue
kdtree = KDTree(n.buses.loc[substation_lv_i, ['x','y']].values)
ppl.loc[ppl_b, 'bus'] = substation_lv_i[kdtree.query(ppl.loc[ppl_b, ['lon','lat']].values)[1]]
if cntries_without_ppl:
logging.warning("No powerplants known in: {}".format(", ".join(cntries_without_ppl)))
bus_null_b = ppl["bus"].isnull()
if bus_null_b.any():
logging.warning("Couldn't find close bus for {} powerplants".format(bus_null_b.sum()))
ppl.to_csv(snakemake.output[0]) ppl.to_csv(snakemake.output[0])