Skip to content

Instantly share code, notes, and snippets.

@ytmytm
Created February 17, 2017 23:16
Show Gist options
  • Save ytmytm/08b265f91029956caf4d71883367082a to your computer and use it in GitHub Desktop.
Save ytmytm/08b265f91029956caf4d71883367082a to your computer and use it in GitHub Desktop.
Mean direction and mean strength of wind by vector addition
# circular means by using vector method ####
# from http://math.stackexchange.com/questions/44621/calculate-average-wind-direction
circmean <- function(ws,wd,na.rm=TRUE) {
V_east = mean(ws*sin(wd * pi/180),na.rm=na.rm)
V_north = mean(ws*cos(wd * pi/180),na.rm=na.rm)
mean_WD = atan2(V_east, V_north) * 180/pi
mean_WD = (360 + mean_WD) %% 360
return(mean_WD)
}
circlength <- function(ws,wd,na.rm=TRUE) {
V_east = mean(ws*sin(wd * pi/180),na.rm=na.rm)
V_north = mean(ws*cos(wd * pi/180),na.rm=na.rm)
mean_WS = sqrt(V_east^2+V_north^2)
return(mean_WS)
}
@ytmytm
Copy link
Author

ytmytm commented Feb 17, 2017

Input
ws = vector = wind speed (any units)
wd = vector = wind direction (0-359 degrees)

Output:
scalar mean wind direction (0-359 degrees), mean wind speed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment