from typing import OrderedDict


def arr_to_dict(arr, ref_dict):
    """
    Transform an array of data into a dictionary keyed by the same keys in
    ref_dict, with data divided into chunks of the same length as in ref_dict.
    Requires that the length of the array is the sum of the lengths of the
    arrays in each entry of ref_dict.  The other dimensions of the input
    array and reference dict can differ.

    Arguments
    ---------
    arr : array
        Input array to be transformed into dictionary.
    ref_dict : dict
        Reference dictionary containing the keys used to construct the output
        dictionary.

    Returns
    -------
    out : dict
        Dictionary of values from arr keyed with keys from ref_dict.
    """
    out = OrderedDict()
    idx = 0
    assert len(arr) == sum([len(v) for v in ref_dict.values()])
    for k, bd in ref_dict.items():
        out[k] = arr[idx : idx + len(bd)]
        idx += len(bd)
    return out