Calculate wind vector-direction

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