I have a raster brick b of global fAPAR data which is 35 years’ worth of daily data (i.e. large).
The Aim
Is there a way I can loop through this brick and run a function on 365 layers (days) at a time? The end goal is to run a function to identify the layer (date) in which the value of each gridcell goes above a threshold of 20% of the max value for that gridcell for each year.
So the code would need to:
- Work out max value of each gridcell in a year
- Return the layer (date) on which each gridcell crosses 20% of that max value
Code
The following code finds the max for each year, but doesn't work on my data due to its size (daily, global, 0.5 deg. data). The key may be to work with 365 layers at a time, not the whole brick (if possible).
# Example 10-year brick:
b <- brick(nrow=108,ncol=132,nl=3650)
values(b) <- runif(ncell(b)*nlayers(b))
i <- rep(1:nlayers(b), each=365)
# if incomplete last set of days:
i <- i[1:nlayers(b)]
x <- stackApply(b, i, max)
My goal is quite similar to this and this. I also wonder whether I need to divide my data up into individual years and run something on multiple files, given the size issues.