Release new version 0.19.7
[grml-etc-core.git] / usr_share_grml / zsh / functions / weather
1 # retrieve weather information on the console
2 # Usage example: 'weather LOWG'
3
4 emulate -L zsh
5
6 if [[ -z "$1" ]]; then
7     print 'Usage: weather <station_id>'
8     print \
9   'List of stations: http://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code'
10     return 0
11 fi
12
13 local VERBOSE="yes"    # TODO: Make this a command line switch
14
15 local ODIR=`pwd`
16 local PLACE="${1:u}"
17 local DIR="${HOME}/.weather"
18 local LOG="${DIR}/log"
19
20 if [[ -d ${DIR} ]]; then
21     print -n "Creating ${DIR}: "
22     mkdir ${DIR}
23     print 'done'
24 fi
25
26 print "Retrieving information for ${PLACE}:"
27 print
28 cd ${DIR} && \
29   wget -T 10 --no-verbose --output-file=$LOG --timestamping \
30        http://weather.noaa.gov/pub/data/observations/metar/decoded/$PLACE.TXT
31
32 if [[ $? -eq 0 ]]; then
33     if [[ -n "$VERBOSE" ]] ; then
34         cat ${PLACE}.TXT
35     else
36         DATE=$(grep 'UTC' ${PLACE}.TXT | sed 's#.* /##')
37         TEMPERATURE=$(awk '/Temperature/ {
38                              print $4" degree Celcius / " $2" degree Fahrenheit"
39                            }' ${PLACE}.TXT | tr -d '(')
40         echo "date: $DATE"
41         echo "temp:  $TEMPERATURE"
42     fi
43 else
44     print "There was an error retrieving the weather information for $PLACE" >&2
45     cat $LOG
46     cd $ODIR
47     return 1
48 fi
49 cd $ODIR