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