I have generated a 8-day resolution gross primary production (GPP) fro several years using MODIS data.Each year has about 46 GPP layers. Now, I want to calculate the carbon uptake periods (in which GPPis larger than 1 g C m-2) based on these data. The result is a single raster layer with the pixel values indicate the number of the raster layers that is larger than 1. I have found a similar answer, but I still cannot solve it.Any response would be help.

The following code is one I used.


CUP.days=calc(GPP2000h26v04,fun=function(x,na.rm) sum(x[x>1]))

You can use either calc or stackApply to perform this kind of operations. See the reproducible example below.


# Generate rasterBrick of random values
r1 <- r2 <- r3 <- raster(ncol=10, nrow=10)
r1[] <- rnorm(ncell(r1), 1, 0.2)
r2[] <- rnorm(ncell(r2), 1, 0.2)
r3[] <- rnorm(ncell(r3), 1, 0.2)
s <- brick(r1,r2,r3)

# StackApply
b1 <- stackApply(s, indices=1, fun=function(x, ...){sum(x > 1)})

# calc
b2 <- calc(s, fun=function(x){sum(x > 1)})

# Compare outputs
all.equal(b1, b2, values = TRUE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.