-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_raw.go
More file actions
311 lines (278 loc) · 6.18 KB
/
Copy pathdecode_raw.go
File metadata and controls
311 lines (278 loc) · 6.18 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"fmt"
"strconv"
"strings"
)
func decodeRawMETARToHuman(raw string) error {
// If multiple lines, take the first non-empty line.
lines := strings.Split(raw, "\n")
var metarLine string
for _, ln := range lines {
ln = strings.TrimSpace(ln)
if ln != "" {
metarLine = ln
break
}
}
if metarLine == "" {
return fmt.Errorf("no METAR content found on stdin")
}
tokens := strings.Fields(metarLine)
if len(tokens) < 3 {
fmt.Printf("Raw: %s\n", metarLine)
return nil
}
i := 0
reportType := ""
if tokens[i] == "METAR" || tokens[i] == "SPECI" {
reportType = tokens[i]
i++
}
station := tokens[i]
i++
obsTime := tokens[i]
i++
fmt.Printf("Station: %s\n", station)
if reportType != "" {
fmt.Printf("Report: %s\n", reportType)
}
fmt.Printf("Observed: %s (DDHHMMZ)\n", obsTime)
// Optional AUTO/COR
if i < len(tokens) && (tokens[i] == "AUTO" || tokens[i] == "COR") {
if tokens[i] == "AUTO" {
fmt.Printf("Modifier: Automated\n")
} else {
fmt.Printf("Modifier: Corrected\n")
}
i++
}
// Wind
if i < len(tokens) && strings.HasSuffix(tokens[i], "KT") {
fmt.Printf("Wind: %s\n", decodeWindToken(tokens[i]))
i++
// optional variable dir 180V240
if i < len(tokens) && strings.Contains(tokens[i], "V") && len(tokens[i]) == 7 {
fmt.Printf("Wind variation: %s\n", tokens[i])
i++
}
}
// Visibility
if i < len(tokens) {
vis, used := decodeVisibility(tokens[i:], 0)
if used > 0 {
fmt.Printf("Visibility: %s\n", vis)
i += used
}
}
// Weather tokens until sky/temps/alt/RMK
var wx []string
for i < len(tokens) {
t := tokens[i]
if isSkyToken(t) || isTempDewToken(t) || isAltimeterToken(t) || t == "RMK" {
break
}
wx = append(wx, t)
i++
}
if len(wx) > 0 {
fmt.Printf("Weather: %s\n", decodeWeatherTokens(strings.Join(wx, " ")))
}
// Sky
var sky []string
for i < len(tokens) {
t := tokens[i]
if isTempDewToken(t) || isAltimeterToken(t) || t == "RMK" {
break
}
if isSkyToken(t) {
sky = append(sky, decodeSkyToken(t))
i++
continue
}
break
}
if len(sky) > 0 {
fmt.Printf("Sky: %s\n", strings.Join(sky, ", "))
}
// Temp/Dew
if i < len(tokens) && isTempDewToken(tokens[i]) {
tc, dc := decodeTempDew(tokens[i])
fmt.Printf("Temp/Dew: %s°C / %s°C\n", tc, dc)
i++
}
// Altimeter
if i < len(tokens) && isAltimeterToken(tokens[i]) {
fmt.Printf("Altimeter: %s\n", decodeAltimeter(tokens[i]))
i++
}
// RMK (raw)
for i < len(tokens) {
if tokens[i] == "RMK" {
fmt.Printf("Remarks: %s\n", strings.Join(tokens[i+1:], " "))
break
}
i++
}
fmt.Printf("Raw: %s\n", metarLine)
return nil
}
func decodeWindToken(tok string) string {
// 19004KT, VRB03KT, 19012G18KT, 00000KT
if !strings.HasSuffix(tok, "KT") {
return tok
}
core := strings.TrimSuffix(tok, "KT")
gust := ""
if strings.Contains(core, "G") {
parts := strings.SplitN(core, "G", 2)
core = parts[0]
gust = parts[1]
}
if strings.HasPrefix(core, "VRB") && len(core) >= 5 {
spd := core[3:]
if gust != "" {
return fmt.Sprintf("Variable at %s kt gusting %s kt", spd, gust)
}
return fmt.Sprintf("Variable at %s kt", spd)
}
if len(core) < 5 {
return tok
}
dir := core[:3]
spd := core[3:]
if dir == "000" && spd == "00" {
return "Calm"
}
if gust != "" {
return fmt.Sprintf("%s° at %s kt gusting %s kt", dir, spd, gust)
}
return fmt.Sprintf("%s° at %s kt", dir, spd)
}
func decodeVisibility(tokens []string, start int) (string, int) {
if start >= len(tokens) {
return "", 0
}
t0 := tokens[start]
if strings.HasSuffix(t0, "SM") {
return humanVisSM(t0), 1
}
// "1 1/2SM"
if start+1 < len(tokens) && strings.HasSuffix(tokens[start+1], "SM") && looksNumeric(tokens[start]) {
return strings.TrimSpace(tokens[start] + " " + humanVisSM(tokens[start+1])), 2
}
return "", 0
}
func humanVisSM(tok string) string {
core := strings.TrimSuffix(tok, "SM")
switch {
case strings.HasPrefix(core, "P"):
return fmt.Sprintf("Greater than %s statute miles", core[1:])
case strings.HasPrefix(core, "M"):
return fmt.Sprintf("Less than %s statute miles", core[1:])
default:
return fmt.Sprintf("%s statute miles", core)
}
}
func looksNumeric(s string) bool {
if s == "" {
return false
}
for _, r := range s {
if r < '0' || r > '9' {
return false
}
}
return true
}
func isSkyToken(t string) bool {
t = strings.ToUpper(t)
if t == "SKC" || t == "CLR" || t == "NSC" || t == "NCD" {
return true
}
// FEW050, SCT080, BKN250, OVC010, VV002
if len(t) == 6 {
p := t[:3]
if p == "FEW" || p == "SCT" || p == "BKN" || p == "OVC" || p == "VV" {
_, err := strconv.Atoi(t[3:])
return err == nil
}
}
return false
}
func decodeSkyToken(t string) string {
t = strings.ToUpper(t)
switch t {
case "SKC":
return "Sky clear"
case "CLR":
return "Clear below 12,000 ft"
case "NSC":
return "No significant clouds"
case "NCD":
return "No clouds detected"
}
if len(t) == 6 {
cover := t[:3]
hundreds, _ := strconv.Atoi(t[3:])
ft := hundreds * 100
return fmt.Sprintf("%s at %d ft AGL", decodeCloudCover(cover), ft)
}
return t
}
func isTempDewToken(t string) bool {
if !strings.Contains(t, "/") {
return false
}
parts := strings.SplitN(t, "/", 2)
if len(parts) != 2 {
return false
}
return looksSignedInt(parts[0]) && looksSignedInt(parts[1])
}
func looksSignedInt(s string) bool {
s = strings.TrimSpace(s)
if s == "" {
return false
}
s = strings.TrimPrefix(s, "M")
for _, r := range s {
if r < '0' || r > '9' {
return false
}
}
return true
}
func decodeTempDew(t string) (tempC string, dewC string) {
parts := strings.SplitN(t, "/", 2)
tempC = parseMInt(parts[0])
dewC = parseMInt(parts[1])
return tempC, dewC
}
func parseMInt(s string) string {
s = strings.TrimSpace(s)
neg := strings.HasPrefix(s, "M")
s = strings.TrimPrefix(s, "M")
if s == "" {
return "?"
}
if neg {
return "-" + s
}
return s
}
func isAltimeterToken(t string) bool {
// A2969
return len(t) == 5 && strings.HasPrefix(strings.ToUpper(t), "A") && looksNumeric(t[1:])
}
func decodeAltimeter(t string) string {
t = strings.ToUpper(t)
if !isAltimeterToken(t) {
return t
}
v := t[1:]
if len(v) != 4 {
return t
}
return fmt.Sprintf("%s.%s inHg", v[:2], v[2:])
}