Converting wind direction in angles to text words

后端 未结 15 764
执笔经年
执笔经年 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:47

    Watch out for rounding, angles between 349...11 should be "N", therefore add half sector first (+(360/16)/2), then handle overflow over 360 by %360, then divide by 360/16:

    ["N","NNW",...,"NNE"][((d+(360/16)/2)%360)/(360/16)]
    
    0 讨论(0)
  • 2021-01-30 13:47

    I believe it is easier to:

    1. Shift the direction by 11.25
    2. Add an "N" at the end of the direction list to handle the 'over 360',

    DirTable = ["N","NNE","NE","ENE","E","ESE", "SE","SSE","S","SSW","SW","WSW", "W","WNW","NW","NNW",**"N"**]; 
    
    wind_direction= DirTable[Math.floor((d+11.25)/22.5)];
    
    0 讨论(0)
  • 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).
    0 讨论(0)
  • 2021-01-30 13:49

    Here's a one-line python function:

    def deg_to_text(deg):
        return ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"][round(deg/22.5)%16]
    

    Obviously it can be split into multiple lines for readability/pep8

    0 讨论(0)
  • 2021-01-30 13:50

    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.

    0 讨论(0)
  • 2021-01-30 13:51

    Javascript function 100% working

    function degToCompass(num) { 
        while( num < 0 ) num += 360 ;
        while( num >= 360 ) num -= 360 ; 
        val= Math.round( (num -11.25 ) / 22.5 ) ;
        arr=["N","NNE","NE","ENE","E","ESE", "SE", 
              "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"] ;
        return arr[ Math.abs(val) ] ;
    }
    

    steps

    1. Given a 360 degree angle
    2. Since north is between -11.25 to 11.25 we subtract 11.25 for accuracy
    3. Divide the angle by 22.5 because 360deg/16 directions = 22.5deg/direction change
    4. Math.abs for as negative is still north
    5. Select the segment from arr from answer

    Hope it helps

    0 讨论(0)
提交回复
热议问题