Problem creating bioclimatic variables using dismo packages

3.1k views Asked by At

I would like to get all the bioclimatic variables of future scenarios for species distribution modelling. So I run the "biovars" function in "dismo" packages using the three variables from worldclim database, and I got a RasterBrick of 12 layers:

prec<-stack(paste(getwd(),"/prec_2080/wc_2_5m_HADCM3_B2a_2080_prec_",1:12,".bil",sep=""))
tmin<-stack(paste(getwd(),"/tmin_2080/wc_2_5m_HADCM3_B2a_2080_tmin_",1:12,".bil",sep=""))
tmax<-stack(paste(getwd(),"/tmax_2080/wc_2_5m_HADCM3_B2a_2080_tmax_",1:12,".bil",sep=""))
x<-biovars(prec=prec,tmin=tmin,tmax=tmax)
 x
class       : RasterBrick 
dimensions  : 3600, 8640, 12  (nrow, ncol, nlayers)
resolution  : 0.04166667, 0.04166667  (x, y)
extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
projection  : NA 
values      : C:/DOCUME~1/Marco/LOCALS~1/TMP/R_raster_tmp/raster_tmp_8984740455.grd 
min values  :     42 -65458  -1017      0     71      0 -65439     22     23     56 ... 
max values  :  65456    213      1  34159  65534  65513  65534  65507  65503  65518 ... 

However, I thought there should be 19 bioclim variables. As you mentioned that there are more arguments in biovars except the there, but I do not know what they are. Could you help me out?

Another problem of this is that I got error writing these variables:

writeRaster(x,paste(getwd(),"/wc_2_5m_HADCM3_B2a_2080_1.grd",sep=""))

Error in dim(res) <- c(ncols, raster@data@nlayers * nrows) : dims [product 933120] do not match the length of object [889920]

and, when I tried to write them band by band, I got the following error:

for (i in 10:12) {
writeRaster(x[[i]],paste(getwd(),"/wc_2_5m_HADCM3_B2a_2080_",i,".grd",sep=""),overwrite=TRUE)
}

Error in result[, i] <- readBin(raster@file@con, what = dtype, n = ncols, : replacement has length zero

The three input variables have the same dimensions, e.g.:

prec
class       : RasterStack 
dimensions  : 3600, 8640, 12  (nrow, ncol, nlayers)
resolution  : 0.04166667, 0.04166667  (x, y)
extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
projection  : NA 
min values  : 0 0 0 0 0 0 0 0 0 0 ... 
max values  : 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 ... 

Could anybody explain why? Thanks in advance~

2

There are 2 answers

0
Robert Hijmans On BEST ANSWER

This is indeed a bug. It has been fixed in version 0.5-19 which should be available from R-Forge in 24 hrs and from CRAN very soon. RH

3
J. Win. On

I have found the same problem with this function in the past, so now I looked a little deeper. The help page for biovars states that it will accept three arguments that are vectors, matrices, or rasterStack/Bricks. For these three cases, the arguments should have respectively length, width, or depth of 12 months, and the return value will have length, width or depth of 19.

The help page example for 3 vector arguments returns a vector of 19 values. This works fine.

tmin.V <- c(10,12,14,16,18,20,22,21,19,17,15,12)
tmax.V <- tmin.V + 5
prec.V <- c(0,2,10,30,80,160,80,20,40,60,20,0)
biovars(prec.V, tmin.V, tmax.V)

An example with three 2x12 matrices returns a 2x19 matrix, also works fine.

tmin.M <- rbind(tmin.V, tmin.V+1)
tmax.M <- rbind(tmax.V, tmax.V+1)
prec.M <- rbind(prec.V, prec.V+1)
biovars(prec.M, tmin.M, tmax.M)

But with rasterstacks and rasterbricks you do not get 19 values. I believe this is a bug. I ran biovars on real *.bil data from worldclim.org and duplicated your result of a 12-value answer. I tried to use the dummy code below which returns an error (not clear why), but may be useful if you want to explain your problem to R. Hijmans in detail. I also get the same error when I call biovars using worldclim *.bil data that has been cropped.

dup12 <- function(clim.M) {
  raslist = list()
  for(i in 1:12) raslist = c(raslist, raster(clim.M))
  do.call(stack, raslist)
}
tmin.S <- dup12(tmin.M)
tmax.S <- dup12(tmax.M)
prec.S <- dup12(prec.M)
biovars(prec.S, tmin.S, tmax.S)

Error in v[tr$row[i]:(tr$row[i] + tr$nrows[i] - 1), ] <- p : 
  number of items to replace is not a multiple of replacement length