# Calculating an average value of the wind direction

V

#### Ville

Hi!

I have a weather station which measures both the direction and speed of the wind. It is connected to a SCADA system (TSX57xxx + Monitor Pro). Now I have a mathematical problem with calculating the average direction of the wind (my client needs the information for their reports). The problem is that the direction 0 degrees and 360 degrees are the same (the north, as You know...). Let's say that the wind is from the north; if I have e.g. 10 samples and half of them are 360 degrees and the other half are 0 degrees the result is 180 degrees! This is mathematically right result but... you know...it isn't!

Ville

M

#### Matt

Modulate the incoming wind direction value by 359 before averaging.

Matt

M

#### Michael Griffin

You need to use modulo 360 arithmetic. Modulo 360 is simply the remainder after dividing the sum by 360. E.g.

(10 + 10 + 10 + 350) / 4 = 95 (incorrect)

((10 + 10 + 10 + 350) modulo 360) / 4 = 5 (correct)

Also - don't allow 360 as a valid reading. It aliases with 0 and is therefore ambiguous.

A

#### Anonymous

When it come to "0 degrees ", the data log for computer mathematical calculating is added another "360 degrees ".

A

#### Anonymous

Take the sinus of the angle. calculate the average of the sinus values and take the inverse sinus of the result. This will give you the average angle.

F

#### Friedrich Haase

Moin Mr. Ville,
moin all,

for angles the mathematics work slightly different as usual (angles do not belong to the body of real numbers.)

Angles are in the one side open range [0..360), i.e. including 0 but excluding 360 and above. While building the sum of all measured angles subtract 360 until the result is in that range.

Sample: 2 times 350 degrees and 1 time 20 degrees. The sum will hold
1. 350
2. 700, which will be reduced to 340
3. 360, which will be reduced to 0
finally divide by 3.

If you have the mod function available, you could simply sum all angles and finally calc the remainder mod 360 before you divide by the number of measured values.

Regards
Friedrich Haase

Ing.-Büro Dr. Friedrich Haase
Consulting - Automatisierungstechnik
email [email protected]
WEB http://www.61131.com

R

#### Robert Scott

All the answers given so far are wrong. The suggestion of averaging the sines and then taking the inverse sine can’t work because the inverse sine only covers one quadrant. The suggestion of Michael Griffin and Friedrich Haase to take the sum module 360 before dividing by the number of entries will not work as illustrated by:

((100+100+100+100)modulo 360) / 4 = 10 (incorrect, should be 100).

Here is a solution that really will work. For each angle sampled, accumulate the sum of the sines and the sum of the cosines. Then divide each of these sums by the number of entries to get:

x = average cosine
y = average sine

Then use the arctan2 function to get the average angle as:

Angle = arctan2( y , x);

Note that it is still possible to get an indeterminate answer if x=0 and y=0, as would be the case when trying to average 90 and 270. There is no solution to that problem anyway. Finally, you might want to weight the averages with the wind velocity. A wind angle of 10 degrees at 20 knots averaged with a wind angle of 20 degrees at 3 knots should probably not be 15 degrees. The angle when the wind was at 20 knots is probably more important than the angle when the wind was almost calm. So they should not be averaged together without some unequal weighting.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting

B

#### Bob Peterson

Convert the wind direction and speed into a vector, sum them up, then divide by the number of samples.

M

#### Michael Griffin

Re: Robert Scott's reply - Your formula is elegant, but I believe that it has an error. "Angle = arctan2( y , x)" should be "Angle = arctan2( x , y)".

However, the trigonometric algorithm is itself not a general solution, as it may produce an average direction which the instrument never at any time pointed to or passed through. For example consider the following set of direction readings (taken in sequence) 120, 120, 120, 90, 0, 0, 270, 240, 240, 240. We can see the wind gradually shifted from 120 degrees to 240 degrees, while passing through 0 degrees. It would seem reasonable that the "average" in this case is 0 degrees. We can also see that the wind did in fact blow at 0 degrees, at one point.

The trigonometric solution though produces a result of 180 degrees, even though the wind never in fact blew in that direction at any time. It would appear that the path taken to travel between points is as important as the readings themselves.

The modulo solution is not incorrect, it is simply incomplete as it does not cover all cases. Admittedly, this distinction is probably of little interest to most people.

