I am trying to fill the gaps in a time series of ndvi images using spline.
I have created a raster stack with the ndvi images I have and some layers with only NA
for the timesteps that I don't have.
The raster stack is available as a Geotiff here: raster stack download
I have written the following function to interpolate the missing values:
f.int.spline <- function(x) { # x is a raster stack or brick
v=as.vector(x) # transform x in a vector for easier manipulation
z=which(v %in% NA) # find which pixel values are to be interpolated
# interpolation with spline
interp <- spline(x=c(1:NROW(v)), y = v,
xout = z, # missing values to be interpolated
method = "natural")
x[z] <-interp$y # including the missing values in the original vector
}
The function works if I use it with one pixel (e.g x[ 50, 200 ]
), but if I run it with calc(x, f.int.spline)
it returns a generic error:
> error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
cannot use this function
If I run it using f.int.spline(x)
it returns an error related to memory usage:
> Error in as.vector(t((matrix(rep(i, rec2), nrow = rec2, byrow = TRUE)) + :
error in evaluating the argument 'x' in selecting a method for function 'as.vector': Error in t((matrix(rep(i, rec2), nrow = rec2, byrow = TRUE)) + add) :
error in evaluating the argument 'x' in selecting a method for function 't': Error: cannot allocate vector of size 4.9 Gb
1) Do you see any flaws or have any workarounds on how to make it work?
2) I can't understand exactly how the calc()
function works for raster stacks: Does it take the values of each pixel in all the layers?
3) as suggested by Jeffrey Evans I am also looking for other interpolation functions that are better suited for the job. Any idea ?