栅格数据集上的Thornthwaite蒸散量.错误公式未向量化 [英] Thornthwaite evapotranspiration on a raster dataset . Error formula not vectorised

查看:382
本文介绍了栅格数据集上的Thornthwaite蒸散量.错误公式未向量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SPEI软件包中包含的Thornthwaite ET公式来计算在栅格数据集上运行SPEI的蒸散量(ET)

I am trying to calculate evapotranspiration (ET) for running SPEI on a raster dataset by using the Thornthwaite ET formula included in the SPEI package

这是我的代码

library(SPEI)
library(raster)
library(zoo)

tm = array(1:(3*4*12*64),c(3,4,12*64))
tm = brick(tm)

dates=seq(as.Date("1950-01-01"), as.Date("2013-12-31"), by="month")
tm<- setZ(tm,dates)
names(tm) <- as.yearmon(getZ(tm))

thornthwaite ET
th <- function(Tave, lat) {
  SPEI::thornthwaite(Tave, lat)
} 

lat <- setValues(a, coordinates(tm)[, "y"])
out <- raster::overlay(tm, lat, fun = th)

但是我遇到了以下错误:

but i got the following error:

Error in (function (x, fun, filename = "", recycle = TRUE, forcefun = FALSE,  : 
  cannot use this formula, probably because it is not vectorized

请您帮忙?

感谢一百万

推荐答案

我不太确定为什么会失败.这是一种解决方法

I am not quite sure why this fails. Here is a workaround

library(SPEI)
library(raster)
library(zoo)

tm = array(20,c(3,4,12*64))
tm = brick(tm)

dates=seq(as.Date("1950-01-01"), as.Date("2013-12-31"), by="month")
tm<- setZ(tm,dates)
names(tm) <- as.yearmon(getZ(tm))

#thornthwaite ET
th <- function(Tave, lat) {
    as.vector(SPEI::thornthwaite(as.vector(Tave), lat))
} 

a <- raster(tm)
lat <- init(a, "y")
#out <- raster::overlay(tm, lat, fun = th)
out <- brick(tm, values=FALSE)

for (i in 1:ncell(tm)) {
    out[i] <- th(tm[i], lat[i])
}

这篇关于栅格数据集上的Thornthwaite蒸散量.错误公式未向量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