diff --git a/usr_share_grml/zsh/completion/grml/_weather b/usr_share_grml/zsh/completion/grml/_weather new file mode 100644 index 00000000..ad9876ef --- /dev/null +++ b/usr_share_grml/zsh/completion/grml/_weather @@ -0,0 +1,15 @@ +#compdef weather + +local -a arguments + +local -a _weather_countries=(${(f)"$(weather -lc 2>/dev/null)"}) +local -a _weather_stations=(${(f)"$(weather -ls 2>/dev/null)"}) + +arguments=( + '(-v --verbose)'{-v,--verbose}'[show all information for station]' + '(: -)'{-lc,--list-countries}'[list countries]' + '(-ls --list-stations)'{-ls,--list-stations}'[list stations]: :_values $_weather_countries' + '(: -)'{-h,--help}'[show help]' +) + +_arguments -A "${arguments[@]}" '*: :_values ${_weather_stations[@]}' diff --git a/usr_share_grml/zsh/functions/weather b/usr_share_grml/zsh/functions/weather index bf303bf7..ddc77ef8 100644 --- a/usr_share_grml/zsh/functions/weather +++ b/usr_share_grml/zsh/functions/weather @@ -3,47 +3,110 @@ emulate -L zsh -if [[ -z "$1" ]]; then - print 'Usage: weather ' - print \ - 'List of stations: http://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code' - return 0 +local DIR="$HOME"/.weather +local LOG="$DIR"/log +local BASEURI='https://tgftp.nws.noaa.gov/data' +local BASESTATIONS="$BASEURI"/observations/metar/decoded +local LOCATIONSOURCE="$BASEURI"/nsd_cccc.txt +local LOCATIONS=nsd_cccc.txt + +local _weather_usage() { + << EOU +Usage: weather [OPTIONS(s] ' + +Options: + -h|--help Show this usage information. + -v|--verbose Show all information for station. + -lc|--list-countries List countries from station list. + -ls|--list-stations [Country] List all or only stations matching country. + +Files: + Stationlist: $DIR/$LOCATIONS + + List of stations: http://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code +EOU +} + +! [ -f "$DIR"/"$LOCATIONS" ] && wget -P "$DIR" -T 10 --no-verbose \ + --output-file="$LOG" --timestamping "$BASEURI"/"$LOCATIONS" + +if [ -f "$DIR"/"$LOCATIONS" ]; then + local -a COUNTRIES=( ${(f)"$(awk -F';' \ + '($6!="") && ($6!="4") {print $6|"sort | uniq"}' \ + "$DIR"/"$LOCATIONS")"} ) fi -local VERBOSE="yes" # TODO: Make this a command line switch +while [[ $1 == -* ]]; do + case $1 in + (-h|--help) + _weather_usage + return 0 + ;; + (-v|--verbose) + local VERBOSE=1 + shift + ;; + (-lc|--list-countries) + printf 'Listing Countries\n' 1>&2 + print -l $COUNTRIES + return 0 + ;; + (-ls|--list-stations) + if ! [ -n "$2" ]; then + printf 'Listing all Stations:\n' 1>&2 + COUNTRY="." + elif [ -n "$2" ] && [ -n "${COUNTRIES[(r)$2]}" ]; then + printf 'Listing Stations for country: "%s"\n' "$2" 1>&2 + local COUNTRY="$2" + else + printf 'Could not find any stations for country: "%s"\n' "$2" 1>&2 + return 1 + fi + awk -F';' -v country="$COUNTRY" \ + '($6~country) {print $1, $4|"sort -k2"}' \ + "$DIR"/"$LOCATIONS" + return 0 + ;; + (*) + _weather_usage 1>&2 + return 1 + ;; + esac +done + +if [[ -z "$1" ]]; then + _weather_usage + return 1 +fi -local ODIR=`pwd` local PLACE="${1:u}" -local DIR="${HOME}/.weather" -local LOG="${DIR}/log" -if ! [[ -d ${DIR} ]]; then - print -n "Creating ${DIR}: " - mkdir ${DIR} - print 'done' +if ! [[ -d "$DIR" ]]; then + print -n "Creating ${DIR}: " >&2 + mkdir "$DIR" + print 'done' >&2 fi -print "Retrieving information for ${PLACE}:" -print -cd ${DIR} && \ - wget -T 10 --no-verbose --output-file=$LOG --timestamping \ - https://tgftp.nws.noaa.gov/data/observations/metar/decoded/$PLACE.TXT +print "Retrieving information for ${PLACE}:" >&2 +wget -P "$DIR" -T 10 --no-verbose --output-file="$LOG" --timestamping \ + "$BASESTATIONS"/"$PLACE".TXT if [[ $? -eq 0 ]]; then if [[ -n "$VERBOSE" ]] ; then - cat ${PLACE}.TXT + cat "$DIR"/"$PLACE".TXT else - DATE=$(grep 'UTC' ${PLACE}.TXT | sed 's#.* /##') - TEMPERATURE=$(awk '/Temperature/ { - print $4" degree Celcius / " $2" degree Fahrenheit" - }' ${PLACE}.TXT | tr -d '(') - echo "date: $DATE" - echo "temp: $TEMPERATURE" + DATE="$(date --date="$(awk -F / \ + '/UTC/ {gsub(/\./, "-");print $2}' "$DIR"/"$PLACE".TXT)")" + STATION="$(awk -F '[()]' -v station="$PLACE" \ + '$2 ~ station { print $1}' "$DIR"/"$PLACE".TXT)" + TEMPERATURE=$(awk -F '[( ]' '/Temperature/ { + print $5" degree Celcius / " $2" degree Fahrenheit"}' \ + "$DIR"/"$PLACE".TXT) + printf 'station: %s\ndate: %s\ntemp: %s\n' \ + "$STATION" "$DATE" "$TEMPERATURE" fi else print "There was an error retrieving the weather information for $PLACE" >&2 - cat $LOG - cd $ODIR + cat "$DIR"/"$LOG" return 1 fi -cd $ODIR