calculate atan2 from two raster object in R?

自闭症网瘾萝莉.ら 提交于 2019-12-11 14:02:02

问题


I have to raster object (u and v) download here . I want to calculate the direction of the velocity based on this equation below

u <- brick('D:/uv.nc', varname = 'U')
v <- brick('D:/uv.nc', varname = 'V')
ws <- sqrt(u^2+v^2)
wd <- (180/pi)*(atan2(u,v))

Unfortunately, I get an error message below:

Error in atan2(y, x) : Non-numeric argument to mathematical function

Then, I refer to atan2 {raster} and create a simple raster object below and work fine..

r1 <- r2 <- raster(nrow=10, ncol=10)
r1[] <- (runif(ncell(r1))-0.5) * 10
r2[] <- (runif(ncell(r1))-0.5) * 10
atan2(r1, r2)

回答1:


raster::atan2 is only implemented for RasterLayer objects, not for a RasterBrick. I have rectified that in version 2.5-5 (under development on R-Forge). With the current version you need to use a loop:

assuming that nlayers(u) == nlayers(v)

a <- list()
for (i in 1:nlayers(u)) {
   a[[i]] <- atan2(u[[i]],v[[i]])
}
a <- stack(a)

wd <- (180/pi) * a


来源:https://stackoverflow.com/questions/36123717/calculate-atan2-from-two-raster-object-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!