]> gitweb.factorcode.org Git - factor.git/blob - extra/metar/metar.factor
metar: fix for timestamps like "2124" meaning "2200".
[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 continuations csv formatting fry grouping
6 http.client io io.encodings.ascii io.files io.styles kernel math
7 math.extras math.parser memoize regexp sequences sorting.human
8 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: / over index [ 1 > [ 1 cut "+" glue ] when ] 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/ [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 : metar-body ( report seq -- report )
304
305     [ { "METAR" "SPECI" } member? ] find-one
306     [ pick type<< ] when*
307
308     [ re-station matches? ] find-one
309     [ pick station<< ] when*
310
311     [ re-timestamp matches? ] find-one
312     [ parse-timestamp pick timestamp<< ] when*
313
314     [ { "AUTO" "COR" } member? ] find-one
315     [ pick modifier<< ] when*
316
317     [ re-wind matches? ] find-one
318     [ parse-wind pick wind<< ] when*
319
320     [ re-wind-variable matches? ] find-one
321     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
322
323     [ re-visibility matches? ] find-one
324     [ parse-visibility pick visibility<< ] when*
325
326     [ re-rvr matches? ] find-all " " join
327     [ parse-rvr ] map ", " join pick rvr<<
328
329     [ re-weather matches? ] find-all
330     [ parse-weather ] map ", " join pick weather<<
331
332     [ re-sky-condition matches? ] find-all
333     [ parse-sky-condition ] map ", " join pick sky-condition<<
334
335     [ re-temperature matches? ] find-one
336     [
337         parse-temperature
338         [ pick temperature<< ]
339         [ pick dew-point<< ] bi*
340     ] when*
341
342     [ re-altimeter matches? ] find-one
343     [ parse-altimeter pick altimeter<< ] when*
344
345     drop ;
346
347 : signed-number ( sign value -- n )
348     [ string>number ] bi@ swap zero? [ neg ] unless 10.0 / ;
349
350 : single-value ( str -- str' )
351     1 cut signed-number ;
352
353 : double-value ( str -- m n )
354     1 cut 3 cut [ signed-number ] dip 1 cut signed-number ;
355
356 : parse-1hr-temp ( str -- str' )
357     "T" ?head drop dup length 4 > [
358         double-value
359         [ dup C>F "%.1f °C (%.1f °F)" sprintf ] bi@
360         "hourly temperature %s and dew point %s" sprintf
361     ] [
362         single-value dup C>F
363         "hourly temperature %.1f °C (%.1f °F)" sprintf
364     ] if ;
365
366 : parse-6hr-max-temp ( str -- str' )
367     "1" ?head drop single-value dup C>F
368     "6-hour maximum temperature %.1f °C (%.1f °F)" sprintf ;
369
370 : parse-6hr-min-temp ( str -- str' )
371     "2" ?head drop single-value dup C>F
372     "6-hour minimum temperature %.1f °C (%.1f °F)" sprintf ;
373
374 : parse-24hr-temp ( str -- str' )
375     "4" ?head drop double-value
376     [ dup C>F "%.1f °C (%.1f °F)" sprintf ] bi@
377     "24-hour maximum temperature %s minimum temperature %s"
378     sprintf ;
379
380 : parse-1hr-pressure ( str -- str' )
381     "5" ?head drop 1 cut single-value [ pressure-tendency at ] dip
382     "hourly pressure %s %s hPa" sprintf ;
383
384 : parse-snow-depth ( str -- str' )
385     "4/" ?head drop string>number "snow depth %s inches" sprintf ;
386
387 CONSTANT: low-clouds H{
388     { 1 "cumulus (fair weather)" }
389     { 2 "cumulus (towering)" }
390     { 3 "cumulonimbus (no anvil)" }
391     { 4 "stratocumulus (from cumulus)" }
392     { 5 "stratocumuls (not cumulus)" }
393     { 6 "stratus or Fractostratus (fair)" }
394     { 7 "fractocumulus / fractostratus (bad weather)" }
395     { 8 "cumulus and stratocumulus" }
396     { 9 "cumulonimbus (thunderstorm)" }
397     { -1 "not valid" }
398 }
399
400 CONSTANT: mid-clouds H{
401     { 1 "altostratus (thin)" }
402     { 2 "altostratus (thick)" }
403     { 3 "altocumulus (thin)" }
404     { 4 "altocumulus (patchy)" }
405     { 5 "altocumulus (thickening)" }
406     { 6 "altocumulus (from cumulus)" }
407     { 7 "altocumulus (with altocumulus, altostratus, nimbostratus)" }
408     { 8 "altocumulus (with turrets)" }
409     { 9 "altocumulus (chaotic)" }
410     { -1 "above overcast" }
411 }
412
413 CONSTANT: high-clouds H{
414     { 1 "cirrus (filaments)" }
415     { 2 "cirrus (dense)" }
416     { 3 "cirrus (often with cumulonimbus)" }
417     { 4 "cirrus (thickening)" }
418     { 5 "cirrus / cirrostratus (low in sky)" }
419     { 6 "cirrus / cirrostratus (hi in sky)" }
420     { 7 "cirrostratus (entire sky)" }
421     { 8 "cirrostratus (partial)" }
422     { 9 "cirrocumulus or cirrocumulus / cirrus / cirrostratus" }
423     { -1 "above overcast" }
424 }
425
426 : parse-cloud-cover ( str -- str' )
427     "8/" ?head drop first3 [ CHAR: 0 - ] tri@
428     [ [ f ] [ low-clouds at "low clouds are %s" sprintf ] if-zero ]
429     [ [ f ] [ mid-clouds at "middle clouds are %s" sprintf ] if-zero ]
430     [ [ f ] [ high-clouds at "high clouds are %s" sprintf ] if-zero ]
431     tri* 3array " " join ;
432
433 : parse-inches ( str -- str' )
434     dup [ CHAR: / = ] all? [ drop "unknown" ] [
435         string>number
436         [ "trace" ] [ 100 /f "%.2f inches" sprintf ] if-zero
437     ] if ;
438
439 : parse-1hr-precipitation ( str -- str' )
440     "P" ?head drop parse-inches
441     "%s precipitation in last hour" sprintf ;
442
443 : parse-6hr-precipitation ( str -- str' )
444     "6" ?head drop parse-inches
445     "%s precipitation in last 6 hours" sprintf ;
446
447 : parse-24hr-precipitation ( str -- str' )
448     "7" ?head drop parse-inches
449     "%s precipitation in last 24 hours" sprintf ;
450
451 ! XXX: "on the hour" instead of "00 minutes past the hour" ?
452
453 : parse-recent-time ( str -- str' )
454     dup length 2 >
455     [ 2 cut ":" glue ]
456     [ " minutes past the hour" append ] if ;
457
458 : parse-peak-wind ( str -- str' )
459     "/" split1 [ parse-wind ] [ parse-recent-time ] bi*
460     "%s occuring at %s" sprintf ;
461
462 : parse-sea-level-pressure ( str -- str' )
463     "SLP" ?head drop string>number 10.0 /f 1000 +
464     "sea-level pressure is %s hPa" sprintf ;
465
466 : parse-lightning ( str -- str' )
467     "LTG" ?head drop 2 group [ lightning at ] map " " join ;
468
469 CONSTANT: re-recent-weather R/ ((\w{2})?[BE]\d{2,4}((\w{2})?[BE]\d{2,4})?)+/
470
471 : parse-began/ended ( str -- str' )
472     unclip swap
473     [ CHAR: B = "began" "ended" ? ]
474     [ parse-recent-time ] bi* "%s at %s" sprintf ;
475
476 : split-recent-weather ( str -- seq )
477     [ dup empty? not ] [
478         dup [ digit? ] find drop
479         over [ digit? not ] find-from drop
480         [ cut ] [ f ] if* swap
481     ] produce nip ;
482
483 : (parse-recent-weather) ( str -- str' )
484     dup [ digit? ] find drop 2 > [
485         2 cut [ weather at " " append ] dip
486     ] [ f swap ] if parse-began/ended "" append-as ;
487
488 : parse-recent-weather ( str -- str' )
489     split-recent-weather
490     [ (parse-recent-weather) ] map " " join ;
491
492 : parse-varying ( str -- str' )
493     "V" split1 [ string>number ] bi@
494     "varying between %s00 and %s00 ft" sprintf ;
495
496 : parse-from-to ( str -- str' )
497     "-" split [ parse-glossary ] map " to " join ;
498
499 : parse-water-equivalent-snow ( str -- str' )
500     "933" ?head drop parse-inches
501     "%s water equivalent of snow on ground" sprintf ;
502
503 : parse-duration-of-sunshine ( str -- str' )
504     "98" ?head drop string>number
505     [ "no" ] [ "%s minutes of" sprintf ] if-zero
506     "%s sunshine" sprintf ;
507
508 : parse-6hr-snowfall ( str -- str' )
509     "931" ?head drop parse-inches
510     "%s snowfall in last 6 hours" sprintf ;
511
512 : parse-probability ( str -- str' )
513     "PROB" ?head drop string>number
514     "probability of %d%%" sprintf ;
515
516 : parse-remark ( str -- str' )
517     {
518         { [ dup glossary key? ] [ glossary at ] }
519         { [ dup R/ 1\d{4}/ matches? ] [ parse-6hr-max-temp ] }
520         { [ dup R/ 2\d{4}/ matches? ] [ parse-6hr-min-temp ] }
521         { [ dup R/ 4\d{8}/ matches? ] [ parse-24hr-temp ] }
522         { [ dup R/ 4\\/\d{3}/ matches? ] [ parse-snow-depth ] }
523         { [ dup R/ 5\d{4}/ matches? ] [ parse-1hr-pressure ] }
524         { [ dup R/ 6[\d\\/]{4}/ matches? ] [ parse-6hr-precipitation ] }
525         { [ dup R/ 7\d{4}/ matches? ] [ parse-24hr-precipitation ] }
526         { [ dup R/ 8\\/\d{3}/ matches? ] [ parse-cloud-cover ] }
527         { [ dup R/ 931\d{3}/ matches? ] [ parse-6hr-snowfall ] }
528         { [ dup R/ 933\d{3}/ matches? ] [ parse-water-equivalent-snow ] }
529         { [ dup R/ 98\d{3}/ matches? ] [ parse-duration-of-sunshine ] }
530         { [ dup R/ T\d{4,8}/ matches? ] [ parse-1hr-temp ] }
531         { [ dup R/ \d{3}\d{2,3}\\/\d{2,4}/ matches? ] [ parse-peak-wind ] }
532         { [ dup R/ P\d{4}/ matches? ] [ parse-1hr-precipitation ] }
533         { [ dup R/ SLP\d{3}/ matches? ] [ parse-sea-level-pressure ] }
534         { [ dup R/ LTG\w+/ matches? ] [ parse-lightning ] }
535         { [ dup R/ PROB\d+/ matches? ] [ parse-probability ] }
536         { [ dup R/ \d{3}V\d{3}/ matches? ] [ parse-varying ] }
537         { [ dup R/ [^-]+(-[^-]+)+/ matches? ] [ parse-from-to ] }
538         { [ dup R/ [^\\/]+(\\/[^\\/]+)+/ matches? ] [ ] }
539         { [ dup R/ \d+.\d+/ matches? ] [ ] }
540         { [ dup re-recent-weather matches? ] [ parse-recent-weather ] }
541         { [ dup re-weather matches? ] [ parse-weather ] }
542         { [ dup re-sky-condition matches? ] [ parse-sky-condition ] }
543         [ parse-glossary ]
544     } cond ;
545
546 : metar-remarks ( report seq -- report )
547     [ parse-remark ] map " " join >>remarks ;
548
549 : <metar-report> ( metar -- report )
550     [ metar-report new ] dip [ >>raw ] keep
551     [ blank? ] split-when { "RMK" } split1
552     [ metar-body ] [ metar-remarks ] bi* ;
553
554 : row. ( name quot -- )
555     '[
556         [ _ write ] with-cell
557         [ @ [ 65 wrap-string write ] when* ] with-cell
558     ] with-row ; inline
559
560 : metar-report. ( report -- )
561     standard-table-style [
562         {
563             [ "Station" [ station>> ] row. ]
564             [ "Timestamp" [ timestamp>> ] row. ]
565             [ "Wind" [ wind>> ] row. ]
566             [ "Visibility" [ visibility>> ] row. ]
567             [ "RVR" [ rvr>> ] row. ]
568             [ "Weather" [ weather>> ] row. ]
569             [ "Sky condition" [ sky-condition>> ] row. ]
570             [ "Temperature" [ temperature>> ] row. ]
571             [ "Dew point" [ dew-point>> ] row. ]
572             [ "Altimeter" [ altimeter>> ] row. ]
573             [ "Remarks" [ remarks>> ] row. ]
574             [ "Raw Text" [ raw>> ] row. ]
575         } cleave
576     ] tabular-output nl ;
577
578 PRIVATE>
579
580 GENERIC: metar ( station -- metar )
581
582 M: station metar cccc>> metar ;
583
584 M: string metar
585     "http://tgftp.nws.noaa.gov/data/observations/metar/stations/%s.TXT"
586     sprintf http-get nip ;
587
588 GENERIC: metar. ( station -- )
589
590 M: station metar. cccc>> metar. ;
591
592 M: string metar.
593     [ metar <metar-report> metar-report. ]
594     [ drop "%s METAR not found\n" printf ] recover ;
595
596 <PRIVATE
597
598 : parse-wind-shear ( str -- str' )
599     "WS" ?head drop "/" split1
600     [ parse-altitude ] [ parse-wind ] bi* prepend
601     "wind shear " prepend ;
602
603 CONSTANT: re-from-timestamp R/ FM\d{6}/
604
605 : parse-from-timestamp ( str -- str' )
606     "FM" ?head drop parse-timestamp ;
607
608 CONSTANT: re-valid-timestamp R/ \d{4}\/\d{4}/
609
610 : parse-valid-timestamp ( str -- str' )
611     "/" split1 [ "00" append parse-timestamp ] bi@ " to " glue ;
612
613 TUPLE: taf-report station timestamp valid-timestamp wind
614 visibility rvr weather sky-condition partials raw ;
615
616 TUPLE: taf-partial from-timestamp wind visibility rvr weather
617 sky-condition raw ;
618
619 : taf-body ( report str -- report )
620     [ blank? ] split-when
621
622     [ "TAF" = ] find-one drop
623
624     [ { "AMD" "COR" "RTD" } member? ] find-one drop
625
626     [ re-station matches? ] find-one
627     [ pick station<< ] when*
628
629     [ re-timestamp matches? ] find-one
630     [ parse-timestamp pick timestamp<< ] when*
631
632     [ re-valid-timestamp matches? ] find-one
633     [ parse-valid-timestamp pick valid-timestamp<< ] when*
634
635     [ re-wind matches? ] find-one
636     [ parse-wind pick wind<< ] when*
637
638     [ re-wind-variable matches? ] find-one
639     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
640
641     [ re-visibility matches? ] find-one
642     [ parse-visibility pick visibility<< ] when*
643
644     [ re-rvr matches? ] find-all " " join
645     [ parse-rvr ] map ", " join pick rvr<<
646
647     [ re-weather matches? ] find-all
648     [ parse-weather ] map ", " join pick weather<<
649
650     [ re-sky-condition matches? ] find-all
651     [ parse-sky-condition ] map ", " join pick sky-condition<<
652
653     drop ;
654
655 : <taf-partial> ( str -- partial )
656     [ taf-partial new ] dip [ blank? ] split-when
657
658     [ re-from-timestamp matches? ] find-one
659     [ parse-from-timestamp pick from-timestamp<< ] when*
660
661     [ re-wind matches? ] find-one
662     [ parse-wind pick wind<< ] when*
663
664     [ re-wind-variable matches? ] find-one
665     [ parse-wind-variable pick wind>> prepend pick wind<< ] when*
666
667     [ re-visibility matches? ] find-one
668     [ parse-visibility pick visibility<< ] when*
669
670     [ re-rvr matches? ] find-all " " join
671     [ parse-rvr ] map ", " join pick rvr<<
672
673     [ re-weather matches? ] find-all
674     [ parse-weather ] map ", " join pick weather<<
675
676     [ re-sky-condition matches? ] find-all
677     [ parse-sky-condition ] map ", " join pick sky-condition<<
678
679     drop ;
680
681 : taf-partials ( report seq -- report )
682     [ <taf-partial> ] map >>partials ;
683
684 : <taf-report> ( taf -- report )
685     [ taf-report new ] dip [ >>raw ] keep
686     string-lines [ [ blank? ] trim ] map
687     rest dup first "TAF" = [ rest ] when
688     harvest unclip swapd taf-body swap taf-partials ;
689
690 : taf-report. ( report -- )
691     [
692         standard-table-style [
693             {
694                 [ "Station" [ station>> ] row. ]
695                 [ "Timestamp" [ timestamp>> ] row. ]
696                 [ "Valid From" [ valid-timestamp>> ] row. ]
697                 [ "Wind" [ wind>> ] row. ]
698                 [ "Visibility" [ visibility>> ] row. ]
699                 [ "RVR" [ rvr>> ] row. ]
700                 [ "Weather" [ weather>> ] row. ]
701                 [ "Sky condition" [ sky-condition>> ] row. ]
702                 [ "Raw Text" [ raw>> ] row. ]
703             } cleave
704         ] tabular-output nl
705     ] [
706         partials>> [
707             standard-table-style [
708                 {
709                     [ "From" [ from-timestamp>> ] row. ]
710                     [ "Wind" [ wind>> ] row. ]
711                     [ "Visibility" [ visibility>> ] row. ]
712                     [ "RVR" [ rvr>> ] row. ]
713                     [ "Weather" [ weather>> ] row. ]
714                     [ "Sky condition" [ sky-condition>> ] row. ]
715                 } cleave
716             ] tabular-output nl
717         ] each
718     ] bi ;
719
720 PRIVATE>
721
722 GENERIC: taf ( station -- taf )
723
724 M: station taf cccc>> taf ;
725
726 M: string taf
727     "http://tgftp.nws.noaa.gov/data/forecasts/taf/stations/%s.TXT"
728     sprintf http-get nip ;
729
730 GENERIC: taf. ( station -- )
731
732 M: station taf. cccc>> taf. ;
733
734 M: string taf.
735     [ taf <taf-report> taf-report. ]
736     [ drop "%s TAF not found\n" printf ] recover ;