def aggregate_fields_time(R, metadata, time_window_min, ignore_nan=False):
    """Aggregate fields in time.

    Parameters
    ----------
    R: array-like
        Array of shape (t,m,n) or (l,t,m,n) containing
        a time series of (ensemble) input fields.
        They must be evenly spaced in time.
    metadata: dict
        Metadata dictionary containing the timestamps and unit attributes as
        described in the documentation of :py:mod:`pysteps.io.importers`.
    time_window_min: float or None
        The length in minutes of the time window that is used to
        aggregate the fields.
        The time spanned by the t dimension of R must be a multiple of
        time_window_min.
        If set to None, it returns a copy of the original R and metadata.
    ignore_nan: bool, optional
        If True, ignore nan values.

    Returns
    -------
    outputarray: array-like
        The new array of aggregated fields of shape (k,m,n) or (l,k,m,n), where
        k = t*delta/time_window_min and delta is the time interval between two
        successive timestamps.
    metadata: dict
        The metadata with updated attributes.

    See also
    --------
    pysteps.utils.dimension.aggregate_fields_space,
    pysteps.utils.dimension.aggregate_fields
    """

    R = R.copy()
    metadata = metadata.copy()

    if time_window_min is None:
        return R, metadata

    unit = metadata["unit"]
    timestamps = metadata["timestamps"]
    if "leadtimes" in metadata:
        leadtimes = metadata["leadtimes"]

    if len(R.shape) < 3:
        raise ValueError("The number of dimension must be > 2")
    if len(R.shape) == 3:
        axis = 0
    if len(R.shape) == 4:
        axis = 1
    if len(R.shape) > 4:
        raise ValueError("The number of dimension must be <= 4")

    if R.shape[axis] != len(timestamps):
        raise ValueError(
            "The list of timestamps has length %i, " % len(timestamps)
            + "but R contains %i frames" % R.shape[axis]
        )

    # assumes that frames are evenly spaced
    delta = (timestamps[1] - timestamps[0]).seconds / 60
    if delta == time_window_min:
        return R, metadata
    if (R.shape[axis] * delta) % time_window_min:
        raise ValueError("time_window_size does not equally split R")

    nframes = int(time_window_min / delta)

    # specify the operator to be used to aggregate
    # the values within the time window
    if unit == "mm/h":
        method = "mean"
    elif unit == "mm":
        method = "sum"
    else:
        raise ValueError(
            "can only aggregate units of 'mm/h' or 'mm'" + " not %s" % unit
        )

    if ignore_nan:
        method = "".join(("nan", method))

    R = aggregate_fields(R, nframes, axis=axis, method=method)

    metadata["accutime"] = time_window_min
    metadata["timestamps"] = timestamps[nframes - 1 :: nframes]
    if "leadtimes" in metadata:
        metadata["leadtimes"] = leadtimes[nframes - 1 :: nframes]

    return R, metadata
Aggregate fields in time.

Parameters
----------
R: array-like
Array of shape (t,m,n) or (l,t,m,n) containing
a time series of (ensemble) input fields.
They must be evenly spaced in time.
metadata: dict
Metadata dictionary containing the timestamps and unit attributes as
described in the documentation of :py:mod:`pysteps.io.importers`.
time_window_min: float or None
The length in minutes of the time window that is used to
aggregate the fields.
The time spanned by the t dimension of R must be a multiple of
time_window_min.
If set to None, it returns a copy of the original R and metadata.
ignore_nan: bool, optional
If True, ignore nan values.

Returns
-------
outputarray: array-like
The new array of aggregated fields of shape (k,m,n) or (l,k,m,n), where
k = t*delta/time_window_min and delta is the time interval between two
successive timestamps.
metadata: dict
The metadata with updated attributes.

See also
--------
pysteps.utils.dimension.aggregate_fields_space,
pysteps.utils.dimension.aggregate_fields
pysteps/utils/dimension.py