]> gitweb.factorcode.org Git - factor.git/blob - extra/metar/metar.factor
metar: fix "1 1/2SM" visibility.
[factor.git] / extra / metar / metar.factor
1 ! Copyright (C) 2013 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: accessors arrays ascii assocs calendar calendar.format
5 combinators command-line continuations csv formatting fry
6 grouping http.client io io.encodings.ascii io.files io.styles
7 kernel math math.extras math.parser memoize namespaces regexp
8 sequences sorting.human splitting strings urls wrap.strings ;
9
10 IN: metar
11
12 TUPLE: station cccc name state country latitude longitude ;
13
14 C: <station> station
15
16 <PRIVATE
17
18 ERROR: bad-location str ;
19
20 : parse-location ( str -- n )
21     "-" split dup length {
22         { 3 [ first3 [ string>number ] tri@ 60.0 / + 60.0 / + ] }
23         { 2 [ first2 [ string>number ] bi@ 60.0 / + ] }
24         { 1 [ first string>number ] }
25         [ drop bad-location ]
26     } case ;
27
28 : string>longitude ( str -- lon/f )
29     dup R/ \d+-\d+(-\d+(\.\d+)?)?[WE]/ matches? [
30         unclip-last
31         [ parse-location ]
32         [ CHAR: W = [ neg ] when ] bi*
33     ] [ drop f ] if ;
34
35 : string>latitude ( str -- lat/f )
36     dup R/ \d+-\d+(-\d+(\.\d+)?)?[NS]/ matches? [
37         unclip-last
38         [ parse-location ]
39         [ CHAR: S = [ neg ] when ] bi*
40     ] [ drop f ] if ;
41
42 : stations-data ( -- seq )
43     URL" http://tgftp.nws.noaa.gov/data/nsd_cccc.txt"
44     http-get nip CHAR: ; [ string>csv ] with-delimiter ;
45
46 PRIVATE>
47
48 MEMO: all-stations ( -- seq )
49     stations-data [
50         {
51             [ 0 swap nth ]
52             [ 3 swap nth ]
53             [ 4 swap nth ]
54             [ 5 swap nth ]
55             [ 7 swap nth string>latitude ]
56             [ 8 swap nth string>longitude ]
57         } cleave <station>
58     ] map ;
59
60 : find-by-cccc ( cccc -- station )
61     all-stations swap '[ cccc>> _ = ] find nip ;
62
63 : find-by-country ( country -- stations )
64     all-stations swap '[ country>> _ = ] filter ;
65
66 : find-by-state ( state -- stations )
67     all-stations swap '[ state>> _ = ] filter ;
68
69 <PRIVATE
70
71 TUPLE: metar-report type station timestamp modifier wind
72 visibility rvr weather sky-condition temperature dew-point
73 altimeter remarks raw ;
74
75 CONSTANT: pressure-tendency H{
76     { "0" "increasing then decreasing" }
77     { "1" "increasing more slowly" }
78     { "2" "increasing" }
79     { "3" "increasing more quickly" }
80     { "4" "steady" }
81     { "5" "decreasing then increasing" }
82     { "6" "decreasing more slowly" }
83     { "7" "decreasing" }
84     { "8" "decreasing more quickly" }
85 }
86
87 CONSTANT: lightning H{
88     { "CA" "cloud-air lightning" }
89     { "CC" "cloud-cloud lightning" }
90     { "CG" "cloud-ground lightning" }
91     { "IC" "in-cloud lightning" }
92 }
93
94 CONSTANT: weather H{
95     { "BC" "patches" }
96     { "BL" "blowing" }
97     { "BR" "mist" }
98     { "DR" "low drifting" }
99     { "DS" "duststorm" }
100     { "DU" "widespread dust" }
101     { "DZ" "drizzle" }
102     { "FC" "funnel clouds" }
103     { "FG" "fog" }
104     { "FU" "smoke" }
105     { "FZ" "freezing" }
106     { "GR" "hail" }
107     { "GS" "small hail and/or snow pellets" }
108     { "HZ" "haze" }
109     { "IC" "ice crystals" }
110     { "MI" "shallow" }
111     { "PL" "ice pellets" }
112     { "PO" "well-developed dust/sand whirls" }
113     { "PR" "partial" }
114     { "PY" "spray" }
115     { "RA" "rain" }
116     { "RE" "recent" }
117     { "SA" "sand" }
118     { "SG" "snow grains" }
119     { "SH" "showers" }
120     { "SN" "snow" }
121     { "SQ" "squalls" }
122     { "SS" "sandstorm" }
123     { "TS" "thuderstorm" }
124     { "UP" "unknown" }
125     { "VA" "volcanic ash" }
126 }
127
128 MEMO: glossary ( -- assoc )
129     "vocab:metar/glossary.txt" ascii file-lines
130     [ "," split1 ] H{ } map>assoc ;
131
132 : parse-glossary ( str -- str' )
133     "/" split [
134         find-numbers [
135             dup number?
136             [ number>string ]
137             [ glossary ?at drop ] if
138         ] map " " join
139     ] map "/" join ;
140
141 : parse-timestamp ( str -- str' )
142     [ now [ year>> ] [ month>> ] bi ] dip
143     2 cut 2 cut 2 cut drop [ string>number ] tri@
144     over 24 = [
145         [ drop 0 ] dip 0 instant <timestamp> 1 days time+
146     ] [
147         0 instant <timestamp>
148     ] if timestamp>rfc822 ;
149
150 CONSTANT: compass-directions H{
151     { 0.0 "N" }
152     { 22.5 "NNE" }
153     { 45.0 "NE" }
154     { 67.5 "ENE" }
155     { 90.0 "E" }
156     { 112.5 "ESE" }
157     { 135.0 "SE" }
158     { 157.5 "SSE" }
159     { 180.0 "S" }
160     { 202.5 "SSW" }
161     { 225.0 "SW" }
162     { 247.5 "WSW" }
163     { 270.0 "W" }
164     { 292.5 "WNW" }
165     { 315.0 "NW" }
166     { 337.5 "NNW" }
167     { 360.0 "N" }
168 }
169
170 : direction>compass ( direction -- compass )
171     22.5 round-to-step compass-directions at ;
172
173 : parse-compass ( str -- str' )
174     string>number [ direction>compass ] keep "%s (%s°)" sprintf ;
175
176 : parse-direction ( str -- str' )
177     dup "VRB" = [ drop "variable" ] [
178         parse-compass "from %s" sprintf
179     ] if ;
180
181 : kt>mph ( kt -- mph ) 1.15077945 * ;
182
183 : mph>kt ( mph -- kt ) 1.15077945 / ;
184
185 : parse-speed ( str -- str'/f )
186     string>number [
187         dup kt>mph "%s knots (%.1f mph)" sprintf
188     ] [ f ] if* ;
189
190 : parse-wind ( str -- str' )
191     dup "00000KT" = [ drop "calm" ] [
192         3 cut "KT" ?tail drop "G" split1
193         [ parse-direction ] [ parse-speed ] [ parse-speed ] tri*
194         [ "%s at %s with gusts to %s " sprintf ]
195         [ "%s at %s" sprintf ] if*
196     ] if ;
197
198 : parse-wind-variable ( str -- str' )
199     "V" split1 [ parse-compass ] bi@
200     ", variable from %s to %s" sprintf ;
201
202 : parse-visibility ( str -- str' )
203     dup first {
204         { CHAR: M [ rest "less than " ] }
205         { CHAR: P [ rest "more than " ] }
206         [ drop "" ]
207     } case swap "SM" ?tail drop
208     CHAR: \s over index [ " " "+" replace ] when
209     string>number "%s%s statute miles" sprintf ;
210
211 : parse-rvr ( str -- str' )
212     "R" ?head drop "/" split1 "FT" ?tail drop
213     "V" split1 [
214         [ string>number ] bi@
215         "varying between %s and %s" sprintf
216     ] [
217         string>number "of %s" sprintf
218     ] if* "runway %s visibility %s ft" sprintf ;
219
220 : (parse-weather) ( str -- str' )
221     dup "+FC" = [ drop "tornadoes or waterspouts" ] [
222         dup first {
223             { CHAR: + [ rest "heavy " ] }
224             { CHAR: - [ rest "light " ] }
225             [ drop f ]
226         } case [
227             2 group dup [ weather key? ] all?
228             [ [ weather at ] map " " join ]
229             [ concat parse-glossary ] if
230         ] dip prepend
231     ] if ;
232
233 : parse-weather ( str -- str' )
234     "VC" over subseq? [ "VC" "" replace t ] [ f ] if
235     [ (parse-weather) ]
236     [ [ " in the vicinity" append ] when ] bi* ;
237
238 : parse-altitude ( str -- str' )
239     string>number " at %s00 ft" sprintf ;
240
241 CONSTANT: sky H{
242     { "BKN" "broken" }
243     { "FEW" "few" }
244     { "OVC" "overcast" }
245     { "SCT" "scattered" }
246     { "SKC" "clear sky" }
247     { "CLR" "clear sky" }
248     { "NSC" "clear sky" }
249
250     { "ACC" "altocumulus castellanus" }
251     { "ACSL" "standing lenticular altocumulus" }
252     { "CCSL" "cirrocumulus standing lenticular cloud" }
253     { "CU" "cumulus" }
254     { "SC" "stratocumulus" }
255     { "SCSL" "stratocumulus standing lenticular cloud" }
256     { "TCU" "towering cumulus" }
257 }
258
259 : parse-sky-condition ( str -- str' )
260     sky ?at [
261         3 cut 3 cut
262         [ sky at ]
263         [ parse-altitude ]
264         [ sky at [ " (%s)" sprintf ] [ f ] if* ]
265         tri* 3append
266     ] unless ;
267
268 : F>C ( F -- C ) 32 - 5/9 * ;
269
270 : C>F ( C -- F ) 9/5 * 32 + ;
271
272 : parse-temperature ( str -- temp dew-point )
273     "/" split1 [
274         [ f ] [
275             "M" ?head [ string>number ] [ [ neg ] when ] bi*
276             dup C>F "%d °C (%.1f °F)" sprintf
277         ] if-empty
278     ] bi@ ;
279
280 : parse-altimeter ( str -- str' )
281     unclip [ string>number ] [ CHAR: A = ] bi*
282     [ 100 /f "%.2f Hg" sprintf ] [ "%s hPa" sprintf ] if ;
283
284 CONSTANT: re-timestamp R/ \d{6}Z/
285 CONSTANT: re-station R/ \w{4}/
286 CONSTANT: re-temperature R/ [M]?\d{2}\/([M]?\d{2})?/
287 CONSTANT: re-wind R/ (VRB|\d{3})\d{2,3}(G\d{2,3})?KT/
288 CONSTANT: re-wind-variable R/ \d{3}V\d{3}/
289 CONSTANT: re-visibility R/ (\d+ )?[MP]?\d+(\/\d+)?SM/
290 CONSTANT: re-rvr R/ R\d{2}[RLC]?\/\d{4}(V\d{4})?FT/
291 CONSTANT: re-weather R/ [+-]?(VC)?(\w{2}|\w{4})/
292 CONSTANT: re-sky-condition R/ (\w{2,3}\d{3}(\w+)?|\w{3}|CAVOK)/
293 CONSTANT: re-altimeter R/ [AQ]\d{4}/
294
295 : find-one ( seq quot: ( elt -- ? ) -- seq elt/f )
296     dupd find drop [ tail unclip ] [ f ] if* ; inline
297
298 : find-all ( seq quot: ( elt -- ? ) -- seq elts )
299     [ find-one swap ] keep '[
300         dup [ f ] [ first @ ] if-empty
301     ] [ unclip ] produce rot [ prefix ] when* ; inline
302
303 : fix-visibility ( seq -- seq' )
304     dup [ re-visibility matches? ] find drop [
305         dup 1 - pick ?nth [ R/ \d+/ matches? ] [ f ] if* [
306             cut [ unclip-last ] [ unclip swap ] bi*
307             [ " " glue 1array ] [ 3append ] bi*
308         ] [ drop ] if
309     ] when* ;
310
311 : metar-body ( report seq -- report )
312     [ { "METAR" "SPECI" } member? ] find-one
313     [ pick type<< ] when*
314
315     [ re-station matches? ] find-one
316     [ pick station<< ] when*
317
318     [ re-timestamp matches? ] find-one
319     [ parse-timestamp pick timestamp<< ] when*
320
321     [ { "AUTO" "COR" } member? ] find-one
322     [ pick modifier<< ] when*
323
324     [ re-wind matches? ] find-one
325     [ parse-wind pick wind<< ] when*
326
327     [ re-wind-variable matches? ] find-one
328     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
329
330     fix-visibility
331     [ re-visibility matches? ] find-one
332     [ parse-visibility pick visibility<< ] when*
333
334     [ re-rvr matches? ] find-all " " join
335     [ parse-rvr ] map ", " join pick rvr<<
336
337     [ re-weather matches? ] find-all
338     [ parse-weather ] map ", " join pick weather<<
339
340     [ re-sky-condition matches? ] find-all
341     [ parse-sky-condition ] map ", " join pick sky-condition<<
342
343     [ re-temperature matches? ] find-one
344     [
345         parse-temperature
346         [ pick temperature<< ]
347         [ pick dew-point<< ] bi*
348     ] when*
349
350     [ re-altimeter matches? ] find-one
351     [ parse-altimeter pick altimeter<< ] when*
352
353     drop ;
354
355 : signed-number ( sign value -- n )
356     [ string>number ] bi@ swap zero? [ neg ] unless 10.0 / ;
357
358 : single-value ( str -- str' )
359     1 cut signed-number ;
360
361 : double-value ( str -- m n )
362     1 cut 3 cut [ signed-number ] dip 1 cut signed-number ;
363
364 : parse-1hr-temp ( str -- str' )
365     "T" ?head drop dup length 4 > [
366         double-value
367         [ dup C>F "%.1f °C (%.1f °F)" sprintf ] bi@
368         "hourly temperature %s and dew point %s" sprintf
369     ] [
370         single-value dup C>F
371         "hourly temperature %.1f °C (%.1f °F)" sprintf
372     ] if ;
373
374 : parse-6hr-max-temp ( str -- str' )
375     "1" ?head drop single-value dup C>F
376     "6-hour maximum temperature %.1f °C (%.1f °F)" sprintf ;
377
378 : parse-6hr-min-temp ( str -- str' )
379     "2" ?head drop single-value dup C>F
380     "6-hour minimum temperature %.1f °C (%.1f °F)" sprintf ;
381
382 : parse-24hr-temp ( str -- str' )
383     "4" ?head drop double-value
384     [ dup C>F "%.1f °C (%.1f °F)" sprintf ] bi@
385     "24-hour maximum temperature %s minimum temperature %s"
386     sprintf ;
387
388 : parse-1hr-pressure ( str -- str' )
389     "5" ?head drop 1 cut single-value [ pressure-tendency at ] dip
390     "hourly pressure %s %s hPa" sprintf ;
391
392 : parse-snow-depth ( str -- str' )
393     "4/" ?head drop string>number "snow depth %s inches" sprintf ;
394
395 CONSTANT: low-clouds H{
396     { 1 "cumulus (fair weather)" }
397     { 2 "cumulus (towering)" }
398     { 3 "cumulonimbus (no anvil)" }
399     { 4 "stratocumulus (from cumulus)" }
400     { 5 "stratocumuls (not cumulus)" }
401     { 6 "stratus or Fractostratus (fair)" }
402     { 7 "fractocumulus / fractostratus (bad weather)" }
403     { 8 "cumulus and stratocumulus" }
404     { 9 "cumulonimbus (thunderstorm)" }
405     { -1 "not valid" }
406 }
407
408 CONSTANT: mid-clouds H{
409     { 1 "altostratus (thin)" }
410     { 2 "altostratus (thick)" }
411     { 3 "altocumulus (thin)" }
412     { 4 "altocumulus (patchy)" }
413     { 5 "altocumulus (thickening)" }
414     { 6 "altocumulus (from cumulus)" }
415     { 7 "altocumulus (with altocumulus, altostratus, nimbostratus)" }
416     { 8 "altocumulus (with turrets)" }
417     { 9 "altocumulus (chaotic)" }
418     { -1 "above overcast" }
419 }
420
421 CONSTANT: high-clouds H{
422     { 1 "cirrus (filaments)" }
423     { 2 "cirrus (dense)" }
424     { 3 "cirrus (often with cumulonimbus)" }
425     { 4 "cirrus (thickening)" }
426     { 5 "cirrus / cirrostratus (low in sky)" }
427     { 6 "cirrus / cirrostratus (hi in sky)" }
428     { 7 "cirrostratus (entire sky)" }
429     { 8 "cirrostratus (partial)" }
430     { 9 "cirrocumulus or cirrocumulus / cirrus / cirrostratus" }
431     { -1 "above overcast" }
432 }
433
434 : parse-cloud-cover ( str -- str' )
435     "8/" ?head drop first3 [ CHAR: 0 - ] tri@
436     [ [ f ] [ low-clouds at "low clouds are %s" sprintf ] if-zero ]
437     [ [ f ] [ mid-clouds at "middle clouds are %s" sprintf ] if-zero ]
438     [ [ f ] [ high-clouds at "high clouds are %s" sprintf ] if-zero ]
439     tri* 3array " " join ;
440
441 : parse-inches ( str -- str' )
442     dup [ CHAR: / = ] all? [ drop "unknown" ] [
443         string>number
444         [ "trace" ] [ 100 /f "%.2f inches" sprintf ] if-zero
445     ] if ;
446
447 : parse-1hr-precipitation ( str -- str' )
448     "P" ?head drop parse-inches
449     "%s precipitation in last hour" sprintf ;
450
451 : parse-6hr-precipitation ( str -- str' )
452     "6" ?head drop parse-inches
453     "%s precipitation in last 6 hours" sprintf ;
454
455 : parse-24hr-precipitation ( str -- str' )
456     "7" ?head drop parse-inches
457     "%s precipitation in last 24 hours" sprintf ;
458
459 ! XXX: "on the hour" instead of "00 minutes past the hour" ?
460
461 : parse-recent-time ( str -- str' )
462     dup length 2 >
463     [ 2 cut ":" glue ]
464     [ " minutes past the hour" append ] if ;
465
466 : parse-peak-wind ( str -- str' )
467     "/" split1 [ parse-wind ] [ parse-recent-time ] bi*
468     "%s occuring at %s" sprintf ;
469
470 : parse-sea-level-pressure ( str -- str' )
471     "SLP" ?head drop string>number 10.0 /f 1000 +
472     "sea-level pressure is %s hPa" sprintf ;
473
474 : parse-lightning ( str -- str' )
475     "LTG" ?head drop 2 group [ lightning at ] map " " join ;
476
477 CONSTANT: re-recent-weather R/ ((\w{2})?[BE]\d{2,4}((\w{2})?[BE]\d{2,4})?)+/
478
479 : parse-began/ended ( str -- str' )
480     unclip swap
481     [ CHAR: B = "began" "ended" ? ]
482     [ parse-recent-time ] bi* "%s at %s" sprintf ;
483
484 : split-recent-weather ( str -- seq )
485     [ dup empty? not ] [
486         dup [ digit? ] find drop
487         over [ digit? not ] find-from drop
488         [ cut ] [ f ] if* swap
489     ] produce nip ;
490
491 : (parse-recent-weather) ( str -- str' )
492     dup [ digit? ] find drop 2 > [
493         2 cut [ weather at " " append ] dip
494     ] [ f swap ] if parse-began/ended "" append-as ;
495
496 : parse-recent-weather ( str -- str' )
497     split-recent-weather
498     [ (parse-recent-weather) ] map " " join ;
499
500 : parse-varying ( str -- str' )
501     "V" split1 [ string>number ] bi@
502     "varying between %s00 and %s00 ft" sprintf ;
503
504 : parse-from-to ( str -- str' )
505     "-" split [ parse-glossary ] map " to " join ;
506
507 : parse-water-equivalent-snow ( str -- str' )
508     "933" ?head drop parse-inches
509     "%s water equivalent of snow on ground" sprintf ;
510
511 : parse-duration-of-sunshine ( str -- str' )
512     "98" ?head drop string>number
513     [ "no" ] [ "%s minutes of" sprintf ] if-zero
514     "%s sunshine" sprintf ;
515
516 : parse-6hr-snowfall ( str -- str' )
517     "931" ?head drop parse-inches
518     "%s snowfall in last 6 hours" sprintf ;
519
520 : parse-probability ( str -- str' )
521     "PROB" ?head drop string>number
522     "probability of %d%%" sprintf ;
523
524 : parse-remark ( str -- str' )
525     {
526         { [ dup glossary key? ] [ glossary at ] }
527         { [ dup R/ 1\d{4}/ matches? ] [ parse-6hr-max-temp ] }
528         { [ dup R/ 2\d{4}/ matches? ] [ parse-6hr-min-temp ] }
529         { [ dup R/ 4\d{8}/ matches? ] [ parse-24hr-temp ] }
530         { [ dup R/ 4\/\d{3}/ matches? ] [ parse-snow-depth ] }
531         { [ dup R/ 5\d{4}/ matches? ] [ parse-1hr-pressure ] }
532         { [ dup R/ 6[\d\/]{4}/ matches? ] [ parse-6hr-precipitation ] }
533         { [ dup R/ 7\d{4}/ matches? ] [ parse-24hr-precipitation ] }
534         { [ dup R/ 8\/\d{3}/ matches? ] [ parse-cloud-cover ] }
535         { [ dup R/ 931\d{3}/ matches? ] [ parse-6hr-snowfall ] }
536         { [ dup R/ 933\d{3}/ matches? ] [ parse-water-equivalent-snow ] }
537         { [ dup R/ 98\d{3}/ matches? ] [ parse-duration-of-sunshine ] }
538         { [ dup R/ T\d{4,8}/ matches? ] [ parse-1hr-temp ] }
539         { [ dup R/ \d{3}\d{2,3}\/\d{2,4}/ matches? ] [ parse-peak-wind ] }
540         { [ dup R/ P\d{4}/ matches? ] [ parse-1hr-precipitation ] }
541         { [ dup R/ SLP\d{3}/ matches? ] [ parse-sea-level-pressure ] }
542         { [ dup R/ LTG\w+/ matches? ] [ parse-lightning ] }
543         { [ dup R/ PROB\d+/ matches? ] [ parse-probability ] }
544         { [ dup R/ \d{3}V\d{3}/ matches? ] [ parse-varying ] }
545         { [ dup R/ [^-]+(-[^-]+)+/ matches? ] [ parse-from-to ] }
546         { [ dup R/ [^\/]+(\/[^\/]+)+/ matches? ] [ ] }
547         { [ dup R/ \d+.\d+/ matches? ] [ ] }
548         { [ dup re-recent-weather matches? ] [ parse-recent-weather ] }
549         { [ dup re-weather matches? ] [ parse-weather ] }
550         { [ dup re-sky-condition matches? ] [ parse-sky-condition ] }
551         [ parse-glossary ]
552     } cond ;
553
554 : metar-remarks ( report seq -- report )
555     [ parse-remark ] map " " join >>remarks ;
556
557 : <metar-report> ( metar -- report )
558     [ metar-report new ] dip [ >>raw ] keep
559     [ blank? ] split-when { "RMK" } split1
560     [ metar-body ] [ metar-remarks ] bi* ;
561
562 : row. ( name quot -- )
563     '[
564         [ _ write ] with-cell
565         [ @ [ 65 wrap-string write ] when* ] with-cell
566     ] with-row ; inline
567
568 : metar-report. ( report -- )
569     standard-table-style [
570         {
571             [ "Station" [ station>> ] row. ]
572             [ "Timestamp" [ timestamp>> ] row. ]
573             [ "Wind" [ wind>> ] row. ]
574             [ "Visibility" [ visibility>> ] row. ]
575             [ "RVR" [ rvr>> ] row. ]
576             [ "Weather" [ weather>> ] row. ]
577             [ "Sky condition" [ sky-condition>> ] row. ]
578             [ "Temperature" [ temperature>> ] row. ]
579             [ "Dew point" [ dew-point>> ] row. ]
580             [ "Altimeter" [ altimeter>> ] row. ]
581             [ "Remarks" [ remarks>> ] row. ]
582             [ "Raw Text" [ raw>> ] row. ]
583         } cleave
584     ] tabular-output nl ;
585
586 PRIVATE>
587
588 GENERIC: metar ( station -- metar )
589
590 M: station metar cccc>> metar ;
591
592 M: string metar
593     "http://tgftp.nws.noaa.gov/data/observations/metar/stations/%s.TXT"
594     sprintf http-get nip ;
595
596 GENERIC: metar. ( station -- )
597
598 M: station metar. cccc>> metar. ;
599
600 M: string metar.
601     [ metar <metar-report> metar-report. ]
602     [ drop "%s METAR not found\n" printf ] recover ;
603
604 <PRIVATE
605
606 : parse-wind-shear ( str -- str' )
607     "WS" ?head drop "/" split1
608     [ parse-altitude ] [ parse-wind ] bi* prepend
609     "wind shear " prepend ;
610
611 CONSTANT: re-from-timestamp R/ FM\d{6}/
612
613 : parse-from-timestamp ( str -- str' )
614     "FM" ?head drop parse-timestamp ;
615
616 CONSTANT: re-valid-timestamp R/ \d{4}\/\d{4}/
617
618 : parse-valid-timestamp ( str -- str' )
619     "/" split1 [ "00" append parse-timestamp ] bi@ " to " glue ;
620
621 TUPLE: taf-report station timestamp valid-timestamp wind
622 visibility rvr weather sky-condition partials raw ;
623
624 TUPLE: taf-partial from-timestamp wind visibility rvr weather
625 sky-condition raw ;
626
627 : taf-body ( report str -- report )
628     [ blank? ] split-when
629
630     [ "TAF" = ] find-one drop
631
632     [ { "AMD" "COR" "RTD" } member? ] find-one drop
633
634     [ re-station matches? ] find-one
635     [ pick station<< ] when*
636
637     [ re-timestamp matches? ] find-one
638     [ parse-timestamp pick timestamp<< ] when*
639
640     [ re-valid-timestamp matches? ] find-one
641     [ parse-valid-timestamp pick valid-timestamp<< ] when*
642
643     [ re-wind matches? ] find-one
644     [ parse-wind pick wind<< ] when*
645
646     [ re-wind-variable matches? ] find-one
647     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
648
649     [ re-visibility matches? ] find-one
650     [ parse-visibility pick visibility<< ] when*
651
652     [ re-rvr matches? ] find-all " " join
653     [ parse-rvr ] map ", " join pick rvr<<
654
655     [ re-weather matches? ] find-all
656     [ parse-weather ] map ", " join pick weather<<
657
658     [ re-sky-condition matches? ] find-all
659     [ parse-sky-condition ] map ", " join pick sky-condition<<
660
661     drop ;
662
663 : <taf-partial> ( str -- partial )
664     [ taf-partial new ] dip [ blank? ] split-when
665
666     [ re-from-timestamp matches? ] find-one
667     [ parse-from-timestamp pick from-timestamp<< ] when*
668
669     [ re-wind matches? ] find-one
670     [ parse-wind pick wind<< ] when*
671
672     [ re-wind-variable matches? ] find-one
673     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
674
675     [ re-visibility matches? ] find-one
676     [ parse-visibility pick visibility<< ] when*
677
678     [ re-rvr matches? ] find-all " " join
679     [ parse-rvr ] map ", " join pick rvr<<
680
681     [ re-weather matches? ] find-all
682     [ parse-weather ] map ", " join pick weather<<
683
684     [ re-sky-condition matches? ] find-all
685     [ parse-sky-condition ] map ", " join pick sky-condition<<
686
687     drop ;
688
689 : taf-partials ( report seq -- report )
690     [ <taf-partial> ] map >>partials ;
691
692 : <taf-report> ( taf -- report )
693     [ taf-report new ] dip [ >>raw ] keep
694     string-lines [ [ blank? ] trim ] map
695     rest dup first "TAF" = [ rest ] when
696     harvest unclip swapd taf-body swap taf-partials ;
697
698 : taf-report. ( report -- )
699     [
700         standard-table-style [
701             {
702                 [ "Station" [ station>> ] row. ]
703                 [ "Timestamp" [ timestamp>> ] row. ]
704                 [ "Valid From" [ valid-timestamp>> ] row. ]
705                 [ "Wind" [ wind>> ] row. ]
706                 [ "Visibility" [ visibility>> ] row. ]
707                 [ "RVR" [ rvr>> ] row. ]
708                 [ "Weather" [ weather>> ] row. ]
709                 [ "Sky condition" [ sky-condition>> ] row. ]
710                 [ "Raw Text" [ raw>> ] row. ]
711             } cleave
712         ] tabular-output nl
713     ] [
714         partials>> [
715             standard-table-style [
716                 {
717                     [ "From" [ from-timestamp>> ] row. ]
718                     [ "Wind" [ wind>> ] row. ]
719                     [ "Visibility" [ visibility>> ] row. ]
720                     [ "RVR" [ rvr>> ] row. ]
721                     [ "Weather" [ weather>> ] row. ]
722                     [ "Sky condition" [ sky-condition>> ] row. ]
723                 } cleave
724             ] tabular-output nl
725         ] each
726     ] bi ;
727
728 PRIVATE>
729
730 GENERIC: taf ( station -- taf )
731
732 M: station taf cccc>> taf ;
733
734 M: string taf
735     "http://tgftp.nws.noaa.gov/data/forecasts/taf/stations/%s.TXT"
736     sprintf http-get nip ;
737
738 GENERIC: taf. ( station -- )
739
740 M: station taf. cccc>> taf. ;
741
742 M: string taf.
743     [ taf <taf-report> taf-report. ]
744     [ drop "%s TAF not found\n" printf ] recover ;
745
746 : metar-main ( -- )
747     command-line get [
748         [ metar print ] [ taf print ] bi nl
749     ] each ;
750
751 MAIN: metar-main