#!/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