def morph(image, displacement, gradient=False):
    """
    Morph image by applying a displacement field (Warping).

    The new image is created by selecting for each position the values of the
    input image at the positions given by the x and y displacements.
    The routine works in a backward sense.
    The displacement vectors have to refer to their destination.

    For more information in Morphing functions see Section 3 in
    `Beezley and Mandel (2008)`_.

    Beezley, J. D., & Mandel, J. (2008).
    Morphing ensemble Kalman filters. Tellus A, 60(1), 131-140.

    .. _`Beezley and Mandel (2008)`: http://dx.doi.org/10.1111/\
    j.1600-0870.2007.00275.x

    The displacement field in x and y directions and the image must have the
    same dimensions.

    The morphing is executed in parallel over x axis.

    The value of displaced pixels that fall outside the limits takes the
    value of the nearest edge. Those pixels are indicated by values greater
    than 1 in the output mask.

    Parameters
    ----------
    image: ndarray (ndim = 2)
        Image to morph
    displacement: ndarray (ndim = 3)
        Displacement field to be applied (Warping). The first dimension
        corresponds to the coordinate to displace.

        The dimensions are: displacement [ i/x (0) or j/y (1) ,
        i index of pixel, j index of pixel ]
    gradient: bool, optional
        If True, the gradient of the morphing function is returned.

    Returns
    -------
    image: ndarray (float64 ,ndim = 2)
        Morphed image.
    mask: ndarray (int8 ,ndim = 2)
        Invalid values mask. Points outside the boundaries are masked.
        Values greater than 1, indicate masked values.
    gradient_values: ndarray (float64 ,ndim = 3), optional
        If gradient keyword is True, the gradient of the function is also
        returned.
    """

    if not isinstance(image, MaskedArray):
        _mask = numpy.zeros_like(image, dtype="int8")
    else:
        _mask = numpy.asarray(numpy.ma.getmaskarray(image), dtype="int8", order="C")

    _image = numpy.asarray(image, dtype="float64", order="C")
    _displacement = numpy.asarray(displacement, dtype="float64", order="C")

    return _warp(_image, _mask, _displacement, gradient=gradient)
Morph image by applying a displacement field (Warping).

The new image is created by selecting for each position the values of the
input image at the positions given by the x and y displacements.
The routine works in a backward sense.
The displacement vectors have to refer to their destination.

For more information in Morphing functions see Section 3 in
`Beezley and Mandel (2008)`_.

Beezley, J. D., & Mandel, J. (2008).
Morphing ensemble Kalman filters. Tellus A, 60(1), 131-140.

.. _`Beezley and Mandel (2008)`: http://dx.doi.org/10.1111/\
j.1600-0870.2007.00275.x

The displacement field in x and y directions and the image must have the
same dimensions.

The morphing is executed in parallel over x axis.

The value of displaced pixels that fall outside the limits takes the
value of the nearest edge. Those pixels are indicated by values greater
than 1 in the output mask.

Parameters
----------
image: ndarray (ndim = 2)
Image to morph
displacement: ndarray (ndim = 3)
Displacement field to be applied (Warping). The first dimension
corresponds to the coordinate to displace.

The dimensions are: displacement [ i/x (0) or j/y (1) ,
i index of pixel, j index of pixel ]
gradient: bool, optional
If True, the gradient of the morphing function is returned.

Returns
-------
image: ndarray (float64 ,ndim = 2)
Morphed image.
mask: ndarray (int8 ,ndim = 2)
Invalid values mask. Points outside the boundaries are masked.
Values greater than 1, indicate masked values.
gradient_values: ndarray (float64 ,ndim = 3), optional
If gradient keyword is True, the gradient of the function is also
returned.
pysteps/motion/vet.py