2022-09-16 13:04:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-02-16 10:50:55 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2017-2023 The PyPSA-Eur Authors
|
2020-05-29 07:50:55 +00:00
|
|
|
#
|
2021-09-14 14:37:41 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
2020-05-29 07:50:55 +00:00
|
|
|
|
2018-02-10 16:16:20 +00:00
|
|
|
# coding: utf-8
|
2019-08-08 13:02:28 +00:00
|
|
|
"""
|
2022-09-16 13:04:04 +00:00
|
|
|
Retrieves conventional powerplant capacities and locations from
|
|
|
|
`powerplantmatching <https://github.com/FRESNA/powerplantmatching>`_, assigns
|
|
|
|
these to buses and creates a ``.csv`` file. It is possible to amend the
|
|
|
|
powerplant database with custom entries provided in
|
|
|
|
``data/custom_powerplants.csv``.
|
2019-08-11 09:40:47 +00:00
|
|
|
|
|
|
|
Relevant Settings
|
|
|
|
-----------------
|
|
|
|
|
2019-08-11 20:34:18 +00:00
|
|
|
.. code:: yaml
|
|
|
|
|
2019-10-31 17:01:33 +00:00
|
|
|
electricity:
|
2019-11-01 12:27:42 +00:00
|
|
|
powerplants_filter:
|
|
|
|
custom_powerplants:
|
2019-08-11 11:17:36 +00:00
|
|
|
|
2019-10-30 22:09:41 +00:00
|
|
|
.. seealso::
|
2023-04-21 08:41:44 +00:00
|
|
|
Documentation of the configuration file ``config/config.yaml`` at
|
2019-11-06 14:59:34 +00:00
|
|
|
:ref:`electricity`
|
2019-08-13 08:03:46 +00:00
|
|
|
|
2019-08-11 09:40:47 +00:00
|
|
|
Inputs
|
|
|
|
------
|
|
|
|
|
2019-08-11 20:34:18 +00:00
|
|
|
- ``networks/base.nc``: confer :ref:`base`.
|
2019-11-06 14:59:34 +00:00
|
|
|
- ``data/custom_powerplants.csv``: custom powerplants in the same format as `powerplantmatching <https://github.com/FRESNA/powerplantmatching>`_ provides
|
2019-08-11 20:34:18 +00:00
|
|
|
|
2019-08-11 09:40:47 +00:00
|
|
|
Outputs
|
|
|
|
-------
|
|
|
|
|
2019-08-12 17:01:53 +00:00
|
|
|
- ``resource/powerplants.csv``: A list of conventional power plants (i.e. neither wind nor solar) with fields for name, fuel type, technology, country, capacity in MW, duration, commissioning year, retrofit year, latitude, longitude, and dam information as documented in the `powerplantmatching README <https://github.com/FRESNA/powerplantmatching/blob/master/README.md>`_; additionally it includes information on the closest substation/bus in ``networks/base.nc``.
|
|
|
|
|
2023-03-09 12:28:42 +00:00
|
|
|
.. image:: img/powerplantmatching.png
|
2019-08-12 17:01:53 +00:00
|
|
|
:scale: 30 %
|
|
|
|
|
|
|
|
**Source:** `powerplantmatching on GitHub <https://github.com/FRESNA/powerplantmatching>`_
|
2019-08-11 20:34:18 +00:00
|
|
|
|
2019-08-11 09:40:47 +00:00
|
|
|
Description
|
|
|
|
-----------
|
|
|
|
|
2019-11-06 14:59:34 +00:00
|
|
|
The configuration options ``electricity: powerplants_filter`` and ``electricity: custom_powerplants`` can be used to control whether data should be retrieved from the original powerplants database or from custom amendmends. These specify `pandas.query <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.query.html>`_ commands.
|
|
|
|
|
|
|
|
1. Adding all powerplants from custom:
|
|
|
|
|
|
|
|
.. code:: yaml
|
|
|
|
|
|
|
|
powerplants_filter: false
|
|
|
|
custom_powerplants: true
|
|
|
|
|
|
|
|
2. Replacing powerplants in e.g. Germany by custom data:
|
|
|
|
|
|
|
|
.. code:: yaml
|
|
|
|
|
|
|
|
powerplants_filter: Country not in ['Germany']
|
|
|
|
custom_powerplants: true
|
|
|
|
|
2019-11-09 08:08:25 +00:00
|
|
|
or
|
2019-11-06 14:59:34 +00:00
|
|
|
|
|
|
|
.. code:: yaml
|
|
|
|
|
|
|
|
powerplants_filter: Country not in ['Germany']
|
|
|
|
custom_powerplants: Country in ['Germany']
|
|
|
|
|
|
|
|
|
|
|
|
3. Adding additional built year constraints:
|
|
|
|
|
|
|
|
.. code:: yaml
|
|
|
|
|
|
|
|
powerplants_filter: Country not in ['Germany'] and YearCommissioned <= 2015
|
|
|
|
custom_powerplants: YearCommissioned <= 2015
|
2019-08-08 13:02:28 +00:00
|
|
|
"""
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2019-10-31 13:48:10 +00:00
|
|
|
import logging
|
2019-11-28 07:22:52 +00:00
|
|
|
|
2019-10-31 13:30:05 +00:00
|
|
|
import pandas as pd
|
2022-09-16 13:04:04 +00:00
|
|
|
import powerplantmatching as pm
|
|
|
|
import pypsa
|
|
|
|
from _helpers import configure_logging
|
2022-04-08 13:25:05 +00:00
|
|
|
from powerplantmatching.export import map_country_bus
|
2020-12-03 18:50:53 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-09-14 14:34:02 +00:00
|
|
|
def add_custom_powerplants(ppl, custom_powerplants, custom_ppl_query=False):
|
2019-10-31 13:48:10 +00:00
|
|
|
if not custom_ppl_query:
|
2019-08-19 16:04:51 +00:00
|
|
|
return ppl
|
2022-09-16 13:04:04 +00:00
|
|
|
add_ppls = pd.read_csv(custom_powerplants, index_col=0, dtype={"bus": "str"})
|
2019-10-31 13:48:10 +00:00
|
|
|
if isinstance(custom_ppl_query, str):
|
2019-12-02 23:52:52 +00:00
|
|
|
add_ppls.query(custom_ppl_query, inplace=True)
|
2022-09-16 13:04:04 +00:00
|
|
|
return pd.concat(
|
|
|
|
[ppl, add_ppls], sort=False, ignore_index=True, verify_integrity=True
|
|
|
|
)
|
2019-10-31 13:48:10 +00:00
|
|
|
|
2018-11-12 20:40:05 +00:00
|
|
|
|
2022-08-04 13:25:00 +00:00
|
|
|
def replace_natural_gas_technology(df):
|
2022-09-16 13:04:04 +00:00
|
|
|
mapping = {"Steam Turbine": "OCGT", "Combustion Engine": "OCGT"}
|
|
|
|
tech = df.Technology.replace(mapping).fillna("OCGT")
|
|
|
|
return df.Technology.where(df.Fueltype != "Natural Gas", tech)
|
|
|
|
|
2022-08-04 13:25:00 +00:00
|
|
|
|
2022-09-16 13:04:04 +00:00
|
|
|
def replace_natural_gas_fueltype(df):
|
|
|
|
return df.Fueltype.where(df.Fueltype != "Natural Gas", df.Technology)
|
2022-04-05 13:12:01 +00:00
|
|
|
|
|
|
|
|
2019-02-05 22:00:35 +00:00
|
|
|
if __name__ == "__main__":
|
2022-09-16 13:04:04 +00:00
|
|
|
if "snakemake" not in globals():
|
2019-12-09 20:29:15 +00:00
|
|
|
from _helpers import mock_snakemake
|
2022-09-16 13:04:04 +00:00
|
|
|
|
|
|
|
snakemake = mock_snakemake("build_powerplants")
|
2019-11-28 07:22:52 +00:00
|
|
|
configure_logging(snakemake)
|
2022-01-14 10:05:15 +00:00
|
|
|
|
2022-01-24 18:48:26 +00:00
|
|
|
n = pypsa.Network(snakemake.input.base_network)
|
2023-06-15 16:52:25 +00:00
|
|
|
countries = snakemake.params.countries
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2022-09-16 13:04:04 +00:00
|
|
|
ppl = (
|
|
|
|
pm.powerplants(from_url=True)
|
|
|
|
.powerplant.fill_missing_decommissioning_years()
|
|
|
|
.powerplant.convert_country_to_alpha2()
|
|
|
|
.query('Fueltype not in ["Solar", "Wind"] and Country in @countries')
|
|
|
|
.assign(Technology=replace_natural_gas_technology)
|
|
|
|
.assign(Fueltype=replace_natural_gas_fueltype)
|
|
|
|
)
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2022-04-08 10:23:24 +00:00
|
|
|
# Correct bioenergy for countries where possible
|
|
|
|
opsd = pm.data.OPSD_VRE().powerplant.convert_country_to_alpha2()
|
|
|
|
opsd = opsd.query('Country in @countries and Fueltype == "Bioenergy"')
|
2022-09-16 13:04:04 +00:00
|
|
|
opsd["Name"] = "Biomass"
|
2022-04-08 10:23:24 +00:00
|
|
|
available_countries = opsd.Country.unique()
|
2022-09-16 13:04:04 +00:00
|
|
|
ppl = ppl.query('not (Country in @available_countries and Fueltype == "Bioenergy")')
|
2022-04-08 10:23:24 +00:00
|
|
|
ppl = pd.concat([ppl, opsd])
|
2022-09-16 13:04:04 +00:00
|
|
|
|
2023-06-15 16:52:25 +00:00
|
|
|
ppl_query = snakemake.params.powerplants_filter
|
2019-10-31 13:48:10 +00:00
|
|
|
if isinstance(ppl_query, str):
|
|
|
|
ppl.query(ppl_query, inplace=True)
|
2018-07-10 14:29:11 +00:00
|
|
|
|
2021-09-14 14:34:02 +00:00
|
|
|
# add carriers from own powerplant files:
|
2023-06-15 16:52:25 +00:00
|
|
|
custom_ppl_query = snakemake.params.custom_powerplants
|
2022-09-16 13:04:04 +00:00
|
|
|
ppl = add_custom_powerplants(
|
|
|
|
ppl, snakemake.input.custom_powerplants, custom_ppl_query
|
|
|
|
)
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2022-09-16 13:04:04 +00:00
|
|
|
countries_wo_ppl = set(countries) - set(ppl.Country.unique())
|
2022-04-08 13:25:05 +00:00
|
|
|
if countries_wo_ppl:
|
|
|
|
logging.warning(f"No powerplants known in: {', '.join(countries_wo_ppl)}")
|
2018-11-12 20:40:05 +00:00
|
|
|
|
2022-09-16 13:04:04 +00:00
|
|
|
substations = n.buses.query("substation_lv")
|
2022-04-08 13:25:05 +00:00
|
|
|
ppl = map_country_bus(ppl, substations)
|
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():
|
2022-09-16 13:04:04 +00:00
|
|
|
logging.warning(
|
|
|
|
f"Couldn't find close bus for {bus_null_b.sum()} powerplants. "
|
|
|
|
"Removing them from the powerplants list."
|
|
|
|
)
|
2022-04-08 12:34:52 +00:00
|
|
|
ppl = ppl[~bus_null_b]
|
2018-02-10 16:16:20 +00:00
|
|
|
|
2022-09-16 13:04:04 +00:00
|
|
|
# TODO: This has to fixed in PPM, some powerplants are still duplicated
|
|
|
|
cumcount = ppl.groupby(["bus", "Fueltype"]).cumcount() + 1
|
2022-04-08 12:23:33 +00:00
|
|
|
ppl.Name = ppl.Name.where(cumcount == 1, ppl.Name + " " + cumcount.astype(str))
|
|
|
|
|
2022-04-08 13:25:05 +00:00
|
|
|
ppl.reset_index(drop=True).to_csv(snakemake.output[0])
|