-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
86 lines (52 loc) · 2.31 KB
/
helpers.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
## Copyright 2024 Tom Brown
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU Affero General Public License as
## published by the Free Software Foundation; either version 3 of the
## License, or (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Affero General Public License for more details.
## License and more information at:
## https://github.com/PyPSA/nowcast
import datetime, pandas as pd, os, re, pypsa
def safe_pypsa_import(filename):
"""If time series are all empty, fill with zeros."""
n = pypsa.Network(filename)
list_attrs = {"Link" : ["p0","p1"],
"Store" : ["e","p"]}
for c in n.iterate_components(list_attrs.keys()):
for attr in list_attrs[c.name]:
if c.pnl[attr].empty:
print(f"Since {attr} is empty for {c.list_name}, filling with zeros")
c.pnl[attr] = c.pnl[attr].reindex(c.df.index, axis=1).fillna(0.)
return n
def concatenate(n,ni):
for c in n.iterate_components():
for attr in c.pnl:
if not c.pnl[attr].empty:
c.pnl[attr] = pd.concat([c.pnl[attr],getattr(ni,c.list_name + "_t")[attr]])
n.set_snapshots(n.snapshots.union(ni.snapshots))
return n
def get_end_date(config):
end_date = config["end_date"]
if end_date == "today":
end_date = datetime.date.today()
elif end_date == "yesterday":
end_date = datetime.date.today() - datetime.timedelta(days=1)
return end_date
def get_last_days(config):
end_date = get_end_date(config)
start_date = end_date - datetime.timedelta(days=config['days_to_plot']-1)
return pd.date_range(start=start_date, end=end_date)
def get_date_index(config):
end_date = get_end_date(config)
return pd.date_range(start=config["start_date"],
end=end_date)
def get_existing_dates(dir_name, pattern):
date_strings = []
for filename in os.listdir(dir_name):
match = re.match(pattern, filename)
if match:
date_strings.append(match.group(1))
return pd.to_datetime(sorted(date_strings))