2018-01-29 21:28:33 +00:00
|
|
|
#!/usr/bin/env python
|
2022-09-16 13:04:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-05-29 07:50:55 +00:00
|
|
|
|
2024-02-19 15:21:48 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors
|
2020-05-29 07:50:55 +00:00
|
|
|
#
|
2021-09-14 14:37:41 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
2019-08-08 13:02:28 +00:00
|
|
|
"""
|
2019-08-11 20:34:18 +00:00
|
|
|
Build hydroelectric inflow time-series for each country.
|
2019-08-08 13:02:28 +00:00
|
|
|
|
2019-08-11 09:40:47 +00:00
|
|
|
Relevant Settings
|
|
|
|
-----------------
|
|
|
|
|
2019-08-11 11:17:36 +00:00
|
|
|
.. code:: yaml
|
|
|
|
|
|
|
|
countries:
|
|
|
|
|
|
|
|
renewable:
|
|
|
|
hydro:
|
|
|
|
cutout:
|
|
|
|
clip_min_inflow:
|
|
|
|
|
2019-11-14 16:50:24 +00:00
|
|
|
.. seealso::
|
2023-04-21 08:41:44 +00:00
|
|
|
Documentation of the configuration file ``config/config.yaml`` at
|
2019-08-13 08:03:46 +00:00
|
|
|
:ref:`toplevel_cf`, :ref:`renewable_cf`
|
|
|
|
|
2019-08-11 09:40:47 +00:00
|
|
|
Inputs
|
|
|
|
------
|
|
|
|
|
2023-12-11 17:24:57 +00:00
|
|
|
- ``data/bundle/eia_hydro_annual_generation.csv``: Hydroelectricity net generation per country and year (`EIA <https://www.eia.gov/beta/international/data/browser/#/?pa=000000000000000000000000000000g&c=1028i008006gg6168g80a4k000e0ag00gg0004g800ho00g8&ct=0&ug=8&tl_id=2-A&vs=INTL.33-12-ALB-BKWH.A&cy=2014&vo=0&v=H&start=2000&end=2016>`_)
|
2019-08-12 17:01:53 +00:00
|
|
|
|
2023-03-09 12:28:42 +00:00
|
|
|
.. image:: img/hydrogeneration.png
|
2019-08-12 17:01:53 +00:00
|
|
|
:scale: 33 %
|
|
|
|
|
2019-08-11 20:34:18 +00:00
|
|
|
- ``resources/country_shapes.geojson``: confer :ref:`shapes`
|
|
|
|
- ``"cutouts/" + config["renewable"]['hydro']['cutout']``: confer :ref:`cutout`
|
|
|
|
|
2019-08-11 09:40:47 +00:00
|
|
|
Outputs
|
|
|
|
-------
|
|
|
|
|
2019-08-11 20:34:18 +00:00
|
|
|
- ``resources/profile_hydro.nc``:
|
|
|
|
|
2019-08-12 21:48:16 +00:00
|
|
|
=================== ================ =========================================================
|
|
|
|
Field Dimensions Description
|
|
|
|
=================== ================ =========================================================
|
|
|
|
inflow countries, time Inflow to the state of charge (in MW),
|
|
|
|
e.g. due to river inflow in hydro reservoir.
|
|
|
|
=================== ================ =========================================================
|
|
|
|
|
2023-03-09 12:28:42 +00:00
|
|
|
.. image:: img/inflow-ts.png
|
2019-08-13 13:48:21 +00:00
|
|
|
:scale: 33 %
|
2019-11-14 16:50:24 +00:00
|
|
|
|
2023-03-09 12:28:42 +00:00
|
|
|
.. image:: img/inflow-box.png
|
2019-08-13 13:48:21 +00:00
|
|
|
:scale: 33 %
|
|
|
|
|
2019-08-11 09:40:47 +00:00
|
|
|
Description
|
|
|
|
-----------
|
|
|
|
|
|
|
|
.. seealso::
|
2019-08-12 17:01:53 +00:00
|
|
|
:mod:`build_renewable_profiles`
|
2019-08-08 13:02:28 +00:00
|
|
|
"""
|
2018-01-29 21:28:33 +00:00
|
|
|
|
2019-11-28 07:22:52 +00:00
|
|
|
import logging
|
|
|
|
|
2018-01-29 21:28:33 +00:00
|
|
|
import atlite
|
2022-03-28 10:02:08 +00:00
|
|
|
import country_converter as coco
|
2018-08-03 09:53:14 +00:00
|
|
|
import geopandas as gpd
|
2022-03-28 10:02:08 +00:00
|
|
|
import pandas as pd
|
2024-03-14 14:15:56 +00:00
|
|
|
from _helpers import configure_logging, get_snapshots, set_scenario_config
|
2023-04-29 10:41:37 +00:00
|
|
|
from numpy.polynomial import Polynomial
|
2022-07-22 13:03:54 +00:00
|
|
|
|
2022-03-28 10:02:08 +00:00
|
|
|
cc = coco.CountryConverter()
|
|
|
|
|
|
|
|
|
2022-07-22 13:03:54 +00:00
|
|
|
def get_eia_annual_hydro_generation(fn, countries, capacities=False):
|
2022-03-28 10:02:08 +00:00
|
|
|
# in billion kWh/a = TWh/a
|
2024-03-14 14:15:56 +00:00
|
|
|
df = pd.read_csv(fn, skiprows=2, index_col=1, na_values=[" ", "--"]).iloc[1:, 1:]
|
2022-03-28 10:02:08 +00:00
|
|
|
df.index = df.index.str.strip()
|
2024-03-14 13:08:58 +00:00
|
|
|
df.columns = df.columns.astype(int)
|
2022-03-28 10:02:08 +00:00
|
|
|
|
2022-07-20 11:01:14 +00:00
|
|
|
former_countries = {
|
|
|
|
"Former Czechoslovakia": dict(
|
2023-01-06 20:51:33 +00:00
|
|
|
countries=["Czechia", "Slovakia"], start=1980, end=1992
|
2022-07-20 11:01:14 +00:00
|
|
|
),
|
|
|
|
"Former Serbia and Montenegro": dict(
|
2024-08-30 13:36:03 +00:00
|
|
|
countries=["Serbia", "Montenegro", "Kosovo"], start=1992, end=2005
|
2022-07-20 11:01:14 +00:00
|
|
|
),
|
|
|
|
"Former Yugoslavia": dict(
|
|
|
|
countries=[
|
|
|
|
"Slovenia",
|
|
|
|
"Croatia",
|
|
|
|
"Bosnia and Herzegovina",
|
|
|
|
"Serbia",
|
2024-08-30 13:36:03 +00:00
|
|
|
"Kosovo",
|
2022-07-20 11:01:14 +00:00
|
|
|
"Montenegro",
|
|
|
|
"North Macedonia",
|
|
|
|
],
|
|
|
|
start=1980,
|
|
|
|
end=1991,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v in former_countries.items():
|
2024-03-14 13:08:58 +00:00
|
|
|
period = [i for i in range(v["start"], v["end"] + 1)]
|
2022-07-20 11:01:14 +00:00
|
|
|
ratio = df.loc[v["countries"]].T.dropna().sum()
|
|
|
|
ratio /= ratio.sum()
|
|
|
|
for country in v["countries"]:
|
|
|
|
df.loc[country, period] = df.loc[k, period] * ratio[country]
|
|
|
|
|
|
|
|
baltic_states = ["Latvia", "Estonia", "Lithuania"]
|
|
|
|
df.loc[baltic_states] = (
|
|
|
|
df.loc[baltic_states].T.fillna(df.loc[baltic_states].mean(axis=1)).T
|
2022-09-16 13:04:04 +00:00
|
|
|
)
|
2022-07-20 11:01:14 +00:00
|
|
|
|
2022-03-28 10:02:08 +00:00
|
|
|
df.loc["Germany"] = df.filter(like="Germany", axis=0).sum()
|
2022-06-23 19:19:41 +00:00
|
|
|
df = df.loc[~df.index.str.contains("Former")]
|
2024-08-30 13:36:03 +00:00
|
|
|
df.drop(["Europe", "Germany, West", "Germany, East"], inplace=True)
|
2022-03-28 10:02:08 +00:00
|
|
|
|
|
|
|
df.index = cc.convert(df.index, to="iso2")
|
|
|
|
df.index.name = "countries"
|
|
|
|
|
2022-07-22 13:03:54 +00:00
|
|
|
# convert to MW of MWh/a
|
|
|
|
factor = 1e3 if capacities else 1e6
|
|
|
|
df = df.T[countries] * factor
|
2022-03-28 10:02:08 +00:00
|
|
|
|
2024-08-30 13:36:03 +00:00
|
|
|
df.ffill(axis=0, inplace=True)
|
|
|
|
|
2022-03-28 10:02:08 +00:00
|
|
|
return df
|
|
|
|
|
2018-01-29 21:28:33 +00:00
|
|
|
|
2022-07-22 13:03:54 +00:00
|
|
|
def correct_eia_stats_by_capacity(eia_stats, fn, countries, baseyear=2019):
|
|
|
|
cap = get_eia_annual_hydro_generation(fn, countries, capacities=True)
|
2024-03-14 13:08:58 +00:00
|
|
|
ratio = cap / cap.loc[baseyear]
|
2022-07-22 13:03:54 +00:00
|
|
|
eia_stats_corrected = eia_stats / ratio
|
2024-08-30 13:36:03 +00:00
|
|
|
to_keep = ["AL", "AT", "CH", "DE", "GB", "NL", "RS", "XK", "RO", "SK"]
|
2022-07-22 13:03:54 +00:00
|
|
|
to_correct = eia_stats_corrected.columns.difference(to_keep)
|
2023-04-29 10:41:37 +00:00
|
|
|
eia_stats.loc[:, to_correct] = eia_stats_corrected.loc[:, to_correct]
|
2022-07-22 13:03:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def approximate_missing_eia_stats(eia_stats, runoff_fn, countries):
|
|
|
|
runoff = pd.read_csv(runoff_fn, index_col=0).T[countries]
|
2024-03-14 13:08:58 +00:00
|
|
|
runoff.index = runoff.index.astype(int)
|
2022-07-22 13:03:54 +00:00
|
|
|
|
2024-03-14 13:08:58 +00:00
|
|
|
# fix outliers; exceptional floods in 1977-1979 in ES & PT
|
2024-05-06 16:46:04 +00:00
|
|
|
if "ES" in runoff:
|
2024-05-07 08:53:07 +00:00
|
|
|
runoff.loc[1978, "ES"] = runoff.loc[1979, "ES"]
|
2024-05-06 16:46:04 +00:00
|
|
|
if "PT" in runoff:
|
2024-05-07 08:53:07 +00:00
|
|
|
runoff.loc[1978, "PT"] = runoff.loc[1979, "PT"]
|
2022-07-22 13:03:54 +00:00
|
|
|
|
|
|
|
runoff_eia = runoff.loc[eia_stats.index]
|
|
|
|
|
|
|
|
eia_stats_approximated = {}
|
|
|
|
|
|
|
|
for c in countries:
|
2022-07-25 09:52:51 +00:00
|
|
|
X = runoff_eia[c]
|
|
|
|
Y = eia_stats[c]
|
2022-07-22 13:03:54 +00:00
|
|
|
|
|
|
|
to_predict = runoff.index.difference(eia_stats.index)
|
2022-07-25 09:52:51 +00:00
|
|
|
X_pred = runoff.loc[to_predict, c]
|
2022-07-22 13:03:54 +00:00
|
|
|
|
2022-07-25 09:52:51 +00:00
|
|
|
p = Polynomial.fit(X, Y, 1)
|
|
|
|
Y_pred = p(X_pred)
|
2022-07-22 13:03:54 +00:00
|
|
|
|
2022-07-25 09:52:51 +00:00
|
|
|
eia_stats_approximated[c] = pd.Series(Y_pred, index=to_predict)
|
2022-07-22 13:03:54 +00:00
|
|
|
|
|
|
|
eia_stats_approximated = pd.DataFrame(eia_stats_approximated)
|
|
|
|
return pd.concat([eia_stats, eia_stats_approximated]).sort_index()
|
|
|
|
|
|
|
|
|
2020-09-11 10:40:53 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-08-08 13:02:28 +00:00
|
|
|
if __name__ == "__main__":
|
2019-12-09 20:29:15 +00:00
|
|
|
if "snakemake" not in globals():
|
|
|
|
from _helpers import mock_snakemake
|
2023-04-29 10:41:37 +00:00
|
|
|
|
2024-03-04 17:24:01 +00:00
|
|
|
snakemake = mock_snakemake("build_hydro_profile")
|
2019-11-28 07:22:52 +00:00
|
|
|
configure_logging(snakemake)
|
2023-08-15 13:02:41 +00:00
|
|
|
set_scenario_config(snakemake)
|
2019-12-09 20:29:15 +00:00
|
|
|
|
2023-06-15 16:52:25 +00:00
|
|
|
params_hydro = snakemake.params.hydro
|
2024-03-14 13:30:50 +00:00
|
|
|
|
2024-03-14 14:15:56 +00:00
|
|
|
time = get_snapshots(snakemake.params.snapshots, snakemake.params.drop_leap_day)
|
2024-03-14 13:30:50 +00:00
|
|
|
|
|
|
|
cutout = atlite.Cutout(snakemake.input.cutout).sel(time=time)
|
2019-08-08 13:02:28 +00:00
|
|
|
|
2023-06-15 16:52:25 +00:00
|
|
|
countries = snakemake.params.countries
|
2022-01-24 18:48:26 +00:00
|
|
|
country_shapes = (
|
|
|
|
gpd.read_file(snakemake.input.country_shapes)
|
Atlite availability (#224)
* adjust buil_cutout.py and Snakefile
* try adjusting build_renewable_profiles, currently crashing due to weird pyproj error
* build_renewable_profiles: -remove printing gid
* build_renewable_profiles: use dask for paralellization, use dense functions
* build_renewable_profiles:
- revise imports
- add logging for long calculation
- revise explaining comment
- revise distance calculation
* build profiles: adjust to cutout.grid
* * fix area to square km
* rename potmatrix -> capacity_potential
* rename available to availibility
* config.default update cutout params
build_renewable_potentials: major refactoring and simplification
hydro_profiles: update code
* build profiles: fix weight output dimensions
* build profiles: fix typo, fix selection of buses
* build profiles: reinsert paths variable
* follow up
* build profiles: move to dask calculation only
* CI: set build cutout to true (add CDSAPI)
* build profiles: use pyproj, test with gleas and geokit upstream
* environment.yaml fix atlite version
* build profiles: use dask 'processes' for more than 25 regions
* build profiles: specify dask scheduler according to number of regions
* backpedal a bit, only allow scheduler='processes'
* follow up, code style and fixup
* build profiles: add logger info for underwater fraction calc
* config adjust cutout parameters
Snakefile fixup
* config.default.yaml: adjust resolution
* config: use one cutout in total
build_cutout: automatic detetection of geographical boundaries
* env: add python>=3.8 requirement
build_cutout: fixup for region bound
* config: allow base cutout
* folllow up, fix up
* follow up II
* clean up
* clean up II
* build profiles: move back to multiprocessing due to performance issues
* small code style corrections
* move in pool context
* swqitch to ratsterio
* switch to rasterio for availibility calculation
* tiny fixup
* * build continental raster for offshore distance calculation
* adjust Snakefile to new script build_raster
* rename continental raster to onshore raster
add projected_mask function (not yet tested)
add docstrings, modularize
* Snakefile: remove build_onhore_raster rule, build mask directly from geometry instead
build_natura_raster: adjust code, add function for exporting
build_profiles:
* add buffer to shore distance to init_globals function
* update docstrings
* improve handling of nodata grid codes
* add geometry mask if natura raster not activated
(the 255 value is an 'eligible' value for the corine data base,
do this for excluding data outside the shape)
* build_profiles: adjust docstrings
* update environment
* build profiles: fixup reproject woth padding
* follow up, small fixups
* fix resampling method
checkpoint: reproduces solar profile in tut data
* reintegrate plot map
code style
* config: rename cutout into "base"
* build profiles: adjust to new atlite code
* natura raster: small fixup
* build natura raster: compress tiff file
* config: adjust cutout names
* build profiles: cover case if no or partial overlap between natura raster and cutout
* config-tutorial: adjust cutout params
* buid-profifiles: fixup in gebco filter
* follow up
* update config files
* build profiles: select layoutmatrix != 0
* build profiles: speed up average_distance and underwaterfraction
* build profiles: fix typo
* update release notes
build_cutout: only build needed features
* update envs
* config: add temperature to sarah features
* temporary fix for atlite v0.2.1 and new xarray version release
* env: remove xarray specification
* * remove rule build_country_flh
* build profiles: remove sneaked in line
* doc: update configuration.rst (section atlite) and corresponding csv table
* release notes: fix quotes
* build profiles: use 3035 for area calculation
* Update envs/environment.docs.yaml
* Update scripts/build_cutout.py
* Update doc/release_notes.rst
Co-authored-by: euronion <42553970+euronion@users.noreply.github.com>
* Update doc/configuration.rst
Co-authored-by: euronion <42553970+euronion@users.noreply.github.com>
* Update scripts/build_cutout.py
Co-authored-by: euronion <42553970+euronion@users.noreply.github.com>
* update release notes
* release notes: add deprecation of 'keep_all_available_areas'
build profiles: remove warning for 'keep_all_available_areas'
* build cutout: rearrage code, set buffer correctly
* Rename tutorial cutout to remove name clash with real cutout.
* Update release_notes.rst: Rename tutorial cutout.
* retrieve: update cutouts and downloads (alternative) (#237)
* retrieve: update cutouts and downloads
* retrieve: remove unnecessary import
* use snakemake remote file functionality
* Snakefile: update zenodo link
* update natura remote link (closes #234)
* env: update atlite version to 0.2.2
* env: fix dask version due to memory issues
* test: retrieve cutout instead of build
* test: use tutorial cutout for CI
Co-authored-by: euronion <42553970+euronion@users.noreply.github.com>
Co-authored-by: Fabian Neumann <fabian.neumann@outlook.de>
2021-04-27 15:58:31 +00:00
|
|
|
.set_index("name")["geometry"]
|
|
|
|
.reindex(countries)
|
2022-09-16 13:04:04 +00:00
|
|
|
)
|
2019-08-08 13:02:28 +00:00
|
|
|
country_shapes.index.name = "countries"
|
2018-01-29 21:28:33 +00:00
|
|
|
|
2022-03-28 10:02:08 +00:00
|
|
|
fn = snakemake.input.eia_hydro_generation
|
|
|
|
eia_stats = get_eia_annual_hydro_generation(fn, countries)
|
2024-02-29 10:38:21 +00:00
|
|
|
|
|
|
|
config_hydro = snakemake.config["renewable"]["hydro"]
|
2020-10-20 11:53:43 +00:00
|
|
|
|
2023-04-29 10:41:37 +00:00
|
|
|
if config_hydro.get("eia_correct_by_capacity"):
|
2022-07-22 13:03:54 +00:00
|
|
|
fn = snakemake.input.eia_hydro_capacity
|
|
|
|
correct_eia_stats_by_capacity(eia_stats, fn, countries)
|
|
|
|
|
2023-04-29 10:41:37 +00:00
|
|
|
if config_hydro.get("eia_approximate_missing"):
|
2022-07-22 13:03:54 +00:00
|
|
|
fn = snakemake.input.era5_runoff
|
|
|
|
eia_stats = approximate_missing_eia_stats(eia_stats, fn, countries)
|
|
|
|
|
2024-03-04 17:24:01 +00:00
|
|
|
contained_years = pd.date_range(freq="YE", **snakemake.params.snapshots).year
|
2023-04-29 10:41:37 +00:00
|
|
|
norm_year = config_hydro.get("eia_norm_year")
|
2024-03-18 10:58:39 +00:00
|
|
|
missing_years = contained_years.difference(eia_stats.index)
|
2022-06-15 12:00:47 +00:00
|
|
|
if norm_year:
|
2024-03-04 17:24:01 +00:00
|
|
|
eia_stats.loc[contained_years] = eia_stats.loc[norm_year]
|
2024-03-18 10:58:39 +00:00
|
|
|
elif missing_years.any():
|
2024-03-04 17:24:01 +00:00
|
|
|
eia_stats.loc[missing_years] = eia_stats.median()
|
2023-04-29 10:40:55 +00:00
|
|
|
|
2019-08-08 13:02:28 +00:00
|
|
|
inflow = cutout.runoff(
|
|
|
|
shapes=country_shapes,
|
2020-12-03 18:50:53 +00:00
|
|
|
smooth=True,
|
|
|
|
lower_threshold_quantile=True,
|
|
|
|
normalize_using_yearly=eia_stats,
|
|
|
|
)
|
2022-09-16 13:04:04 +00:00
|
|
|
|
2023-05-17 17:25:45 +00:00
|
|
|
if "clip_min_inflow" in params_hydro:
|
|
|
|
inflow = inflow.where(inflow > params_hydro["clip_min_inflow"], 0)
|
2018-12-21 12:44:59 +00:00
|
|
|
|
2022-07-25 15:04:56 +00:00
|
|
|
inflow.to_netcdf(snakemake.output.profile)
|