def _linda_perturbation_init(
    precip,
    precip_lagr_diff,
    velocity,
    forecast_gen,
    pert_thrs,
    localization_window_radius,
    errdist_window_radius,
    acf_window_radius,
    vel_pert_method,
    vel_pert_kwargs,
    kmperpixel,
    timestep,
    num_workers,
    use_multiprocessing,
    measure_time,
):
    """Initialize the LINDA perturbation generator."""
    if measure_time:
        starttime = time.time()

    print("Estimating forecast errors... ", end="", flush=True)

    forecast_gen = forecast_gen.copy()
    forecast_gen["add_perturbations"] = False
    forecast_gen["num_ens_members"] = 1

    precip_forecast_det = _linda_forecast(
        precip[:-1],
        precip_lagr_diff[:-1],
        1,
        forecast_gen,
        None,
        None,
        1,
        None,
        False,
        False,
        True,
        None,
    )

    # compute multiplicative forecast errors
    err = precip_forecast_det[-1] / precip[-1]

    # mask small precipitation intensities
    mask = np.logical_or(
        np.logical_and(
            precip_forecast_det[-1] >= pert_thrs[1], precip[-1] >= pert_thrs[0]
        ),
        np.logical_and(
            precip_forecast_det[-1] >= pert_thrs[0], precip[-1] >= pert_thrs[1]
        ),
    )
    err[~mask] = np.nan

    if measure_time:
        print(f"{time.time() - starttime:.2f} seconds.")
    else:
        print("done.")

    pert_gen = _estimate_perturbation_params(
        err,
        forecast_gen,
        errdist_window_radius,
        acf_window_radius,
        localization_window_radius,
        measure_time,
        num_workers,
        use_multiprocessing,
    )

    if vel_pert_method == "bps":
        init_vel_noise, generate_vel_noise = noise.get_method("bps")

        vp_par = vel_pert_kwargs["vp_par"]
        vp_perp = vel_pert_kwargs["vp_perp"]

        kwargs = {
            "p_par": vp_par,
            "p_perp": vp_perp,
        }
        velocity_pert_gen = {
            "gen_func": generate_vel_noise,
            "init_func": lambda seed: init_vel_noise(
                velocity, 1.0 / kmperpixel, timestep, seed=seed, **kwargs
            ),
            "timestep": timestep,
        }
    else:
        velocity_pert_gen = None

    if measure_time:
        return pert_gen, velocity_pert_gen, time.time() - starttime
    else:
        return pert_gen, velocity_pert_gen
Initialize the LINDA perturbation generator.
pysteps/nowcasts/linda.py