wxrx/build_images.sh

186 lines
4.6 KiB
Bash
Raw Normal View History

2022-01-29 12:02:49 -08:00
#!/usr/bin/env sh
## Producess NOAA Satellite images from WAV
##
## Usage: __PROG__ [options] wavfile
2022-01-22 15:15:37 -08:00
##
2022-01-29 12:02:49 -08:00
prog="$0"
me=${HELP:-`basename "$prog"`}
rootdir=$(dirname $(realpath $0))
2022-01-29 12:02:49 -08:00
count=0
2022-01-22 15:15:37 -08:00
enhancements="ZA NO MSA MCIR therm"
2022-01-29 12:02:49 -08:00
tle_file="${rootdir}/satellites.tle"
2022-01-22 15:15:37 -08:00
2022-01-29 12:02:49 -08:00
# Lines starting with '##' are intended for usage documentation
function usage() {
grep '^##' "$prog" | sed -e 's/^##\s\?//' -e "s/__PROG__/$me/" 1>&2
}
# Like printf, but prints to stderr with prettier formatting if TTY
function logerr() {
if [ -t 2 ]; then
printf "$(tput setaf 1)ERROR$(tput sgr0) ${1}\n" ${@:2} 1>&2
else
printf "${1}\n" ${@:2} 1>&2
fi
}
# Guesses the timestamp of a file based on the duration and mtime
# This assumes the file was finished writing at the end of a pass
# @param filename
# @output unix timestamp
function guess_timestamp() {
wavfile=${1}
duration=$(printf "%.0f\n" $(soxi -D ${1}))
mtime=$(stat -c %Y ${1})
expr ${mtime} - ${duration}
}
2022-01-29 12:02:49 -08:00
# Creates images from a recorded pass on the filesystem
# @global output_prefix
# @global satellite
# @global timestamp
# @global tle_file
# @global enhancements
# @param wavfile path to file to create images from
# @output one line per file created
function make_images() {
wavfile=${1}
prefix=${output_prefix:-$(basename ${wavfile} .wav)}
# check wavfile exists
if [ ! -f ${wavfile} ]; then
logerr "No such file ${wavfile}"
exit 3
fi
# quietly mkdir -p the output
mkdir -p $(dirname ${prefix})
ts=${timestamp:=$(guess_timestamp $wavfile)}
2022-01-29 12:02:49 -08:00
# if sat and timestamp supplied, generate a map
if [ ! -z "${satellite}" ]; then
2022-01-29 12:02:49 -08:00
mapfile="${prefix}-map.png"
map_flag="-m ${mapfile}"
# working cmd:
# wxmap -T 'NOAA 15' -G . -H satellites.tle 1643720373 map.png
wxmap -T "${satellite}" -H "${tle_file}" -p 0 -l 0 -o ${ts} ${mapfile}
if [ $? -gt 0 ]; then
2022-01-29 12:02:49 -08:00
logerr "Error generating map"
exit 4
fi
echo ${mapfile}
fi
for en in $enhancements; do
imgfile=${prefix}-${en}.png
wxtoimg ${map_flag:- } -e ${en} ${wavfile} ${imgfile}
if [ $? -ne 0 ]; then
logerr "Error generating %s with %s enhancement" "${en}" "${imgfile}"
2022-01-29 12:02:49 -08:00
fi
done
((count++))
}
## Options:
while (( "$#" ));
do
2022-01-22 15:15:37 -08:00
2022-01-29 12:02:49 -08:00
## --help, -h Help message
case "${1}" in
'--help' | '-h')
usage
exit
;;
2022-01-22 15:15:37 -08:00
## --noaa-15 Aliases for --satellite <name>
## --noaa-18
## --noaa-19
--noaa-1[589])
satellite="NOAA $(echo ${1} | grep -oP '[0-9]+')"
;;
## --timestamp <timestamp> Unix timestamp of AOS. (default: based on duration and mtime)
2022-01-29 12:02:49 -08:00
'--timestamp')
# check that $2 exists and is not another flag
if [[ $# -lt 2 ]] || [[ $2 == -* ]] ; then
logerr "Option ${1} requires an argument"
usage
exit 1
fi
if ! [[ "$2" =~ ^[0-9]+$ ]]; then
logerr "Option ${1} must have an integer argument"
usage
exit 1
fi
timestamp=${2}
shift
;;
## --satellite <name> Satellite name. Needed to overlay a map
2022-01-29 12:02:49 -08:00
'--satellite')
# check that $2 exists and is not another flag
if [[ $# -lt 2 ]] || [[ $2 == -* ]] ; then
logerr "Option ${1} requires an argument"
usage
exit 1
fi
satellite=${2}
shift
;;
## --output <prefix> Prefix output with string (default based in input filename)
'--output')
# check that $2 exists and is not another flag
if [[ $# -lt 2 ]] || [[ $2 == -* ]] ; then
logerr "Option ${1} requires an argument"
usage
exit 1
fi
output_prefix=${2}
shift
;;
## --tle <file> Path to tle file (default: satellites.tle)
'--tle')
# check that $2 exists and is not another flag
if [[ $# -lt 2 ]] || [[ $2 == -* ]] ; then
logerr "Option ${1} requires an argument"
usage
exit 1
fi
if ! [[ "$2" =~ ^[0-9]+$ ]]; then
logerr "Option ${1} must have an integer argument"
usage
exit 1
fi
tle_file=${2}
if [ ! -f ${tle_file} ]; then
logerr "Unable to locate tle file: ${tle_file}"
exit 6
fi
shift
;;
*)
# if this is an unrecognized flag, log an error
if [[ $1 == -* ]] ; then
logerr "Unknown option %s. If you meant to process the a file named '%s' use './%s'" ${1} ${1} ${1}
usage
exit 1
fi
make_images ${1}
;;
esac
shift
2022-01-22 15:15:37 -08:00
done
2022-01-29 12:02:49 -08:00
if [[ ${count} -lt 1 ]]; then
logerr "No files processed. Supply the path to one or more wav files"
usage
exit 1
fi
exit # normal exit