Skip to content

balances.py

view_balance_biomass(result_path, nc, config)

Evaluate and export the solid biomass balance showing biomass supply and consumption.

This function analyzes solid biomass flows in the energy system, including biomass supply from forestry and agriculture, biomass imports, and consumption in biomass boilers, combined heat and power plants, and other biomass-using technologies. It delegates to the simple_bus_balance function to collect and export supply and withdrawal statistics for solid biomass buses.

Parameters:

Name Type Description Default
result_path str | pathlib.Path

Path where the evaluation results will be saved.

required
nc pypsa.NetworkCollection

Dictionary containing PyPSA network objects, typically keyed by year or scenario.

required
config dict

Configuration dictionary containing view settings including bus_carrier specification, chart type, and export parameters.

required
Source code in evals/views/balances.py
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
def view_balance_biomass(
    result_path: str | Path,
    nc: NetworkCollection,
    config: dict,
) -> None:
    """
    Evaluate and export the solid biomass balance showing biomass supply and consumption.

    This function analyzes solid biomass flows in the energy system, including biomass
    supply from forestry and agriculture, biomass imports, and consumption in biomass
    boilers, combined heat and power plants, and other biomass-using technologies. It
    delegates to the simple_bus_balance function to collect and export supply and
    withdrawal statistics for solid biomass buses.

    Parameters
    ----------
    result_path
        Path where the evaluation results will be saved.
    nc
        Dictionary containing PyPSA network objects, typically keyed by year or scenario.
    config
        Configuration dictionary containing view settings including bus_carrier specification,
        chart type, and export parameters.
    """
    simple_bus_balance(nc, config, result_path)

view_balance_carbon(result_path, nc, config)

Evaluate and export the carbon balance showing CO2 flows to and from atmosphere.

This function analyzes CO2 emissions and deductions from the atmosphere bus only. It corresponds shows the national CO2 budget per year.

Parameters:

Name Type Description Default
result_path str | pathlib.Path

Path where the evaluation results will be saved.

required
nc pypsa.NetworkCollection

Dictionary containing PyPSA network objects, typically keyed by year or scenario.

required
config dict

Configuration dictionary containing view settings including bus_carrier specification, chart type, and export parameters.

required
Source code in evals/views/balances.py
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
def view_balance_carbon(
    result_path: str | Path,
    nc: NetworkCollection,
    config: dict,
) -> None:
    """
    Evaluate and export the carbon balance showing CO2 flows to and from atmosphere.

    This function analyzes CO2 emissions and deductions from the atmosphere bus only.
    It corresponds shows the national CO2 budget per year.

    Parameters
    ----------
    result_path
        Path where the evaluation results will be saved.
    nc
        Dictionary containing PyPSA network objects, typically keyed by year or scenario.
    config
        Configuration dictionary containing view settings including bus_carrier specification,
        chart type, and export parameters.
    """
    bus_carrier = config["view"]["bus_carrier"]

    co2_balance = collect_myopic_statistics(
        nc, "energy_balance", bus_carrier=bus_carrier
    )

    # need to deduct emission from international aviation.
    first_year = nc.index[0]
    energy_totals = pd.DataFrame.from_dict(
        nc[first_year].meta["resources"]["energy_totals"], orient="tight"
    )
    domestic_aviation_factors = get_energy_totals_domestic_share(
        energy_totals, kind="aviation"
    )

    # The domestic aviation factor reduces co2 emissions for aviation per country
    for ct in energy_totals.index:
        mask_country = co2_balance.index.get_level_values(DM.LOCATION).str.startswith(
            ct
        )
        mask_carrier = (
            co2_balance.index.get_level_values(DM.CARRIER) == "kerosene for aviation"
        )
        co2_balance.loc[mask_country & mask_carrier] *= domestic_aviation_factors[ct]

    exporter = Exporter(statistics=[co2_balance], view_config=config["view"])
    exporter.export(result_path, config["global"]["subdir"])

view_balance_electricity(result_path, nc, config)

Evaluate and export electricity production and demand by country and year.

This function calculates the electricity balance showing generation sources and consumption patterns across the network. It delegates to the simple_bus_balance function to collect and export supply and withdrawal statistics for electricity buses.

Parameters:

Name Type Description Default
result_path str | pathlib.Path

Path where the evaluation results will be saved.

required
nc pypsa.NetworkCollection

Dictionary containing PyPSA network objects, typically keyed by year or scenario.

required
config dict

Configuration dictionary containing view settings including bus_carrier specification, chart type, and export parameters.

required
Notes

Balances do not add up to zero, because of domestic transmission losses.

