/
windbearing.go
157 lines (152 loc) · 4.27 KB
/
windbearing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Code generated by "genbearing.go"; DO NOT EDIT.
package wx
import "fmt"
// WindBearing returns the compass direction for the wind bearing name
func WindBearing(bearing string) (float64, error) { // nolint:gocyclo
switch bearing {
case "N": // North
return 0.00, nil
case "NbE": // North by east
return 11.25, nil
case "NNE": // North-northeast
return 22.50, nil
case "NEbN": // Northeast by north
return 33.75, nil
case "NE": // Northeast
return 45.00, nil
case "NEbE": // Northeast by east
return 56.25, nil
case "ENE": // East-northeast
return 67.50, nil
case "EbN": // East by north
return 78.75, nil
case "E": // East
return 90.00, nil
case "EbS": // East by south
return 101.25, nil
case "ESE": // East-southeast
return 112.50, nil
case "SEbE": // Southeast by east
return 123.75, nil
case "SE": // Southeast
return 135.00, nil
case "SEbS": // Southeast by south
return 146.25, nil
case "SSE": // South-southeast
return 157.50, nil
case "SbE": // South by east
return 168.75, nil
case "S": // South
return 180.00, nil
case "SbW": // South by west
return 191.25, nil
case "SSW": // South-southwest
return 202.50, nil
case "SWbS": // Southwest by south
return 213.75, nil
case "SW": // Southwest
return 225.00, nil
case "SWbW": // Southwest by west
return 236.25, nil
case "WSW": // West-southwest
return 247.50, nil
case "WbS": // West by south
return 258.75, nil
case "W": // West
return 270.00, nil
case "WbN": // West by north
return 281.25, nil
case "WNW": // West-northwest
return 292.50, nil
case "NWbW": // Northwest by west
return 303.75, nil
case "NW": // Northwest
return 315.00, nil
case "NWbN": // Northwest by north
return 326.25, nil
case "NNW": // North-northwest
return 337.50, nil
case "NbW": // North by west
return 348.75, nil
default:
return 0.0, fmt.Errorf("unknown bearing: %s", bearing)
}
}
func between(bearing, lower, upper float64) bool {
if lower > upper {
// this is the case for North which straddles 360°
return (bearing >= lower && bearing <= 360.0) || (bearing >= 0.0 && bearing < upper)
}
return bearing >= lower && bearing < upper
}
// CompassPoint converts a wind bearing to the text compass point name
func CompassPoint(bearing float64) (string, error) { // nolint:gocyclo
switch {
case between(bearing, 354.375, 5.625):
return "N", nil
case between(bearing, 5.625, 16.875):
return "NbE", nil
case between(bearing, 16.875, 28.125):
return "NNE", nil
case between(bearing, 28.125, 39.375):
return "NEbN", nil
case between(bearing, 39.375, 50.625):
return "NE", nil
case between(bearing, 50.625, 61.875):
return "NEbE", nil
case between(bearing, 61.875, 73.125):
return "ENE", nil
case between(bearing, 73.125, 84.375):
return "EbN", nil
case between(bearing, 84.375, 95.625):
return "E", nil
case between(bearing, 95.625, 106.875):
return "EbS", nil
case between(bearing, 106.875, 118.125):
return "ESE", nil
case between(bearing, 118.125, 129.375):
return "SEbE", nil
case between(bearing, 129.375, 140.625):
return "SE", nil
case between(bearing, 140.625, 151.875):
return "SEbS", nil
case between(bearing, 151.875, 163.125):
return "SSE", nil
case between(bearing, 163.125, 174.375):
return "SbE", nil
case between(bearing, 174.375, 185.625):
return "S", nil
case between(bearing, 185.625, 196.875):
return "SbW", nil
case between(bearing, 196.875, 208.125):
return "SSW", nil
case between(bearing, 208.125, 219.375):
return "SWbS", nil
case between(bearing, 219.375, 230.625):
return "SW", nil
case between(bearing, 230.625, 241.875):
return "SWbW", nil
case between(bearing, 241.875, 253.125):
return "WSW", nil
case between(bearing, 253.125, 264.375):
return "WbS", nil
case between(bearing, 264.375, 275.625):
return "W", nil
case between(bearing, 275.625, 286.875):
return "WbN", nil
case between(bearing, 286.875, 298.125):
return "WNW", nil
case between(bearing, 298.125, 309.375):
return "NWbW", nil
case between(bearing, 309.375, 320.625):
return "NW", nil
case between(bearing, 320.625, 331.875):
return "NWbN", nil
case between(bearing, 331.875, 343.125):
return "NNW", nil
case between(bearing, 343.125, 354.375):
return "NbW", nil
default:
return "", fmt.Errorf("unknown bearing: %0.3f", bearing)
}
}