TITLE: Calculate average wind direction
QUESTION [9 upvotes]: What is the best way to average wind direction? I have found many conflicting suggestions elsewhere. Best one I saw is to average SINs of all angles in radians and take inverse SIN of the result. 
This is to be used to produce a windrose where the input must have one record per hour, but the data provided has several records per hour.

REPLY [6 votes]: Assuming you use the standard meterological convention that wind direction is the source direction of winds (i.e. 270º means blowing west → "here"):
Given two arrays containing wind speed (WS) and wind direction (WD, in degrees) observations, the vector mean wind direction is calculated as follows:
V_east[i] = mean(WS[i] * sin(WD[i] * pi/180))
V_north[i] = mean(WS[i] * cos(WD[i] * pi/180))

mean_WD = arctan2(V_east, V_north)) * 180/pi
mean_WD = (360 + mean_WD) % 360

The final line remaps the range ($-\pi$ to $\pi$) ($-$180 to 180) → (0 to 359).
Alternately, the unit vector mean wind direction can be calculated by omitting the wind speed components. The unit vector mean wind direction is often a good approximation to the scalar mean wind direction (which is a more involved calculation).
u_east = mean(sin(WD * pi/180))
u_north = mean(cos(WD * pi/180))
unit_WD = arctan2(u_east, u_north) * 180/pi
unit_WD = (360 + unit_WD) % 360


N.B. The implementation of the arctan2 or atan2 function is important: most programming languages respect the atan2(y, x) convention but spreadsheets tend to reverse the arguments as atan2(x, y).

Edited to clarify array computation. Thanks comments!