I have a very large raster file (number of row=14810; number of columns=12392) and I would like to use the 'focal' function to average (mean function) around windows of a specific size (for instance 5 pixel X 5 pixel). I used to apply the 'focal' function in R (raster R package). However, for this large raster size the function is very slow.
I know that there is a function in 'spatial.tools' called 'focal_hpc' that can speed up the focal processing (in parallel). However, I do not know how to specifies the 'mean' function. I used the following script:
require(raster)
require(spatial.tools)
tahoe_highrez <- brick(system.file("external/tahoe_highrez.tif", package="spatial.tools"))
res<-focal_hpc(x=tahoe_highrez,fun=mean,window_dims=c(5,5))
But It gives me this error:
Error in (function (x, ...) :
call to standardGeneric("mean") apparently not from the body of that generic function
Has someone experience with this package and function?
Hmmm. Me too:
> res<-focal_hpc(x=tahoe_highrez,fun=mean,window_dims=c(5,5))
Error in (function (x, ...) :
call to standardGeneric("mean") apparently not from the body of that generic function
but if you make a function that does the same thing:
> f = function(...){mean(...)}
it works...
> res<-focal_hpc(x=tahoe_highrez,fun=f,window_dims=c(5,5))
Warning messages:
1: In .local(x, ...) : min value not known, use setMinMax
2: In .local(x, ...) : max value not known, use setMinMax
3: In .local(x, ...) : min value not known, use setMinMax
> res
class : RasterBrick
dimensions : 400, 400, 160000, 1 (nrow, ncol, ncell, nlayers)
resolution : 5.472863e-06, 5.472863e-06 (x, y)
extent : -119.9328, -119.9306, 39.28922, 39.29141 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : /tmp/Rtmp2WTR9g/file5f335faf907.grd
names : layer
I suspect its something to do with the way generic functions are dispatched. Make a test raster to be sure my function substitution is computing the mean of the things you want to take the mean, and read the docs for more examples of writing functions for focal_hpc
.