-
Notifications
You must be signed in to change notification settings - Fork 16
Neighbourhood methods
Neighbourhood methods compute for each gridpoint a statistic based on values within a neighbourhood surrounding this gridpoint.
These methods can be computationally expensive. Gridpp uses several tricks and approximations to improve the computational speed. To improve speed, the main strategy is to find ways that reuse calculations that have been done for one neighbourhood when processing another neighbourhood.
To find the neighbourhood minimum or maximum, the domain is split into Nx1 slices. A neighbourhood is then represented by N of these Nx1 slices. The minimum (maximum) is computed for each slice, and then a minimum (maximum) of N of the Nx1 slices is computed. This saves time because the Nx1 minimums (maximums) are reused N times.
To find the neighbourhood mean, the domain is accumulated from the lower left corner to the upper right. A neighbourhood mean can be computed by the values at the 4 corners of the neighbourhood:
v = (upper_left + lower_left - upper_right - lower_right) / N / N
To find a neighbourhood quantile (such as the median), there is no way to reuse the calculations from one neighbourhood when processing the next. This is because the whole neighbourhood must be sorted. Instead, gridpp uses a method that gives an approximate value. Gridpp computes the fraction of cells that exceed a set of thresholds. The quantile can then be approximated by linearly interpolating between these fractions. Gridpp can also compute the exact neighbourhood quantile, without approximation if desired.
The neighbourhood calibrator replaces each gridpoint with a value based on a square neighbourhood around this point. For example, a field can be smoothed by taking the average value in a box with a half-width of 3 gridpoints:
gridpp input.nc output.nc -v precipitation_amount -c neighbourhood radius=3
Other statistics can be specified using the stat=
option, for example to find the maximum value in the neighbourhood:
gridpp input.nc output.nc -v precipitation_amount -c neighbourhood radius=3 stat=max
To compute a specific quantile, such as the 90th percentile, use:
gridpp input.nc output.nc -v precipitation_amount -c neighbourhood radius=3 stat=quantile quantile=0.9
- radius (int): Number of gridpoints.
- stat (string): One of 'min', 'mean', 'median', 'max', 'std', 'sum', 'quantile'.
- quantile (float): If stat=quantile, specify the quantile.
- fast (bool): Enable fast computation of 'mean', 'min', 'max, or 'sum'. Default is true.
- approx (bool): Compute the result approximately for 'median' or 'quantile' in order to run faster.
Optional. Only global parameter files are supported. If provided, then the parameter set must have one value. This value represents the radius and can be used to have a leadtime-dependent smoothing radius. In this case it overrides the radius
option.
Neighbourhood calculations on square grids for 'mean', 'min', and 'max' can be calculated as follows:
radius = 7
ovalues = gridpp.neighbourhood(ivalues, radius, 'mean')
To calculate quantiles, use the neighbourhood_quantile
method:
radius = 7
quantile = 0.9
num_thresholds = 20
ovalues = gridpp.neighbourhood_quantile(ivalues, quantile, radius, 'mean', num_thresholds)