I have wind direction data coming from a weather vane, and the data is represented in 0 to 359 degrees.
I want to convert this into text format (compass rose) with 16 different directions.
Basically I want to know if there is a fast slick way to scale the angle reading to a 16 string array to print out the correct wind direction without using a bunch of if statements and checking for ranges of angles
Wind direction can be found here.
thanks!
The truth is that wind direction can also be presented in azimuth degrees, i.e., in a numerical measure that moves around the Rose of the Winds in a clockwise circle from 0 degrees (N) to 360 degrees (N). So, if north represents 0 degrees, then east is 90 degrees, south is 180 degrees, and west sits at 270 degrees.
To convert degrees to compass directions, I first divide the compass into 16 sectors of 22.5 degrees each. The sectors are like slices of pie, centered on the compass points. Tip: If you would like to use eight sectors instead of 16, with a shorter lookup table, divide by 45 degrees instead of 22.5 degrees.
Wind direction is typically reported in degrees, and describes the direction from which the wind emanates. A direction of 0 degrees is due North on a compass, and 180 degrees is due South. A direction of 270 degrees would indicate a wind blowing in from the west.
North is 360 degrees. A wind direction of 0 degrees is only used when wind is calm.
EDIT :
Since there is an angle change at every 22.5 degrees, the direction should swap hands after 11.25 degrees.
Therefore:
349-360//0-11 = N 12-33 = NNE 34-56 = NE
Using values from 327-348 (The entire NNW spectrum) failed to produce a result for eudoxos' answer. After giving it some thought I could not find the flaw in his logic, so i rewrote my own..
def degToCompass(num): val=int((num/22.5)+.5) arr=["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"] print arr[(val % 16)] >>> degToCompass(0) N >>> degToCompass(180) S >>> degToCompass(720) N >>> degToCompass(11) N >>> 12 12 >>> degToCompass(12) NNE >>> degToCompass(33) NNE >>> degToCompass(34) NE
STEPS :
Here's a javascript implementation of steve-gregory's answer, which works for me.
function degToCompass(num) { var val = Math.floor((num / 22.5) + 0.5); var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]; return arr[(val % 16)]; }
See his answer for an explanation of the logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With