def clip_domain(R, metadata, extent=None):
    """
    Clip the field domain by geographical coordinates.

    Parameters
    ----------
    R: array-like
        Array of shape (m,n) or (t,m,n) containing the input fields.
    metadata: dict
        Metadata dictionary containing the x1, x2, y1, y2,
        xpixelsize, ypixelsize,
        zerovalue and yorigin attributes as described in the documentation of
        :py:mod:`pysteps.io.importers`.
    extent: scalars (left, right, bottom, top), optional
        The extent of the bounding box in data coordinates to be used to clip
        the data.
        Note that the direction of the vertical axis and thus the default
        values for top and bottom depend on origin. We follow the same
        convention as in the imshow method of matplotlib:
        https://matplotlib.org/tutorials/intermediate/imshow_extent.html

    Returns
    -------
    R: array-like
        the clipped array
    metadata: dict
        the metadata with updated attributes.
    """

    R = R.copy()
    R_shape = np.array(R.shape)
    metadata = metadata.copy()

    if extent is None:
        return R, metadata

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

    # extract original domain coordinates
    left = metadata["x1"]
    right = metadata["x2"]
    bottom = metadata["y1"]
    top = metadata["y2"]

    # extract bounding box coordinates
    left_ = extent[0]
    right_ = extent[1]
    bottom_ = extent[2]
    top_ = extent[3]

    # compute its extent in pixels
    dim_x_ = int((right_ - left_) / metadata["xpixelsize"])
    dim_y_ = int((top_ - bottom_) / metadata["ypixelsize"])
    R_ = np.ones((R.shape[0], R.shape[1], dim_y_, dim_x_)) * metadata["zerovalue"]

    # build set of coordinates for the original domain
    y_coord = (
        np.linspace(bottom, top - metadata["ypixelsize"], R.shape[2])
        + metadata["ypixelsize"] / 2.0
    )
    x_coord = (
        np.linspace(left, right - metadata["xpixelsize"], R.shape[3])
        + metadata["xpixelsize"] / 2.0
    )

    # build set of coordinates for the new domain
    y_coord_ = (
        np.linspace(bottom_, top_ - metadata["ypixelsize"], R_.shape[2])
        + metadata["ypixelsize"] / 2.0
    )
    x_coord_ = (
        np.linspace(left_, right_ - metadata["xpixelsize"], R_.shape[3])
        + metadata["xpixelsize"] / 2.0
    )

    # origin='upper' reverses the vertical axes direction
    if metadata["yorigin"] == "upper":
        y_coord = y_coord[::-1]
        y_coord_ = y_coord_[::-1]

    # extract original domain
    idx_y = np.where(np.logical_and(y_coord < top_, y_coord > bottom_))[0]
    idx_x = np.where(np.logical_and(x_coord < right_, x_coord > left_))[0]

    # extract new domain
    idx_y_ = np.where(np.logical_and(y_coord_ < top, y_coord_ > bottom))[0]
    idx_x_ = np.where(np.logical_and(x_coord_ < right, x_coord_ > left))[0]

    # compose the new array
    R_[:, :, idx_y_[0] : (idx_y_[-1] + 1), idx_x_[0] : (idx_x_[-1] + 1)] = R[
        :, :, idx_y[0] : (idx_y[-1] + 1), idx_x[0] : (idx_x[-1] + 1)
    ]

    # update coordinates
    metadata["y1"] = bottom_
    metadata["y2"] = top_
    metadata["x1"] = left_
    metadata["x2"] = right_

    R_shape[-2] = R_.shape[-2]
    R_shape[-1] = R_.shape[-1]

    return R_.reshape(R_shape), metadata
Clip the field domain by geographical coordinates.

Parameters
----------
R: array-like
Array of shape (m,n) or (t,m,n) containing the input fields.
metadata: dict
Metadata dictionary containing the x1, x2, y1, y2,
xpixelsize, ypixelsize,
zerovalue and yorigin attributes as described in the documentation of
:py:mod:`pysteps.io.importers`.
extent: scalars (left, right, bottom, top), optional
The extent of the bounding box in data coordinates to be used to clip
the data.
Note that the direction of the vertical axis and thus the default
values for top and bottom depend on origin. We follow the same
convention as in the imshow method of matplotlib:
https://matplotlib.org/tutorials/intermediate/imshow_extent.html

Returns
-------
R: array-like
the clipped array
metadata: dict
the metadata with updated attributes.
pysteps/utils/dimension.py