r Terra创建一年的日长的狂人
可以从library(geosphere)
的日长
函数找到特定纬度的日光长度。一天的长度在一年中有所不同。这是函数day -lengtth(lat,doy)
。我想生成一个1/2度用366层生成spatraster
,每个doy一层。下面的代码使我结束了一天。 COORDS DATAFRAME似乎与Rast()示例集中的示例具有相同的结构,但在下面的错误和警告消息中失败。
library(data.table)
library(geosphere)
library(terra)
latVals <- seq(-60, 90, 0.5)
lonVals <- seq(-180, 180, 0.5)
dl_doy <- data.table(NULL)
temp <- vector()
for (j in 1:366) {
for (i in 1:length(latVals)) {
temp[i] <- daylength(latVals[i], j)
}
new_name <- paste0("day_", j)
# dl_doy[, (new_name) := .(temp)]
dl_doy <- cbind(dl_doy, temp)
}
setnames(dl_doy, new = paste0("day_", seq(1, 366, 1)))
coords <- data.frame(x = rep(-100, length(latVals)), y = latVals)
coords <- cbind(coords, dl_doy$day_1)
names(coords) <- c("x", "y", "lyr.1")
test <- rast(coords, type = "xyz")
#Error: [round] invalid extent
#In addition: Warning message:
#In min(dx) : no non-missing arguments to min; returning Inf
The length of daylight at a particular latitude can be found with the daylength
function from library(geosphere)
. The day length varies by the day of the year. Here's the function daylength(lat, doy)
. I'd like to generate a 1/2 degree spatraster
with 366 layers, one for each doy. The code below gets me close for one day. The coords dataframe appears to have the same structure as the example in the rast() set of examples but fails with the error and warning messages just below.
library(data.table)
library(geosphere)
library(terra)
latVals <- seq(-60, 90, 0.5)
lonVals <- seq(-180, 180, 0.5)
dl_doy <- data.table(NULL)
temp <- vector()
for (j in 1:366) {
for (i in 1:length(latVals)) {
temp[i] <- daylength(latVals[i], j)
}
new_name <- paste0("day_", j)
# dl_doy[, (new_name) := .(temp)]
dl_doy <- cbind(dl_doy, temp)
}
setnames(dl_doy, new = paste0("day_", seq(1, 366, 1)))
coords <- data.frame(x = rep(-100, length(latVals)), y = latVals)
coords <- cbind(coords, dl_doy$day_1)
names(coords) <- c("x", "y", "lyr.1")
test <- rast(coords, type = "xyz")
#Error: [round] invalid extent
#In addition: Warning message:
#In min(dx) : no non-missing arguments to min; returning Inf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以得到这样的事情:
替代方法可以是:
这比第一种方法效率不高,因为它计算每个经度的日程(而不是计算一个经度和使用对于所有纵向)。需要
应用
,因为day -lengthth
仅部分矢量化。发生的错误是因为您只有一个唯一的“ x”变量 - 使得无法猜测栅格的分辨率。无论如何,当您从常规的光栅开始时,不必使用
rast(coords,type =“ xyz”)
。看来您没有使用正确的坐标(单元格中心)。I think you can get what you are after like this:
An alternative approach could be:
This is less efficient than the first approach because it computes daylength for each longitude (instead of computing it for one longitude and using that for all longitudes). The
apply
is needed becausedaylength
is only partially vectorized.The error you get occurs because you only have one unique "x" variable -- making it impossible to guess the resolution of the raster. It should not be necessary to use
rast(coords, type = "xyz")
anyway, as you start out with a regular raster. It seems you are not using the correct coordinates (the center of cells).