radarx.retrieve.motion.optical_flow#

radarx.retrieve.motion.optical_flow(obj, *, method='lucas_kanade', field=None, dt=None, floor=5.0, prefilter=2000.0, window=6000.0, smooth=20000.0, min_eigenvalue=0.01, order=2, ridge=0.001, uniform=False, max_speed=60.0, n_levels=None, iterations=3, x='x', y='y', n_threads=None, engine='auto')[source]#

Storm motion from a sequence of gridded fields by optical flow.

The echo pattern I is assumed to be carried by the motion, I(x, t + dt) = I(x - u dt, t), which for small displacements is the optical-flow constraint I_t + u I_x + v I_y = 0. All pairs of consecutive frames share one motion. Two methods solve it:

"lucas_kanade"

The local least squares of Lucas and Kanade (1981) [1]: at every cell the constraints of the cells in a Gaussian window (standard deviation window) give a 2 by 2 system for (u, v). Where its smaller eigenvalue is below min_eigenvalue times the largest found in the field the cell has no texture along some direction (the aperture problem; the corner criterion of Shi and Tomasi 1994 [2]) and takes the average of its neighbours, a Gaussian average of standard deviation smooth that weights each cell by its smaller eigenvalue.

"darts"

A global least squares in the spirit of DARTS (Ruzanski et al. 2011 [3]), which solves the flow equation over the whole domain: the constraints of all cells and frames are fitted at once with u and v as the sum of the (order + 1)**2 leading terms of a cosine series. order=0 is one vector for the domain, larger orders allow a smoother or sharper variation of the motion. The cosine series stands in for the leading Fourier coefficients of the DARTS flow equation (the field is not periodic).

Both start on a coarse image pyramid (2 by 2 block means) and refine the estimate on every finer level: the later field is warped with the current motion (cubic convolution) and the correction is found from the remaining difference, iterations times per level (Bouguet 2001 [4]). This finds displacements of many cells, n_levels limits their size.

The echo is max(dBZ - floor, 0) smoothed by a Gaussian of standard deviation prefilter; missing values count as no echo. This preprocessing, the choice of the aperture test and its default, the normalised averaging, the limit of the correction per iteration, the ridge term and all defaults are radarx’s own; Lucas and Kanade (1981) give the local least squares, Ruzanski et al. (2011) the idea of the global spectral fit. The method of Ruzanski et al. (2011) is not copied: only its flow equation is used, and the cosine series replaces the discrete Fourier coefficients.

Parameters:
  • obj (list of xarray.Dataset or xarray.DataArray, or xarray.Dataset) – Gridded volumes in time order (at least two), or one object with a time dimension, with evenly spaced x and y in metres. Extra dimensions (z) are reduced to the column maximum.

  • method ({"lucas_kanade", "darts"}, optional) – Default "lucas_kanade".

  • field (str, optional) – Field to track. Default: the first reflectivity field found.

  • dt (float or timedelta, optional) – Time between frames in seconds. Default: from their time.

  • floor (float, optional) – Reflectivity (dBZ) below which there is no echo. Default 5.

  • prefilter (float, optional) – Standard deviation (m) of the Gaussian smoothing of the echo before the derivatives are taken. Default 2 km.

  • window (float, optional) – Standard deviation (m) of the Lucas-Kanade window. Default 6 km.

  • smooth (float, optional) – Standard deviation (m) of the Gaussian average that fills cells without texture (Lucas-Kanade). Default 20 km.

  • min_eigenvalue (float, optional) – Smaller eigenvalue of the structure tensor, as a share of the largest eigenvalue of the field, below which a cell is not trusted (Lucas-Kanade). Default 0.01.

  • order (int, optional) – Highest order of the cosine series (DARTS). Default 2 (9 terms for each component).

  • ridge (float, optional) – Weight of the term that keeps the higher coefficients small (DARTS), relative to the mean diagonal of the normal matrix. Default 0.001.

  • uniform (bool, optional) – One motion vector for the whole domain: the weighted mean over the field for Lucas-Kanade, order=0 for DARTS. Default False.

  • max_speed (float, optional) – Largest motion expected (m/s); sets the number of pyramid levels when n_levels is not given. Default 60.

  • n_levels (int, optional) – Pyramid levels (1: no pyramid). Default: enough for max_speed, at most what the grid allows (the coarsest level has at least 16 cells on its shorter side).

  • iterations (int, optional) – Corrections per level. Default 3.

  • x, y (str, optional) – Names of the horizontal coordinates. Default "x" and "y".

  • n_threads (int, optional) – Threads for the kernels. Default: all cores.

  • engine ({"auto", "compiled", "numpy"}, optional) – Implementation of the least squares and of the warping.

Returns:

xarray.Dataset – u (eastward, along x) and v (northward, along y) motion in m/s, on (y, x) (scalars for uniform=True or DARTS with order=0), and quality, the share of the change between the frames that the final motion explains (0 to 1).

Raises:
  • ValueError – For an unknown method, fewer than two frames, frames that are not evenly spaced in time or on different grids, or invalid options.

  • ImportError – If engine="compiled" and a kernel is not available.

Notes

Dask. This function computes the dask array: it tracks the whole fields of all frames. The input is loaded into memory and the result is held in memory.

See also

estimate_motion, advect, nowcast

References

Examples

>>> frames = [grid0, grid1, grid2]
>>> motion = radarx.retrieve.optical_flow(frames)
>>> moved = radarx.retrieve.advect(grid2, motion, dt=300.0)