| 1 | #! /usr/bin/awk -f |
| 2 | # |
| 3 | # Qualifying the name as "_d_output" lest it be mistaken for parser of actual |
| 4 | # metar format. |
| 5 | |
| 6 | BEGIN { |
| 7 | OFS = msg_fs ? msg_fs : "|" |
| 8 | Kfs = key_fs ? key_fs : ":" |
| 9 | } |
| 10 | |
| 11 | /METAR pattern not found in NOAA data/ { |
| 12 | failures++ |
| 13 | exit |
| 14 | } |
| 15 | |
| 16 | /[A-z][a-z]+ *: / { |
| 17 | split($0, line, ":") |
| 18 | key = strip(line[1]) |
| 19 | val = strip(line[2]) |
| 20 | values[NR] = val |
| 21 | first[key] = NR |
| 22 | last[key] = NR |
| 23 | } |
| 24 | |
| 25 | /^ +/ { |
| 26 | values[NR] = strip($0) |
| 27 | last[key] = NR |
| 28 | } |
| 29 | |
| 30 | END { |
| 31 | if (failures) { |
| 32 | print "metar fetch failed" > "/dev/stderr" |
| 33 | } else { |
| 34 | temp_string = values[first["Temperature"]] |
| 35 | split(temp_string, temp_parts, " +") |
| 36 | temp_celsius = temp_parts[1] |
| 37 | temp_fahrenheit = (temp_celsius * (9 / 5)) + 32 |
| 38 | print("temperature_c", temp_celsius) # °C |
| 39 | print("temperature_f", temp_fahrenheit) # °F |
| 40 | for (i=first["Phenomena"]; i<=last["Phenomena"]; i++) { |
| 41 | phenomenon = values[i] |
| 42 | if (phenomenon) { |
| 43 | print("phenomenon" Kfs i, phenomenon) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function strip(s) { |
| 50 | sub("^ *", "", s) |
| 51 | sub(" *$", "", s) |
| 52 | return s |
| 53 | } |