使用 dismo 包创建生物气候变量时出现问题

发布于 2024-10-19 08:00:10 字数 1926 浏览 6 评论 0原文

我想获得未来场景的所有生物气候变量,用于物种分布建模。因此,我使用 worldclim 数据库中的三个变量运行“dismo”包中的“biovars”函数,得到了 12 层的 RasterBrick:

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 ... 

但是,我认为应该有 19 个 bioclim 变量。正如您提到的,除了那里之外,生物变量中还有更多参数,但我不知道它们是什么。你能帮我一下吗?

另一个问题是我在写入这些变量时出错:

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

dim(res) <- c(ncols, raster@data@nlayers * nrows) 中的错误: dims [产品 933120] 与对象的长度 [889920] 不匹配

,当我尝试逐段写入它们时,出现以下错误:

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

结果错误[, i] <- readBin(raster@file@con, What = dtype, n = ncols, : 替换长度为零

三个输入变量具有相同的维度,例如:

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 ... 

有人能解释为什么吗?先谢谢啦~

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~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信愁 2024-10-26 08:00:10

这确实是一个错误。它已在 0.5-19 版本中修复,应该会在 24 小时内从 R-Forge 提供,并很快从 CRAN 提供。相对湿度

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

帝王念 2024-10-26 08:00:10

我过去也发现过这个函数有同样的问题,所以现在我看得更深入一些。 Biovars 的帮助页面指出它将接受三个参数,即向量、矩阵或 rasterStack/Bricks。对于这三种情况,参数的长度、宽度或深度应分别为 12 个月,返回值的长度、宽度或深度将为 19。3

个向量参数的帮助页面示例返回一个包含 19 个值的向量。这很好用。

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)

具有三个 2x12 矩阵的示例返回一个 2x19 矩阵,也可以正常工作。

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)

但对于 rasterstacks 和 rasterbricks,你得不到 19 个值。我相信这是一个错误。我对来自 worldclim.org 的真实 *.bil 数据运行了 biovars 并复制了 12 值答案的结果。我尝试使用下面的虚拟代码,该代码返回错误(不清楚原因),但如果您想向 R. Hijmans 详细解释您的问题,这可能会很有用。当我使用已裁剪的 worldclim *.bil 数据调用 biovars 时,我也会遇到相同的错误。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文