Source code in evals/views/balances.py
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
def view_balance_electricity(
    result_path: str | Path,
    nc: NetworkCollection,
    config: dict,
) -> None:
    """
    Evaluate and export electricity production and demand by country and year.

    This function calculates the electricity balance showing generation sources and
    consumption patterns across the network. It delegates to the simple_bus_balance
    function to collect and export supply and withdrawal statistics for electricity buses.

    Parameters
    ----------
    result_path
        Path where the evaluation results will be saved.
    nc
        Dictionary containing PyPSA network objects, typically keyed by year or scenario.
    config
        Configuration dictionary containing view settings including bus_carrier specification,
        chart type, and export parameters.

    Notes
    -----
    Balances do not add up to zero, because of domestic transmission losses.
    """
    simple_bus_balance(nc, config, result_path)

view_balance_fuels(result_path, nc, config)

Evaluate and export the fuel balance showing primary fuel supply and consumption.

This function calculates the energy balance for primary fuel carriers including coal, lignite, oil, solid biomass, methanol, ammonia, waste, and uranium. It delegates to the simple_bus_balance function to collect and export supply and withdrawal statistics for specified fuel bus carriers. The view supports grouping of related fuels (e.g., coal and lignite) through the bus_carrier_groups configuration.

Parameters:

Name Type Description Default
result_path str | pathlib.Path

Path where the evaluation results will be saved.

required
nc pypsa.NetworkCollection

Dictionary containing PyPSA network objects, typically keyed by year or scenario.

required
config dict

Configuration dictionary containing view settings including bus_carrier specification, bus_carrier_groups for aggregating related fuels, chart type, and export parameters.

required
Notes

This view is particularly useful for analyzing primary energy supply and fuel consumption patterns across different sectors (industry, transport, heat, power generation). The bus_carrier_groups configuration allows aggregation of similar fuels for clearer visualization, such as combining coal and lignite into a single "Coal" category.

Source code in evals/views/balances.py
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
def view_balance_fuels(
    result_path: str | Path,
    nc: NetworkCollection,
    config: dict,
) -> None:
    """
    Evaluate and export the fuel balance showing primary fuel supply and consumption.

    This function calculates the energy balance for primary fuel carriers including coal,
    lignite, oil, solid biomass, methanol, ammonia, waste, and uranium. It delegates to
    the simple_bus_balance function to collect and export supply and withdrawal statistics
    for specified fuel bus carriers. The view supports grouping of related fuels (e.g.,
    coal and lignite) through the bus_carrier_groups configuration.

    Parameters
    ----------
    result_path
        Path where the evaluation results will be saved.
    nc
        Dictionary containing PyPSA network objects, typically keyed by year or scenario.
    config
        Configuration dictionary containing view settings including bus_carrier specification,
        bus_carrier_groups for aggregating related fuels, chart type, and export parameters.

    Notes
    -----
    This view is particularly useful for analyzing primary energy supply and fuel consumption
    patterns across different sectors (industry, transport, heat, power generation). The
    bus_carrier_groups configuration allows aggregation of similar fuels for clearer
    visualization, such as combining coal and lignite into a single "Coal" category.
    """
    simple_bus_balance(nc, config, result_path)

view_balance_heat(result_path, nc, config)

Evaluate and export the heat balance showing heat production and consumption.

This function calculates the heat balance for specified heat bus carriers (urban central, urban decentral, rural heat) by analyzing link energy flows and load withdrawals. Heat supply is determined from link energy balance data, showing the input energy carriers feeding heat production. Heat demand includes consumption loads, with central heat losses separated from actual consumption. The function exports both supply and demand data with appropriate chart formatting based on the configured chart type.

Parameters:

Name Type Description Default
result_path str | pathlib.Path

Path where the evaluation results will be saved.

required
nc pypsa.NetworkCollection

Dictionary containing PyPSA network objects, typically keyed by year or scenario.

required
config dict

Configuration dictionary containing view settings including bus_carrier specification, chart type, and export parameters.

required
Notes

Heat supply is calculated from link energy balances, excluding CO2 flows. For urban central heat, distribution losses are separated from consumption using heat loss factors. The function supports both ESMGroupedBarChart (preserving individual bus carriers) and ESMBarChart (aggregating bus carriers) visualization modes.

