Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions usr_share_grml/zsh/completion/grml/_weather
Original file line number Diff line number Diff line change
@@ -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[@]}'
119 changes: 91 additions & 28 deletions usr_share_grml/zsh/functions/weather
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,110 @@

emulate -L zsh

if [[ -z "$1" ]]; then
print 'Usage: weather <station_id>'
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] <station_id>'

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"}' \
Comment on lines +34 to +35
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is probably a much neater way with zsh's expansion /(°o°)\

"$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