]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/metar/metar.factor
core: subseq-index? -> subseq-of?
[factor.git] / extra / metar / metar.factor
index 427c3d689b81c55d3907cd0a48639f6025638250..230081f620c316f695095a8eddfe9a46702e9dc8 100644 (file)
@@ -3,8 +3,8 @@
 
 USING: accessors arrays ascii assocs calendar calendar.format
 classes.tuple combinators command-line continuations csv
-formatting fry grouping http.client io io.encodings.ascii
-io.files io.styles kernel math math.extras math.parser memoize
+formatting grouping http.client io io.encodings.ascii io.files
+io.styles kernel math math.extras math.functions math.parser
 namespaces regexp sequences sorting.human splitting strings urls
 wrap.strings ;
 
@@ -151,7 +151,7 @@ MEMO: glossary ( -- assoc )
             dup number?
             [ number>string ]
             [ glossary ?at drop ] if
-        ] map " " join
+        ] map join-words
     ] map "/" join ;
 
 : parse-timestamp ( str -- str' )
@@ -211,6 +211,7 @@ CONSTANT: compass-directions H{
         [ parse-direction ] dip {
             { [ "KT" ?tail ] [ "knots" ] }
             { [ "MPS" ?tail ] [ "meters per second" ] }
+            [ "knots" ]
         } cond [ "G" split1 ] dip '[ _ parse-speed ] bi@
         [ "%s at %s with gusts to %s " sprintf ]
         [ "%s at %s" sprintf ] if*
@@ -281,7 +282,7 @@ CONSTANT: compass-directions H{
     ] if ;
 
 : parse-weather ( str -- str' )
-    "VC" over subseq? [ "VC" "" replace t ] [ f ] if
+    dup "VC" subseq-of? [ "VC" "" replace t ] [ f ] if
     [ (parse-weather) ]
     [ [ " in the vicinity" append ] when ] bi* ;
 
@@ -617,6 +618,13 @@ CONSTANT: re-recent-weather R/ ((\w{2})?[BE]\d{2,4}((\w{2})?[BE]\d{2,4})?)+/
         [ @ [ 65 wrap-string write ] when* ] with-cell
     ] with-row ; inline
 
+: calc-humidity ( report -- humidity/f )
+    [ dew-point>> ] [ temperature>> ] bi 2dup and [
+        [ " " split1 drop string>number ] bi@
+        [ [ 17.625 * ] [ 243.04 + ] bi / e^ ] bi@ / 100 *
+        round "%d%%" sprintf
+    ] [ 2drop f ] if ;
+
 : metar-report. ( report -- )
     standard-table-style [
         {
@@ -630,6 +638,7 @@ CONSTANT: re-recent-weather R/ ((\w{2})?[BE]\d{2,4}((\w{2})?[BE]\d{2,4})?)+/
             [ "Temperature" [ temperature>> ] row. ]
             [ "Dew point" [ dew-point>> ] row. ]
             [ "Altimeter" [ altimeter>> ] row. ]
+            [ "Humidity" [ calc-humidity ] row. ]
             [ "Remarks" [ remarks>> ] row. ]
             [ "Raw Text" [ raw>> ] row. ]
         } cleave
@@ -701,7 +710,7 @@ sky-condition raw ;
     [ re-visibility matches? ] find-one
     [ parse-visibility pick visibility<< ] when*
 
-    [ re-rvr matches? ] find-all " " join
+    [ re-rvr matches? ] find-all join-words
     [ parse-rvr ] map ", " join pick rvr<<
 
     [ re-weather matches? ] find-all
@@ -727,7 +736,7 @@ sky-condition raw ;
     [ re-visibility matches? ] find-one
     [ parse-visibility pick visibility<< ] when*
 
-    [ re-rvr matches? ] find-all " " join
+    [ re-rvr matches? ] find-all join-words
     [ parse-rvr ] map ", " join pick rvr<<
 
     [ re-weather matches? ] find-all