diff --git a/doc/release_notes.rst b/doc/release_notes.rst index ddd1483e..727675c2 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -16,6 +16,8 @@ Upcoming Release * Bugfix: Correct typo in the CPLEX solver configuration in ``config.default.yaml``. +* Bugfix: Error in ``add_electricity`` where carriers were added multiple times to the network, resulting in a non-unique carriers error. + * Renamed script file from PyPSA-EUR ``build_load_data`` to ``build_electricity_demand`` and ``retrieve_load_data`` to ``retrieve_electricity_demand``. * Fix docs readthedocs built diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index 94fa09e2..9031c44d 100755 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -121,6 +121,15 @@ def calculate_annuity(n, r): return 1 / n +def add_missing_carriers(n, carriers): + """ + Function to add missing carriers to the network without raising errors. + """ + missing_carriers = set(carriers) - set(n.carriers.index) + if len(missing_carriers) > 0: + n.madd("Carrier", missing_carriers) + + def sanitize_carriers(n, config): """ Sanitize the carrier information in a PyPSA Network object. @@ -150,9 +159,7 @@ def sanitize_carriers(n, config): for c in n.iterate_components(): if "carrier" in c.df: - missing_carrier = set(c.df.carrier.unique()) - set(n.carriers.index) - {""} - if len(missing_carrier): - n.madd("Carrier", missing_carrier) + add_missing_carriers(n, c.df) carrier_i = n.carriers.index nice_names = ( @@ -351,6 +358,7 @@ def update_transmission_costs(n, costs, length_factor=1.0): def attach_wind_and_solar( n, costs, input_profiles, carriers, extendable_carriers, line_length_factor=1 ): + for car in carriers: if car == "hydro": continue @@ -415,7 +423,7 @@ def attach_conventional_generators( fuel_price=None, ): carriers = list(set(conventional_carriers) | set(extendable_carriers["Generator"])) - n.madd("Carrier", carriers) + add_missing_carriers(n, carriers) add_co2_emissions(n, costs, carriers) ppl = ( @@ -492,7 +500,7 @@ def attach_conventional_generators( def attach_hydro(n, costs, ppl, profile_hydro, hydro_capacities, carriers, **params): - n.madd("Carrier", carriers) + add_missing_carriers(n, carriers) add_co2_emissions(n, costs, carriers) ppl = ( @@ -625,7 +633,7 @@ def attach_extendable_generators(n, costs, ppl, carriers): logger.warning( "The function `attach_extendable_generators` is deprecated in v0.5.0." ) - n.madd("Carrier", carriers) + add_missing_carriers(n, carriers) add_co2_emissions(n, costs, carriers) for tech in carriers: