an aside on weather: Wind direction

Most weather APIs don’t convert the wind bearing into a cardinal direction. That’s okay if you’re a pilot or something where you’re used to figuring the bearing, but for the rest of us, what does wind bearing 142 mean?

I found a handy website (there are probably many) that had a little graphic to show how the two mapped to one another here.  I used that info to make a killer if-structure to find the cardinal direction.  Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
wDir = "";
if (wBearing >= 348.75 || wBearing < 11.25) { wDir = "N";} else
if (wBearing >= 11.25 && wBearing < 33.75) { wDir = "NNE";} else
if (wBearing >= 33.75 && wBearing < 56.25) { wDir = "NE";} else
if (wBearing >= 56.25 && wBearing < 78.75) { wDir = "ENE";} else
if (wBearing >= 78.75 && wBearing < 101.25) { wDir = "E";} else
if (wBearing >= 101.25 && wBearing < 123.75) { wDir = "ESE";} else
if (wBearing >= 123.75 && wBearing < 146.25) { wDir = "SE";} else
if (wBearing >= 146.25 && wBearing < 168.75) { wDir = "SSE";} else
if (wBearing >= 186.75 && wBearing < 191.25) { wDir = "S";} else
if (wBearing >= 191.25 && wBearing < 213.75) { wDir = "SSW";} else
if (wBearing >= 213.75 && wBearing < 236.25) { wDir ="SW";} else
if (wBearing >= 236.25 && wBearing < 258.75) { wDir = "WSW";} else
if (wBearing >= 258.75 && wBearing < 281.25) { wDir = "W" ;} else
if (wBearing >= 281.25 && wBearing < 303.75) { wDir = "WNW";} else
if (wBearing >= 303.75 && wBearing < 326.25) { wDir = "NW";} else
{ wDir = "NNW";};

It’s very readable and that’s important for something like this.  Sure, it takes a bit of time to set up, but it makes the direction so much more user-friendly.

Leave a Reply

Your email address will not be published. Required fields are marked *