问题
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