Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate .report.legacy code from message_data dev branch #254

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
359 changes: 4 additions & 355 deletions message_ix_models/report/legacy/ENGAGE_SSP2_v417_tables.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd

from . import pp_utils
import message_data.tools.post_processing.pp_utils as pp_utils

pp = None
mu = None
Expand Down Expand Up @@ -941,357 +941,6 @@ def retr_pop(units):
return df


@_register
def retr_price(
units_CPrc_co2,
units_CPrc_co2_outp,
units_energy,
units_energy_outp,
units_CPrc_c,
conv_CPrc_co2_to_c,
units_agri,
):
"""Price: Prices for Emissions, Energy and Land-Use.

Parameters
----------

units_CPrc_co2 : str
Units to which carbon price (in CO2 equivalent) should be converted.
units_CPrc_co2_outp : str
Units in which carbon price (in CO2 equivalent) should be reported in.
units_energy : str
Units to which energy variables should be converted.
units_energy_outp : str
Units to which variables should be converted.
units_CPrc_c : str
Units to which carbon price (in carbon equivalent) should be converted.
conv_CPrc_co2_to_c : number
Conversion factor for carbon price from CO2 to C.
units_agri : str
Units to which agricultural variables should be converted.
"""

dfs = []

# ----------------------
# Calculate Carbon Price
# ----------------------

vars = {}
vars["Carbon"] = pp.cprc(units_CPrc_co2)
dfs.append(pp_utils.make_outputdf(vars, units_CPrc_co2_outp, param="max"))

# -------------------------------------------------
# Calculate Final Energy Prices incl. carbon prices
# -------------------------------------------------

# Carbon price must also be multiplied by factor as retrieved
# prices are already converted to $/GJ

_cbprc = pp.cprc(units_CPrc_c) * conv_CPrc_co2_to_c
vars = {}
# Final Energy prices are reported including the carbon price
vars["Final Energy|Residential|Electricity"] = pp.eneprc(
enefilter={"commodity": ["electr"], "level": ["final"]}, units=units_energy
)

vars["Final Energy|Residential|Gases|Natural Gas"] = pp.eneprc(
enefilter={"commodity": ["gas"], "level": ["final"]}, units=units_energy
)

vars["Final Energy|Residential|Liquids|Biomass"] = pp.eneprc(
enefilter={"commodity": ["ethanol"], "level": ["final"]}, units=units_energy
)

vars["Final Energy|Residential|Liquids|Oil"] = pp.eneprc(
enefilter={"commodity": ["lightoil"], "level": ["final"]}, units=units_energy
)

vars["Final Energy|Residential|Solids|Biomass"] = pp.eneprc(
enefilter={"commodity": ["biomass"], "level": ["final"]}, units=units_energy
)

vars["Final Energy|Residential|Solids|Coal"] = pp.eneprc(
enefilter={"commodity": ["coal"], "level": ["final"]}, units=units_energy
)

# -------------------------------------------------
# Calculate Final Energy Prices excl. carbon prices
# -------------------------------------------------

vars["Final Energy wo carbon price|Residential|Electricity"] = pp.eneprc(
enefilter={"commodity": ["electr"], "level": ["final"]}, units=units_energy
)

vars["Final Energy wo carbon price|Residential|Gases|Natural Gas"] = (
pp.eneprc(
enefilter={"commodity": ["gas"], "level": ["final"]}, units=units_energy
)
- _cbprc * mu["crbcnt_gas"]
)

vars["Final Energy wo carbon price|Residential|Liquids|Biomass"] = pp.eneprc(
enefilter={"commodity": ["ethanol"], "level": ["final"]}, units=units_energy
)

vars["Final Energy wo carbon price|Residential|Liquids|Oil"] = (
pp.eneprc(
enefilter={"commodity": ["lightoil"], "level": ["final"]},
units=units_energy,
)
- _cbprc * mu["crbcnt_oil"]
)

vars["Final Energy wo carbon price|Residential|Solids|Biomass"] = pp.eneprc(
enefilter={"commodity": ["biomass"], "level": ["final"]}, units=units_energy
)

vars["Final Energy wo carbon price|Residential|Solids|Coal"] = (
pp.eneprc(
enefilter={"commodity": ["coal"], "level": ["final"]}, units=units_energy
)
- _cbprc * mu["crbcnt_coal"]
)

