River Gage Bot

From Pabut
Revision as of 13:24, 3 September 2021 by Pabut (talk | contribs) (Created page with "This script checks the USGS waterdata site (https://waterdata.usgs.gov/) and sends a group me message when a gage height exceeds a certain level. ''(No I'm not misspelling gu...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This script checks the USGS waterdata site (https://waterdata.usgs.gov/) and sends a group me message when a gage height exceeds a certain level. (No I'm not misspelling guage, on the USGS site that's how they spell it. Turns out google says it's an accepted alternative spelling. You learn something new everyday)

The script is called:

./river_guage.bash <USGS SITE ID> <limit in feet> <path to the groupme script> <groupme bot id>

Example:

./river_guage.bash 01443500 0 /tmp/post-group-me.bash 123456789abcd12345

The Bot will post text in the group that resembles:

Gage Height for Paulins Kill at Blairstown NJ at 2021-09-03 12:15 EDT is 5.16 feet.

you can set this up in a cron job that looks like this:

15 04,10,16,22 * * * /home/user/river_guage.bash 01443500 5 /home/roschews/post-group-me.bash 123456789abcd12345

In this case it will execute every 6 hours at 15 min past the hour and only send a message if the level exceeds 5 feet.

#!/bin/bash
###
# $1 = USGS Site ID
# $2 = Limit in feet
# $3 = Path to groupme bot script
# $4 = GroupMe Bot ID
###

if [ "$#" -ne 4 ]; then
    echo "Illegal number of parameters"
    echo "     $0 <SITE ID> <feet> <groupme script> <bot id>"
    exit
fi

SITE=$1
LIMIT=$2
BOT_SCRIPT=$3
BOT_ID=$4


rm -f /tmp/${SITE}.dat
URL="https://waterdata.usgs.gov/nj/nwis/uv?cb_00065=on&format=rdb&site_no=${SITE}&period=1"
wget -q -O /tmp/${SITE}.dat $URL 

# decode the gage height first to see if we need to bother doing anything else
# Sorry BASH limits us to INTEGER precision 
GAGE_HEIGHT=$(sed -ne '$p' /tmp/${SITE}.dat | cut -f5)
GAGE_INTEGER=$(echo ${GAGE_HEIGHT} | echo $((10#$(sed -e 's/\.[0-9]\+//'))))

if [ $GAGE_INTEGER -ge ${LIMIT} ]; then

        SITE_TEXT=$(sed -ne '/Data for the following/{N;s/.* [0-9]\{8\}//p}' /tmp/${SITE}.dat)
        DATETIME=$(sed -ne '$p' /tmp/${SITE}.dat | cut -f3)
        DATETIME_ZONE=$(sed -ne '$p' /tmp/${SITE}.dat | cut -f4)
        MSG_TEXT="Gage Height for${SITE_TEXT} at ${DATETIME} ${DATETIME_ZONE} is ${GAGE_HEIGHT} feet."
        ${BOT_SCRIPT} "${MSG_TEXT}" "${BOT_ID}"
fi