使用 dismo 包创建生物气候变量时出现问题
我想获得未来场景的所有生物气候变量,用于物种分布建模。因此,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实是一个错误。它已在 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
我过去也发现过这个函数有同样的问题,所以现在我看得更深入一些。 Biovars 的帮助页面指出它将接受三个参数,即向量、矩阵或 rasterStack/Bricks。对于这三种情况,参数的长度、宽度或深度应分别为 12 个月,返回值的长度、宽度或深度将为 19。3
个向量参数的帮助页面示例返回一个包含 19 个值的向量。这很好用。
具有三个 2x12 矩阵的示例返回一个 2x19 矩阵,也可以正常工作。
但对于 rasterstacks 和 rasterbricks,你得不到 19 个值。我相信这是一个错误。我对来自 worldclim.org 的真实 *.bil 数据运行了
biovars
并复制了 12 值答案的结果。我尝试使用下面的虚拟代码,该代码返回错误(不清楚原因),但如果您想向 R. Hijmans 详细解释您的问题,这可能会很有用。当我使用已裁剪的 worldclim *.bil 数据调用 biovars 时,我也会遇到相同的错误。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.
An example with three 2x12 matrices returns a 2x19 matrix, also works fine.
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.