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