Skip to content

Count values "greater than" in rasterbrick layers

4 messages · Thiago V. dos Santos, Stachelek, Joseph, Dominik Schneider

#
Hi Thiago,

Ignoring the fact that you have negative rainfall amounts in your example, I would go about this problem by reclassifying the raster brick to a binary classification. Then if you sum you get raw counts.

#count wet days
b.wet<-reclassify(b,c(-Inf,0,0))
b.wet<-reclassify(b.wet,c(0,+Inf,1))
plot(b.wet[[1]])
plot(sum(b.wet))

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Thiago V. dos Santos
Sent: Wednesday, July 16, 2014 5:51 AM
To: R-SIG list
Subject: [R-sig-Geo] Count values "greater than" in rasterbrick layers

Dear all,

I have a 31-layer rasterbrick where each layer represents daily rainfall records for a certain month.

My goal is to perform two calculations in this brick:

a) Sum up total rainfall for the entire month

b) For the entire month (all rasterbrick layers), calculate the number of days where it rained (i.e. rainfall > 1)

Problem a) is straightforward and I solved it with the help of a simple calc command.

Problem b), however, is where I am having troubles. The command I came up with doesn't work, as the final raster has values way greater than 31 (which would be the limit of each cell, if it had rained every day of the month on that cell).

This is my code so far. Any suggestions?

library(raster)

# generate raster data
r <- raster(ncol=384, nrow=190)
b <- brick(sapply(1:31, function(i) setValues(r, rnorm(ncell(r), i, 3))))

# sum up rain
acc.rain <- calc(b, fun=sum)

# count wet days
wet.days <- calc(b, fun=function(x,na.rm) sum(x[x>1]))


Many thanks in advance,
--
Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm
Phone: (612) 323 9898



We value your opinion. Please take a few minutes to share your comments on the service you received from the District by clicking on this link<http://my.sfwmd.gov/portal/page/portal/pg_grp_surveysystem/survey%20ext?pid=1653>.
5 days later
1 day later
#
Thiago - 
With regards to your original attempt for part b, I  believe using length()
instead of sum() will get you what you want.  In this case, sum(x) and
sum(x[x>1]) are the same assuming you have no values less than 1. Length can
be used as a proxy for counting how many days rain>1.




--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Count-values-greater-than-in-rasterbrick-layers-tp7586746p7586782.html
Sent from the R-sig-geo mailing list archive at Nabble.com.