Converting wind direction in angles to text words

后端 未结 15 770
执笔经年
执笔经年 2021-01-30 13:16

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 di

15条回答
  •  温柔的废话
    2021-01-30 13:48

    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).

提交回复
热议问题