There is however, another approach which is probably simpler than the modulo version, but also does not require trigonometric functions (which are not always available). Unless I am mistaken, I believe there are two cases. One case is where the line which connects the extreme and all intermediate values passes through zero, and the other case is where it does not.

Where the line passes through zero, the values to the "right" of zero (e.g. 359, 358, 357, etc.) could be converted to negative angles (e.g. 359 would be -1, 358 would be -2, etc.). If the average is negative, this would then be corrected to a positive angle by adding 360 again.

Where the line does not pass through zero, this step would be unecessary. In either case, a simple mathematical average could be used as there is no longer a numerical discontinuity. The objective in either case is to produce a single continuous scale describing the path actually taken.

Determining whether the line passes through zero is an exercise that is left for the student.

R

#### Robert Scott

Michael, when you say that 'It would seem reasonable that the "average" in this case is 0 degrees’, you would have to ask the original poster (Ville) what the client wanted the average for. It is not at all clear that in your example, 0 degrees is the best answer. Your "method" relies on the extrapolation of sampled data to continuous data, something that is not always possible if the sampled data changes fast enough compared to the sampling rate. If the wind is sampled just once every 5 minutes, it will be impossible to say if the wind ever did blow exactly at the average angle at any time. Let me propose a different data set, and you tell me what the "correct" average is:

179,179,179,179,179,10,195,181,181,181,181,181

If your purpose in forming the average is to determine where to place a windscreen to protect a rose bush, I would say something near is 180 is best. But your method would have me place the windscreen on exactly the wrong side.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting

M

#### Michael Griffin

Re: Robert Scott's reply - You are simply raising the question of whether the sampling rate is fast enough. That wasn't a point I was trying to address. The example I presented was intended to illustrate a mathematical point, not to serve as a genuine data set. Your counter-example does not change the fact that a simple averaging of readings can under the right circumstances produce an answer that can reasonably considered to be misleading. Averaging a scale which repeats after a finite point is not the same as averaging a scale of infinite length.

If the wind is sampled every 5 minutes, then averaging the reading via a simple formula is pointless. If the readings are intended for use in weather prediction, then the appropriate source to be consulted on this should be a
meteorologist, not the Automation List. Meteorologists no doubt have standard measurement and analysis techniques to address these questions in a manner which is consistant with readings taken elsewhere. We could come up with dozens of clever algorithms, but none of them are of much use if they don't follow the conventions used in collecting weather data.

As for protecting your rose bushes, the normal means to do this is to wrap them completely in burlap for the winter, is it not? Knowing the average is of little use in the exception.

C

#### Chris Deverill

<p>Hi.