Source code in evals/views/balances.py
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
def view_balance_heat(
    result_path: str | Path,
    nc: NetworkCollection,
    config: dict,
) -> None:
    """
    Evaluate and export the heat balance showing heat production and consumption.

    This function calculates the heat balance for specified heat bus carriers (urban central,
    urban decentral, rural heat) by analyzing link energy flows and load withdrawals. Heat
    supply is determined from link energy balance data, showing the input energy carriers
    feeding heat production. Heat demand includes consumption loads, with central heat losses
    separated from actual consumption. The function exports both supply and demand data with
    appropriate chart formatting based on the configured chart type.

    Parameters
    ----------
    result_path
        Path where the evaluation results will be saved.
    nc
        Dictionary containing PyPSA network objects, typically keyed by year or scenario.
    config
        Configuration dictionary containing view settings including bus_carrier specification,
        chart type, and export parameters.

    Notes
    -----
    Heat supply is calculated from link energy balances, excluding CO2 flows. For urban
    central heat, distribution losses are separated from consumption using heat loss factors.
    The function supports both ESMGroupedBarChart (preserving individual bus carriers) and
    ESMBarChart (aggregating bus carriers) visualization modes.
    """
    bus_carrier = config["view"]["bus_carrier"]

    heat_mix = get_energy_for_heat_production(nc, drop_regex="")
    heat_mix = heat_mix.swaplevel(DM.CARRIER, DM.BUS_CARRIER)
    heat_mix.index.names = DM.YEAR_IDX_NAMES

    generator_supply = collect_myopic_statistics(
        nc,
        statistic="supply",
        comps="Generator",
        bus_carrier=bus_carrier,
    )

    heat_loss_factor = get_heat_loss_factor(nc)
    demand = (
        collect_myopic_statistics(
            nc,
            statistic="withdrawal",
            bus_carrier=bus_carrier,
        )
        .pipe(split_urban_central_heat_losses_and_consumption, heat_loss_factor)
        .mul(-1)
    )

    exporter = Exporter(
        statistics=[heat_mix, demand, generator_supply], view_config=config["view"]
    )
    exporter.export(result_path, config["global"]["subdir"])

view_balance_hydrogen(result_path, nc, config)

Evaluate and export the hydrogen balance showing H2 production and consumption.

This function analyzes hydrogen flows in the energy system, including production from electrolyzers and other sources, as well as consumption from fuel cells, industrial processes, and other hydrogen-using technologies. It delegates to the simple_bus_balance function to collect and export supply and withdrawal statistics for hydrogen buses.

Parameters:

Name Type Description Default
result_path str | pathlib.Path

Path where the evaluation results will be saved.

required
nc pypsa.NetworkCollection

Dictionary containing PyPSA network objects, typically keyed by year or scenario.

required
config dict

Configuration dictionary containing view settings including bus_carrier specification, chart type, and export parameters.

required
Source code in evals/views/balances.py
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
def view_balance_hydrogen(
    result_path: str | Path,
    nc: NetworkCollection,
    config: dict,
) -> None:
    """
    Evaluate and export the hydrogen balance showing H2 production and consumption.

    This function analyzes hydrogen flows in the energy system, including production
    from electrolyzers and other sources, as well as consumption from fuel cells,
    industrial processes, and other hydrogen-using technologies. It delegates to the
    simple_bus_balance function to collect and export supply and withdrawal statistics
    for hydrogen buses.

    Parameters
    ----------
    result_path
        Path where the evaluation results will be saved.
    nc
        Dictionary containing PyPSA network objects, typically keyed by year or scenario.
    config
        Configuration dictionary containing view settings including bus_carrier specification,
        chart type, and export parameters.
    """
    simple_bus_balance(nc, config, result_path)

view_balance_methane(result_path, nc, config)

Evaluate and export the methane balance showing natural gas and biogas flows.

This function analyzes methane (CH4) flows in the energy system, including natural gas supply from pipelines and storage, biogas production, methanation processes, and consumption in gas boilers, combined heat and power plants, and other gas-consuming technologies. It delegates to the simple_bus_balance function to collect and export supply and withdrawal statistics for methane buses.

Parameters:

Name Type Description Default
result_path str | pathlib.Path

Path where the evaluation results will be saved.

required
nc pypsa.NetworkCollection

Dictionary containing PyPSA network objects, typically keyed by year or scenario.

required
config dict

Configuration dictionary containing view settings including bus_carrier specification, chart type, and export parameters.

required
Source code in evals/views/balances.py
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
def view_balance_methane(
    result_path: str | Path,
    nc: NetworkCollection,
    config: dict,
) -> None:
    """
    Evaluate and export the methane balance showing natural gas and biogas flows.

    This function analyzes methane (CH4) flows in the energy system, including natural gas
    supply from pipelines and storage, biogas production, methanation processes, and
    consumption in gas boilers, combined heat and power plants, and other gas-consuming
    technologies. It delegates to the simple_bus_balance function to collect and export
    supply and withdrawal statistics for methane buses.

    Parameters
    ----------
    result_path
        Path where the evaluation results will be saved.
    nc
        Dictionary containing PyPSA network objects, typically keyed by year or scenario.
    config
        Configuration dictionary containing view settings including bus_carrier specification,
        chart type, and export parameters.
    """
    simple_bus_balance(nc, config, result_path)