From 2d5063a55572c0918f883aa7e9ad77d75459839a Mon Sep 17 00:00:00 2001 From: Al Bowles Date: Tue, 27 Dec 2022 11:39:41 -0600 Subject: [PATCH 1/7] new and improved wx --- .bin/wx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.bin/wx b/.bin/wx index 234aa03..bf9aee0 100755 --- a/.bin/wx +++ b/.bin/wx @@ -278,4 +278,9 @@ heat_index() { _temperature_conversion $heat_index $1 } -$@ && _check_last_modified || refresh +summary() { + temp f + phenomena || condition +} + +"$@" && _check_last_modified || refresh From 9d6fe649fdf19523d47cb6ead6f0e9c0aebb79dc Mon Sep 17 00:00:00 2001 From: Al Bowles Date: Tue, 27 Dec 2022 11:57:29 -0600 Subject: [PATCH 2/7] Refactor and remove lastmod check This script now runs on an interval and so lastmod check is no longer needed. --- .bin/wx | 62 ++++++++++----------------------------------------------- 1 file changed, 10 insertions(+), 52 deletions(-) diff --git a/.bin/wx b/.bin/wx index bf9aee0..8b9f610 100755 --- a/.bin/wx +++ b/.bin/wx @@ -21,60 +21,18 @@ STATION=KRYV #STATION=KHNB DATA_URI="data/observations/metar/stations/$STATION.TXT" DATA_URL="https://tgftp.nws.noaa.gov/$DATA_URI" -CACHE="$HOME/.cache/wx/metar" -TIMEOUT=10 -PLATFORM=$(uname) - -[[ ! -d "$(dirname $CACHE)" ]] && mkdir -p "$(dirname $CACHE)" -[[ ! -f "$CACHE" ]] && touch $CACHE - -# TODO uuoc -metar=`cat $CACHE` - -_check_last_modified() { - last_modified=$(curl -I -s $DATA_URL | grep Last\-Modified | awk -F': ' '{ print $2 }') - [[ "$last_modified" == "" ]] && return 1 - - case $PLATFORM in - Linux) - src=$(date --date="$last_modified" +'%s') - # FIXME are we handling the case where $CACHE is empty? - cache=$(date -r $CACHE +'%s') - ;; - Darwin) - src=$(date -j -f "%a, %e %b %Y %T %Z" "$last_modified" "+%s" 2&> /dev/null) - cache=$(stat -f "%m" -q $CACHE) - ;; - esac - [[ "$src" > "$cache" ]] && return 1 || return 0 -} -_check_md5sum() { - case $PLATFORM in - Linux) - md5sum $1 | awk '{print $1}' - ;; - Darwin) - md5 $1 | awk '{print $4}' - ;; - esac -} - -refresh() { - data=$(curl --connect-timeout $TIMEOUT --create-dirs -s $DATA_URL | tr "\n" " ") - [[ "$data" != "" && $(_check_md5sum <<< $data) != $(_check_md5sum $CACHE) ]] && echo $data > $CACHE -} +metar=`curl --connect-timeout 2 -s $DATA_URL | tr "\n" " "` metar() { echo $metar } - remarks() { - awk -F 'RMK' '{print $2}' $CACHE + awk -F 'RMK' '{print $2}' <<< $metar } rvr() { - awk '$0 ~ /\(R\[0-9\]\+\/\[0-9\]\+V?\(M\|P\)\[0-9\]\+FT\)/' $CACHE + awk '$0 ~ /\(R\[0-9\]\+\/\[0-9\]\+V?\(M\|P\)\[0-9\]\+FT\)/' <<< $metar } phenomena() { @@ -139,7 +97,7 @@ phenomena() { } condition() { - cond=$(grep -o '\(\(CLR\|SKC\|FEW\|SCT\|BKN\|OVC\|VV\)\([0-9]\+\)\?\(\|TCU\|CB\)\?\)\+' $CACHE) + cond=$(grep -o '\(\(CLR\|SKC\|FEW\|SCT\|BKN\|OVC\|VV\)\([0-9]\+\)\?\(\|TCU\|CB\)\?\)\+' <<< $metar) case ${cond[${#cond[@]} - 1]} in CLR*|SKC*) condition="clear" ;; FEW*) condition="partly cloudy" ;; @@ -154,11 +112,11 @@ condition() { } visibility() { - grep -o '\([0-9]\+SM\)' $CACHE + grep -o '\([0-9]\+SM\)' <<< $metar } altimeter() { - grep -o 'A[0-9]\{4\}' $CACHE + grep -o 'A[0-9]\{4\}' <<< $metar } _wind_speed_conversion() { @@ -172,11 +130,11 @@ _wind_speed_conversion() { } _wind_speed(){ - read wind_speed gusting <<< $(grep -o '\([0-9]\{2\}G\)\?[0-9]\{2\}KT' $CACHE | awk -F 'G' '{ printf "%d %d", $1, $2 }') + read wind_speed gusting <<< $(grep -o '\([0-9]\{2\}G\)\?[0-9]\{2\}KT' <<< $metar | awk -F 'G' '{ printf "%d %d", $1, $2 }') } _wind_direction() { - wd=$(printf "%.3s" $(grep -o '\(VRB\|[0-9]\{3\}\)\([0-9]\{2\}G\)\?[0-9]\{2\}KT' $CACHE)) + wd=$(printf "%.3s" $(grep -o '\(VRB\|[0-9]\{3\}\)\([0-9]\{2\}G\)\?[0-9]\{2\}KT' <<< $metar)) if [[ "$wd" == "VRB" ]] then @@ -226,7 +184,7 @@ wind() { } _temperature_dewpoint() { - temperature_dewpoint=$(grep -o '\s\(M\)\?\([0-9]\{2\}\/\(M\)\?[0-9]\{2\}\)\s' $CACHE) + temperature_dewpoint=$(grep -o '\s\(M\)\?\([0-9]\{2\}\/\(M\)\?[0-9]\{2\}\)\s' <<< $metar) read temp dp <<< $(awk -F '/' '{ printf "%d %d", $1, $2 }' <<< $(echo $temperature_dewpoint | sed 's/M/-/g')) } @@ -283,4 +241,4 @@ summary() { phenomena || condition } -"$@" && _check_last_modified || refresh +"$@" From 18c55717ab9d0ce26e09e0e6ae717a81943dbed1 Mon Sep 17 00:00:00 2001 From: Al Bowles Date: Tue, 27 Dec 2022 12:25:02 -0600 Subject: [PATCH 3/7] Fix summary --- .bin/wx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.bin/wx b/.bin/wx index 8b9f610..967e074 100755 --- a/.bin/wx +++ b/.bin/wx @@ -27,6 +27,7 @@ metar=`curl --connect-timeout 2 -s $DATA_URL | tr "\n" " "` metar() { echo $metar } + remarks() { awk -F 'RMK' '{print $2}' <<< $metar } @@ -90,9 +91,9 @@ phenomena() { if [[ -n "$qual" || -n "$desc" || -n "$precip" || -n "$obsc" || -n "$other" ]] then printf " %s" $qual $desc $precip $obsc $other - exit 0 + return 0 else - exit 1 + return 1 fi } @@ -108,7 +109,7 @@ condition() { **) condition="" ;; esac - printf " %s" $condition + printf " $condition" } visibility() { @@ -241,4 +242,4 @@ summary() { phenomena || condition } -"$@" +$@ From 40d22121e84a633fd1ca6590757f26316774b026 Mon Sep 17 00:00:00 2001 From: Al Bowles Date: Tue, 27 Dec 2022 13:21:40 -0600 Subject: [PATCH 4/7] Add feelslike subcommand The feelslike subcommand returns wind chill or heat index as appropriate to conditions, in the specified units --- .bin/wx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.bin/wx b/.bin/wx index 967e074..c7fa848 100755 --- a/.bin/wx +++ b/.bin/wx @@ -1,10 +1,10 @@ #!/bin/bash ## wx: a METAR parser written in Bash -# (c) 2021 Allyson Bowles +# (c) 2022 Al Bowles ## Usage: -# wx [[ temp [ f|F | c|C | k|K ]] | humidity | wind | altimeter | visibility | rvr | condition | phenomena | metar | remarks | refresh ] +# wx [[ temp [ f|F | c|C | k|K ]] | humidity | wind | altimeter | visibility | rvr | condition | phenomena | metar | remarks | feelslike ] ## References: # http://www.met.tamu.edu/class/metar/quick-metar.html @@ -214,6 +214,7 @@ temp() { wind_chill() { _temperature_dewpoint + [[ "$temp" < 11 ]] || return 1 _wind_speed # bc does not support floating point exponents y=$(awk -v "wc=$wind_speed" 'BEGIN {print wc**0.16}') @@ -223,6 +224,7 @@ wind_chill() { heat_index() { _temperature_dewpoint + [[ "$temp" > 26 ]] || return 1 _relative_humidity c1="-8.78469475556" c2="1.61139411" @@ -237,8 +239,13 @@ heat_index() { _temperature_conversion $heat_index $1 } +feelslike() { + wind_chill $1 || heat_index $1 +} + summary() { temp f + printf " (%s)" $(feelslike f) phenomena || condition } From 61f3dfe71a5f7fa72073e0f1c3f164032ea5b2ff Mon Sep 17 00:00:00 2001 From: Al Bowles Date: Tue, 25 Oct 2022 10:59:50 -0500 Subject: [PATCH 5/7] Add some more linters and fixers --- .vimrc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.vimrc b/.vimrc index 3709967..69d1484 100755 --- a/.vimrc +++ b/.vimrc @@ -93,20 +93,26 @@ let g:ale_fix_on_save = 1 let g:ale_echo_msg_error_str = 'E' let g:ale_echo_msg_warning_str = 'W' let g:ale_echo_msg_format = '[%linter%] %s [%severity%]' +let g:ale_list_window_size = 5 let g:ale_fixers = { \ '*': ['remove_trailing_lines', 'trim_whitespace'], \ 'javascript': ['prettier'], +\ 'json': ['jq'], \ 'terraform': ['terraform'], -\ 'yaml': ['yamlfix'] +\ 'yaml': ['yamlfix'], \} let g:ale_linters = { -\ 'go': ['gofmt', 'golint', 'go vet', 'gopls'], +\ 'go': ['gofmt', 'golint', 'go vet', 'gopls', 'golangci-lint'], \ 'ansible': ['ansible-lint'], \ 'perl': ['perlcritic'], \ 'terraform': ['terraform'], +\ 'ruby': ['rubocop'], +\ 'javascript': ['eslint'], \} " TODO markdown linter + +"let b:ale_ruby_rubocop_executable = './vendor/bundle/ruby/2.6.0/bin/rubocop' " TODO j2-lint integration " TODO new panes bottom / right " TODO middle click = set paste/insert/set nopaste From 761a6fa8a0d039521d32ac050a1e107d944415c2 Mon Sep 17 00:00:00 2001 From: Al Bowles Date: Fri, 6 Jan 2023 20:57:32 -0600 Subject: [PATCH 6/7] Fix windchill calculation --- .bin/wx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/.bin/wx b/.bin/wx index c7fa848..bf91955 100755 --- a/.bin/wx +++ b/.bin/wx @@ -213,18 +213,24 @@ temp() { } wind_chill() { + # Wind chill is only a useful metric at or below 10C /and/ + # when wind speeds exceed 4.8 kph. _temperature_dewpoint - [[ "$temp" < 11 ]] || return 1 + [[ $(bc <<< "$temp < 11") == 1 ]] || return 1 _wind_speed + x=$(_wind_speed_conversion "$wind_speed" kph) + [[ $(bc <<< "$x > 4.8") == 1 ]] || return 1 + # bc does not support floating point exponents - y=$(awk -v "wc=$wind_speed" 'BEGIN {print wc**0.16}') + y=$(awk -v "wc=$x" 'BEGIN {print wc**0.16}') wind_chill=$(bc <<< "13.12 + (0.6215 * $temp) - (11.37 * $y) + (0.3965 * $temp * $y)") _temperature_conversion $wind_chill $1 } heat_index() { + # heat index is only a useful metric at or above 26C _temperature_dewpoint - [[ "$temp" > 26 ]] || return 1 + [[ $(bc <<< "$temp > 26") == 1 ]] || return 1 _relative_humidity c1="-8.78469475556" c2="1.61139411" @@ -240,11 +246,12 @@ heat_index() { } feelslike() { - wind_chill $1 || heat_index $1 + wind_chill $1 || heat_index $1 || return 1 } summary() { temp f + # TODO don't print if feelslike returns 1 printf " (%s)" $(feelslike f) phenomena || condition } From a089f0dba546d96673c79b8d7fffd507fbd43f5c Mon Sep 17 00:00:00 2001 From: Al Bowles Date: Mon, 16 Jan 2023 17:14:47 -0600 Subject: [PATCH 7/7] tweaks --- .bin/wx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.bin/wx b/.bin/wx index bf91955..cab095f 100755 --- a/.bin/wx +++ b/.bin/wx @@ -1,7 +1,7 @@ #!/bin/bash ## wx: a METAR parser written in Bash -# (c) 2022 Al Bowles +# (c) 2023 Al Bowles ## Usage: # wx [[ temp [ f|F | c|C | k|K ]] | humidity | wind | altimeter | visibility | rvr | condition | phenomena | metar | remarks | feelslike ] @@ -22,10 +22,10 @@ STATION=KRYV DATA_URI="data/observations/metar/stations/$STATION.TXT" DATA_URL="https://tgftp.nws.noaa.gov/$DATA_URI" -metar=`curl --connect-timeout 2 -s $DATA_URL | tr "\n" " "` +metar=$(curl --connect-timeout 2 -s $DATA_URL) metar() { - echo $metar + echo "$metar" } remarks() { @@ -81,7 +81,6 @@ phenomena() { case ${phen[0]} in *SQ*) other="squall" ;; - *FC*) other="funnel cloud" ;; *SS*) other="sandstorm" ;; *FC*) other="tornado" ;; *DS*) other="dust storm" ;; @@ -216,7 +215,7 @@ wind_chill() { # Wind chill is only a useful metric at or below 10C /and/ # when wind speeds exceed 4.8 kph. _temperature_dewpoint - [[ $(bc <<< "$temp < 11") == 1 ]] || return 1 + [[ $(bc <<< "$temp <= 10") == 1 ]] || return 1 _wind_speed x=$(_wind_speed_conversion "$wind_speed" kph) [[ $(bc <<< "$x > 4.8") == 1 ]] || return 1 @@ -252,7 +251,7 @@ feelslike() { summary() { temp f # TODO don't print if feelslike returns 1 - printf " (%s)" $(feelslike f) + printf " (%s)" "$(feelslike f)" phenomena || condition }