Calculate wind vector-direction

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

1 Like

@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

2 Likes

you need to use vectors for wind direction averaging

2 Likes

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
    };
},
6 Likes

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

2 Likes

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

2 Likes

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

2 Likes

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 :sunglasses:

3 Likes

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

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

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

1 Like

I prefer my math vector-less.
:hugs:


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

3 Likes

10 posts were split to a new topic: The ol’ guys and coding skills from way way back :wink:

A post was merged into an existing topic: The ol’ guys and coding skills from way way back :wink:

It is weighted. Look closer at the formula.

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?

Those two lines get the average.

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

Okay. Try it both ways and see for yourself.

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

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

1 Like