angle

Converting wind direction in angles to text words


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!


Solution

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

    1. Divide the angle by 22.5 because 360deg/16 directions = 22.5deg/direction change.
    2. Add .5 so that when you truncate the value you can break the 'tie' between the change threshold.
    3. Truncate the value using integer division (so there is no rounding).
    4. Directly index into the array and print the value (mod 16).