library(SPEI)
library(terra)
基本上,给定两个变量A和B的每月时间序列如下:
a = array(1:(3*4*12*64),c(3,4,12*64))
a = rast(a)
dates=seq(as.Date("1950-01-01"), as.Date("2013-12-31"), by="month")
terra::time(a)=dates
names(a) <- zoo::as.yearmon(time(a))
b = array(1:(3*4*12*64),c(3,4,12*64))
b = rast(b)
dates=seq(as.Date("1950-01-01"), as.Date("2013-12-31"), by="month")
terra::time(b) <- dates
names(b) <- zoo::as.yearmon(time(b))
a
和b
是时间序列的时间序列。现在,我想将功能SPEI::hargreaves
应用于a
和b
中每个像素的时间序列,然后返回spatrasterC
从spei包,每个像素的har thr中,在a和b中是相同的。一旦我了解如何将功能应用于我的spatrasters。 <- hargreaves(TMIN,TMAX,lat=37.6475). Here, consider
那一刻,我正在使用Data.table折叠我的栅格,然后将hargreaves涂在桌子上。到目前为止,这种方法效率很低。 到目前为止,我到处都是:
Tmin=a, Tmax=b and lat=latitude
Howvever,我会收到错误:
library(SPEI)
library(terra)
library(zoo)
har <- function(a, b, lat) {
SPEI::hargreaves(as.vector(a), as.vector(b), lat,na.rm = TRUE)
}
lat <- init(rast(a), "y")
PET1 <- terra::lapp(x=c(a, b, lat), fun = Vectorize(har))
我认为以下情况是可能的,但这是失败的,也许可以解决。
#Error: [lapp] cannot use 'fun'. The number of values returned is less
#than the number of input cells.
#Perhaps the function is not properly vectorized
下面很丑陋,但似乎有效:s <- sds(a, a+10, lat)
names(s) <- c("Tmin", "Tmax", "lat")
x <- lapp(s, hargreaves, usenames=TRUE, recycle=TRUE, verbose=FALSE)