-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputs.py
541 lines (468 loc) · 23.3 KB
/
inputs.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import streamlit as st
import pandas as pd
import numpy as np
import pypsa
import time
import os
import datetime
from streamlit import session_state as ss
import plotly.graph_objects as go
from P2G_case1 import network_execute
import json
network = pypsa.Network()
st.set_page_config(layout='wide', initial_sidebar_state='expanded')
st.title('Twin P2G')
# Set session_state keys = 0
count_vars = ['b1_count', 'b2_count', 'b3_count', 'b4_count', 'b5_count', 'b6_count']
for var in count_vars:
if var not in ss:
ss[var] = 0
def count(key):
ss[key] += 1
ss[key] %= 2
@st.cache_data
def convert_df(df):
return df.to_csv().encode('utf-8')
def time_convert(sec):
mins = sec // 60
sec = sec % 60
hours = mins // 60
mins = mins % 60
st.write('Time Lapsed = {0}:{1}:{2}'.format(int(hours),int(mins),sec))
def list_csv_files(folder_path):
csv_files = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.csv'):
csv_files.append(os.path.join(root, file))
return csv_files
def selected_dates(start,end):
start = pd.to_datetime(start)
end = pd.to_datetime(end)
start=start.strftime('%Y-%m-%d %H:%M:%S+00:00')
end=end.strftime('%Y-%m-%d %H:%M:%S+00:00')
return(start,end)
def validate_date_format(date_str):
try:
datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S+00:00')
except ValueError:
raise ValueError('Incorrect date format. Please use YYYY-MM-DD.')
@st.cache_data
def plotly_chart_results(df):
fig = go.Figure()
for col in df.columns:
fig.add_trace(go.Scatter(x=df.index, y=df[col], mode='lines', name=col))
return fig
@st.cache_data
def read_uploaded_csv(uploaded):
df = pd.read_csv(uploaded, delimiter=',', parse_dates=True)
return df
@st.cache_data
def read_output_csv(csv_file):
df = pd.read_csv(csv_file, index_col=0, parse_dates=True)
return df
use_case=st.selectbox('Select your use case', options=('Your use case', 'See use cases'))
if use_case=='See use cases':
# Set the default folder to 'results'
default_folder = 'results'
# Select a folder within 'results'
selected_subfolder = st.sidebar.radio('Select a subfolder', os.listdir(default_folder))
preferred_csv_file = 'inputs.csv'
st.write(f'Selected Subfolder: {selected_subfolder}')
selected_subfolder_path = os.path.join(default_folder, selected_subfolder)
csv_files = list_csv_files(selected_subfolder_path)
if csv_files:
csv_files.sort(key=lambda x: x == os.path.join(selected_subfolder_path, preferred_csv_file), reverse=True)
st.write('CSV Files in Selected Subfolder:')
for csv_file in csv_files:
st.write(f'📄{csv_file}')
else:
st.warning('No CSV files found in the selected subfolder.')
button3 = bool(ss.b3_count %2)
button5 = bool(ss.b5_count %2)
def submit_disabled():
if button5:
ss.b5_count=0
ss.b3_count=0
ss.b4_count=0
return bool(ss.b3_count % 2)
col = st.columns(6)
col[0].button('Process Selected CSV Files', key='b3', on_click=count, args=('b3_count',), type='primary', disabled=submit_disabled())
col[1].button('Stop', key='b5', on_click=count, args=('b5_count',))
ss.b1_count = 0
ss.b2_count = 0
if button5:
st.stop()
if button3==False:
ss.b4_count =0
else:
for csv_file in csv_files:
st.write(f'Processing {csv_file}...')
df=read_output_csv(csv_file)
if csv_file.endswith(('statistics.csv' , 'inputs.csv')):
st.write(df)
st.divider()
if csv_file.endswith('output_series.csv'):
tab1, tab2=st.tabs(['📈 Chart', '🗃 Data'])
tab1.subheader('Components Chart')
with tab1:
fig = plotly_chart_results(df)
st.plotly_chart(fig, use_container_width=True)
tab2.subheader('Components Data')
tab2.write(df)
csv = convert_df(df)
tab2.download_button(label='Download data', data=csv, file_name='df.csv', mime='text/csv')
st.divider()
col1, col2 = st.columns(2)
start = col1.date_input('Start')
end = col2.date_input('End')
start,end=selected_dates(start,end)
st.button('Process Selected Dates', key='b4', on_click=count, args=('b4_count',))
button4 = bool(ss.b4_count %2)
if button4:
for csv_file in csv_files:
dff=read_output_csv(csv_file)
filtered_df = dff[(dff.index >= start) & (dff.index <= end)]
if csv_file.endswith('output_series.csv'):
tab1, tab2=st.tabs(['📈 Chart', '🗃 Data'])
with tab1:
fig = plotly_chart_results(filtered_df)
st.plotly_chart(fig, use_container_width=True)
tab2.write(filtered_df)
if use_case=='Your use case':
st.text_input('Name your use case', key='use_case', placeholder='Name ie use case 1234')
use_case_name=st.session_state.use_case
if use_case_name=='':
st.error('Provide a name!')
with st.container(border=True):
st.header('Buses')
buses=st.number_input('Select your electrical network buses', min_value=1)
bus=[]
data_bus=[]
j=0
l=0
for i in range(buses):
st.subheader(f'Bus {i}')
bus.append(f'Bus {i}')
bus_carrier=st.selectbox(f'Select energy carrier for bus {i}', ('AC','DC','hydrogen','gas','heat'))
bus_data = {
'Component':'Bus',
'carrier': bus_carrier,
'bus': bus[i],
'from_bus':None,
'to_bus':None,
'p_nom':None,
'capital_cost':None,
'marginal_cost':None,
'input_series_source_type':None,
'input_series_source_uri':None,
'use_case_name':use_case_name,
'investment_period':None,
'efficiency':None,
'timestamp':None
}
data_bus.append(bus_data)
st.divider()
with st.container(border=True):
st.header('Generators')
generators=st.number_input('Select your generators', min_value=1)
data_generator=[]
for i in range(generators):
st.subheader(f'Generator {i}')
generator_bus=st.selectbox(f'Select bus for generator {i}', options=bus, key=f'generator_bus{i}')
generator_carrier=st.selectbox(f'Select generator type {i}', ('Diesel','Coal','Natural Gas', 'Hydro','Solar','Wind'))
generator_p_nom=st.number_input(f'Generator {i} nominal power (MW)', min_value=0.0, help='')
generator_capital_cost=st.number_input(f'Generator {i} capital cost (€/MW)', min_value=0.0)
generator_marginal_cost=st.number_input(f'Generator {i} marginal cost (€/MWh)', min_value=0.0)
if generator_carrier=='Solar':
res_source_type=st.radio('Select your pv production data', ['pvlib','csv file','TimescaleDB'],key=f'pv_data{i}', captions=['*PVLib is a python library that generates pv production data*', '*Path to your pv production data*'])
if res_source_type=='csv file':
j+=1
st.info("""
Upload a file in format: Datetime, GR_solar_generation. \n\r Datetime format: %Y-%m-%d %H:%M:%S+00:00 (e.g. 2018-01-01 09:00:00+00:00)\n\r GR_solar_generation must be in MW.
""", icon="ℹ️")
st.markdown(
"""
|Datetime |GR_solar_generation |
|- | -|
|2018-01-01 10:00:00 | 2.566 |
|2018-01-01 11:00:00 | 3.8 |
|2018-01-01 12:00:00 | 6.842 |
|... |... |
"""
)
uploaded = st.file_uploader('Choose a file for pv production', type='csv', key=f'res_source_type{i}')
if uploaded is not None:
df=read_uploaded_csv(uploaded)
# df = pd.read_csv(uploaded, delimiter=',', parse_dates=True)
expected_columns = ['Datetime', 'GR_solar_generation']
if not set(expected_columns).issubset(df.columns):
st.error('Upload a file in format: Datetime, GR_solar_generation')
else:
df.set_index(['Datetime'], inplace=True)
for date in df.index:
validate_date_format(date)
st.dataframe(df)
df.to_csv(f'pv_data{j}.csv', index=True, header=True)
res_source_uri=f'pv_data{j}.csv'
else:
res_source_uri=None
elif res_source_type=='pvlib':
res_source_uri=None
elif res_source_type=='TimescaleDB':
col1,col2=st.columns(2)
start_date=col1.date_input('Select start date')
end_date=col2.date_input('Select end date')
res_source_uri=f"*&and=(timestamp.gte.{start_date},timestamp.lte.{end_date})"
if generator_carrier=='Wind':
res_source_type=st.radio('Select your wind production data', ['csv file','TimescaleDB'],key=f'wind_data{i}', captions=['*Path to your pv production data*'])
if res_source_type=='csv file':
l+=1
st.info("""
Upload a file in format: Datetime, GR_wind_onshore_generation_actual. \n\r Datetime format: %Y-%m-%d %H:%M:%S+00:00 (e.g. 2018-01-01 09:00:00+00:00)\n\r GR_wind_onshore_generation_actual must be in MW.
""", icon="ℹ️")
st.markdown(
"""
|Datetime |GR_wind_onshore_generation_actual |
|- | -|
|2018-01-01 10:00:00 | 2.566 |
|2018-01-01 11:00:00 | 3.8 |
|2018-01-01 12:00:00 | 6.842 |
|... |... |
"""
)
uploaded = st.file_uploader('Choose a file for wind production', type='csv', key=f'res_wind_data{i}')
if uploaded is not None:
df=read_uploaded_csv(uploaded)
expected_columns = ['Datetime', 'GR_wind_onshore_generation_actual']
if not set(expected_columns).issubset(df.columns):
st.error('Upload a file in format: Datetime, GR_wind_onshore_generation_actual')
else:
df.set_index(['Datetime'], inplace=True)
for date in df.index:
validate_date_format(date)
st.dataframe(df)
df.to_csv(f'wind_data{l}.csv', index=True, header=True)
res_source_uri = f'wind_data{l}.csv'
else:
res_source_uri=None
elif res_source_type == 'TimescaleDB':
col1,col2=st.columns(2)
start_date=col1.date_input('Select start date')
end_date=col2.date_input('Select end date')
res_source_uri=f"*&and=(timestamp.gte.{start_date},timestamp.lte.{end_date})"
if generator_carrier not in (('Solar','Wind')):
res_source_type = None
res_source_uri=None
generator_data = {
'Component':'Generator',
'carrier': generator_carrier,
'bus':generator_bus,
'p_nom': generator_p_nom,
'capital_cost': generator_capital_cost,
'marginal_cost': generator_marginal_cost,
'input_series_source_type':res_source_type ,
'input_series_source_uri': res_source_uri,
}
data_generator.append(generator_data)
st.divider()
with st.container(border=True):
st.header('Lines')
lines=st.number_input('Select your lines', min_value=0)
line=[]
data_line=[]
for i in range(lines):
st.subheader(f'Line {i}')
line.append(f'Line {i}')
line_bus0=st.selectbox('From bus:', bus, key=f'line_bus0{i}')
line_bus1=st.selectbox('To bus:', bus, key=f'line_bus1{i}')
line_data = {
'Component':'Line',
'carrier': 'AC',
'from_bus': line_bus0,
'to_bus': line_bus1,
}
data_line.append(line_data)
st.divider()
with st.container(border=True):
st.header('Loads')
loads=st.number_input('Select your load buses', min_value=1)
data_load=[]
k=0
# investment_period=st.number_input('Investment period', 0,25,5)
for i in range(loads):
k+=1
st.subheader(f'Load {i}')
load_buses=st.selectbox('Load in bus:', bus, key=f'load_bus{i}')
load_carriers=st.selectbox(f'Select load type {i}', ('AC','Natural Gas', 'hydrogen'))
load_source_type=st.radio('Select your load data', ['csv file','TimescaleDB'],key=f'load_source_type_data{i}', captions=['*Path to your load data*'])
if load_source_type=='csv file':
st.info("""
Upload a file in format: Datetime, GR_load. \n\r Datetime format: %Y-%m-%d %H:%M:%S+00:00 (e.g. 2018-01-01 09:00:00+00:00)\n\r GR_load must be in MW.
""", icon="ℹ️")
st.markdown(
"""
| Datetime | GR_load |
|---------------------|---------------------|
| 2018-01-01 10:00:00 | 0 |
| 2018-01-01 11:00:00 | 1.175 |
| 2018-01-01 12:00:00 | 2.55 |
| ... | ... |
"""
)
uploaded = st.file_uploader('Choose a file for load', type='csv', key=f'load_data{i}')
if uploaded is not None:
df=read_uploaded_csv(uploaded)
expected_columns = ['Datetime', 'GR_load']
if not set(expected_columns).issubset(df.columns):
st.error('Upload a file in format: Datetime, GR_load')
else:
df.set_index(['Datetime'], inplace=True)
for date in df.index:
validate_date_format(date)
st.dataframe(df)
df.to_csv(f'load_data{k}.csv', index=True, header=True)
load_source_type='csv file'
load_source_uri=f'load_data{k}.csv'
else:
load_source_uri=None
elif load_source_type=='TimescaleDB':
col1,col2=st.columns(2)
start_date=col1.date_input('Select load data start date')
end_date=col2.date_input('Select load data end date')
if load_carriers=='Natural Gas':
with open('desfa_flows_hourly_archive.json', 'r') as file:
data = json.load(file)
allowed_values=data['properties']['point_id']['allowed_values']
exit_point=st.selectbox('Exit Point', allowed_values)
st.write(f'Exit point: {exit_point}')
load_source_uri=f"*&and=(timestamp.gte.{start_date},timestamp.lte.{end_date},point_id.eq.{exit_point})"
else:
load_source_uri=f"*&and=(timestamp.gte.{start_date},timestamp.lte.{end_date})"
load_data={
'Component':'Load',
'carrier': load_carriers,
'bus': load_buses,
'investment_period':None,#investment_period,
'input_series_source_type':load_source_type,
'input_series_source_uri':load_source_uri,
}
data_load.append(load_data)
st.divider()
with st.container(border=True):
st.header('Links')
st.info("""
The link is a component for controllable directed flows between two buses, bus0 and bus1 with arbitrary energy carriers.\n
The Link component can be used for any element with a controllable power flow: Energy conversion from AC to hydrogen network via **Electrolysis** and vice versa via **Fuel Cell** and energy conversion from hydrogen to synthetic natural gas via **Methanation**.
""",icon="ℹ️")
links=st.number_input('Select your links', min_value=0)
data_link=[]
for i in range(links):
link_carriers=st.radio(f'Select link {i} type', options=['Electrolysis','Fuel Cell', 'Methanation'], key=f'link_carrier{i}')
st.subheader(link_carriers)
link_bus0=st.selectbox(f'{link_carriers} link from bus:', bus, key=f'link_bus0{i}')
link_bus1=st.selectbox(f'{link_carriers} link to bus:', bus, key=f'link_bus1{i}')
link_p_nom=st.number_input(f'{link_carriers} nominal power (MW)',min_value=0.0, key=f'link_p_nom{i}', help='Limit of active power which can pass through link')
link_capital_cost=st.number_input(f'{link_carriers} capital cost (€/MW)',min_value=0.0, step=100.0, key=f'link_capex{i}', help='Capital cost of extending nominal power by 1 MW')
link_marginal_cost=st.number_input(f'{link_carriers} marginal cost (€/MWh)',min_value=0.0, key=f'link_opex{i}', help='Marginal cost of transfering 1 MWh (before efficiency losses) from bus0 to bus1')
link_efficiency=st.number_input(f'{link_carriers} efficiency',min_value=0.0, value=0.6, key=f'link_efficiency{i}', help='Efficiency of power transfer from bus0 to bus1')
link_data={
'Component':'Link',
'carrier':link_carriers,
'from_bus': link_bus0,
'to_bus': link_bus1,
'p_nom': link_p_nom,
'capital_cost': link_capital_cost,
'marginal_cost': link_marginal_cost,
'efficiency':link_efficiency
}
data_link.append(link_data)
st.divider()
with st.container(border=True):
st.header('Storage')
stores=st.number_input('Select your stores', min_value=0)
data_store=[]
for i in range(stores):
store_carrier=st.radio(f'Select store {i} type', options=['Hydrogen Store'], key=(f'store_carrier {i}'))
store_buses=st.selectbox(f'{store_carrier} {i} bus:', options=bus, key=(f'store_bus {i}'))
store_p_nom=st.number_input(f'{store_carrier} {i} nominal energy capacity (MWh)',min_value=0.0, key=(f'store_p_nom {i}'))
store_capital_cost=st.number_input(f'{store_carrier} {i} capital cost (€/MWh)', min_value=0.0, step=100.0, key=(f'store_capex {i}'), help='Capital cost of extending nominal energy capacity by 1 MWh')
store_marginal_cost=st.number_input(f'{store_carrier} {i} marginal cost (€/MWh)',min_value=0.0, key=(f'store_opex {i}'), help='Marginal cost of production of 1 MWh')
store_data={
'Component':'Store',
'carrier':store_carrier,
'bus': store_buses,
'p_nom': store_p_nom,
'capital_cost': store_capital_cost,
'marginal_cost': store_marginal_cost
}
data_store.append(store_data)
st.divider()
results = bool(ss.b1_count %2)
button_stop = bool(ss.b6_count %2)
def submit_disabled():
if button_stop:
ss.b6_count=0
ss.b1_count=0
ss.b2_count=0
return bool(ss.b1_count % 2)
col = st.columns(14)
col[0].button('Submit', key='b1', on_click=count, args=('b1_count',), type='primary', disabled=submit_disabled())
col[1].button('Stop', key='b6', on_click=count, args=('b6_count',))
ss.b3_count = 0
ss.b4_count = 0
if button_stop:
st.empty()
st.stop()
if results==False:
ss.b2_count=0
else:
with st.container(border=True):
with st.spinner():
start= time.time()
timestamp=time.strftime('%Y.%m.%d %H.%M.%S', time.localtime())
st.write(timestamp)
for i in range(buses):
data_bus[i]['timestamp'] = timestamp
df_bus = pd.DataFrame(data_bus)
df_generator = pd.DataFrame(data_generator)
df_line = pd.DataFrame(data_line)
df_load = pd.DataFrame(data_load)
df_link = pd.DataFrame(data_link)
df_H2 = pd.DataFrame(data_store)
inputs = pd.concat([df_bus, df_generator, df_line, df_load, df_link, df_H2], ignore_index=True)
inputs.to_csv('data_inputs2.csv', index=False)
network_execute()
# st.write(inputs)
uploaded=f'./results/{use_case_name}/{timestamp}/outputs/statistics.csv'
statistics=read_uploaded_csv(uploaded)
st.write(statistics)
uploaded=f'./results/{use_case_name}/{timestamp}/outputs/output_series.csv'
df=read_uploaded_csv(uploaded)
df.set_index(['Datetime'], inplace=True)
tab1, tab2 = st.tabs(['📈 Chart', '🗃 Data'])
tab1.subheader('Components Chart')
with tab1:
fig = plotly_chart_results(df)
st.plotly_chart(fig, use_container_width=True)
tab2.subheader('Components Data')
tab2.write(df)
csv = convert_df(df)
tab2.download_button(label='Download data as CSV', data=csv, file_name='df.csv', mime='text/csv')
st.write(time.strftime('%d/%m/%Y, %H:%M:%S', time.localtime()))
end=time.time()
time_lapsed = end - start
time_convert(time_lapsed)
col1, col2 = st.columns(2)
start = col1.date_input('Start')
end = col2.date_input('End')
start,end=selected_dates(start,end)
st.button('Process Selected Dates', key='b2', on_click=count, args=('b2_count',))
button2 = bool(ss.b2_count %2)
if button2:
filtered_df = df[(df.index >= start) & (df.index <= end)]
tab5, tab6=st.tabs(['📈 Chart', '🗃 Data'])
with tab5:
fig = plotly_chart_results(filtered_df)
st.plotly_chart(fig, use_container_width=True)
tab6.write(filtered_df)