//  [python]
def _current_source_current(net, ppci, bus_idx):
    case = net._options["case"]
    fault_impedance = net._options["fault_impedance"]
    ppci["bus"][:, IKCV] = 0
    ppci["bus"][:, PHI_IKCV_DEGREE] = -90
    ppci["bus"][:, IKSS2] = 0
    type_c = net._options["use_pre_fault_voltage"]
    # sgen current source contribution only for Type A and case "max" or type C:
    if case != "max" and not type_c:
        return

    bus_lookup = net["_pd2ppc_lookups"]["bus"]
    fault = net._options["fault"]
    # _is_elements_final exists for some reason, and weirdly it can be different than _is_elements. 
    # it is not documented anywhere why it exists and I don't have any time to find out, but this here fixes the problem.

    if np.all(net.sgen.current_source.values):
        sgen = net.sgen[net._is_elements_final["sgen"]]
    else:
        sgen = net.sgen[net._is_elements_final["sgen"] & net.sgen.current_source]
    if len(sgen) == 0:
        return
    if any(pd.isnull(sgen.sn_mva)):
        raise ValueError("sn_mva needs to be specified for all sgens in net.sgen.sn_mva")
    if "current_angle_degree" in sgen.columns:
        sgen_angle = np.deg2rad(sgen.current_angle_degree.values)
    else:
        sgen_angle = None

    baseI = ppci["internal"]["baseI"]
    sgen_buses = sgen.bus.values
    sgen_buses_ppc = bus_lookup[sgen_buses]

    if "k" not in sgen:
        raise ValueError("Nominal to short-circuit current has to specified in net.sgen.k")
    if type_c and "kappa" not in sgen:
        raise ValueError("Max. short-circuit current in p.u. must be specified in net.sgen.kappa for the "
                         "short-circuit calculation with superposition method")

    if type_c:
        # voltage difference between pre-fault condition and the voltage from the calculation with
        # rotating machines only is used to determine the current injection. The parameter kappa here
        # is used to denote the maximal current contribution of the sgen in the short-circuit case "Type C"
        V_ikss = ppci["internal"]["V_ikss"]
        i_sgen_n_pu = (sgen.sn_mva.values.reshape(-1, 1) / net.sn_mva)
        delta_V = ppci["bus"][sgen_buses_ppc][:, [VM]] - np.abs(V_ikss[sgen_buses_ppc])
        i_sgen_pu = np.where(sgen.k.values.reshape(-1, 1) * delta_V < sgen.kappa.values.reshape(-1, 1),
                             sgen.k.values.reshape(-1, 1) * i_sgen_n_pu * delta_V,
                             i_sgen_n_pu * sgen.kappa.values.reshape(-1, 1))
        i_sgen_pu = np.abs(i_sgen_pu)
        extra_angle = ppci["bus"][sgen_buses_ppc, VA]
    else:
        i_sgen_pu = (sgen.sn_mva.values / net.sn_mva * sgen.k.values)
        extra_angle = 0

    if sgen_angle is not None and fault == "3ph":
        i_sgen_pu = i_sgen_pu * np.exp(sgen_angle * 1j)
    # if case == "min":
    #     i_sgen_pu *= 0

    buses, ikcv_pu, _ = _sum_by_group(sgen_buses_ppc, i_sgen_pu, i_sgen_pu)
    ikcv_pu = ikcv_pu.flatten()
    ppci["bus"][buses, [IKCV]] = ikcv_pu if sgen_angle is None else np.abs(ikcv_pu)
    if sgen_angle is not None and fault == "3ph":
        ppci["bus"][buses, PHI_IKCV_DEGREE] = np.angle(ikcv_pu, deg=True)

    if net["_options"]["inverse_y"]:
        Zbus = ppci["internal"]["Zbus"]
        diagZ = np.diag(Zbus).copy()  # here diagZ is not writeable
        if sgen_angle is None and fault == "3ph":
            ppci["bus"][buses, PHI_IKCV_DEGREE] = -np.angle(diagZ[buses], deg=True) + extra_angle
        diagZ[bus_idx] += fault_impedance
        i_kss_2 = 1 / diagZ * np.dot(Zbus, ppci["bus"][:, IKCV] * np.exp(np.deg2rad(ppci["bus"][:, PHI_IKCV_DEGREE]) * 1j))
    else:
        ybus_fact = ppci["internal"]["ybus_fact"]
        diagZ = _calc_zbus_diag(net, ppci)
        if sgen_angle is None and fault == "3ph":
            ppci["bus"][buses, PHI_IKCV_DEGREE] = -np.angle(diagZ[buses], deg=True) + extra_angle
        diagZ[bus_idx] += fault_impedance
        i_kss_2 = ybus_fact(ppci["bus"][:, IKCV] * np.exp(np.deg2rad(ppci["bus"][:, PHI_IKCV_DEGREE]) * 1j)) / diagZ

    ppci["bus"][:, IKSS2] = np.abs(i_kss_2 / baseI)
    ppci["bus"][:, PHI_IKSS2_DEGREE] = np.angle(i_kss_2, deg=True) if fault == "3ph" else 0
    ppci["bus"][buses, IKCV] /= baseI[buses]