Skip to content

Commit

Permalink
irena capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorb1 committed Oct 13, 2024
1 parent 5c17463 commit 3145012
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 33 deletions.
60 changes: 37 additions & 23 deletions workflow/scripts/osemosys_global/validation/irena.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@
"Nuclear": "URN",
}

OG_NAME_MAPPER = {
"BIO": "BIO",
"CCG": "GAS",
"COA": "COA",
"CSP": "SPV",
"HYD": "HYD",
"OCG": "GAS",
"OIL": "OIL",
"SPV": "SPV",
"TRN": None,
"URN": "URN",
"WON": "WON",
"WOF": "WOF",
"WAV": "WAV",
}

###
# public functions
###
Expand All @@ -44,24 +60,13 @@ def get_irena_generation(csv_file: str, iso_codes: str, **kwargs) -> pd.DataFram
return _format_irena_generation_data(df)


def format_og_generation(prod_tech_annual: pd.DataFrame) -> pd.DataFrame:
"""Formats ProductionByTechnologyAnnual data for irena comparison"""

name_mapper = {
"BIO": "BIO",
"CCG": "GAS",
"COA": "COA",
"CSP": "SPV",
"HYD": "HYD",
"OCG": "GAS",
"OIL": "OIL",
"SPV": "SPV",
"TRN": None,
"URN": "URN",
"WON": "WON",
"WOF": "WOF",
"WAV": "WAV",
}
def format_og_data(prod_tech_annual: pd.DataFrame) -> pd.DataFrame:
"""Formats OG results for irena comparison
Works on:
- ProductionByTechnologyAnnual
- TotalCapacityAnnual
"""

df = prod_tech_annual.copy()

Expand All @@ -71,10 +76,10 @@ def format_og_generation(prod_tech_annual: pd.DataFrame) -> pd.DataFrame:
df = df[(df.TECHNOLOGY.str.startswith("PWR")) & (df.YEAR < 2023)]
df["COUNTRY"] = df.TECHNOLOGY.str[6:9]
df["CODE"] = df.TECHNOLOGY.str[3:6]
df["CODE"] = df.CODE.map(name_mapper)
df = df.dropna(subset="CODE")
df["CODE"] = df.CODE.map(OG_NAME_MAPPER)
df = df.dropna(subset="CODE").copy()
df["TECHNOLOGY"] = df.CODE + df.COUNTRY
df = df.drop(columns=["FUEL", "COUNTRY", "CODE"])
df = df[["REGION", "TECHNOLOGY", "YEAR", "VALUE"]]
return df.groupby(["REGION", "TECHNOLOGY", "YEAR"]).sum()


Expand Down Expand Up @@ -119,9 +124,18 @@ def _get_iso_mapper(iso_codes: str) -> dict[str, str]:
return df


def _format_irena_capacity_data(eia: pd.DataFrame) -> pd.DataFrame:
def _format_irena_capacity_data(irena: pd.DataFrame) -> pd.DataFrame:
"""Formats data into otoole compatiable data structure"""
raise NotImplementedError

df = irena.copy()

df["TECHNOLOGY"] = df.TECHNOLOGY + df.COUNTRY
df["REGION"] = "GLOBAL"

# MW -> GW
df["VALUE"] = df.VALUE.div(1000)
df = df[["REGION", "TECHNOLOGY", "YEAR", "VALUE"]]
return df.groupby(["REGION", "TECHNOLOGY", "YEAR"]).sum()


def _format_irena_generation_data(irena: pd.DataFrame) -> pd.DataFrame:
Expand Down
34 changes: 24 additions & 10 deletions workflow/scripts/osemosys_global/validation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@


def plot_gen_cap(
modelled: pd.DataFrame, actual: pd.DataFrame, dataset_name: Optional[str] = None
modelled: pd.DataFrame,
actual: pd.DataFrame,
variable: str,
dataset_name: Optional[str] = None,
) -> dict[str, tuple[plt.figure, plt.axes]]:

def _join_data(
Expand All @@ -42,6 +45,15 @@ def _join_data(

assert modelled.index.names == actual.index.names

if variable == "generation":
units = "PJ"
elif variable == "capacity":
units = "GW"
else:
raise ValueError(
f"Variable must be one of ['generation', 'capacity']. Recieved {variable}"
)

df = _join_data(modelled, actual, dataset_name).reset_index()
df["TECH"] = df["TECHNOLOGY"].str[0:3]
df["COUNTRY"] = df["TECHNOLOGY"].str[3:]
Expand All @@ -60,12 +72,14 @@ def _join_data(
.drop(columns=["TECHNOLOGY", "YEAR", "COUNTRY"])
.set_index("TECH")
)
title = f"{country} Generation in {year}"
title = f"{country} {variable.capitalize()} in {year}"
if n_rows > 1:
ax = axs[i]
else:
ax = axs
df_year.plot(kind="bar", ax=ax, rot=45, title=title, xlabel="", ylabel="PJ")
df_year.plot(
kind="bar", ax=ax, rot=45, title=title, xlabel="", ylabel=units
)

data[country] = (fig, axs)

Expand All @@ -88,7 +102,7 @@ def get_generation_funcs(datasource: str) -> dict[str, callable]:
case "irena" | "IRENA" | "Irena":
return {
"getter": irena.get_irena_generation,
"formatter": irena.format_og_generation,
"formatter": irena.format_og_data,
"plotter": plot_gen_cap,
}
case "ember" | "EMBER" | "Ember":
Expand All @@ -112,7 +126,7 @@ def get_capacity_funcs(datasource: str) -> dict[str, callable]:
case "irena" | "IRENA" | "Irena":
return {
"getter": irena.get_irena_capacity,
"formatter": irena.format_og_capacity,
"formatter": irena.format_og_data,
"plotter": plot_gen_cap,
}
case "ember" | "EMBER" | "Ember":
Expand All @@ -133,12 +147,12 @@ def get_capacity_funcs(datasource: str) -> dict[str, callable]:
if "snakemake" in globals():
raise NotImplementedError
else:
datasource = "ember"
datasource = "irena"
variable = "capacity"
result_dir = "results/India/results"
data_file = "resources/data/validation/ember.csv"
data_file = "resources/data/validation/irena_capacity.csv"
options = {}
# options = {"iso_codes": "resources/data/validation/iso.csv"}
options = {"iso_codes": "resources/data/validation/iso.csv"}

csv_results = Path(result_dir)
validation_results = Path(csv_results, "..", "validation")
Expand All @@ -160,10 +174,10 @@ def get_capacity_funcs(datasource: str) -> dict[str, callable]:
actual = funcs["getter"](data_file, **options)
modelled = pd.read_csv(Path(result_dir, f"{og_result}.csv"))
modelled = funcs["formatter"](modelled)
except KeyError:
except KeyError as e:
actual = None
modelled = None
logger.error(f"No validation for {variable} from {datasource}")
logger.error(f"No validation for {variable} from {datasource}: \n{e}")

if isinstance(actual, pd.DataFrame) and isinstance(modelled, pd.DataFrame):
gen = funcs["plotter"](modelled, actual, datasource)
Expand Down

0 comments on commit 3145012

Please sign in to comment.