-4

How do I calculate the average wind speed and direction from a list of values over a period?

const directions = ["S","S","NE","SW","NNE","N"];
const speeds     = [11,9,9,7,18,10]

There numerous resources online to calculate the average between A and B (javascript, MYSQL, SQLServer), but nothing that is simple to understand and easy to use.

Kevin Potgieter
  • 672
  • 10
  • 23
  • And `+((numbers.reduce((a, b) => a + b, 0) / numbers.length).toFixed(0))` is why easier than those mentioned other solutions? – Andreas Jan 09 '23 at 10:12
  • Can you clarify the question. It seems like you want to convert a list of compass points into a mean value in degrees. Is that correct? And then what? You want the mean wind speed divided by the mean degrees? Maybe if you explain why you want to do this, it would help people figure out the best approach. – Toby Jan 09 '23 at 10:12
  • check this article: https://stackanswers.blogspot.com/2023/01/visualizing-wind-data-with-javascript.html – Anass Kartit Jan 09 '23 at 11:53

1 Answers1

-3

I don't think this is the answer but could help


    function getAverageNumber(numbers){
        return +((numbers.reduce((a, b) => a + b, 0) / numbers.length).toFixed(0))
    }
    function getWindDirection(d){
        if(d===0 || d===360) return "N";
        if(d===90) return "E";
        if(d===180) return "S";
        if(d===270) return "W";
        if (d>0 && d<90) return "NE";
        if (d>90 && d<180) return "SE";
        if (d>180 && d<270) return "SW";
        if (d>270 && d<360) return "NW";
    } 
    function getWindDegrees(w){
        if(w==="N") return 0;
        if(w==="NE") return 45;
        if(w==="E") return 90;
        if(w==="SE") return 135;
        if(w==="S") return 180;
        if(w==="SW") return 225;
        if(w==="W") return 270;
        if(w==="NW") return 315;
    } 
    function getAverageWind(_directions, _speeds){
        const averageDirection = getAverageNumber(_directions.map(getWindDegrees));
        const averageSpeed = getAverageNumber(_speeds);
        return { speed: averageSpeed, direction: getWindDirection(averageDirection) }
    }

const result = getAverageWind(directions, speeds)

// i don't this answer is entirely accurate
console.log(`${result.speed} km/h ${result.direction}`)
Kevin Potgieter
  • 672
  • 10
  • 23