-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadcomp.py
More file actions
94 lines (80 loc) · 3.15 KB
/
loadcomp.py
File metadata and controls
94 lines (80 loc) · 3.15 KB
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
"""Load Composition Data
Compile load composition data
"""
import os
import sys
import pandas as pd
class LoadComp(pd.DataFrame):
"""Load composition dataframe"""
def __init__(self,
path:str="data",
region:str|list[str]|None=None,
season:str|list[str]|None=None,
nomd:bool|None=None,
):
"""Construct load composition dataframe
Arguments
---------
- `path`: path to data LC files
- `region`: regions to include
"""
data = []
for file in sorted(os.listdir(path)):
if not file.startswith("LoadComp_") or not file.endswith(".xlsx"):
continue
spec = os.path.splitext(file.upper())[0].split("_", 3)
if isinstance(region,str): region = region.split(",")
if region is not None and spec[1] not in map(str.upper,region):
continue
if isinstance(season,str): season = season.split(",")
if season is not None and spec[2] not in map(str.upper,season):
continue
if nomd != None and nomd == ( len(spec) < 4 ):
continue
pathname = f"{path}/{file}"
try:
print(file, end="... ", flush=True)
xlsx = pd.ExcelFile(pathname,
engine='openpyxl',
engine_kwargs={'data_only': True, 'read_only': True},
)
sheet_names = sorted(xlsx.sheet_names)
xlsx.close()
for sheet in sheet_names:
if not sheet.upper()[:4] == "HOUR":
continue
lcdata = pd.read_excel(
f"{path}/{file}",
sheet_name=sheet,
engine="openpyxl",
engine_kwargs={"data_only": True, "read_only": True},
)
lcdata[["RO", "SEASON", "NOMD", "HOUR"]] = (
spec[1].upper(),
spec[2].upper(),
1 if len(spec) > 3 else 0,
int(sheet.replace("Hour", "")) - 1,
)
lcdata.rename({"#AREA": "LOADTYPE"}, axis=1, inplace=True)
lcdata.drop(
[x for x in lcdata.columns if x.startswith("ID_")],
inplace=True,
axis=1,
)
lcdata.columns = [x.upper().replace("LC_","") for x in lcdata.columns]
data.append(
lcdata.set_index(
["RO", "LOADTYPE", "SEASON", "NOMD", "HOUR"]
).round(6)
)
print("ok",flush=True)
except Exception as err:
print(f"ERROR [{file}]: {err}", flush=True, file=sys.stderr)
if not data:
raise RuntimeError("no data")
super().__init__(pd.concat(data).sort_index())
def main(*args,**kwargs):
lc = LoadComp()
lc.to_csv("loadcomp.csv",index=True,header=True)
if __name__ == '__main__':
main()