Converting wind direction in angles to text words

后端 未结 15 918
执笔经年
执笔经年 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 14:10

    this works fine

    #!/usr/bin/env python
    
    def wind_deg_to_str1(deg):
            if   deg >=  11.25 and deg <  33.75: return 'NNE'
            elif deg >=  33.75 and deg <  56.25: return 'NE'
            elif deg >=  56.25 and deg <  78.75: return 'ENE'
            elif deg >=  78.75 and deg < 101.25: return 'E'
            elif deg >= 101.25 and deg < 123.75: return 'ESE'
            elif deg >= 123.75 and deg < 146.25: return 'SE'
            elif deg >= 146.25 and deg < 168.75: return 'SSE'
            elif deg >= 168.75 and deg < 191.25: return 'S'
            elif deg >= 191.25 and deg < 213.75: return 'SSW'
            elif deg >= 213.75 and deg < 236.25: return 'SW'
            elif deg >= 236.25 and deg < 258.75: return 'WSW'
            elif deg >= 258.75 and deg < 281.25: return 'W'
            elif deg >= 281.25 and deg < 303.75: return 'WNW'
            elif deg >= 303.75 and deg < 326.25: return 'NW'
            elif deg >= 326.25 and deg < 348.75: return 'NNW'
            else: return 'N'
    
    def wind_deg_to_str2(deg):
            arr = ['NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']
            return arr[int(abs((deg - 11.25) % 360)/ 22.5)]
    
    i = 0
    while i < 360:
            s1 = wind_deg_to_str1(i)
            s2 = wind_deg_to_str2(i)
            print '%5.1f deg -> func1(%-3s), func2(%-3s), same:%s' % (i, s1, s2, ('ok' if s1 == s2 else 'different'))
            i += 0.5
    

提交回复
热议问题