def blend_optical_flows(flows, weights):
    """Combine advection fields using given weights. Following :cite:`BPS2006`
    the second level of the cascade is used for the weights

    Parameters
    ----------
    flows : array-like
      A stack of multiple advenction fields having shape
      (S, 2, m, n), where flows[N, :, :, :] contains the motion vectors
      for source N.
      Advection fields for each source can be obtanined by
      calling any of the methods implemented in
      pysteps.motion and then stack all together
    weights : array-like
      An array of shape [number_sources]
      containing the weights to be used to combine
      the advection fields of each source.
      weights are modified to make their sum equal to one.
    Returns
    -------
    out: ndarray
        Return the blended advection field having shape
        (2, m, n), where out[0, :, :] contains the x-components of
        the blended motion vectors and out[1, :, :] contains the y-components.
        The velocities are in units of pixels / timestep.
    """

    # check inputs
    if isinstance(flows, (list, tuple)):
        flows = np.stack(flows)

    if isinstance(weights, (list, tuple)):
        weights = np.asarray(weights)

    # check weights dimensions match number of sources
    num_sources = flows.shape[0]
    num_weights = weights.shape[0]

    if num_weights != num_sources:
        raise ValueError(
            "dimension mismatch between flows and weights.\n"
            "weights dimension must match the number of flows.\n"
            f"number of flows={num_sources}, number of weights={num_weights}"
        )
    # normalize weigths
    weights = weights / np.sum(weights)

    # flows dimension sources, 2, m, n
    # weights dimension sources
    # move source axis to last to allow broadcasting
    # TODO: Check if broadcasting has worked well
    all_c_wn = weights * np.moveaxis(flows, 0, -1)
    # sum uses last axis
    combined_flows = np.sum(all_c_wn, axis=-1)
    # combined_flows [2, m, n]
    return combined_flows
Combine advection fields using given weights. Following :cite:`BPS2006`
the second level of the cascade is used for the weights

Parameters
----------
flows : array-like
A stack of multiple advenction fields having shape
(S, 2, m, n), where flows[N, :, :, :] contains the motion vectors
for source N.
Advection fields for each source can be obtanined by
calling any of the methods implemented in
pysteps.motion and then stack all together
weights : array-like
An array of shape [number_sources]
containing the weights to be used to combine
the advection fields of each source.
weights are modified to make their sum equal to one.
Returns
-------
out: ndarray
Return the blended advection field having shape
(2, m, n), where out[0, :, :] contains the x-components of
the blended motion vectors and out[1, :, :] contains the y-components.
The velocities are in units of pixels / timestep.
pysteps/blending/utils.py