您如何临时将大型 Raster Stack 对象插入到更高的周期(每周到每天)
How do you temporally interpolate a large RasterStack object to a higher periodicity (weekly to daily)
在 R 中,我试图在以每周时间间隔创建的堆栈与每天时间间隔之间进行插值。插值方法可以是最近邻或线性插值。
我已经看到可以使用 na.approx 或样条曲线对时间序列完成此操作。
此外,如果可能的话,我想将该对象保留为堆栈(无数据帧)。
#Dummy example
#---#
library(raster)
# Create date sequence
idx <- seq(as.Date("2000/1/1"), as.Date("2000/12/31"), by = "week")
# Create raster stack and assign dates
r <- raster(ncol=20, nrow=20)
s <- stack(lapply(1:length(idx), function(x) setValues(r,
runif(ncell(r)))))
s <- setZ(s, idx)
# Do interpolation to daily resolution
# (Perhaps it should be done one by one, perhaps all at once...)
# ...
假设我的实际堆栈的维度为 c(20,20,52),结果的维度为 c(20,20,366)。
感谢您的帮助
您需要编写一个函数 f
,为向量(一个单元格)执行此操作,比如 s[1]
。然后使用 calc
应用此函数,如 calc(s, f)
这里是一个使用approx
的简单示例,可以用样条或其他插值器代替
library(raster)
r <- raster(ncol=20, nrow=20)
s <- stack(lapply(1:length(idx), function(x) setValues(r, runif(ncell(r)))))
idx <- seq(as.Date("2000/1/1"), as.Date("2000/12/31"), by = "week")
dr <- seq(as.Date("2000/1/1"), as.Date("2000/12/31"), by = "day")
f <- function(x) approx(idx, x, dr, rule=2)$y
# test <- f(s[1])
x <- calc(s, f)
一个单元格的结果
plot(dr, as.vector(x[1]), pch="+")
points(idx, as.vector(s[1]), pch=20, col="red", cex=2)
lines(idx, as.vector(s[1]), col="blue")
在 R 中,我试图在以每周时间间隔创建的堆栈与每天时间间隔之间进行插值。插值方法可以是最近邻或线性插值。
我已经看到可以使用 na.approx 或样条曲线对时间序列完成此操作。
此外,如果可能的话,我想将该对象保留为堆栈(无数据帧)。
#Dummy example
#---#
library(raster)
# Create date sequence
idx <- seq(as.Date("2000/1/1"), as.Date("2000/12/31"), by = "week")
# Create raster stack and assign dates
r <- raster(ncol=20, nrow=20)
s <- stack(lapply(1:length(idx), function(x) setValues(r,
runif(ncell(r)))))
s <- setZ(s, idx)
# Do interpolation to daily resolution
# (Perhaps it should be done one by one, perhaps all at once...)
# ...
假设我的实际堆栈的维度为 c(20,20,52),结果的维度为 c(20,20,366)。
感谢您的帮助
您需要编写一个函数 f
,为向量(一个单元格)执行此操作,比如 s[1]
。然后使用 calc
应用此函数,如 calc(s, f)
这里是一个使用approx
的简单示例,可以用样条或其他插值器代替
library(raster)
r <- raster(ncol=20, nrow=20)
s <- stack(lapply(1:length(idx), function(x) setValues(r, runif(ncell(r)))))
idx <- seq(as.Date("2000/1/1"), as.Date("2000/12/31"), by = "week")
dr <- seq(as.Date("2000/1/1"), as.Date("2000/12/31"), by = "day")
f <- function(x) approx(idx, x, dr, rule=2)$y
# test <- f(s[1])
x <- calc(s, f)
一个单元格的结果
plot(dr, as.vector(x[1]), pch="+")
points(idx, as.vector(s[1]), pch=20, col="red", cex=2)
lines(idx, as.vector(s[1]), col="blue")