#!/bin/bash # LinBPQ Weather & Alerts - OpenWeather One Call 3.0 # Updated with Wind Speed and Cardinal Direction API_KEY="YOUR API CODE HERE" UNITS="imperial" DELAY=1 # RX delay for packet modems # Function to convert degrees to cardinal direction get_cardinal() { local deg=$1 if [ "$deg" == "null" ]; then echo "Unknown"; return; fi directions=("N" "NE" "E" "SE" "S" "SW" "W" "NW" "N") # Divide degrees by 45 and round to find index in array index=$(echo "($deg + 22.5) / 45" | bc) echo "${directions[$index]}" } echo "--- OpenWeather Alert & Wind Service ---"; sleep $DELAY echo -n "Enter Zip Code: " read ZIP # Clean input ZIP=$(echo $ZIP | tr -d '\r' | xargs) if [[ $ZIP =~ ^[0-9]{5}$ ]]; then echo "Processing request for $ZIP..."; sleep $DELAY # Step 1: Geocoding GEO_DATA=$(curl -s "http://api.openweathermap.org/geo/1.0/zip?zip=$ZIP,US&appid=$API_KEY") LAT=$(echo "$GEO_DATA" | jq -r '.lat') LON=$(echo "$GEO_DATA" | jq -r '.lon') NAME=$(echo "$GEO_DATA" | jq -r '.name') if [ "$LAT" != "null" ]; then # Step 2: Fetch Data WX_DATA=$(curl -s "https://api.openweathermap.org/data/3.0/onecall?lat=$LAT&lon=$LON&exclude=minutely,hourly,daily&appid=$API_KEY&units=$UNITS") # Extract Current Data including Wind TEMP=$(echo "$WX_DATA" | jq -r '.current.temp') DESC=$(echo "$WX_DATA" | jq -r '.current.weather[0].description') WIND_SPD=$(echo "$WX_DATA" | jq -r '.current.wind_speed') WIND_DEG=$(echo "$WX_DATA" | jq -r '.current.wind_deg') CARDINAL=$(get_cardinal "$WIND_DEG") echo ""; sleep $DELAY echo "Location: $NAME ($ZIP)"; sleep $DELAY echo "Current: $DESC, $TEMP°F"; sleep $DELAY echo "Wind: $WIND_SPD mph from the $CARDINAL ($WIND_DEG°)"; sleep $DELAY # Step 3: Alerts ALERTS=$(echo "$WX_DATA" | jq -r '.alerts[]? | "\(.event): \(.description)"') if [ -n "$ALERTS" ]; then echo "--- ACTIVE ALERTS ---"; sleep $DELAY echo "$ALERTS" | while read -r line; do echo "$line"; sleep $DELAY done else echo "No active weather alerts."; sleep $DELAY fi else echo "Error: Zip code not found."; sleep $DELAY fi else echo "Error: Please enter a valid 5-digit Zip Code."; sleep $DELAY fi sleep 2 exit