从R中的两个光栅对象计算atan2? [英] calculate atan2 from two raster object in R?

查看:45
本文介绍了从R中的两个光栅对象计算atan2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须光栅对象(u 和 v)在此处下载 .我想根据下面的这个方程计算速度的方向

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

然后,我参考 atan2 {raster} 和在下面创建一个简单的光栅对象并正常工作..

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)

推荐答案

raster::atan2 仅针对 RasterLayer 对象实现,不适用于 RasterBrick.我已经在 2.5-5 版中纠正了这个问题(正在 R-Forge 上开发).对于当前版本,您需要使用循环:

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:

假设 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

这篇关于从R中的两个光栅对象计算atan2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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