-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_decipherer.py
More file actions
277 lines (238 loc) · 8.46 KB
/
code_decipherer.py
File metadata and controls
277 lines (238 loc) · 8.46 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
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
"""
Script: Code Decoder and CSV Modifier
Description:
This Python script decodes technology and fuel codes.
The main function creates a new or updates a CSV file with appended decoded descriptions in a new column.
Functions:
1. decode_code(code): decodes a single fuel or tech code of 4 or 9 digits.
3. add_code_descriptions_to_csv(input_csv_filename, output_csv_filename=None): creates a new csv file including a new column with code descriptions.
Example Usages:
```python
code = 'DEBFCCH1'
print(decode_code(code))
# Output: Germany (DE)| Biofuel (BF)| Combined cycle (CC)| Primary energy commodity (P)| age 1| size 1
```
or
```python
input_csv_filename = 'input_data\\FUEL.csv'
output_csv_filename = 'output_codes_fuel.csv'
add_code_descriptions_to_csv(input_csv_filename, output_csv_filename)
# Output: Code descriptions saved to 'output_codes_fuel.csv'.
```
Author: Timon Renzelmann
"""
import sys
import pandas as pd
import os
# Define code mappings
country_codes = {
'AT': 'Austria',
'BE': 'Belgium',
'BG': 'Bulgaria',
'CH': 'Switzerland',
'CY': 'Cyprus',
'CZ': 'Czech Republic',
'DE': 'Germany',
'DK': 'Denmark',
'EE': 'Estonia',
'ES': 'Spain',
'FI': 'Finland',
'FR': 'France',
'GR': 'Greece',
'HR': 'Croatia',
'HU': 'Hungary',
'IE': 'Ireland',
'IT': 'Italy',
'LT': 'Lithuania',
'LU': 'Luxembourg',
'LV': 'Latvia',
'MT': 'Malta',
'NL': 'the Netherlands',
'NO': 'Norway',
'PL': 'Poland',
'PT': 'Portugal',
'RO': 'Romania',
'SE': 'Sweden',
'SI': 'Slovenia',
'SK': 'Slovakia',
'UK': 'United Kingdom'
}
commodity_codes = {
'BF': 'Biofuel',
'BM': 'Biomass',
'CO': 'Coal',
'EL': 'Electricity',
'E1': 'Electricity 1',
'E2': 'Electricity 2',
'GO': 'Geothermal',
'HF': 'Heavy fuel oil',
'HY': 'Hydro',
'NG': 'Natural gas',
'NU': 'Nuclear',
'OC': 'Ocean',
'OI': 'Oil',
'OS': 'Oil Shale',
'SO': 'Sun',
'UR': 'Uranium',
'WS': 'Waste',
'WI': 'Wind',
}
technology_codes = {
'CC': 'Combined cycle',
'CH': 'Combined heat and power',
'CS': 'Carbon Capture and Storage',
'CV': 'Conventional',
'DI': 'Distributed PV',
'DM': 'Dam',
'DS': 'Pumped Storage',
'FC': 'Fuel cell',
'GC': 'Gas cycle',
'G2': 'Generation 2',
'G3': 'Generation 3',
'HP': 'Internal combustion engine with heat recovery',
'OF': 'Offshore',
'ON': 'Onshore',
'ST': 'Steam cycle',
'UT': 'Utility PV',
'WV': 'Wave power',
'00': 'unspecified',
}
energy_level_codes = {
'P': 'Primary energy commodity',
'F': 'Final electricity',
'I': 'Import technology',
'X': 'Extraction or generation technology',
}
def decode_tech(tech_code):
"""Decodes a single technology code of 9 digits.
Args:
tech_code (str): The technology code to decode.
Returns:
str: The decoded description of the technology code.
"""
if len(tech_code) != 9:
return "Error: Code length must be 9"
country = tech_code[0:2]
commodity = tech_code[2:4]
technology = tech_code[4:6]
energy_level = tech_code[6]
age = tech_code[7]
size = tech_code[8]
country_description = country_codes.get(country, f'Unknown country')
commodity_description = commodity_codes.get(commodity, f'Unknown commodity')
if technology in technology_codes:
technology_description = technology_codes[technology]
elif technology in country_codes:
technology_description = f'connected to {country_codes[technology]}'
else:
technology_description = 'Unknown technology'
energy_level_description = energy_level_codes.get(energy_level, f'Unknown energy level')
age_description = f'age {age}'
size_description = f'size {size}'
full_description = (
f"{country_description} ({country})| "
f"{commodity_description} ({commodity})| "
f"{technology_description} ({technology})| "
f"{energy_level_description} ({energy_level})| "
f"{age_description}| "
f"{size_description}"
)
return full_description
def decode_fuel(fuel_code):
"""Decodes a single fuel code of 4 digits.
Args:
fuel_code (str): The fuel code to decode.
Returns:
str: The decoded description of the fuel code.
"""
if len(fuel_code) != 4:
return "Error: Code length must be 4"
country = fuel_code[0:2]
commodity = fuel_code[2:4]
country_description = country_codes.get(country, f'Unknown country')
commodity_description = commodity_codes.get(commodity, f'Unknown commodity')
full_description = (
f"{country_description} ({country})| "
f"{commodity_description} ({commodity})"
)
return full_description
def decode_code(code):
"""Decodes a single code of 4 or 9 digits.
Args:
code (str): The code to decode.
Returns:
str: The decoded description of the code.
"""
if len(code) == 9:
return decode_tech(code)
elif len(code) == 4:
return decode_fuel(code)
else:
return "Error: Code length must be 4 or 9"
def add_code_descriptions_to_csv(input_csv_filename, output_csv_filename=None):
"""Modifies a CSV file by adding a ' #codedescription' column.
This function reads a CSV file containing codes in one column, decodes each
code using the decode_tech function for 9 digit codes and decode_fuel for 4-digit codes,
and adds a new column to the CSV file with the decoded descriptions in the format ' #codedescription'.
It overrides the input CSV file with the modified data if the output file name is not provided
or if it's the same as the input file name.
Args:
input_csv_filename (str): The name of the input CSV file.
output_csv_filename (str, optional): The name of the output CSV file.
Returns:
None
"""
try:
# Read the input CSV file
df = pd.read_csv(input_csv_filename, header=None)
# Store the existing header
header = df.iloc[0].tolist()
header.append("description")
df1 = pd.DataFrame([header])
# Remove the header from the DataFrame
df = df.iloc[1:]
# Apply the decode_tech or decode_fuel function based on code length and add a new column
df[1] = df.apply(lambda row: f'#{decode_tech(row[0])}' if len(row[0]) == 9 and not pd.isna(row[0])
else (f' #{decode_fuel(row[0])}' if len(row[0]) == 4 and not pd.isna(row[0])
else ''), axis=1)
#create new dataframe
df = pd.concat([df1, df], ignore_index=True)
if output_csv_filename is None or output_csv_filename == input_csv_filename:
while True:
overwrite_input = input(f"Overwrite '{input_csv_filename}'? (yes/no): ").strip().lower()
if overwrite_input == 'yes':
# Save the modified DataFrame back to the input CSV file, overriding it
df.to_csv(input_csv_filename, index=False, header=False)
print(f"Code descriptions added to '{input_csv_filename}'.")
break
elif overwrite_input == 'no':
break
else:
print("Please enter 'yes' or 'no'.")
else:
# Save the modified DataFrame to the output CSV file
df.to_csv(output_csv_filename, index=False, header=False)
print(f"Code descriptions saved to '{output_csv_filename}'.")
except Exception as e:
print(f"An error occurred: {e}")
# Add a new functions to handle decoding codes from the command line
def decode_code_from_cli():
if len(sys.argv) != 3:
print("Usage: python script.py decode_code code")
else:
code = sys.argv[2]
result = decode_code(code)
print(result)
if __name__ == '__main__':
# input_file = 'input_data\\WP1_NetZero\\data\\TECHNOLOGY.csv'
# output_file = 'tech_codes.csv'
# input_file2 = 'input_data\WP1_NetZero\data\FUEL.csv'
# output_file2 = 'fuel_codes.csv'
# add_code_descriptions_to_csv(input_file, output_file)
# add_code_descriptions_to_csv(input_file2, output_file2)
if len(sys.argv) < 2 or len(sys.argv) > 3:
print("Usage: python script.py input_file [output_file]")
else:
input_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else None
add_code_descriptions_to_csv(input_file, output_file)