def replace_gen(network,gen_to_replace):
    """Replace the generator gen_to_replace with a bus for the energy
    carrier, a link for the conversion from the energy carrier to electricity
    and a store to keep track of the depletion of the energy carrier and its
    CO2 emissions."""
    

    gen = network.generators.loc[gen_to_replace]

    bus_name = "{} {}".format(gen["bus"], gen["carrier"])
    link_name = "{} converter {} to AC".format(gen_to_replace,gen["carrier"])
    store_name = "{} store {}".format(gen_to_replace,gen["carrier"])

    network.add("Bus",
            bus_name,
            carrier=gen["carrier"])

    network.add("Link",
            link_name,
            bus0=bus_name,
            bus1=gen["bus"],
            capital_cost=gen["capital_cost"]*gen["efficiency"],
            p_nom = gen["p_nom"]/gen["efficiency"],
            p_nom_extendable=gen["p_nom_extendable"],
            p_nom_max = gen["p_nom_max"]/gen["efficiency"],
            p_nom_min = gen["p_nom_min"]/gen["efficiency"],
            p_max_pu = network.generators_t.p_max_pu.loc[:,gen_to_replace] if gen_to_replace in network.generators_t.p_max_pu.columns else gen["p_max_pu"],
            p_min_pu = network.generators_t.p_min_pu.loc[:,gen_to_replace] if gen_to_replace in network.generators_t.p_min_pu.columns else gen["p_min_pu"],
            marginal_cost=gen["marginal_cost"]*gen["efficiency"],
            efficiency=gen["efficiency"])

    network.add("Store",
            store_name,
            bus=bus_name,
            e_nom_min=-float("inf"),
            e_nom_max=0,
            e_nom_extendable=True,
            e_min_pu=1.,
            e_max_pu=0.)

    network.remove("Generator",gen_to_replace)

    return bus_name, link_name, store_name
Replace the generator gen_to_replace with a bus for the energy
    carrier, a link for the conversion from the energy carrier to electricity
    and a store to keep track of the depletion of the energy carrier and its
    CO2 emissions.