summaryrefslogtreecommitdiff
path: root/bin/radar
blob: 3e0e15a462d83c70986bb093856e80bf24cdb209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/sh
# radar.sh - retrieves NEXRAD doppler radar imagery from all sites in the USA.


radarfile=/usr/local/share/radar/radars.txt
baseurl=https://radar.weather.gov/ridge/standard/
siteregex="^[Kk|Pp][A-Za-z][A-Za-z][A-Za-z]$"

viewsite() { # use viewsite $url -v for verbose output
	[ -z "$(which mpv)" ] && echo "This script depends on mpv, but you don't have it installed (or maybe it's not in your \$PATH.)" && exit
	[ "$2" = "-v" ] && mpv $1 --loop-file=inf --no-osc || mpv $1 --loop-file=inf --no-osc > /dev/null 2>&1
}

list() {
	cat $radarfile
}

makeurl() {
	echo ${baseurl}$(caps $1)_loop.gif
}

search() {
	grep -w "$1" $radarfile
}

caps() {
	echo $1 | tr [:lower:] [:upper:]
}

hasspaces() {
	[ $(echo $1 | grep \  | wc -l) -gt 0 ] && echo "yes" || echo "no"
}

siteorcity() {
	[ $(hasspaces "$1") = yes ] && echo "city" && exit 
	[ $(expr "$1" : "$siteregex") -eq 4 ] && [ $(hasspaces "$1") = "no" ] && echo "site" || echo "city"
}

findsite() {
	echo $(echo $(search "$1") | cut -f2 -d":")
}

exists() {
	[ $(findsite "$1" | wc -m) -ne 1 ] && echo "yes" || echo "no"
}

inputwrapper() {
	[ $(exists "$1") = "no" ] && [ $(exists $(caps "$1")) = "no" ] && exit
	[ $(siteorcity "$1") = "site" ] && makeurl $1
	[ $(siteorcity "$1") = "city" ] && makeurl $(findsite "$1")
}

help() {
cat << EOF
radar - National Weather Service (NWS) Doppler radar from the command line.
Written in 2020 by Ray Patrick.

Options:

  $ radar [sitecode|city] 
    Display the latest Doppler radar imagery from the specified site. "sitecode" is any
    ICAO code. For example, the ICAO code for Memphis International Airport is KNQA.
    Running "radar knqa" will display the latest Doppler radar imagery from Memphis, TN.

    If the argument is a city name, radar will do its best to find the corresponding
    city in the list. While it's not strictly necessary to enclose single-word city
    names in quotes (e.g. "Chicago") it's best practice to always do so. For city
    names with multiple words (e.g. "Los Angeles") quotes are mandatory.
    Cities are case-sensitive.

  $ radar -l, --list
    List all the NWS NEXRAD Doppler radar sites in the USA.

  $ radar -s, --search [regex] 
    Match the output of "radar --list" against a regular expression.
    For instance, you could list all the Doppler radars in the state of Texas
    by running "radar search TX:" or find the site for Chicago, IL by running
    "radar search Chicago", or list all the sites at US Air Force bases by
    running "radar search AFB."

  $ radar -u, --url [sitecode|city] 
    Print the generated URL to standard output instead of opening it with mpv(1). Useful
    in case you don't want to use mpv(1) wish instead to open the imagery with, say, a
    Web browser, or curl(1) or wget(1) it.

  $ radar -h, --help
    Display this help and exit. Type 'man radar' for more thorough documentation.

EOF
}

case "$1" in
      -l) list ;;
  --list) list ;;
      -h) help ;;
  --help) help ;;
--search) search "$2" ;;
      -s) search "$2" ;;
   --url) inputwrapper "$2" ;;
      -u) inputwrapper "$2" ;;
       *) case "$2" in
         -l) list ;;
     --list) list ;;
         -h) help ;;
     --help) help ;;
         -s) search "$1" ;;
   --search) search "$1" ;;
         -u) inputwrapper "$1" ;;
      --url) inputwrapper "$1" ;;
          *) [ -z "$1" ] && help || viewsite $(inputwrapper "$1") ;;
          esac
esac