Skip to content

Commit

Permalink
Process comagg as well
Browse files Browse the repository at this point in the history
  • Loading branch information
olejandro committed Jan 4, 2025
1 parent 9249382 commit c42d743
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion xl2times/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def convert_xl_to_times(
transforms.capitalise_table_values,
transforms.process_regions,
transforms.process_commodities,
transforms.process_commodity_emissions,
transforms.convert_com_tables,
transforms.process_time_periods,
transforms.remove_exreg_cols,
transforms.generate_dummy_processes,
Expand Down
4 changes: 2 additions & 2 deletions xl2times/config/times-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,13 @@
"REG",
"YEAR",
"COM",
"COM"
"COM2"
],
"mapping": [
"region",
"year",
"commodity",
"commodity"
"other_indexes"
]
},
{
Expand Down
26 changes: 25 additions & 1 deletion xl2times/config/veda-tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@
}
]
},
{
"tag_name": "comagg",
"tag_allowed_in": [
"BY",
"SubRES"
],
"valid_fields": [
{
"name": "commname",
"aliases": [
"commodity"
],
"use_name": "other_indexes",
"row_ignore_symbol": [
"\\I:",
"*"
],
"query_field": false,
"inherit_above": false,
"remove_first_row_if_absent": false,
"remove_any_row_if_absent": true
}
]
},
{
"tag_name": "comemi",
"tag_allowed_in": [
Expand All @@ -53,7 +77,7 @@
"query_field": false,
"inherit_above": false,
"remove_first_row_if_absent": false,
"remove_any_row_if_absent": false
"remove_any_row_if_absent": true
}
]
},
Expand Down
31 changes: 22 additions & 9 deletions xl2times/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1624,31 +1624,44 @@ def remove_fill_tables(
return result


def process_commodity_emissions(
def convert_com_tables(
config: Config,
tables: list[EmbeddedXlTable],
model: TimesModel,
) -> list[EmbeddedXlTable]:
"""Transform comemi tables to fi_t."""
"""Transform comemi and comagg tables to fi_t."""
convert_tags = {
Tag.comemi: {
"attribute": "vda_emcb",
"index_column": "commodity",
"other_column": "other_indexes",
},
Tag.comagg: {
"attribute": "com_agg",
"index_column": "other_indexes",
"other_column": "commodity",
},
}
result = []
for table in tables:
if table.tag != Tag.comemi:
if table.tag not in convert_tags:
result.append(table)
else:
info = convert_tags[table.tag]
index_column = info["index_column"]
other_column = info["other_column"]
df = table.dataframe.copy()
# Remove columns that are not allowed
# TODO: Base this on the config file instead
remove_cols = ["region", "year"]
df.drop(columns=remove_cols, errors="ignore", inplace=True)

index_columns = ["commodity"]
data_columns = [
colname for colname in df.columns if colname not in index_columns
colname for colname in df.columns if colname != index_column
]
df, names = utils.explode(df, data_columns)
df.rename(columns={"value": "vda_emcb"}, inplace=True)
df["other_indexes"] = names
df["other_indexes"] = df["other_indexes"].str.upper()
df.rename(columns={"value": info["attribute"]}, inplace=True)
df[other_column] = names
df[other_column] = df[other_column].str.upper()

df = df.reset_index(drop=True)
result.append(replace(table, dataframe=df, tag=Tag.fi_t))
Expand Down

0 comments on commit c42d743

Please sign in to comment.