[R-sig-Geo] Bioclimatic variables - wettest quarter

Robert J. Hijmans r.hijmans at gmail.com
Mon Nov 8 17:46:51 CET 2010


Brian,

The problem is that the the function needs to be either vectorized
(return a vector when the input consists of vectors), or appropriate
for apply (e.g. 'max'). I shall make this clearer in the help file.

You could make your function work by doing

 o<- a + b + d
 p<- b + d + e
 etc.
 ad <- calc(stack(o,p,r,u,v,w,x,y,z,aa,ab,ac), fun= which.max)
 return(ad)

or avoid the overlay function: and use map algebra

 o<- sum(a,b,d)
 p<- sum(b,d,e)
 r<- sum(d,e,f)
etc.
 s <- stack(o,p,r,u,v,w,x,y,z,aa,ab,ac)
 x <- calc(s, which.max)

Finally, the computation of bioclimatic variables has been implemented
(but not extensively tested; please provide feedback if you use it) in
the 'biovars' function in the 'dismo' package.

see
getMethod(biovars, c('Raster', 'Raster', 'Raster'))
getMethod(biovars, c('matrix', 'matrix', 'matrix'))

for its implenentation

Robert


On Sun, Nov 7, 2010 at 4:40 PM, Brian Oney <zenlines at gmail.com> wrote:
> Hello All,
>
> I am attempting to reproduce the bioclimatic (Worldclim, Hijmans et al
> 2005) variables with the raster package and I am stuck on the group
> "temp of driest quarter" etc. variables.
> I am attempting it with the function "overlay" ("raster" package).
>
> Here is how it looks:
> # To know the first month of the wettest quarter
> maxmonth<- function(a,b,d,e,f,g,i,j,k,l,m,n){
>                    o<- sum(c(a,b,d))
>                    p<- sum(c(b,d,e))
>                    r<- sum(c(d,e,f))
>                    u<- sum(c(e,f,g))
>                    v<- sum(c(f,g,i))
>                    w<- sum(c(g,i,j))
>                    x<- sum(c(i,j,k))
>                    y<- sum(c(j,k,l))
>                    z<- sum(c(k,l,m))
>                    aa<- sum(c(l,m,n))
>                    ab<- sum(c(m,n,a))
>                    ac<- sum(c(n,a,b))
>                    ad<- which.max(c(o,p,r,u,v,w,x,y,z,aa,ab,ac))
>                    return(ad)}
>>
>>  maxmonth(a=13,b=16,d=41,e=61,f=41,g=16,i=15,j=14,k=13,l=11,m=31,n=11)
>
> # works: 2nd element (month) is the beginning of the wettest quarter
> [1] 2
> mon_stac<- stack(paste(pc_clim,"",y,"_prec",1:12,".asc", sep=""))
> # Now attempt it
> avg<- overlay(mon_stac, fun=maxmonth)
> Error in .overlayList(rasters, fun = fun, filename = filename, ...) :
>  cannot use this formula; lenghts do not match
>
> # How about manually?
> a<- raster(paste(pc_clim,"",y,"_prec",1,".asc", sep=""))
> b<- raster(paste(pc_clim,"",y,"_prec",2,".asc", sep=""))
> d<- raster(paste(pc_clim,"",y,"_prec",3,".asc", sep=""))
> e<- raster(paste(pc_clim,"",y,"_prec",4,".asc", sep=""))
> f<- raster(paste(pc_clim,"",y,"_prec",5,".asc", sep=""))
> g<- raster(paste(pc_clim,"",y,"_prec",6,".asc", sep=""))
> i<- raster(paste(pc_clim,"",y,"_prec",7,".asc", sep=""))
> j<- raster(paste(pc_clim,"",y,"_prec",8,".asc", sep=""))
> k<- raster(paste(pc_clim,"",y,"_prec",9,".asc", sep=""))
> l<- raster(paste(pc_clim,"",y,"_prec",10,".asc", sep=""))
> m<- raster(paste(pc_clim,"",y,"_prec",11,".asc", sep=""))
> n<- raster(paste(pc_clim,"",y,"_prec",12,".asc", sep=""))
>
> avg<- overlay(a,b,d,e,f,g,i,j,k,l,m,n, fun=maxmonth)
> Error in .overlayList(rasters, fun = fun, filename = filename, datatype
> = datatype,  :
>  cannot use this formula; lenghts do not match
>
> Also doesn't work...
>>
>>  length(mon_stac[1])
>
> [1] 12
> I have 12 rasters being fed into overlay, but it won't buy it.
> I think that my first step is to know the first month of the wettest
> month. Once I can do that I can do it for temperature as well.
>
> Thanks and Regards,
> Brian
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list