# Calculate wind vector-direction

**URL:** <https://community.tempest.earth/t/calculate-wind-vector-direction/921>\
**Category:** Developers\
**Tags:** formula\
**Created:** [May 1, 2018, 5:16pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921 "2018-05-01T17:16:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![todd.lorey](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@todd.lorey](https://community.tempest.earth/u/todd.lorey)\
**Post date:** [May 1, 2018, 5:16pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/1 "2018-05-01T17:16:53Z")

</div>

Gary, you got me thinking…

I have been thinking about this average wind direction and it is actually pretty tricky to implement! The periodic nature of wind direction 0-359 degrees with a “reset” to 0 degrees is the kicker. Taking an average is more complicated than I thought!

Assume we have this reduced data set to make an average: 355, 355, 5, 5  
If you add them up and divide by 4 you get 180, which is exactly the opposite of the true wind direction average of 0 degrees. I’m sure there’s an algorithm that can fix this but it isn’t as straightforward as I originally thought, at first glance.

It probably would do something like figure out the general span of wind direction in a 180 or 270 degree range, that re-maps the raw numbers to greater than 359, so 5 degrees might become 365 degrees for the calculation, then the simple averaging method would work… i.e.  
355, 355, 365, 365 added together and divided by 4 yields 360 = 0 degrees

---

<div class="post-metadata">

**Author:** ![btst.online](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/btst.online/32/16538_2.png) [@btst.online](https://community.tempest.earth/u/btst.online)\
**Post date:** [May 1, 2018, 5:30pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/2 "2018-05-01T17:30:34Z")

</div>

@todd.lorey, I do a similar thing on my Wx Console… where I use the ‘min’ and ‘max’ as a far left and far right bearing…

```
                double bmax = sBmax.toDouble();
                double bmin = sBmin.toDouble();
                double bcurr = sBcur.toDouble();

                double bleft, bright, bavg, ang;

                if( bmax == 360.0 ) bmax = 359.9;

                if( (bmax - bmin) < 180.0 ) {
                    bleft = bmin;
                    bright = bmax;
                    ang = bmax - bmin;
                    bavg = ang/2.0 + bmin;
                } else {
                    if( firstMsg ) {
                        firstMsg = false;
                        oldLeft = bmax;
                        oldRight = bmin;
                    }
                    ang = bmax - bmin;
                    if( (bcurr > bmin) && (bcurr < (bmin + (ang / 2.0))) && (bcurr > oldRight) ) {
                        bright = bcurr;
                    } else {
                        if( bmin > oldRight ) {
                            bright = bmin;
                        } else {
                            bright = oldRight;
                        }
                    }
                    oldRight = bright;

                    if( (bcurr < bmax) && (bcurr >= (bmin + (ang / 2.0))) && (bcurr < oldLeft) ) {
                        bleft = bcurr;
                    } else {
                        if( bmax < oldLeft ) {
                            bleft = bmax;
                        } else {
                            bleft = oldLeft;
                        }
                    }
                    oldLeft = bleft;

                    ang = (bright + 360.0) - bleft;
                    ang = ang / 2.0;
                    bavg = bleft + ang;
                    if( bavg > 360.0 ) bavg = bavg - 360.0; 

```

–Sam

---

<div class="post-metadata">

**Author:** ![weather-display](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/weather-display/32/989_2.png) [@weather-display](https://community.tempest.earth/u/weather-display)\
**Post date:** [May 1, 2018, 6:27pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/3 "2018-05-01T18:27:49Z")

</div>

you need to use vectors for wind direction averaging

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [May 1, 2018, 6:33pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/4 "2018-05-01T18:33:28Z")

</div>

Here is the code I use to determine average wind direction. Past tests have shown it to match WeatherFow 100%,

d is an array of 20 rapid\_wind observations and is the formula uses wind speed to “weight” the direction vector.

```
WeatherCalc.calcWind = function(d) {
	var i,
		EW_Vector = 0,
		NS_Vector = 0,
		gust = 0,
		lull = 999,
		s = 0;
	for (i = 0; i < d.length; i++) {
		s += d[i][1];
		EW_Vector += Math.sin(0.01745329252 * d[i][2]) * d[i][1];
		NS_Vector += Math.cos(0.01745329252 * d[i][2]) * d[i][1];
		if (d[i][1] > gust) gust = d[i][1];
		if (d[i][1] < lull) lull = d[i][1];
	}
	var speed = s / d.length;
	var EW_Average = (EW_Vector / d.length) * -1;
	var NS_Average = (NS_Vector / d.length) * -1;
	var dir = (Math.atan2(EW_Average, NS_Average) / 0.01745329252);
	var direction = Math.round( dir > 180 ? dir -= 180 : dir += 180 );
    return {
    	date: d[0][0],
        direction: direction.toString(),
        speed: speed,
        lull: lull,
        gust: gust
    };
},
```

---

<div class="post-metadata">

**Author:** ![weather-display](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/weather-display/32/989_2.png) [@weather-display](https://community.tempest.earth/u/weather-display)\
**Post date:** [May 1, 2018, 6:35pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/5 "2018-05-01T18:35:24Z")

</div>

I would be going a step further and have a wind speed weight with the direction vector

---

<div class="post-metadata">

**Author:** ![eric](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/eric/32/13553_2.png) [@eric](https://community.tempest.earth/u/eric)\
**Post date:** [May 1, 2018, 6:37pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/6 "2018-05-01T18:37:41Z")

</div>

Took liberty to create a new topic as this might interest many others with coding skills

---

<div class="post-metadata">

**Author:** ![todd.lorey](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@todd.lorey](https://community.tempest.earth/u/todd.lorey)\
**Post date:** [May 1, 2018, 6:53pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/7 "2018-05-01T18:53:28Z")

</div>

Thanks guys! I see there has been some thought put into this issue. Cool to see the different coding methods.

---

<div class="post-metadata">

**Author:** ![weather-display](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/weather-display/32/989_2.png) [@weather-display](https://community.tempest.earth/u/weather-display)\
**Post date:** [May 1, 2018, 7:00pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/8 "2018-05-01T19:00:40Z")

</div>

I have been in the business of software for electronic weather stations for nearly 20 years now  
So I do know a thing or two 🕶

---

<div class="post-metadata">

**Author:** ![peter](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/peter/32/1365_2.png) [@peter](https://community.tempest.earth/u/peter)\
**Post date:** [May 1, 2018, 7:30pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/9 "2018-05-01T19:30:04Z")

</div>

This is the Wikipedia reference I use whenever it comes to averaging circular quantities:

> **[Mean of circular quantities](https://en.wikipedia.org/wiki/Mean_of_circular_quantities)**
>
> In mathematics, a mean of circular quantities is a mean which is sometimes better-suited for quantities like angles, daytimes, and fractional parts of real numbers. This is necessary since most of the usual means may not be appropriate on circular quantities. For example, the arithmetic mean of 0° and 360° is 180°, which is misleading because for most purposes 360° is the same thing as 0°. As another example, the "average time" between 11 PM and 1 AM is either midnight or noon, depending on wh

I can never remember the exact expression for the average off the top of my head!

---

<div class="post-metadata">

**Author:** ![weather-display](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/weather-display/32/989_2.png) [@weather-display](https://community.tempest.earth/u/weather-display)\
**Post date:** [May 1, 2018, 7:47pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/10 "2018-05-01T19:47:44Z")

</div>

you need to use vectors  
i.e using sin and cos  
as like Gary Funk showed  
but even better is too weight that vector with the windspeed

---

<div class="post-metadata">

**Author:** ![btst.online](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/btst.online/32/16538_2.png) [@btst.online](https://community.tempest.earth/u/btst.online)\
**Post date:** [May 1, 2018, 7:57pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/11 "2018-05-01T19:57:02Z")

</div>

I prefer my math vector-less.  
🤗

* * *

I don’t do Math often, but when I do, I use Vectors!  
Stay Mathy, my friends.

---

<div class="post-metadata">

**Author:** ![eric](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/eric/32/13553_2.png) [@eric](https://community.tempest.earth/u/eric)\
**Post date:** [May 9, 2018, 12:49pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/12 "2018-05-09T12:49:18Z")

</div>

Split this topic on May 9, 2018, 12:49pm UTC

10 posts were split to a new topic: [The ol’ guys and coding skills from way way back 😉](https://community.tempest.earth/t/the-ol-guys-and-coding-skills-from-way-way-back/983)

---

<div class="post-metadata">

**Author:** ![eric](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/eric/32/13553_2.png) [@eric](https://community.tempest.earth/u/eric)\
**Post date:** [May 9, 2018, 12:56pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/13 "2018-05-09T12:56:45Z")

</div>

Split this topic on May 9, 2018, 12:56pm UTC

A post was merged into an existing topic: [The ol’ guys and coding skills from way way back 😉](https://community.tempest.earth/t/the-ol-guys-and-coding-skills-from-way-way-back/983/11)

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [May 9, 2018, 1:02pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/14 "2018-05-09T13:02:15Z")

</div>

> [@weather-display](#):
>
> as like Gary Funk showed  
> but even better is too weight that vector with the windspeed

It is weighted. Look closer at the formula.

---

<div class="post-metadata">

**Author:** ![sunny](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/sunny/32/16368_2.png) [@sunny](https://community.tempest.earth/u/sunny)\
**Post date:** [September 9, 2018, 11:02pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/15 "2018-09-09T23:02:56Z")

</div>

> [@GaryFunk](#):
>
> var EW\_Average = (EW\_Vector / d.length) \* -1; var NS\_Average = (NS\_Vector / d.length) \* -1;

just wondering where these two lines are good for, atan2(y,x) is almost the same as atan(y/x) so multiplying both with the same number is kind of pointless. isn’t it?

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [September 9, 2018, 11:35pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/16 "2018-09-09T23:35:25Z")

</div>

Those two lines get the average.

---

<div class="post-metadata">

**Author:** ![sunny](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/sunny/32/16368_2.png) [@sunny](https://community.tempest.earth/u/sunny)\
**Post date:** [September 9, 2018, 11:50pm UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/17 "2018-09-09T23:50:49Z")

</div>

But atan2(10,20) is the same as atan2(1,2), so it’s not needed.

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [September 10, 2018, 2:32am UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/18 "2018-09-10T02:32:07Z")

</div>

Okay. Try it both ways and see for yourself.

---

<div class="post-metadata">

**Author:** ![sunny](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/sunny/32/16368_2.png) [@sunny](https://community.tempest.earth/u/sunny)\
**Post date:** [September 10, 2018, 7:03am UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/19 "2018-09-10T07:03:20Z")

</div>

It doesn’t matter too much, I was just curious about how you calculated it. But those two lines are redundant. Atan((y_a) /(x_a)) is independent of the value of a ( unless a is zero). atan2 is just a fancy, better way of calculating this.

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [October 10, 2018, 10:43am UTC](https://community.tempest.earth/t/calculate-wind-vector-direction/921/20 "2018-10-10T10:43:55Z")

</div>

@peter Here is thread with the formula to calculation for average wind direction.

[Next page](https://community.tempest.earth/t/calculate-wind-vector-direction/921.md?page=2)
