Skip to content

Commit

Permalink
Merge 9e61ead into 3b218c3
Browse files Browse the repository at this point in the history
  • Loading branch information
uvchik committed Oct 20, 2020
2 parents 3b218c3 + 9e61ead commit 70a17e7
Show file tree
Hide file tree
Showing 14 changed files with 1,176 additions and 53 deletions.
36 changes: 0 additions & 36 deletions .appveyor.yml
Expand Up @@ -10,42 +10,6 @@ environment:
PYTHON_HOME: C:\Python36
PYTHON_VERSION: '3.6'
PYTHON_ARCH: '32'
- TOXENV: py36-nocov
TOXPYTHON: C:\Python36\python.exe
PYTHON_HOME: C:\Python36
PYTHON_VERSION: '3.6'
PYTHON_ARCH: '32'
WHEEL_PATH: .tox/dist
- TOXENV: py36-nocov
TOXPYTHON: C:\Python36-x64\python.exe
PYTHON_HOME: C:\Python36-x64
PYTHON_VERSION: '3.6'
PYTHON_ARCH: '64'
WHEEL_PATH: .tox/dist
- TOXENV: py37-nocov
TOXPYTHON: C:\Python37\python.exe
PYTHON_HOME: C:\Python37
PYTHON_VERSION: '3.7'
PYTHON_ARCH: '32'
WHEEL_PATH: .tox/dist
- TOXENV: py37-nocov
TOXPYTHON: C:\Python37-x64\python.exe
PYTHON_HOME: C:\Python37-x64
PYTHON_VERSION: '3.7'
PYTHON_ARCH: '64'
WHEEL_PATH: .tox/dist
- TOXENV: py38-nocov
TOXPYTHON: C:\Python38\python.exe
PYTHON_HOME: C:\Python38
PYTHON_VERSION: '3.8'
PYTHON_ARCH: '32'
WHEEL_PATH: .tox/dist
- TOXENV: py38-nocov
TOXPYTHON: C:\Python38-x64\python.exe
PYTHON_HOME: C:\Python38-x64
PYTHON_VERSION: '3.8'
PYTHON_ARCH: '64'
WHEEL_PATH: .tox/dist
init:
- ps: echo $env:TOXENV
- ps: ls C:\Python*
Expand Down
20 changes: 9 additions & 11 deletions .travis.yml
@@ -1,34 +1,31 @@
language: python
dist: xenial
cache: false

addons:
apt:
packages:
- libspatialindex-dev

env:
global:
- LD_PRELOAD=/lib/x86_64-linux-gnu/libSegFault.so
- SEGFAULT_SIGNALS=all
- LANG=en_US.UTF-8
matrix:
include:
- python: '3.6'
- python: '3.8'
env:
- TOXENV=check
- python: '3.6'
- python: '3.8'
env:
- TOXENV=docs
- env:
- TOXENV=py36-cover,codecov,coveralls
python: '3.6'
- env:
- TOXENV=py36-nocov
python: '3.6'
- env:
- TOXENV=py37-cover,codecov,coveralls
python: '3.7'
- env:
- TOXENV=py37-nocov
python: '3.7'
- env:
- TOXENV=py38-cover,codecov,coveralls
python: '3.8'
- env:
- TOXENV=py38-nocov
python: '3.8'
Expand All @@ -51,3 +48,4 @@ notifications:
email:
on_success: never
on_failure: always

4 changes: 4 additions & 0 deletions docs/requirements.txt
@@ -1,2 +1,6 @@
sphinx>=1.3
sphinx-rtd-theme
reegis
pandas

setuptools
5 changes: 2 additions & 3 deletions setup.py
@@ -1,7 +1,5 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function

import io
import re
Expand Down Expand Up @@ -69,7 +67,8 @@ def read(*names, **kwargs):
],
python_requires=">=3.6",
install_requires=[
# eg: 'aspectlib==1.1.1', 'six>=1.7',
"pandas",
"reegis@https://github.com/reegis/reegis/archive/master.zip",
],
extras_require={
# eg:
Expand Down
124 changes: 124 additions & 0 deletions src/scenario_builder/commodity.py
@@ -0,0 +1,124 @@
"""Create a basic scenario from the internal data structure.
SPDX-FileCopyrightText: 2016-2019 Uwe Krien <krien@uni-bremen.de>
SPDX-License-Identifier: MIT
"""

import logging
from warnings import warn

import pandas as pd
from reegis import commodity_sources

from scenario_builder import config as cfg
from scenario_builder import data


def scenario_commodity_sources(year):
"""
Parameters
----------
year
Returns
-------
Examples
--------
>>> from reegis import geometries
>>> from scenario_builder import powerplants
>>> fs=geometries.get_federal_states_polygon() # doctest: +SKIP
>>> pp=powerplants.scenario_powerplants(dict(), fs, 2014, "federal_states"
... ) # doctest: +SKIP
>>> src=scenario_commodity_sources(pp) # doctest: +SKIP
>>> round(src.loc[("DE", "hard coal"), "costs"], 2) # doctest: +SKIP
12.53
>>> round(src.loc[("DE", "natural gas"), "emission"], 2) # doctest: +SKIP
201.0
"""
if cfg.get("basic", "costs_source") == "reegis":
commodity_src = create_commodity_sources_reegis(year)
elif cfg.get("basic", "costs_source") == "ewi":
commodity_src = create_commodity_sources_ewi()
else:
commodity_src = None

commodity_src = commodity_src.transpose()

# Add region level to be consistent to other tables
commodity_src.columns = pd.MultiIndex.from_product(
[["DE"], commodity_src.columns]
)

return commodity_src.transpose()


def create_commodity_sources_ewi():
"""
Returns
-------
"""
ewi = data.get_ewi_data()
df = pd.DataFrame()
df["costs"] = ewi.fuel_costs["value"] + ewi.transport_costs["value"]
df["emission"] = ewi.emission["value"].multiply(1000)
df["co2_price"] = float(ewi.co2_price["value"])
missing = "bioenergy"
msg = (
"Costs/Emission for {0} in ewi is missing.\n"
"Values for {0} are hard coded! Use with care."
)
warn(msg.format(missing), UserWarning)
df.loc[missing, "emission"] = 7.2
df.loc[missing, "costs"] = 20
df.loc[missing, "co2_price"] = df.loc["natural gas", "co2_price"]
return df


def create_commodity_sources_reegis(year, use_znes_2014=True):
"""
Parameters
----------
year
use_znes_2014
Returns
-------
"""
msg = (
"The unit for {0} of the source is '{1}'. "
"Will multiply it with {2} to get '{3}'."
)

converter = {
"costs": ["costs", "EUR/J", 1e9 * 3.6, "EUR/MWh"],
"emission": ["emission", "g/J", 1e6 * 3.6, "kg/MWh"],
}

cs = commodity_sources.get_commodity_sources()
rename_cols = {
key.lower(): value
for key, value in cfg.get_dict("source_names").items()
}
cs = cs.rename(columns=rename_cols)
cs_year = cs.loc[year]
if use_znes_2014:
before = len(cs_year[cs_year.isnull()])
cs_year = cs_year.fillna(cs.loc[2014])
after = len(cs_year[cs_year.isnull()])
if before - after > 0:
logging.warning("Values were replaced with znes2014 data.")
cs_year = cs_year.sort_index().unstack()

# convert units
for key in converter.keys():
cs_year[key] = cs_year[key].multiply(converter[key][2])
logging.warning(msg.format(*converter[key]))

return cs_year
18 changes: 18 additions & 0 deletions src/scenario_builder/config.py
@@ -0,0 +1,18 @@
"""Create a basic scenario from the internal data structure.
SPDX-FileCopyrightText: 2016-2019 Uwe Krien <krien@uni-bremen.de>
SPDX-License-Identifier: MIT
"""

import os

from reegis.config import get # noqa: F401
from reegis.config import get_dict # noqa: F401
from reegis.config import get_dict_list # noqa: F401
from reegis.config import init # noqa: F401

_loaded = False
if not _loaded:
init(paths=[os.path.dirname(__file__)])
_loaded = True
79 changes: 79 additions & 0 deletions src/scenario_builder/data.py
@@ -0,0 +1,79 @@
"""Create a basic scenario from the internal data structure.
SPDX-FileCopyrightText: 2016-2019 Uwe Krien <krien@uni-bremen.de>
SPDX-License-Identifier: MIT
"""

import os
from types import SimpleNamespace

import pandas as pd
from reegis.tools import download_file

from scenario_builder import config as cfg

TRANSLATION_FUEL = {
"Abfall": "waste",
"Kernenergie": "nuclear",
"Braunkohle": "lignite",
"Steinkohle": "hard coal",
"Erdgas": "natural gas",
"GuD": "natural gas",
"Gasturbine": "natural gas",
"Öl": "oil",
"Sonstige": "other",
"Emissionszertifikatspreis": "co2_price",
}


def get_ewi_data():
"""
Returns
-------
namedtuple
Examples
--------
>>> my_ewi_data = get_ewi_data()
>>> round(my_ewi_data.fuel_costs.loc["hard coal", "value"], 2)
11.28
"""
# Download file
url = (
"https://www.ewi.uni-koeln.de/cms/wp-content/uploads/2019/12"
"/EWI_Merit_Order_Tool_2019_1_4.xlsm"
)
fn = os.path.join(cfg.get("paths", "general"), "ewi.xlsm")
download_file(fn, url)

# Create named tuple with all sub tables
ewi_tables = {
"fuel_costs": {"skiprows": 7, "usecols": "C:F", "nrows": 7},
"transport_costs": {"skiprows": 21, "usecols": "C:F", "nrows": 7},
"variable_costs": {"skiprows": 31, "usecols": "C:F", "nrows": 8},
"downtime_factor": {
"skiprows": 31,
"usecols": "H:K",
"nrows": 8,
"scale": 0.01,
},
"emission": {"skiprows": 31, "usecols": "M:P", "nrows": 7},
"co2_price": {"skiprows": 17, "usecols": "C:F", "nrows": 1},
}
ewi_data = {}
cols = ["fuel", "value", "unit", "source"]
xls = pd.ExcelFile(fn)
for table in ewi_tables.keys():
tmp = xls.parse("Start", header=[0], **ewi_tables[table]).replace(
TRANSLATION_FUEL
)
tmp.drop_duplicates(tmp.columns[0], keep="first", inplace=True)
tmp.columns = cols
ewi_data[table] = tmp.set_index("fuel")
if "scale" in ewi_tables[table]:
ewi_data[table]["value"] *= ewi_tables[table]["scale"]

return SimpleNamespace(**ewi_data)

0 comments on commit 70a17e7

Please sign in to comment.