def aggregate_fields_space(R, metadata, space_window, ignore_nan=False):
    """
    Upscale fields in space.

    Parameters
    ----------
    R: array-like
        Array of shape (m,n), (t,m,n) or (l,t,m,n) containing a single field or
        a time series of (ensemble) input fields.
    metadata: dict
        Metadata dictionary containing the xpixelsize, ypixelsize and unit
        attributes as described in the documentation of
        :py:mod:`pysteps.io.importers`.
    space_window: float, tuple or None
        The length of the space window that is used to upscale the fields.
        If a float is given, the same window size is used for the x- and
        y-directions. Separate window sizes are used for x- and y-directions if
        a two-element tuple is given. The space_window unit is the same used in
        the geographical projection of R and hence the same as for the xpixelsize
        and ypixelsize attributes. The space spanned by the n- and m-dimensions
        of R must be a multiple of space_window. If set to None, the function
        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,j), (t,k,j) or (l,t,k,j),
        where k = m*ypixelsize/space_window[1] and j = n*xpixelsize/space_window[0].
    metadata: dict
        The metadata with updated attributes.

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

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

    if space_window is None:
        return R, metadata

    unit = metadata["unit"]
    ypixelsize = metadata["ypixelsize"]
    xpixelsize = metadata["xpixelsize"]

    if len(R.shape) < 2:
        raise ValueError("The number of dimensions must be >= 2")
    if len(R.shape) == 2:
        axes = [0, 1]
    if len(R.shape) == 3:
        axes = [1, 2]
    if len(R.shape) == 4:
        axes = [2, 3]
    if len(R.shape) > 4:
        raise ValueError("The number of dimensions must be <= 4")

    if np.isscalar(space_window):
        space_window = (space_window, space_window)

    # assumes that frames are evenly spaced
    if ypixelsize == space_window[1] and xpixelsize == space_window[0]:
        return R, metadata

    ysize = R.shape[axes[0]] * ypixelsize
    xsize = R.shape[axes[1]] * xpixelsize

    if (
        abs(ysize / space_window[1] - round(ysize / space_window[1])) > 1e-10
        or abs(xsize / space_window[0] - round(xsize / space_window[0])) > 1e-10
    ):
        raise ValueError("space_window does not equally split R")

    nframes = [int(space_window[1] / ypixelsize), int(space_window[0] / xpixelsize)]

    # specify the operator to be used to aggregate the values
    # within the space window
    if unit == "mm/h" or unit == "mm":
        method = "mean"
    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[0], axis=axes[0], method=method)
    R = aggregate_fields(R, nframes[1], axis=axes[1], method=method)

    metadata["ypixelsize"] = space_window[1]
    metadata["xpixelsize"] = space_window[0]

    return R, metadata
Upscale fields in space.

Parameters
----------
R: array-like
Array of shape (m,n), (t,m,n) or (l,t,m,n) containing a single field or
a time series of (ensemble) input fields.
metadata: dict
Metadata dictionary containing the xpixelsize, ypixelsize and unit
attributes as described in the documentation of
:py:mod:`pysteps.io.importers`.
space_window: float, tuple or None
The length of the space window that is used to upscale the fields.
If a float is given, the same window size is used for the x- and
y-directions. Separate window sizes are used for x- and y-directions if
a two-element tuple is given. The space_window unit is the same used in
the geographical projection of R and hence the same as for the xpixelsize
and ypixelsize attributes. The space spanned by the n- and m-dimensions
of R must be a multiple of space_window. If set to None, the function
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,j), (t,k,j) or (l,t,k,j),
where k = m*ypixelsize/space_window[1] and j = n*xpixelsize/space_window[0].
metadata: dict
The metadata with updated attributes.

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