2018-02-10 16:16:20 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
import logging
|
2018-11-12 20:40:05 +00:00
|
|
|
import numpy as np
|
2018-02-10 16:16:20 +00:00
|
|
|
import pandas as pd
|
|
|
|
from scipy.spatial import cKDTree as KDTree
|
2018-11-12 20:40:05 +00:00
|
|
|
import pycountry as pyc
|
2018-02-10 16:16:20 +00:00
|
|
|
|
|
|
|
import pypsa
|
|
|
|
import powerplantmatching as ppm
|
|
|
|
|
2018-11-12 20:40:05 +00:00
|
|
|
def country_alpha_2(name):
|
|
|
|
try:
|
|
|
|
cntry = pyc.countries.get(name=name)
|
|
|
|
except KeyError:
|
2019-02-05 16:46:51 +00:00
|
|
|
cntry = None
|
|
|
|
if cntry is None:
|
2018-11-12 20:40:05 +00:00
|
|
|
cntry = pyc.countries.get(official_name=name)
|
|
|
|
return cntry.alpha_2
|
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if 'snakemake' not in globals():
|
|
|
|
from vresutils.snakemake import MockSnakemake, Dict
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
snakemake = MockSnakemake(
|
|
|
|
input=Dict(base_network='networks/base.nc'),
|
|
|
|
output=['resources/powerplants.csv']
|
|
|
|
)
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
logging.basicConfig(level=snakemake.config['logging_level'])
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
n = pypsa.Network(snakemake.input.base_network)
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
ppl = (ppm.collection.matched_data()
|
|
|
|
[lambda df : ~df.Fueltype.isin(('Solar', 'Wind'))]
|
|
|
|
.pipe(ppm.cleaning.clean_technology)
|
|
|
|
.assign(Fueltype=lambda df: (
|
|
|
|
df.Fueltype.where(df.Fueltype != 'Natural Gas',
|
|
|
|
df.Technology.replace('Steam Turbine', 'OCGT').fillna('OCGT'))))
|
|
|
|
.pipe(ppm.utils.fill_geoposition))
|
2018-07-10 14:29:11 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
# 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'
|
2018-07-10 14:29:11 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
ppl = ppl.loc[ppl.lon.notnull() & ppl.lat.notnull()]
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
ppl_country = ppl.Country.map(country_alpha_2)
|
|
|
|
countries = n.buses.country.unique()
|
|
|
|
cntries_without_ppl = []
|
2018-11-12 20:40:05 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
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
|
2018-11-12 20:40:05 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
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]]
|
2018-11-12 20:40:05 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
if cntries_without_ppl:
|
|
|
|
logging.warning("No powerplants known in: {}".format(", ".join(cntries_without_ppl)))
|
2018-11-12 20:40:05 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
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()))
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
ppl.to_csv(snakemake.output[0])
|