-
-
Save bonelifer/b07b836f52b42b969b63f35e96858cf8 to your computer and use it in GitHub Desktop.
NOAA Weather Forecast to PUSHOVER
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once dirname(__FILE__) . '/noaa/Forecaster.php'; #require_once 'noaa/Forecaster.php'; | |
$config = new \noaa\weather\Configuration(); | |
$myWritableCacheDirectory = dirname(__FILE__) . '/cache'; | |
$config->setCache(new \noaa\weather\cache\FileCache($myWritableCacheDirectory)); | |
$forecaster = new \noaa\Forecaster($config); | |
// php MATH: https://www.w3schools.com/php/php_operators.asp | |
// Function to calculate HeatIndex | |
function heatIndex($currTempF,$currentHumidity){ | |
## Function Found at https://github.com/RunnenLate/HeatIndexCalculator/blob/master/HeatIndexCalculator/src/HeatIndexCalculator.java#L38 | |
$c1 = -42.379; | |
$c2 = 2.04901523; | |
$c3 = 10.14333127; | |
$c4 = -0.22475541; | |
$c5 = -0.00683783; | |
$c6 = -0.05481717; | |
$c7 = 0.00122874; | |
$c8 = 0.00085282; | |
$c9 = -0.00000199; | |
$HeatIndex=$c1+ | |
($c2*$currTempF)+ | |
($c3*$currentHumidity)+ | |
($c4*$currTempF*$currentHumidity)+ | |
($c5*($currTempF*$currTempF))+ | |
($c6*($currentHumidity*$currentHumidity))+ | |
($c7*($currTempF*$currTempF)*$currentHumidity)+ | |
($c8*$currTempF*($currentHumidity*$currentHumidity))+ | |
($c9*($currTempF*$currTempF)*($currentHumidity*$currentHumidity)); | |
return $HeatIndex; | |
} | |
// Function to calculate Feels Like Temperature | |
function feelsLikeTemp($currTempF,$currentHumidity,$currentWindMPH){ | |
# Try Wind Chill first | |
if ($currTempF <= 50 and $currentWindMPH >= 3) { | |
$vFeelsLike = 35.74 + (0.6215*$currTempF) - 35.75*($currentWindMPH**0.16) + ((0.4275*$currTempF)*($currentWindMPH**0.16)); | |
} else { | |
$vFeelsLike = $currTempF; | |
} | |
# Replace it with the Heat Index, if necessary | |
if ($vFeelsLike == $currTempF and $currTempF >= 80) { | |
$vFeelsLike = 0.5 * ($currTempF + 61.0 + (($currTempF-68.0)*1.2) + ($currentHumidity*0.094)); | |
} | |
if ($vFeelsLike >= 80) { | |
$vFeelsLike = -42.379 + 2.04901523*$currTempF + 10.14333127*$currentHumidity - .22475541*$currTempF*$currentHumidity - .00683783*$currTempF*$currTempF - .05481717*$currentHumidity*$currentHumidity + .00122874*$currTempF*$currTempF*$currentHumidity + .00085282*$currTempF*$currentHumidity*$currentHumidity - .00000199*$currTempF*$currTempF*$currentHumidity*$currentHumidity; | |
if ($currentHumidity < 13 and $currTempF >= 80 and $currTempF <= 112) { | |
$vFeelsLike = $vFeelsLike - ((13-$currentHumidity)/4)*math.sqrt((17-math.fabs($currTempF-95.))/17); | |
} | |
if ($currentHumidity > 85 and $currTempF >= 80 and $currTempF <= 87) { | |
$vFeelsLike = $vFeelsLike + (($currentHumidity-85)/10) * ((87-$currTempF)/5); | |
} | |
} | |
return $vFeelsLike; | |
} | |
// Begin Change These Varibles | |
$stationId = 'KCXW'; | |
$lat = '35.1509'; | |
$lng = '-92.7441'; | |
$offset = -5; # Your UTC/timezone offset (ex CDT-5/CDT-6) | |
$startTime = date('Y-m-d', time()); | |
$numDays = 1; | |
// End Change These Varibles | |
// Zenith Calculation: https://blog.ip2location.com/knowledge-base/display-sunrise-sunset-time-using-php-and-mysql-database/ | |
// zenith = 90 + (50/60) | |
// zenith = 90.833333333 | |
// | |
// Don't mess with this variable or you'll end up with strange/incorrect Sunrise/Sunset predictions. | |
$zenith = 90.833333333; | |
try { | |
// returns a CurrentWeather object or throws an exception on API error | |
$current = $forecaster->getCurrentWeather($stationId); | |
} catch (\Exception $e) { | |
echo "Error: " . $e->getMessage() . "\n"; | |
} | |
$forecast = $forecaster->getForecastByLatLng($lat, $lng, $startTime, $numDays); | |
$day = $forecast->getDay(0); | |
echo "Current Conditions: " . $day->getConditions() . "\n"; | |
echo "Current Temp: " . $current->getTemperatureF() . "°F\n"; | |
$high = ($day->getHighTemperature() === null) ? 'NULL' : $day->getHighTemperature(); | |
echo "High: " . $high . "°F\n"; | |
$low = ($day->getLowTemperature() === null) ? 'NULL' : $day->getLowTemperature(); | |
echo "Low: " . $low . "°F\n"; | |
echo "Wind Speed: " . $current->getWindSpeedMPH() . " MPH (" . $current->getWindSpeedKnots() . " KT)\n"; | |
echo "Wind Direction: " . $current->getWindDirection() . "\n"; | |
echo "Visibility: " . $current->getVisibilityMiles() . " miles\n"; | |
echo "Humidity: " . $current->getRelativeHumidity() . "%\n"; | |
echo "Dew Point: " . $current->getDewPointF() . "%\n"; | |
# $precipDay = ($day->getPrecipitationProbabilityDay() === null) ? 'NULL' : $day->getPrecipitationProbabilityDay(); | |
# echo "Precipitation Chance Day: " . $precipDay . "%\n"; | |
# $precipNight = ($day->getPrecipitationProbabilityNight() === null) ? 'NULL' : $day->getPrecipitationProbabilityNight(); | |
# echo "Precipitation Chance Night: " . $precipNight . "%\n"; | |
$currTempF = $current->getTemperatureF(); | |
$currentHumidity = $current->getRelativeHumidity(); | |
$currentWindMPH = $current->getWindSpeedMPH(); | |
if ($currTempF >= 80 and $currentHumidity >= 40) { | |
$heatindex = round(heatIndex($currTempF,$currentHumidity),0); | |
echo "Heat Index: " . $heatindex . "°F\n"; | |
} | |
# This calculation can only be made for air temperatures between -50°F (-45°C, 228K) and 50°F (10°C, 283K) only. | |
# https://www.calculator.net/wind-chill-calculator.html?windspeed=6.9&windspeedunit=mph&airtemperature=68&airtemperatureunit=fahrenheit&x=63&y=18 | |
if ($currTempF >= -50 and $currTempF <= 50) { | |
$windChill = 35.74 + 0.6215*$currTempF - 35.75*pow($currentWindMPH, 0.16) + 0.4275*$currTempF*pow($currentWindMPH, 0.16); | |
$windChill = round($windChill,0); | |
echo "Wind Chill: " . $windChill . "°F\n"; | |
} | |
$vFeelsLike = round(feelsLikeTemp($currTempF,$currentHumidity,$currentWindMPH),0); | |
echo "Feels Like: " . $vFeelsLike . "°F\n"; | |
$hazards = $forecast->getHazards(); | |
if (count($hazards) > 0) { | |
echo "HAZARDS: " . implode(', ', $hazards) . "\n"; | |
} | |
echo("Sunrise: " . date("F j, Y") . " at "); | |
$sunrise_time = date_sunrise(time(),SUNFUNCS_RET_STRING,$lat,$lng,$zenith,$offset); | |
echo date('g:i', strtotime($sunrise_time)); | |
echo("AM"); | |
echo("\nSunset: " . date("F j, Y") . " at "); | |
$sunset_time = date_sunset(time(),SUNFUNCS_RET_STRING,$lat,$lng,$zenith,$offset); | |
echo date('g:i', strtotime($sunset_time)); | |
echo("PM\n"); | |
echo "\nLocation: ". $current->getStationId() . " -- " . $current->getLocation(); | |
echo("\nTime Observed: "); | |
$timeChecked = ($current->getObservationTime()); | |
echo date('F j, Y g:iA', strtotime($timeChecked)); | |
echo ("\n"); | |
echo "Time Checked: "; | |
$startChecked = date('F j, Y g:iA', time()); | |
echo (" " . $startChecked); | |
// WRITE URL to url.txt | |
// This Bitly URL is the NWS forecast page for my local station, you'll | |
// need to create your own Bitly URL or at least replace this with the | |
// NWS url directly. You'll see this when you click on the notification | |
// in the phone app. This page will show longer range forcasts for | |
// your area. | |
$myURL = fopen("url.txt", "w") or die("Unable to open file!"); | |
$URL = "https://bit.ly/3hIyJOj\n"; | |
fwrite($myURL, $URL); | |
fclose($myURL); | |
// END WRITE URL to url.txt | |
// WRITE TITLE to title.txt | |
// This outputs the title for the Pushover message. | |
$todaysDate = date('F j Y', time()); | |
$todaysSunrise = date('g:i', strtotime($sunrise_time)) . "AM"; | |
$myTITLE = fopen("title.txt", "w") or die("Unable to open file!"); | |
$title = "Todays sunrise - $todaysDate at $todaysSunrise"; | |
fwrite($myTITLE, $title); | |
fclose($myTITLE); | |
// END WRITE TITLE to title.txt | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment