-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_profiles_cin.py
More file actions
98 lines (75 loc) · 2.83 KB
/
Copy pathwrite_profiles_cin.py
File metadata and controls
98 lines (75 loc) · 2.83 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
95
96
97
98
import pandas as pd
import string
from pathlib import Path
import re
# Directory containing your files
home_dir = Path("E:/Work/SG_Model")
data_dir = Path("E:/Work/SG_Model/sol")
out_dir = Path("E:/Work/SG_Model/output")
# Match all .txt files
files = list(data_dir.glob("*.sol"))
ns_per_csv = 10
seasons = 12
daytypes = 2
hours = 24
lt = string.ascii_lowercase
techcodes_df = pd.read_csv('techcodes.csv')
n_ts = seasons * daytypes * hours
alpha_ts = []
alpha_ts_dict ={}
for i in range(0, seasons):
for j in range(0, daytypes):
for k in range(0, hours):
t = f"{lt[i]}{lt[j]}{lt[k]}"
alpha_ts.append(t)
alpha_ts_dict[t] = {"season":i+1, "daytype":j+1, "hour":k+1}
# List of codes you want to extract
codes = list(techcodes_df['MessageCode'])
token_re = re.compile(r"^([A-Za-z.]+?)\.+([A-Za-z]+)(\d+)$")
rows = []
for fid, filepath in enumerate(files):
print(f"processing {filepath}")
with open(filepath, "r") as f:
for line in f:
parts = line.split()
if len(parts) < 5:
continue # skip malformed lines
token = parts[1] # e.g. "s.ve......fal037"
value_str = parts[3] # e.g. "0.2397099" (first numeric column after BS/LL)
m = token_re.match(token)
if not m:
continue # doesn't match expected pattern
code, v1, v2_str = m.groups()
# Filter to codes we care about
if codes and code not in codes:
continue
# Convert v2 to integer (strip leading zeros if any)
v2 = int(v2_str)
# Convert value to float
value = float(value_str)
#get second element of solution file name as scenario name
rows.append((filepath.name.split('_')[1], code, v1, v2, alpha_ts_dict[v1]['season'], alpha_ts_dict[v1]['daytype'], alpha_ts_dict[v1]['hour'], value))
if (fid+1) % ns_per_csv == 0:
# Build DataFrame
df = pd.DataFrame(rows, columns=["filename", "code", "v1", "year", "season","daytype","hour", "value"])
#print(df)
df.to_csv(f'{out_dir}/profiles_{(fid+1)//ns_per_csv}.csv')
rows = []
aa =0
if aa == 1:
cin_out = ("title: Battery\n"
"unit: , 1.\n"
"direction: hor\n"
"@\n")
for i in range(0, seasons):
for j in range(0, daytypes):
for k in range(0, hours):
t = f"{lt[i]}{lt[j]}{lt[k]}"
alpha_ts.append(t)
for index, row in techcodes_df.iterrows():
cin_out += f"{str(i)}_{str(j)}_{str(k)}_{row['Technology']} = {row['MessageCode']}....F.{t}: out\n"
cin_out += ("@\n"
"@\n"
"@\n")
with open("profile.cin", "w", encoding="utf-8") as file:
file.write(cin_out)