Weather conditions at airports are reported to pilots in METAR (METeorological Aerodrome Report) format. This format could be described as being "golfed." Here are some examples:
METAR KPDX 241653Z 16003KT 1/4SM R10R/4500VP6000FT FG SCT000 01/M01 A3040 RMK AO2 SLP293 FG SCT000 T00061006 $
METAR PAMR 241753Z 00000KT 10SM OVC080 00/M02 A2985 RMK AO2 SLP111 T00001017 10022 21006 55003
METAR KLAX 241653Z VRB05KT 10SM FEW030 SCT060 17/11 A2992 RMK AO2 SLP130 T01720106
METAR KBOS 241754Z 27012G19KT 10SM FEW045 SCT250 09/M05 A3000 RMK AO2 SLP158 T00941050 10094 20044 56020
These are all American METARs, which is all your program needs to accept. The international format is slightly different.
You only need the wind component, which is after the third space.
METAR KLAX 241653Z | VRB05KT | 10SM FEW030 SCT060 17/11 A2992 RMK AO2 SLP130 T01720106
Things that don't matter | *Wind* | More things that don't matter
However, your program will be given the entire METAR.
Let's take the METAR from KPDX as the first example. Its wind component is 16003KT
. When the code looks like this, it's simply indicating the wind direction and speed. The first three digits, 160
, are the direction in degrees; the last two, 03
, are the speed in knots (KT
; a knot is a nautical mile per hour). In this case, the output should clearly indicate the direction and speed as separate values. You do not have to indicate units.
When there is no wind, it will look like the PAMR METAR: 00000KT
. This indicates zero knots; zero degrees is used as a placeholder, because there is, of course, no way to measure wind direction without wind. Just parse these the same as regular codes; your program does not have to treat this as a special case.
Sometimes, as was the case at KLAX at the time of writing, the wind direction is variable. This is encoded as VRB05KT
. The direction is replaced with VRB
, which your program must parse, and the speed is encoded normally.
Wind with gusts is encoded like this: 27012G19KT
. The 270
indicates the direction. The 12
indicates that the average wind speed is 12 knots, while G19
indicates that gusts are reaching 19 knots.
I did not find a current METAR that actually had this, but your program must also handle variable winds with gusts, which would look like this: VRB12G19KT
.
Your program must return or output three numbers. The first is the wind direction in degrees, or -1 if the wind is VRB
. The second is the wind speed. The third is the gust speed if there are gusts, or -1
otherwise. Leading zeros are not allowed.
Here are some test cases. They are the four real METARs from the start of the question, plus a constructed one to test for variable gusty winds.
METAR KPDX 241653Z 16003KT 1/4SM R10R/4500VP6000FT FG SCT000 01/M01 A3040 RMK AO2 SLP293 FG SCT000 T00061006 $
160 3 -1
METAR PAMR 241753Z 00000KT 10SM OVC080 00/M02 A2985 RMK AO2 SLP111 T00001017 10022 21006 55003
0 0 -1
METAR KLAX 241653Z VRB05KT 10SM FEW030 SCT060 17/11 A2992 RMK AO2 SLP130 T01720106
-1 5 -1
METAR KBOS 241754Z 27012G19KT 10SM FEW045 SCT250 09/M05 A3000 RMK AO2 SLP158 T00941050 10094 20044 56020
270 12 19
METAR KTVG 241754Z VRB12G19KT 10SM FEW045 SCT250 09/M05 A3000 RMK AO2 SLP158 T00941050 10094 20044 56020
-1 12 19