]> gitweb.factorcode.org Git - factor.git/blob - extra/metar/metar.factor
metar: add a main.
[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 ! FIXME: "1 1/2SM" visibility doesn't work
203 : parse-visibility ( str -- str' )
204     dup first {
205         { CHAR: M [ rest "less than " ] }
206         { CHAR: P [ rest "more than " ] }
207         [ drop "" ]
208     } case swap "SM" ?tail drop
209     CHAR: / over index [ 1 > [ 1 cut "+" glue ] when ] when*
210     string>number "%s%s statute miles" sprintf ;
211
212 : parse-rvr ( str -- str' )
213     "R" ?head drop "/" split1 "FT" ?tail drop
214     "V" split1 [
215         [ string>number ] bi@
216         "varying between %s and %s" sprintf
217     ] [
218         string>number "of %s" sprintf
219     ] if* "runway %s visibility %s ft" sprintf ;
220
221 : (parse-weather) ( str -- str' )
222     dup "+FC" = [ drop "tornadoes or waterspouts" ] [
223         dup first {
224             { CHAR: + [ rest "heavy " ] }
225             { CHAR: - [ rest "light " ] }
226             [ drop f ]
227         } case [
228             2 group dup [ weather key? ] all?
229             [ [ weather at ] map " " join ]
230             [ concat parse-glossary ] if
231         ] dip prepend
232     ] if ;
233
234 : parse-weather ( str -- str' )
235     "VC" over subseq? [ "VC" "" replace t ] [ f ] if
236     [ (parse-weather) ]
237     [ [ " in the vicinity" append ] when ] bi* ;
238
239 : parse-altitude ( str -- str' )
240     string>number " at %s00 ft" sprintf ;
241
242 CONSTANT: sky H{
243     { "BKN" "broken" }
244     { "FEW" "few" }
245     { "OVC" "overcast" }
246     { "SCT" "scattered" }
247     { "SKC" "clear sky" }
248     { "CLR" "clear sky" }
249     { "NSC" "clear sky" }
250
251     { "ACC" "altocumulus castellanus" }
252     { "ACSL" "standing lenticular altocumulus" }
253     { "CCSL" "cirrocumulus standing lenticular cloud" }
254     { "CU" "cumulus" }
255     { "SC" "stratocumulus" }
256     { "SCSL" "stratocumulus standing lenticular cloud" }
257     { "TCU" "towering cumulus" }
258 }
259
260 : parse-sky-condition ( str -- str' )
261     sky ?at [
262         3 cut 3 cut
263         [ sky at ]
264         [ parse-altitude ]
265         [ sky at [ " (%s)" sprintf ] [ f ] if* ]
266         tri* 3append
267     ] unless ;
268
269 : F>C ( F -- C ) 32 - 5/9 * ;
270
271 : C>F ( C -- F ) 9/5 * 32 + ;
272
273 : parse-temperature ( str -- temp dew-point )
274     "/" split1 [
275         [ f ] [
276             "M" ?head [ string>number ] [ [ neg ] when ] bi*
277             dup C>F "%d °C (%.1f °F)" sprintf
278         ] if-empty
279     ] bi@ ;
280
281 : parse-altimeter ( str -- str' )
282     unclip [ string>number ] [ CHAR: A = ] bi*
283     [ 100 /f "%.2f Hg" sprintf ] [ "%s hPa" sprintf ] if ;
284
285 CONSTANT: re-timestamp R/ \d{6}Z/
286 CONSTANT: re-station R/ \w{4}/
287 CONSTANT: re-temperature R/ [M]?\d{2}\/([M]?\d{2})?/
288 CONSTANT: re-wind R/ (VRB|\d{3})\d{2,3}(G\d{2,3})?KT/
289 CONSTANT: re-wind-variable R/ \d{3}V\d{3}/
290 CONSTANT: re-visibility R/ [MP]?\d+(\/\d+)?SM/
291 CONSTANT: re-rvr R/ R\d{2}[RLC]?\/\d{4}(V\d{4})?FT/
292 CONSTANT: re-weather R/ [+-]?(VC)?(\w{2}|\w{4})/
293 CONSTANT: re-sky-condition R/ (\w{2,3}\d{3}(\w+)?|\w{3}|CAVOK)/
294 CONSTANT: re-altimeter R/ [AQ]\d{4}/
295
296 : find-one ( seq quot: ( elt -- ? ) -- seq elt/f )
297     dupd find drop [ tail unclip ] [ f ] if* ; inline
298
299 : find-all ( seq quot: ( elt -- ? ) -- seq elts )
300     [ find-one swap ] keep '[
301         dup [ f ] [ first @ ] if-empty
302     ] [ unclip ] produce rot [ prefix ] when* ; inline
303
304 : metar-body ( report seq -- report )
305
306     [ { "METAR" "SPECI" } member? ] find-one
307     [ pick type<< ] when*
308
309     [ re-station matches? ] find-one
310     [ pick station<< ] when*
311
312     [ re-timestamp matches? ] find-one
313     [ parse-timestamp pick timestamp<< ] when*
314
315     [ { "AUTO" "COR" } member? ] find-one
316     [ pick modifier<< ] when*
317
318     [ re-wind matches? ] find-one
319     [ parse-wind pick wind<< ] when*
320
321     [ re-wind-variable matches? ] find-one
322     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
323
324     [ re-visibility matches? ] find-one
325     [ parse-visibility pick visibility<< ] when*
326
327     [ re-rvr matches? ] find-all " " join
328     [ parse-rvr ] map ", " join pick rvr<<
329
330     [ re-weather matches? ] find-all
331     [ parse-weather ] map ", " join pick weather<<
332
333     [ re-sky-condition matches? ] find-all
334     [ parse-sky-condition ] map ", " join pick sky-condition<<
335
336     [ re-temperature matches? ] find-one
337     [
338         parse-temperature
339         [ pick temperature<< ]
340         [ pick dew-point<< ] bi*
341     ] when*
342
343     [ re-altimeter matches? ] find-one
344     [ parse-altimeter pick altimeter<< ] when*
345
346     drop ;
347
348 : signed-number ( sign value -- n )
349     [ string>number ] bi@ swap zero? [ neg ] unless 10.0 / ;
350
351 : single-value ( str -- str' )
352     1 cut signed-number ;
353
354 : double-value ( str -- m n )
355     1 cut 3 cut [ signed-number ] dip 1 cut signed-number ;
356
357 : parse-1hr-temp ( str -- str' )
358     "T" ?head drop dup length 4 > [
359         double-value
360         [ dup C>F "%.1f °C (%.1f °F)" sprintf ] bi@
361         "hourly temperature %s and dew point %s" sprintf
362     ] [
363         single-value dup C>F
364         "hourly temperature %.1f °C (%.1f °F)" sprintf
365     ] if ;
366
367 : parse-6hr-max-temp ( str -- str' )
368     "1" ?head drop single-value dup C>F
369     "6-hour maximum temperature %.1f °C (%.1f °F)" sprintf ;
370
371 : parse-6hr-min-temp ( str -- str' )
372     "2" ?head drop single-value dup C>F
373     "6-hour minimum temperature %.1f °C (%.1f °F)" sprintf ;
374
375 : parse-24hr-temp ( str -- str' )
376     "4" ?head drop double-value
377     [ dup C>F "%.1f °C (%.1f °F)" sprintf ] bi@
378     "24-hour maximum temperature %s minimum temperature %s"
379     sprintf ;
380
381 : parse-1hr-pressure ( str -- str' )
382     "5" ?head drop 1 cut single-value [ pressure-tendency at ] dip
383     "hourly pressure %s %s hPa" sprintf ;
384
385 : parse-snow-depth ( str -- str' )
386     "4/" ?head drop string>number "snow depth %s inches" sprintf ;
387
388 CONSTANT: low-clouds H{
389     { 1 "cumulus (fair weather)" }
390     { 2 "cumulus (towering)" }
391     { 3 "cumulonimbus (no anvil)" }
392     { 4 "stratocumulus (from cumulus)" }
393     { 5 "stratocumuls (not cumulus)" }
394     { 6 "stratus or Fractostratus (fair)" }
395     { 7 "fractocumulus / fractostratus (bad weather)" }
396     { 8 "cumulus and stratocumulus" }
397     { 9 "cumulonimbus (thunderstorm)" }
398     { -1 "not valid" }
399 }
400
401 CONSTANT: mid-clouds H{
402     { 1 "altostratus (thin)" }
403     { 2 "altostratus (thick)" }
404     { 3 "altocumulus (thin)" }
405     { 4 "altocumulus (patchy)" }
406     { 5 "altocumulus (thickening)" }
407     { 6 "altocumulus (from cumulus)" }
408     { 7 "altocumulus (with altocumulus, altostratus, nimbostratus)" }
409     { 8 "altocumulus (with turrets)" }
410     { 9 "altocumulus (chaotic)" }
411     { -1 "above overcast" }
412 }
413
414 CONSTANT: high-clouds H{
415     { 1 "cirrus (filaments)" }
416     { 2 "cirrus (dense)" }
417     { 3 "cirrus (often with cumulonimbus)" }
418     { 4 "cirrus (thickening)" }
419     { 5 "cirrus / cirrostratus (low in sky)" }
420     { 6 "cirrus / cirrostratus (hi in sky)" }
421     { 7 "cirrostratus (entire sky)" }
422     { 8 "cirrostratus (partial)" }
423     { 9 "cirrocumulus or cirrocumulus / cirrus / cirrostratus" }
424     { -1 "above overcast" }
425 }
426
427 : parse-cloud-cover ( str -- str' )
428     "8/" ?head drop first3 [ CHAR: 0 - ] tri@
429     [ [ f ] [ low-clouds at "low clouds are %s" sprintf ] if-zero ]
430     [ [ f ] [ mid-clouds at "middle clouds are %s" sprintf ] if-zero ]
431     [ [ f ] [ high-clouds at "high clouds are %s" sprintf ] if-zero ]
432     tri* 3array " " join ;
433
434 : parse-inches ( str -- str' )
435     dup [ CHAR: / = ] all? [ drop "unknown" ] [
436         string>number
437         [ "trace" ] [ 100 /f "%.2f inches" sprintf ] if-zero
438     ] if ;
439
440 : parse-1hr-precipitation ( str -- str' )
441     "P" ?head drop parse-inches
442     "%s precipitation in last hour" sprintf ;
443
444 : parse-6hr-precipitation ( str -- str' )
445     "6" ?head drop parse-inches
446     "%s precipitation in last 6 hours" sprintf ;
447
448 : parse-24hr-precipitation ( str -- str' )
449     "7" ?head drop parse-inches
450     "%s precipitation in last 24 hours" sprintf ;
451
452 ! XXX: "on the hour" instead of "00 minutes past the hour" ?
453
454 : parse-recent-time ( str -- str' )
455     dup length 2 >
456     [ 2 cut ":" glue ]
457     [ " minutes past the hour" append ] if ;
458
459 : parse-peak-wind ( str -- str' )
460     "/" split1 [ parse-wind ] [ parse-recent-time ] bi*
461     "%s occuring at %s" sprintf ;
462
463 : parse-sea-level-pressure ( str -- str' )
464     "SLP" ?head drop string>number 10.0 /f 1000 +
465     "sea-level pressure is %s hPa" sprintf ;
466
467 : parse-lightning ( str -- str' )
468     "LTG" ?head drop 2 group [ lightning at ] map " " join ;
469
470 CONSTANT: re-recent-weather R/ ((\w{2})?[BE]\d{2,4}((\w{2})?[BE]\d{2,4})?)+/
471
472 : parse-began/ended ( str -- str' )
473     unclip swap
474     [ CHAR: B = "began" "ended" ? ]
475     [ parse-recent-time ] bi* "%s at %s" sprintf ;
476
477 : split-recent-weather ( str -- seq )
478     [ dup empty? not ] [
479         dup [ digit? ] find drop
480         over [ digit? not ] find-from drop
481         [ cut ] [ f ] if* swap
482     ] produce nip ;
483
484 : (parse-recent-weather) ( str -- str' )
485     dup [ digit? ] find drop 2 > [
486         2 cut [ weather at " " append ] dip
487     ] [ f swap ] if parse-began/ended "" append-as ;
488
489 : parse-recent-weather ( str -- str' )
490     split-recent-weather
491     [ (parse-recent-weather) ] map " " join ;
492
493 : parse-varying ( str -- str' )
494     "V" split1 [ string>number ] bi@
495     "varying between %s00 and %s00 ft" sprintf ;
496
497 : parse-from-to ( str -- str' )
498     "-" split [ parse-glossary ] map " to " join ;
499
500 : parse-water-equivalent-snow ( str -- str' )
501     "933" ?head drop parse-inches
502     "%s water equivalent of snow on ground" sprintf ;
503
504 : parse-duration-of-sunshine ( str -- str' )
505     "98" ?head drop string>number
506     [ "no" ] [ "%s minutes of" sprintf ] if-zero
507     "%s sunshine" sprintf ;
508
509 : parse-6hr-snowfall ( str -- str' )
510     "931" ?head drop parse-inches
511     "%s snowfall in last 6 hours" sprintf ;
512
513 : parse-probability ( str -- str' )
514     "PROB" ?head drop string>number
515     "probability of %d%%" sprintf ;
516
517 : parse-remark ( str -- str' )
518     {
519         { [ dup glossary key? ] [ glossary at ] }
520         { [ dup R/ 1\d{4}/ matches? ] [ parse-6hr-max-temp ] }
521         { [ dup R/ 2\d{4}/ matches? ] [ parse-6hr-min-temp ] }
522         { [ dup R/ 4\d{8}/ matches? ] [ parse-24hr-temp ] }
523         { [ dup R/ 4\/\d{3}/ matches? ] [ parse-snow-depth ] }
524         { [ dup R/ 5\d{4}/ matches? ] [ parse-1hr-pressure ] }
525         { [ dup R/ 6[\d\/]{4}/ matches? ] [ parse-6hr-precipitation ] }
526         { [ dup R/ 7\d{4}/ matches? ] [ parse-24hr-precipitation ] }
527         { [ dup R/ 8\/\d{3}/ matches? ] [ parse-cloud-cover ] }
528         { [ dup R/ 931\d{3}/ matches? ] [ parse-6hr-snowfall ] }
529         { [ dup R/ 933\d{3}/ matches? ] [ parse-water-equivalent-snow ] }
530         { [ dup R/ 98\d{3}/ matches? ] [ parse-duration-of-sunshine ] }
531         { [ dup R/ T\d{4,8}/ matches? ] [ parse-1hr-temp ] }
532         { [ dup R/ \d{3}\d{2,3}\/\d{2,4}/ matches? ] [ parse-peak-wind ] }
533         { [ dup R/ P\d{4}/ matches? ] [ parse-1hr-precipitation ] }
534         { [ dup R/ SLP\d{3}/ matches? ] [ parse-sea-level-pressure ] }
535         { [ dup R/ LTG\w+/ matches? ] [ parse-lightning ] }
536         { [ dup R/ PROB\d+/ matches? ] [ parse-probability ] }
537         { [ dup R/ \d{3}V\d{3}/ matches? ] [ parse-varying ] }
538         { [ dup R/ [^-]+(-[^-]+)+/ matches? ] [ parse-from-to ] }
539         { [ dup R/ [^\/]+(\/[^\/]+)+/ matches? ] [ ] }
540         { [ dup R/ \d+.\d+/ matches? ] [ ] }
541         { [ dup re-recent-weather matches? ] [ parse-recent-weather ] }
542         { [ dup re-weather matches? ] [ parse-weather ] }
543         { [ dup re-sky-condition matches? ] [ parse-sky-condition ] }
544         [ parse-glossary ]
545     } cond ;
546
547 : metar-remarks ( report seq -- report )
548     [ parse-remark ] map " " join >>remarks ;
549
550 : <metar-report> ( metar -- report )
551     [ metar-report new ] dip [ >>raw ] keep
552     [ blank? ] split-when { "RMK" } split1
553     [ metar-body ] [ metar-remarks ] bi* ;
554
555 : row. ( name quot -- )
556     '[
557         [ _ write ] with-cell
558         [ @ [ 65 wrap-string write ] when* ] with-cell
559     ] with-row ; inline
560
561 : metar-report. ( report -- )
562     standard-table-style [
563         {
564             [ "Station" [ station>> ] row. ]
565             [ "Timestamp" [ timestamp>> ] row. ]
566             [ "Wind" [ wind>> ] row. ]
567             [ "Visibility" [ visibility>> ] row. ]
568             [ "RVR" [ rvr>> ] row. ]
569             [ "Weather" [ weather>> ] row. ]
570             [ "Sky condition" [ sky-condition>> ] row. ]
571             [ "Temperature" [ temperature>> ] row. ]
572             [ "Dew point" [ dew-point>> ] row. ]
573             [ "Altimeter" [ altimeter>> ] row. ]
574             [ "Remarks" [ remarks>> ] row. ]
575             [ "Raw Text" [ raw>> ] row. ]
576         } cleave
577     ] tabular-output nl ;
578
579 PRIVATE>
580
581 GENERIC: metar ( station -- metar )
582
583 M: station metar cccc>> metar ;
584
585 M: string metar
586     "http://tgftp.nws.noaa.gov/data/observations/metar/stations/%s.TXT"
587     sprintf http-get nip ;
588
589 GENERIC: metar. ( station -- )
590
591 M: station metar. cccc>> metar. ;
592
593 M: string metar.
594     [ metar <metar-report> metar-report. ]
595     [ drop "%s METAR not found\n" printf ] recover ;
596
597 <PRIVATE
598
599 : parse-wind-shear ( str -- str' )
600     "WS" ?head drop "/" split1
601     [ parse-altitude ] [ parse-wind ] bi* prepend
602     "wind shear " prepend ;
603
604 CONSTANT: re-from-timestamp R/ FM\d{6}/
605
606 : parse-from-timestamp ( str -- str' )
607     "FM" ?head drop parse-timestamp ;
608
609 CONSTANT: re-valid-timestamp R/ \d{4}\/\d{4}/
610
611 : parse-valid-timestamp ( str -- str' )
612     "/" split1 [ "00" append parse-timestamp ] bi@ " to " glue ;
613
614 TUPLE: taf-report station timestamp valid-timestamp wind
615 visibility rvr weather sky-condition partials raw ;
616
617 TUPLE: taf-partial from-timestamp wind visibility rvr weather
618 sky-condition raw ;
619
620 : taf-body ( report str -- report )
621     [ blank? ] split-when
622
623     [ "TAF" = ] find-one drop
624
625     [ { "AMD" "COR" "RTD" } member? ] find-one drop
626
627     [ re-station matches? ] find-one
628     [ pick station<< ] when*
629
630     [ re-timestamp matches? ] find-one
631     [ parse-timestamp pick timestamp<< ] when*
632
633     [ re-valid-timestamp matches? ] find-one
634     [ parse-valid-timestamp pick valid-timestamp<< ] when*
635
636     [ re-wind matches? ] find-one
637     [ parse-wind pick wind<< ] when*
638
639     [ re-wind-variable matches? ] find-one
640     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
641
642     [ re-visibility matches? ] find-one
643     [ parse-visibility pick visibility<< ] when*
644
645     [ re-rvr matches? ] find-all " " join
646     [ parse-rvr ] map ", " join pick rvr<<
647
648     [ re-weather matches? ] find-all
649     [ parse-weather ] map ", " join pick weather<<
650
651     [ re-sky-condition matches? ] find-all
652     [ parse-sky-condition ] map ", " join pick sky-condition<<
653
654     drop ;
655
656 : <taf-partial> ( str -- partial )
657     [ taf-partial new ] dip [ blank? ] split-when
658
659     [ re-from-timestamp matches? ] find-one
660     [ parse-from-timestamp pick from-timestamp<< ] when*
661
662     [ re-wind matches? ] find-one
663     [ parse-wind pick wind<< ] when*
664
665     [ re-wind-variable matches? ] find-one
666     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
667
668     [ re-visibility matches? ] find-one
669     [ parse-visibility pick visibility<< ] when*
670
671     [ re-rvr matches? ] find-all " " join
672     [ parse-rvr ] map ", " join pick rvr<<
673
674     [ re-weather matches? ] find-all
675     [ parse-weather ] map ", " join pick weather<<
676
677     [ re-sky-condition matches? ] find-all
678     [ parse-sky-condition ] map ", " join pick sky-condition<<
679
680     drop ;
681
682 : taf-partials ( report seq -- report )
683     [ <taf-partial> ] map >>partials ;
684
685 : <taf-report> ( taf -- report )
686     [ taf-report new ] dip [ >>raw ] keep
687     string-lines [ [ blank? ] trim ] map
688     rest dup first "TAF" = [ rest ] when
689     harvest unclip swapd taf-body swap taf-partials ;
690
691 : taf-report. ( report -- )
692     [
693         standard-table-style [
694             {
695                 [ "Station" [ station>> ] row. ]
696                 [ "Timestamp" [ timestamp>> ] row. ]
697                 [ "Valid From" [ valid-timestamp>> ] row. ]
698                 [ "Wind" [ wind>> ] row. ]
699                 [ "Visibility" [ visibility>> ] row. ]
700                 [ "RVR" [ rvr>> ] row. ]
701                 [ "Weather" [ weather>> ] row. ]
702                 [ "Sky condition" [ sky-condition>> ] row. ]
703                 [ "Raw Text" [ raw>> ] row. ]
704             } cleave
705         ] tabular-output nl
706     ] [
707         partials>> [
708             standard-table-style [
709                 {
710                     [ "From" [ from-timestamp>> ] row. ]
711                     [ "Wind" [ wind>> ] row. ]
712                     [ "Visibility" [ visibility>> ] row. ]
713                     [ "RVR" [ rvr>> ] row. ]
714                     [ "Weather" [ weather>> ] row. ]
715                     [ "Sky condition" [ sky-condition>> ] row. ]
716                 } cleave
717             ] tabular-output nl
718         ] each
719     ] bi ;
720
721 PRIVATE>
722
723 GENERIC: taf ( station -- taf )
724
725 M: station taf cccc>> taf ;
726
727 M: string taf
728     "http://tgftp.nws.noaa.gov/data/forecasts/taf/stations/%s.TXT"
729     sprintf http-get nip ;
730
731 GENERIC: taf. ( station -- )
732
733 M: station taf. cccc>> taf. ;
734
735 M: string taf.
736     [ taf <taf-report> taf-report. ]
737     [ drop "%s TAF not found\n" printf ] recover ;
738
739 : metar-main ( -- )
740     command-line get [
741         [ metar print ] [ taf print ] bi nl
742     ] each ;
743
744 MAIN: metar-main