Skip to content

Commit

Permalink
Merge pull request #169 from agoenergy/various_small_issues
Browse files Browse the repository at this point in the history
Various small issues
  • Loading branch information
joAschauer authored Dec 12, 2023
2 parents 24ef2c5 + 7bb8acd commit b661753
Show file tree
Hide file tree
Showing 32 changed files with 330 additions and 196 deletions.
22 changes: 5 additions & 17 deletions app/plot_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ def plot_costs_on_map(
-------
go.Figure
"""
# define title:
title_string = (
f"{cost_component} cost of exporting "
f"{st.session_state['chain']} to "
f"{st.session_state['country']}"
)

if scope == "world":
# Create a choropleth world map:
fig = _choropleth_map_world(
Expand All @@ -99,9 +92,7 @@ def plot_costs_on_map(
custom_data_func=_make_costs_hoverdata,
)

return _set_map_layout(
fig, title=title_string, colorbar_title=st.session_state["output_unit"]
)
return _set_map_layout(fig, colorbar_title=st.session_state["output_unit"])


def plot_input_data_on_map(
Expand All @@ -111,7 +102,6 @@ def plot_input_data_on_map(
"PV tilted", "Wind Offshore", "Wind Onshore", "Wind-PV-Hybrid", "interest rate"
],
scope: Literal["world", "Argentina", "Morocco", "South Africa"] = "world",
title: str = "",
) -> go.Figure:
"""
Plot input data on a map.
Expand Down Expand Up @@ -192,9 +182,7 @@ def plot_input_data_on_map(
custom_data_func_kwargs=custom_data_func_kwargs,
)

return _set_map_layout(
fig, title=title, colorbar_title=custom_data_func_kwargs["unit"]
)
return _set_map_layout(fig, colorbar_title=custom_data_func_kwargs["unit"])


def _choropleth_map_world(
Expand Down Expand Up @@ -278,7 +266,7 @@ def _choropleth_map_deep_dive_country(
return fig


def _set_map_layout(fig: go.Figure, title: str, colorbar_title: str) -> go.Figure:
def _set_map_layout(fig: go.Figure, colorbar_title: str) -> go.Figure:
"""
Apply a unified layout for all maps used in the app.
Expand Down Expand Up @@ -319,7 +307,7 @@ def _set_map_layout(fig: go.Figure, title: str, colorbar_title: str) -> go.Figur
"len": 0.5,
}, # colorbar
margin={"t": 20, "b": 20, "l": 20, "r": 20}, # reduce margin around figure
title=title,
height=500,
)

# Set the hover template to use the custom data
Expand Down Expand Up @@ -403,7 +391,7 @@ def create_bar_chart_costs(
marker={"size": 10, "color": "black"},
name="Total",
text=res_costs["Total"].apply(
lambda x: f"{x:.2f}"
lambda x: f"{x:.0f}"
), # Use 'total' column values as text labels
textposition="top center", # Position of the text label above the marker
)
Expand Down
89 changes: 70 additions & 19 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Utility functions for streamlit app."""
from pathlib import Path
from typing import Literal

import pandas as pd
Expand Down Expand Up @@ -209,8 +210,10 @@ def subset_and_pivot_input_data(
def get_data_type_from_input_data(
api: PtxboaAPI,
data_type: Literal[
"electricity_generation",
"conversion_processes",
"transportation_processes",
"reconversion_processes",
"CAPEX",
"full load hours",
"interest rate",
Expand All @@ -230,8 +233,8 @@ def get_data_type_from_input_data(
api class instance
data_type : str
the data type which should be selected. Needs to be one of
"conversion_processes", "transportation_processes", "CAPEX", "full load hours",
and "interest rate".
"electricity_generation", "conversion_processes", "transportation_processes",
"reconversion_processes", "CAPEX", "full load hours", and "interest rate".
scope : Literal[None, "world", "Argentina", "Morocco", "South Africa"]
The regional scope. Is automatically set to None for data of
data type "conversion_processes" and "transportation_processes" which is not
Expand All @@ -246,13 +249,29 @@ def get_data_type_from_input_data(
user_data=st.session_state["user_changes_df"],
)

if data_type in ["conversion_processes", "transportation_processes"]:
if data_type in [
"electricity_generation",
"conversion_processes",
"transportation_processes",
"reconversion_processes",
]:
scope = None
source_region_code = [""]
index = "process_code"
columns = "parameter_code"
processes = api.get_dimension("process")

if data_type == "electricity_generation":
parameter_code = [
"CAPEX",
"OPEX (fix)",
"lifetime / amortization period",
"efficiency",
]
process_code = processes.loc[
processes["is_re_generation"], "process_name"
].to_list()

if data_type == "conversion_processes":
parameter_code = [
"CAPEX",
Expand All @@ -261,7 +280,7 @@ def get_data_type_from_input_data(
"efficiency",
]
process_code = processes.loc[
~processes["is_transport"], "process_name"
~processes["is_transport"] & ~processes["is_re_generation"], "process_name"
].to_list()

if data_type == "transportation_processes":
Expand All @@ -272,7 +291,18 @@ def get_data_type_from_input_data(
# FIXME: add bunker fuel consumption
]
process_code = processes.loc[
processes["is_transport"], "process_name"
processes["is_transport"] & ~processes["is_transformation"], "process_name"
].to_list()

if data_type == "reconversion_processes":
parameter_code = [
"CAPEX",
"OPEX (fix)",
"lifetime / amortization period",
"efficiency",
]
process_code = processes.loc[
processes["is_transport"] & processes["is_transformation"], "process_name"
].to_list()

if data_type in ["CAPEX", "full load hours", "interest rate"]:
Expand Down Expand Up @@ -463,9 +493,10 @@ def config_number_columns(df: pd.DataFrame, **kwargs) -> {}:
def display_and_edit_input_data(
api: PtxboaAPI,
data_type: Literal[
"electricity_generation",
"conversion_processes",
"transportation_processes",
"CAPEX",
"reconversion_processes" "CAPEX",
"full load hours",
"interest rate",
],
Expand All @@ -484,8 +515,8 @@ def display_and_edit_input_data(
an instance of the api class
data_type : str
the data type which should be selected. Needs to be one of
"conversion_processes", "transportation_processes", "CAPEX", "full load hours",
and "interest rate".
"electricity_generation", "conversion_processes", "transportation_processes",
"reconversion_processes", "CAPEX", "full load hours", and "interest rate".
scope : Literal[None, "world", "Argentina", "Morocco", "South Africa"]
The regional scope. Is automatically set to None for data of
data type "conversion_processes" and "transportation_processes" which is not
Expand All @@ -499,14 +530,23 @@ def display_and_edit_input_data(
"""
df = get_data_type_from_input_data(api, data_type=data_type, scope=scope)

if data_type in ["conversion_processes", "transportation_processes"]:
if data_type in [
"electricity_generation",
"conversion_processes",
"transportation_processes",
"reconversion_processes",
]:
index = "process_code"
columns = "parameter_code"
missing_index_name = "source_region_code"
missing_index_value = None
column_config = None

if data_type == "conversion_processes":
if data_type in [
"electricity_generation",
"conversion_processes",
"reconversion_processes",
]:
column_config = {
"CAPEX": st.column_config.NumberColumn(format="%.0f USD/kW", min_value=0),
"OPEX (fix)": st.column_config.NumberColumn(
Expand All @@ -522,18 +562,15 @@ def display_and_edit_input_data(

if data_type == "transportation_processes":
column_config = {
"levelized_costs": st.column_config.NumberColumn(
format="%.0f USD/kW", min_value=0
),
"OPEX (fix)": st.column_config.NumberColumn(
format="%.0f USD/kW", min_value=0
),
"efficiency": st.column_config.NumberColumn(
format="%.2f", min_value=0, max_value=1
"levelized costs": st.column_config.NumberColumn(
format="%.2e USD/(kW km)", min_value=0
),
"lifetime / amortization period": st.column_config.NumberColumn(
format="%.0f a", min_value=0
),
"losses (own fuel, transport)": st.column_config.NumberColumn(
format="%.2e fraction per km", min_value=0
),
}

if data_type == "interest rate":
Expand Down Expand Up @@ -605,7 +642,6 @@ def display_and_edit_input_data(
use_container_width=True,
column_config=column_config,
)

return df


Expand All @@ -622,3 +658,18 @@ def move_to_tab(tab_name):
old_tab_key_nb = int(st.session_state["tab_key"].replace("tab_key_", ""))
st.session_state["tab_key"] = f"tab_key_{old_tab_key_nb + 1}"
st.session_state[st.session_state["tab_key"]] = tab_name


def read_markdown_file(markdown_file: str) -> str:
"""Import markdown file as string."""
return Path(markdown_file).read_text(encoding="UTF-8")


def get_region_from_subregion(subregion: str) -> str:
"""
For a subregion, get the name of the region it belongs to.
If subregion is not a subregion, return its own name.
"""
region = subregion.split(" (")[0]
return region
11 changes: 3 additions & 8 deletions app/tab_certification_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
import streamlit as st

from app.ptxboa_functions import read_markdown_file


def _render_scheme_info(context_data, scheme_name):
df = context_data["certification_schemes"]
Expand Down Expand Up @@ -68,14 +70,7 @@ def _render_scheme_info(context_data, scheme_name):

def content_certification_schemes(context_data: dict):
with st.expander("What is this?"):
st.markdown(
"""
**Get supplementary information on H2-relevant certification frameworks**
This sheet provides you with an overview of current governmental regulations
and voluntary standards for H2 products.
"""
)
st.markdown(read_markdown_file("md/whatisthis_certification_schemes.md"))

helptext = "Select the certification scheme you want to know more about."
scheme_name = st.selectbox(
Expand Down
71 changes: 25 additions & 46 deletions app/tab_country_fact_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""Content of country fact sheets tab and functions to create it."""
import streamlit as st

from app.ptxboa_functions import get_region_from_subregion, read_markdown_file


def _create_fact_sheet_demand_country(context_data: dict):
# select country:
Expand Down Expand Up @@ -67,61 +69,38 @@ def _create_fact_sheet_demand_country(context_data: dict):

def _create_fact_sheet_supply_country(context_data: dict):
"""Display information on a chosen supply country."""
# select country:
# select region:
country_name = st.session_state["region"]
df = context_data["supply"]
data = df.loc[df["country_name"] == country_name].iloc[0].to_dict()

st.subheader(f"Fact sheet for {country_name}")
text = (
"**Technical potential for renewable electricity generation:**\n"
f"- {data['source_re_tech_pot_EWI']}: "
f"\t{data['re_tech_pot_EWI']:.0f} TWh/a\n"
f"- {data['source_re_tech_pot_PTXAtlas']}: "
f"\t{data['re_tech_pot_PTXAtlas']:.0f} TWh/a\n"
)

st.markdown(text)

text = (
"**LNG infrastructure:**\n"
f"- {data['lng_export']} export terminals\n"
f"- {data['lng_import']} import terminals.\n\n"
f"*Source: {data['source_lng']}*"
)
# for subregions, select name of region they belong to:
region_name = get_region_from_subregion(country_name)
df = context_data["supply"]
data = df.loc[df["country_name"] == region_name].iloc[0].to_dict()

st.subheader(f"Fact sheet for {region_name}")
with st.expander("**Technical potential for renewable electricity generation**"):
text = (
f"- {data['source_re_tech_pot_EWI']}: "
f"\t{data['re_tech_pot_EWI']:.0f} TWh/a\n"
f"- {data['source_re_tech_pot_PTXAtlas']}: "
f"\t{data['re_tech_pot_PTXAtlas']:.0f} TWh/a\n"
)
st.markdown(text)

st.markdown(text)
with st.expander("**LNG infrastructure**"):
text = (
f"- {data['lng_export']} export terminals\n"
f"- {data['lng_import']} import terminals.\n\n"
f"*Source: {data['source_lng']}*"
)
st.markdown(text)

st.write("TODO: CCS pot, elec prices, H2 strategy")


def content_country_fact_sheets(context_data):
with st.expander("What is this?"):
st.markdown(
"""
**Country fact sheets**
This sheet provides you with additional information on the production and import of
hydrogen and derivatives in all PTX BOA supply and demand countries.
For each selected supply and demand country pair, you will find detailed
country profiles.
For demand countries, we cover the following aspects:
country-specific projected hydrogen demand,
target sectors for hydrogen use,
hydrogen-relevant policies and competent authorities,
certification and regulatory frameworks,
and country-specific characteristics as defined in the demand countries'
hydrogen strategies.
For the supplying countries, we cover the country-specific technical potential
for renewables (based on various data sources),
LNG export and import infrastructure,
CCS potentials,
availability of an H2 strategy
and wholesale electricity prices.
"""
)
st.markdown(read_markdown_file("md/whatisthis_country_fact_sheets.md"))
with st.container(border=True):
_create_fact_sheet_demand_country(context_data)
with st.container(border=True):
Expand Down
Loading

0 comments on commit b661753

Please sign in to comment.