<p>There seems to have been rather a lot of wide-ranging discussion already on this topic (I've also learnt something about gardening too, which is a bonus).

<p>I've done something like this before, and used a vector based approach. As you have a SCADA system, it is straightforward to build two analog calculation tags, say WindX and WindY, which represent the X and Y component of the wind magnitude and direction. These two tags should be historized, so that it is easy to extract the 1 hour average or 8 hour average or whatever you need for the report.
<pre>
WindX = Magnitude*COS(2*3.1416*(90-Direction)/360)
WindY = Magnitude*SIN(2*3.1416*(90-Direction)/360)
</pre>
<p>The convention for X and Y is standard rectangular coordinates, ie X = East, Y = North

<p>Once you've got the X and Y values being historized, the average wind strength and direction can be calculated by getting the average of X and Y over the required period.

<p>You then need to convert back to compass-based co-ordinates. The following VBA script (part of a macro in a spreadsheet) does this:
<pre>
'---------------------
Sub WindCalculations()
'---------------------
'
Dim nRow As Long, nCol As Long
Dim rAlpha As Single ' Angle, polar coordinates
Dim rBeta As Single ' Angle, from North
Dim x As Double, y As Double

Dim rVal As Double

For nRow = 2 To 18
x = Cells(nRow, 5) ' Column E, WindX
y = Cells(nRow, 6) ' Column F, WindY

' Magnitude
' ---------
Cells(nRow, 8) = Sqr(x * x + y * y)

' Calculate polar coordinate angle
' --------------------------------
If x = 0 Then
If y > 0 Then
rAlpha = 90
ElseIf y < 0 Then
rAlpha = -90
Else
rAlpha = 0
End If
Else
rVal = y / x
If x > 0 And y > 0 Then
rAlpha = 180 / 3.14159265358979 * Atn(rVal)
ElseIf x < 0 And y < 0 Then
rAlpha = 180 + 180 / 3.14159265358979 * Atn(rVal)
ElseIf x > 0 And y < 0 Then
rAlpha = 180 / 3.14159265358979 * Atn(rVal)
Else
' x < 0 and y > 0
rAlpha = -180 + 180 / 3.14159265358979 * Atn(rVal)
End If

End If

Cells(nRow, 9) = rAlpha

' Convert to compass bearing
' --------------------------
rBeta = 90 - rAlpha
If rBeta < 0 Then
rBeta = rBeta + 360
End If

Cells(nRow, 10) = rBeta

Next nRow

End Sub

--- End of WindCalcs ---
</pre>
<p>This VBA code forms part of a macro in a spreadsheet, which can be downloaded from my web site:

<p>Hope that helps you out. Now I'd better get back to my regular job...

<p>Chris D<br>
http://www.procdev.com

D

I propose the following algorithm for calculating the average of a set of directions, measured from 0 to 360 degrees:

1. Find the average and standard deviation of the given numbers.
2. Increase the smallest number by 360.
3. Repeat steps 1 and 2 until all numbers are greater than 360.
4. Choose the average that yields the smallest standard deviation.
5. If the average is greater than 360 then subtract 360.

I have posted a longer explanation at http://polytope.com/average-angle.txt

R

#### Romeli

I suggest to devide them by 2 axis X for Cos a & Y for Sin a. We can make average value for each axis X & Y. Then we can find the final number of average value of the wind direction.
--
Romeli
Electrical engineer
PT.Smelting
Affiliate of Mitsubishi Materials
phone: 62-31-3976464 fax: 62-31-3976466
www.smelting.co.id

J

#### juanpa

Ok, how it works if you have, for example:

(90+90+90+90+90) modulo 360) / 5 = 18 (incorrect)
(90+90+90+90+90) / 5 = 90 (correct).

The method that you proposed doesn't work!

R

#### Rex Morgan

First convert for Degrees to Radians.
Then take the SIN of the Radians.
Next Average the SINs.
Now Take the inverse sin of the average.
Finally convert back to degrees.

Using EXCEL do this
Input directions in degrees say from cell A1 through A10.
in Cell B1 type -> Radians(A1)
in Cell C1 type -> SIN(B1)
in Cell A13 type -> Average(A1:A10)
in Cell A14 type -> ASIN(A13)
in Cell A15 type -> Degrees(A14)

Then if you would like to round to nearest five degrees you can
in Cell A16 type -> Mround(A15,5)

D

#### Dr. Hasan TATLI

You have circular data that you must apply circular mathematics.

In first step, you should calculate sum of sin(theta) and cos (theta), where theta is your wind direction. In second step take atan2[sum(sin(theta),sum(cos(theta)]), represents the average wind direction. But the answer is positively defined, however this type of wind direction traditionally does not used in meteorology, since 0 (or 360) indicates Northly but in mathematics it is 90 degree; hence you should convert to meteorological wind directions.

Dr. Hasan TATLI (Canakkale Onsekiz Mart University, Dept. of Geography, Turkey)

R

#### Rik Fransens

Hi guys,

Sorry to disrupt the party, but I'm pretty sure most answers to this posting are not correct.

Averaging x,y-directions, sines of angles... (e.g. obtained after atan()-calls) is bound to fail because it will produce biased results.

What should be done is computing the first eigen-vector of the direction covariance-matrix. Here is how to do it:

1. Stack all n directions (normalised wind-vectors) in an (n x 2)-matrix M (i.e. 1 direction per row).

2. Compute the direction covariance matrix as follows:
C = M' * M
where M' is the transpose of M and * is the regular matrix-product. C should now be a (2 x 2)-matrix.

3. Compute the first eigen-vector of this matrix. This is the dominant direction.

Notice that this procedure does not take the magnitude of each direction into account, i.e. the dominant direction is not biased towards the direction(s) with largest wind-magnitude. If you also want to take this into account, just omit the normalisation of wind-vectors when computing C.

Kind regards,

Rik