How can I export a PyPSA network with multilinks?

87 Views Asked by At

I have a PyPSA network with multilinks which I defined with the override_component_attrs argument of pypsa.Network. When exporting and reimporting the network as netcfd or csv folder the non-standard attributes are lost.

Is there a way to keep the additionally defined attributes when exporting?

2

There are 2 best solutions below

0
On

If you provide the same override_component_attrs dict that you used to create the network when loading the pypsa.Network from file, the non-standard attributes are loaded as well. (They are always saved, just not loaded as of pypsa=0.23.0).

Instead of duplicating code you can either either carry the dict over between along with your network:

import pickle
import pypsa

override_component_attrs = pypsa.descriptors.Dict(
    {k: v.copy() for k, v in pypsa.components.component_attrs.items()}
)

override_component_attrs["Link"].loc["bus2"] = [
     "string", np.nan, np.nan,
     "Name of optional 3rd bus to which link is attached.",
     "Input (optional)",
]
override_component_attrs["Link"].loc["efficiency2"] = [
    "static or series", "per unit", np.nan,
    "Efficiency of power transfer from bus0 to bus2 (static or time-dependent)",
]

# more overwrites
# ...

# Save dict, e.g. as pickle
with open("override_component_attrs.pkl", "wb") as f:
        pickle.dump(override_component_attrs, f)

# When loading your Network, use the file for overwriting components
with open("override_component_attrs.pkl", "rb") as f:
    oca = pickle.load(f)

network = pypsa.Network(<path of network to load>, override_component_attrs=oca)

Or you create a common function which you call from your different scripts which creates a Network with the components overwritten and then call pypsa.Network.import_from_<netcdf|csv_folder>(...).

0
On

Currently, no. But it's high on the list for new features.

The non-standard attributes will be saved when exporting a network to netCDF, but not read in when importing by default.

At the moment, you need to redefine the override_component_attrs when importing a network with non-standard attributes.

https://fneum.github.io/data-science-for-esm/12-workshop-pypsa-sector-coupling.html#previous-capacity-expansion-model

override_component_attrs = pypsa.descriptors.Dict(
    {k: v.copy() for k, v in pypsa.components.component_attrs.items()}
)

override_component_attrs["Link"].loc["bus2"] = [
    "string",
    np.nan,
    np.nan,
    "2nd bus",
    "Input (optional)",
]
override_component_attrs["Link"].loc["efficiency2"] = [
    "static or series",
    "per unit",
    1.0,
    "2nd bus efficiency",
    "Input (optional)",
]
override_component_attrs["Link"].loc["p2"] = [
    "series",
    "MW",
    0.0,
    "2nd bus output",
    "Output",
]

See issues: