generated from rl-institut/rli_template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
194 lines (156 loc) · 5.84 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import pandas as pd
import base64
from oemof.tools.economics import annuity
from openpyxl import load_workbook
def read_input_file(filename):
"""Parse a .xlsx input file"""
wb = load_workbook(filename=filename)
sheet_names = wb.sheetnames
for sn in ("costs", "timeseries", "settings"):
if sn not in sheet_names:
raise ValueError(
f"The sheet '{sn}' is missing in your input file {filename}"
)
name = "costs"
headers = [c.value for c in wb[name][1]]
df = pd.DataFrame(tuple(wb[name].values)[2:], columns=headers)
# drop the lines which do not define a new asset
df = df.loc[df.asset.notna()]
# drop the columns with no headers
df_costs = df[df.columns.dropna()].set_index("asset").fillna(0)
for col_name in (
"asset",
"capex_variable",
"opex_variable",
"lifetime",
"co2_emissions",
"density",
"energy_density",
"volumetric_cost",
):
if col_name not in headers:
raise ValueError(
f"The column header '{col_name}' is missing in your input file {filename} under the '{name}' sheet"
)
name = "timeseries"
headers = [c.value for c in wb[name][1]]
for hd in ("CriticalDemand", "Demand", "SolarGen"):
if hd not in headers:
raise ValueError(
f"The column header '{hd}' is missing in your input file {filename} under the '{name}' sheet"
)
df_timeseries = pd.DataFrame(tuple(wb[name].values)[1:], columns=headers)
name = "settings"
headers = [c.value for c in wb[name][1]]
for hd in ("param", "value"):
if hd not in headers:
raise ValueError(
f"The column header '{hd}' is missing in your input file {filename} under the '{name}' sheet"
)
df = pd.DataFrame(tuple(wb[name].values)[1:], columns=headers)
df = df[df.columns.dropna()].set_index("param")
df_settings = df["value"]
# compute the epc needed for oemof investments if not provided
if "annuity" not in df_costs.columns:
wacc = df_settings.wacc
project_lifetime = df_costs.lifetime.project
# correspond to equation (6)
df_costs["annuity"] = df_costs.apply(
lambda x: annualized_capex(
x.capex_variable, project_lifetime, x.lifetime, wacc
)
+ x.opex_fix,
axis=1,
)
name = "sensitivity"
headers = [c.value for c in wb[name][1]]
for hd in ("category", "variable_name", "min_val", "max_val", "step"):
if hd not in headers:
raise ValueError(
f"The column header '{hd}' is missing in your input file {filename} under the '{name}' sheet"
)
df_sensitivity = pd.DataFrame(tuple(wb[name].values)[2:], columns=headers).dropna()
if df_sensitivity.empty is False:
return df_costs, df_timeseries, df_settings, df_sensitivity
else:
return (
df_costs,
df_timeseries,
df_settings,
)
def encode_image_file(img_path):
"""Encode image files to load them in the dash layout under img html tag
Parameters
----------
img_path: str
path to the image file
Returns
-------
encoded_img: bytes
encoded bytes of the image file
"""
try:
with open(img_path, "rb") as ifs:
encoded_img = base64.b64encode(ifs.read())
except FileNotFoundError:
encoded_img = base64.b64encode(bytes())
return encoded_img
def annualized_capex(
investment_t0, project_lifetime, asset_lifetime=None, wacc=0.08, tax=0
):
"""Return output of capex_from_investment annualised"""
capex = capex_from_investment(
investment_t0, project_lifetime, asset_lifetime, wacc, tax
)
return annuity(capex, project_lifetime, wacc)
def capex_from_investment(
investment_t0, project_lifetime, asset_lifetime=None, wacc=0.09, tax=0
):
"""
Parameters
----------
investment_t0: float
Specific investment in year 0
project_lifetime: int
Project lifetime in years
asset_lifetime: int
Asset lifetime in years
wacc: float
Discount factor
tax: float
Tax factor
Returns
-------
Capex of the asset
"""
if asset_lifetime is None:
asset_lifetime = project_lifetime
# [quantity, investment, installation, weight, lifetime, om, first_investment]
if project_lifetime == asset_lifetime:
number_of_investments = 1
else:
number_of_investments = int(round(project_lifetime / asset_lifetime + 0.5))
# costs with quantity and import tax at t=0
first_time_investment = investment_t0 * (1 + tax)
for count_of_replacements in range(0, number_of_investments):
# Very first investment is in year 0
if count_of_replacements == 0:
capex = first_time_investment
else:
# replacements taking place in year = number_of_replacement * asset_lifetime
if count_of_replacements * asset_lifetime != project_lifetime:
capex = capex + first_time_investment / (
(1 + wacc) ** (count_of_replacements * asset_lifetime)
)
# # Substraction of component value at end of life with last replacement (= number_of_investments - 1)
#if number_of_investments * asset_lifetime > project_lifetime:
#last_investment = first_time_investment / (
# (1 + wacc) ** ((number_of_investments - 1) * asset_lifetime)
#)
#linear_depreciation_last_investment = last_investment / asset_lifetime
#capex = capex - linear_depreciation_last_investment * (
# number_of_investments * asset_lifetime - project_lifetime
#) / ((1 + wacc) ** (project_lifetime))
return capex
#import ipdb;
#ipdb.set_trace()