间隙填充时态栅格对象 [英] Gap filling temporal raster objects
问题描述
假设我有 4 个栅格图层,每个图层都属于该月的每隔一周.我想使用线性插值为每一天创建新图层.在这种情况下,前 2 个栅格属于 Feb
月份,29 天
,第二个栅格属于 March
,31天
.我想知道如何创建可以在考虑一个月中的天数的情况下填充时间段的每日栅格对象(二月为 29 个栅格,三月为 31 个栅格).谢谢!
Suppose I have 4 raster layers each belong to every other week of the month. I want to use linear interpolation to create new layers for each day. In this case, the first 2 rasters belonging to the month of Feb
with 29 days
and the second 2 belong to March
with 31 days
. I want to know how to create daily raster objects that can fill the time period with the consideration of number of days in the month (29 rasters for Feb and 31 rasters for March). Thanks!
library(raster)
r1 <- raster(nrow=5, ncol=5)
feb15 <- setValues(r1, runif(ncell(r1)))
feb29 <- setValues(r1, runif(ncell(r1)))
mar15 <- setValues(r1, runif(ncell(r1)))
mar30 <- setValues(r1, runif(ncell(r1)))
推荐答案
关于 同样的问题两周前被问到.这是您示例的改编答案.
About the same question was asked two weeks ago. Here is the adapted answer for your example.
library(raster)
r1 <- raster(nrow=5, ncol=5)
feb15 <- setValues(r1, runif(ncell(r1)))
feb29 <- setValues(r1, runif(ncell(r1)))
mar15 <- setValues(r1, runif(ncell(r1)))
mar30 <- setValues(r1, runif(ncell(r1)))
s <- stack(feb15, feb29, mar15, mar30)
x <- calc(s, fun=function(y) approx(c(15,29,29+15,29+30), y, 1:59, rule=2)$y)
names(x) <- c(paste('feb', 1:29), paste('march', 1:30))
不幸的是,当 approx
仅获取 NA
值时失败.它需要至少两个非 NA 值进行插值".这是一个解决方法:
Unfortunately, approx
fails when it only gets NA
values. It "need at least two non-NA values to interpolate". Here is a work around:
s[1:2] <- NA
# fail <- calc(s, fun=function(y) approx(c(15,29,29+15,29+30), y, 1:59, rule=2)$y)
f <- function(y) {
if (sum(! is.na(y)) < 2) {
return(rep(NA, 59))
} else {
approx(c(15,29,29+15,29+30), y, 1:59, rule=2)$y
}
}
x <- calc(s, fun=f)
这篇关于间隙填充时态栅格对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!