# ---------------------------------------------------
# Calculate Primary Energy Prices excl. carbon prices
# ---------------------------------------------------

# Primary energy prices are reported excluding the carbon price, hence the
# subtraction of the carbonprice * the carbon content of the fuel

vars["Primary Energy w carbon price|Biomass"] = pp.eneprc(
enefilter={"commodity": ["biomass"], "level": ["primary"]}, units=units_energy
)

vars["Primary Energy|Biomass"] = vars["Primary Energy w carbon price|Biomass"]

# -----------------------------------------------------
# Calculate Secondary Energy Prices excl. carbon prices
# -----------------------------------------------------

# Secondary energy prices are reported excluding the carbon price, hence the
# subtraction of the carbonprice * the carbon content of the fuel

# vars["Secondary Energy|Gases|Natural Gas"] =pp.eneprc(
# enefilter={"commodity": [""],
# "level": ["secondary"]},
# units=units_energy)

# vars["Secondary Energy|Liquids"] = pp.eneprc(
# enefilter={"commodity": [""],
# "level": ["secondary"]},
# units=units_energy)

vars["Secondary Energy|Liquids"] = pp_utils._make_zero()

# vars["Secondary Energy|Solids|Biomass"] = pp.eneprc(
# enefilter={"commodity": [""],
# "level": ["secondary"]},
# units=units_energy)

# vars["Secondary Energy|Solids|Coal"] = pp.eneprc(
# enefilter={"commodity": [""],
# "level": ["secondary"]},
# units=units_energy) - _cbprc * crbcnt_coal

# Secondary energy prices are reported including the carbon price
vars["Secondary Energy w carbon price|Liquids"] = pp_utils._make_zero()

dfs.append(pp_utils.make_outputdf(vars, units_energy_outp, param="max"))

# ------------------------------------------------------
# Calculate Energy Prices for variables where the GLOBAL
# value variable differs from regional value variable
# ------------------------------------------------------

vars = {}

# Primary energy prices are reported excluding the carbon price, hence the
# subtraction of the carbonprice * the carbon content of the fuel

# Coal uses primary-coal for regions and import-coal for GLB
vars["Primary Energy w carbon price|Coal"] = pd.concat(
[
pp_utils.rem_glb(
pp.eneprc(
enefilter={"commodity": ["coal"], "level": ["primary"]},
units=units_energy,
)
),
pp_utils.rem_reg(
pp.eneprc(
enefilter={"commodity": ["coal"], "level": ["import"]},
units=units_energy,
)
),
],
sort=True,
)

vars["Primary Energy|Coal"] = vars["Primary Energy w carbon price|Coal"].subtract(
pp_utils.rem_glb(_cbprc) * mu["crbcnt_coal"], fill_value=0
)

# Gas uses primary-gas for regions and import-LNG for GLB
# Note: GLOBAL region uses LNG prices
vars["Primary Energy w carbon price|Gas"] = pd.concat(
[
pp_utils.rem_glb(
pp.eneprc(
enefilter={"commodity": ["gas"], "level": ["primary"]},
units=units_energy,
)
),
pp_utils.rem_reg(
pp.eneprc(
enefilter={"commodity": ["LNG"], "level": ["import"]},
units=units_energy,
)
),
],
sort=True,
)

vars["Primary Energy|Gas"] = vars["Primary Energy w carbon price|Gas"].subtract(
pp_utils.rem_glb(_cbprc) * mu["crbcnt_gas"], fill_value=0
)

# Oil uses primary-crudeoil for regions and import-crudeoil for GLB
vars["Primary Energy w carbon price|Oil"] = pd.concat(
[
pp_utils.rem_glb(
pp.eneprc(
enefilter={"commodity": ["crudeoil"], "level": ["secondary"]},
units=units_energy,
)
),
pp_utils.rem_reg(
pp.eneprc(
enefilter={"commodity": ["crudeoil"], "level": ["import"]},
units=units_energy,
)
),
],
sort=True,
)

vars["Primary Energy|Oil"] = vars["Primary Energy w carbon price|Oil"].subtract(
pp_utils.rem_glb(_cbprc) * mu["crbcnt_oil"], fill_value=0
)

