Files
dotfiles/.config/xmobar/ens_brief.sh
T

56 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# Next day from ens-forecast for xmobar, e.g. "Sat 19/10° 39%" (today before 18:00, then tomorrow)
# With "week": show the next 7 days in a notification (for a click on the bar)
# Uses IFS unless its newest saved run is STALE; then the first fallback in MODELS whose download names its
# run and isn't STALE. The bar names the model when it isn't IFS, and adds a red "!" when it is stale.
# AIFS/AIGEFS are left out: AIFS comes through the same ECMWF pipeline as IFS and fails with it.
MODELS=(ecmwf_ifs025 icon_seamless_eps ncep_gefs025 google_weathernext2_ensemble)
declare -A LABEL=([ncep_gefs025]=GEFS [icon_seamless_eps]=ICON [google_weathernext2_ensemble]=Google)
# pick DAYS: sets $line (the --brief output) and $model. IFS unless it is STALE; a fallback only if its
# download names its run (so it can be checked) and isn't STALE; otherwise IFS anyway.
pick() {
local m l head first="" first_model="" err=""
for m in "${MODELS[@]}"; do
if ! l=$(ens-forecast --brief "$1" --model "$m" 2>&1); then
[[ -z $err ]] && err=$l
continue
fi
l=$(grep -v '^note: ' <<< "$l") # e.g. "note: ... forecasts only 10 days" (on stderr, models < 14 days)
[[ -z $first ]] && first=$l first_model=$m
head=${l%%: *}
[[ $m != "${MODELS[0]}" && $head != *", run "* ]] && continue # unlabelled fallback: can't check
if [[ $head != *STALE* ]]; then
line=$l model=$m
return 0
fi
done
if [[ -n $first ]]; then line=$first model=$first_model; return 0; fi
line=$err
return 1
}
if [ "$1" = "week" ]; then
if ! pick 7; then
notify-send "ens-forecast" "$line"
exit
fi
body=""
while IFS= read -r part; do
read -r day temps rain extra <<< "$part"
temps=${temps%°C}
body+="$day ${temps%/*}° / ${temps#*/}° rain $rain${extra:+ $extra}"$'\n'
done <<< "$(sed "s/ | /\n/g" <<< "${line#*: }")"
notify-send "${line%%: *}" "${body%$'\n'}"
exit
fi
pick 1 2>/dev/null || exit 0
read -r day temps rain <<< "${line#*: }"
label=""
[[ $model != "${MODELS[0]}" ]] && label="${LABEL[$model]:-$model} "
stale=""
[[ ${line%%: *} == *STALE* ]] && stale=" <fc=#f7768e>!</fc>"
echo "$label$day ${temps%C} "$''" $rain$stale"