[R-sig-Geo] counting the depressions(group of negative numbers) over time from a raster brick

Vlad valdounet at gmail.com
Thu Mar 8 14:08:58 CET 2012


Hi Sajid,

you don't need to transform anything to a data.frame.
Instead you may use the function 'calc' with a function that calculate 
the two values you are looking for.
(see the help page on 'calc').
I think the following function will do what you expect to :

theFun <- function(x)
{
       start.grp <- which (x < 0 & c(TRUE, x[-length(x)]) >= 0)
       end.grp <- which (x < 0 & c(x[-1], TRUE) >= 0)
       nb.grps <- length(start.grp)
       if (nb.grps > 0) {
               grps <-  rep (NA, length(x))
               grps[x<0] <- unlist(mapply (rep, 1:nb.grps, 
end.grp-start.grp+1, SIMPLIFY=FALSE))
               sum.val <- sum (tapply(x[x<0], grps[x<0], min))
               return (c(nb.grps, sum.val))
       } else {
               return(c(0, 0))
       }

}

calc (cntneg, theFun)

Best,

Vlad

---
Vladislav Navel
SCAN
www.scan-datamining.com


Le 08/03/2012 11:46, sajid pareeth a écrit :
> Dear all
>
> I am trying to count the number of group of depressions (negative
> values) from a climate data set and have least idea on how to go about it.
> Let me explain the scenario.
> I have a raster brick with 468 layers and and each layer has 7458 cells.
>
>
>> cntnegclass       : RasterBrick
> dimensions  : 66, 113, 7458, 468  (nrow, ncol, ncell, nlayers)
> resolution  : 0.108, 0.108  (x, y)
> extent      : 77.946, 90.15, 24.946, 32.074  (xmin, xmax, ymin, ymax)
> coord. ref. : NA
> values      : in memory
> min values  : -359.51 -341.21 -315.45 -148.10 -187.39  -52.87  -66.72
> -52.17 -286.81 -306.74 ...
> max values  :  -7.589   0.000   0.000   0.000   0.000   0.000   0.000
>   0.000   0.000   0.000 ...
>
>
>
> Now for example lets take the 5000th pixel
>
>> cntneg[5000]
> Which will give me 468 values of that pixel over time.
>
>
> [1]  -90.795107  -89.990016  -94.840754    0.000000  -15.085517    0.000000
>    [7]    0.000000    0.000000    0.000000  -12.469657 -114.757702 -115.372023
>   [13] -107.194478  -92.916680 -115.105817 -113.205776 -115.003430  -62.175070
>   [19]    0.000000    0.000000    0.000000  -72.358073 -105.006508 -115.372023
>   [25]  -48.836959 -102.314928 -113.271826 -115.372023  -79.530055    0.000000
>   [31]    0.000000    0.000000    0.000000  -15.048987 -115.208204 -115.372023
>   [37] -115.003430 -108.757617 -113.122594 -115.372023 -111.699048  -17.618498
>   [43]    0.000000    0.000000
>
>
> Now here I need to do two tasks
>
> 1) count number of times the rainfall went below the average - those with
> negative values. And zeros are with positive RF values (which i converted
> to zero using reclass) for the ease of calculation.
> In the above example I want to pick the group of negative numbers and
> count. ie,  (-90.795107  -89.990016  -94.840754), (-15.085517), (
> -12.469657 -114.757702 -115.372023,  -107.194478  -92.916680 -115.105817
> -113.205776 -115.003430  -62.175070), (-72.358073 -105.006508 -115.372023,
> -48.836959 -102.314928 -113.271826 -115.372023  -79.530055)  etc. The
> resulted layer pixel value should be the count of these groups, which in
> this case is 5. Like wise need to do for all the pixels along time
> dimension.
>
> 2) For each group I want to pick the minimum values and resulted pixel will
> have the sum of those minimum values. If a group has one value, keep the
> same.
>
> I am stuck to start with this process. I am assuming I need to convert the
> brick into dataframe and do this.
>
> Can any one help me in giving a lead on how to go about it?
>
> Really appreciate any help.
>
>
> Sorry if the explanation is confusing.
>
> Regards
>
> Sajid
>
>
>
>
>
>



More information about the R-sig-Geo mailing list