# Secondary energy prices are reported including the carbon price
# Oil uses secondary-lightoil for regions and import-lightoil for GLB
vars["Secondary Energy w carbon price|Liquids|Oil"] = pd.concat(
[
pp_utils.rem_glb(
pp.eneprc(
enefilter={"commodity": ["lightoil"], "level": ["secondary"]},
units=units_energy,
)
),
pp_utils.rem_reg(
pp.eneprc(
enefilter={"commodity": ["lightoil"], "level": ["import"]},
units=units_energy,
)
),
],
sort=True,
)

vars["Secondary Energy|Liquids|Oil"] = vars[
"Secondary Energy w carbon price|Liquids|Oil"
].subtract(pp_utils.rem_glb(_cbprc) * mu["crbcnt_oil"], fill_value=0)

# Biomass uses secondary-ethanol for regions and import-ethanol for GLB
vars["Secondary Energy w carbon price|Liquids|Biomass"] = pd.concat(
[
pp_utils.rem_glb(
pp.eneprc(
enefilter={"commodity": ["ethanol"], "level": ["primary"]},
units=units_energy,
)
),
pp_utils.rem_reg(
pp.eneprc(
enefilter={"commodity": ["ethanol"], "level": ["import"]},
units=units_energy,
)
),
],
sort=True,
)

# Carbon-price is NOT subtracted
vars["Secondary Energy|Liquids|Biomass"] = vars[
"Secondary Energy w carbon price|Liquids|Biomass"
]

# Hydrogen uses secondary-hydrogen for regions and import-lh2 for GLB
vars["Secondary Energy w carbon price|Hydrogen"] = pd.concat(
[
pp_utils.rem_glb(
pp.eneprc(
enefilter={"commodity": ["hydrogen"], "level": ["secondary"]},
units=units_energy,
)
),
pp_utils.rem_reg(
pp.eneprc(
enefilter={"commodity": ["lh2"], "level": ["import"]},
units=units_energy,
)
),
],
sort=True,
)

# Carbon-price is NOT subtracted
vars["Secondary Energy|Hydrogen"] = vars["Secondary Energy w carbon price|Hydrogen"]

dfs.append(pp_utils.make_outputdf(vars, units_energy_outp, glb=False))

# ------------------------------------------------------
# Calculate Energy Prices for variables where the GLOBAL
# value is set to ZERO
# ------------------------------------------------------

vars = {}

vars["Secondary Energy|Electricity"] = pp.eneprc(
enefilter={"commodity": ["electr"], "level": ["secondary"]}, units=units_energy
)

vars["Secondary Energy w carbon price|Electricity"] = pp.eneprc(
enefilter={"commodity": ["electr"], "level": ["secondary"]}, units=units_energy
)

dfs.append(pp_utils.make_outputdf(vars, units_energy_outp, glb=False))

# ----------------------------------------------------
# Calculate Agricultural Prices where the global price
# is derived using a formula provided by GLOBIOM
# ----------------------------------------------------

vars = {}

# Define the technology which represents the quantities used to derive
# sums accross regions values.
scale_tec = "Agricultural Production|Non-Energy Crops"

vars["Agriculture|Non-Energy Crops and Livestock|Index"] = pp.retrieve_lu_price(
"Price|Agriculture|Non-Energy Crops and Livestock|Index", scale_tec
)
vars["Agriculture|Non-Energy Crops|Index"] = pp.retrieve_lu_price(
"Price|Agriculture|Non-Energy Crops|Index", scale_tec
)
dfs.append(pp_utils.make_outputdf(vars, units_agri, glb=False))

return pd.concat(dfs, sort=True)


@_register
def retr_kyoto(units):
"""Emissions: Kyoto Gases.
Expand Down Expand Up @@ -3862,9 +3511,9 @@ def retr_pe(units, method=None):
# Primary Energy Biomass
# ----------------------

vars["Biomass"] = pp.out(
["biomass_imp", "land_use_biomass"], units, outfilter={"commodity": ["biomass"]}
) - pp.inp(["biomass_exp"], units, inpfilter={"commodity": ["biomass"]})
vars["Biomass"] = pp.land_out(
lu_out_filter={"level": ["land_use"], "commodity": ["bioenergy"]}, units=units
)

# Note OFR 20180412: Correction inserted. scrubber output per vintage cannot
# be divided by powerplant eff per vintage. In most cases this does work except
Expand Down
Loading
Loading