#!/usr/bin/php -q

<?php

// Warn if low is expected at or below this threshold

$LOW_WARN_THRESHOLD = 32 ;

// the Degree symbol, just to make referencing it easier

$DEG = chr ( 176 ) ;

// base url for weather xml feed

$url_base = "http://forecast.weather.gov/MapClick.php?" ;

// who to notify about what locations

// location is the lat&lon arguments from a weather.gov URL

// e.g., from "http://forecast.weather.gov/MapClick.php?lat=34.0995&lon=-118.414",

// you would use "lat=34.0995&lon=-118.414"

$to_notify = array ( ) ;

$to_notify [ ] = array ( "email" => "user1@gmail.com" , "url_loc" => "lat=42.8018&lon=-73.9281" ) ;

$to_notify [ ] = array ( "email" => "user2@gmail.com" , "url_loc" => "lat=34.0995&lon=-118.414" ) ;

// Pull the xml weather feed from weather.gov and extract the high and low

// forecasts as an array matched up with day of week, and return the array

function get_forecast ( $url_loc ) {

global $url_base ;

// fetch the weather feed xml

$xmlstr = file_get_contents ( $url_base . $url_loc . "&FcstType=dwml" ) ;

// convert the xml string into an xml object

$xmlobj = new SimpleXMLElement ( $xmlstr ) ;

// extract the temperature xml object

$temps = $xmlobj -> data -> parameters -> temperature ;

foreach ( $xmlobj -> data -> parameters -> temperature as $temps ) {

if ( $temps -> name == "Daily Minimum Temperature" ) {

foreach ( $temps -> value as $val ) {

$min_temps [ ] = ( int ) $val ;

}

} else if ( $temps -> name == "Daily Maximum Temperature" ) {

foreach ( $temps -> value as $val ) {

$max_temps [ ] = ( int ) $val ;

}

}

}

// build a unified array of day/min/max tuples

for ( $i = 0 ; $i < sizeof ( $min_temps ) ; $i ++ ) {

$forecast [ $i ] [ "dayName" ] = date ( "l" , strtotime ( "+ $i days" ) ) ;

$forecast [ $i ] [ "min" ] = $min_temps [ $i ] ;

$forecast [ $i ] [ "max" ] = $max_temps [ $i ] ;

}

return $forecast ;

}

// For each email/location pair, pull

foreach ( $to_notify as $info ) {

$email = $info [ "email" ] ;

$url_loc = $info [ "url_loc" ] ;

$forecast = get_forecast ( $url_loc ) ;

$subject = "" ;

$body = "" ;

foreach ( $forecast as $temps ) {

$day = $temps [ "dayName" ] ;

$min = $temps [ "min" ] ;

$max = $temps [ "max" ] ;

if ( $min <= $LOW_WARN_THRESHOLD ) {

if ( $subject == "" ) {

$subject = "Frost expected $day night!

" ;

}

$body .= "Low of $min " . $DEG . " expected $day night!

" ;

}

}

// if the subject is not empty, then there's upcoming frost,

// so send an email alert about it

if ( $subject != "" ) {

// write up a summary of upcoming high/low temps

$body .= "



Upcoming forecast:

" ;

$body .= " \t \t High \t Low

" ;

foreach ( $forecast as $i => $temps ) {

$min = $temps [ "min" ] . $DEG ;

$max = $temps [ "max" ] . $DEG ;

if ( $i == 0 ) {

$day = "Today" ;

} else if ( $i == 1 ) {

$day = "Tomorrow" ;

} else {

$day = $temps [ "dayName" ] ;

}

if ( strlen ( $day ) <= 7 ) {

$day .= " \t " ;

}

$body .= " $day \t $max \t $min

" ;

} // done with temp table

$body .= "

Full forecast available here: " . $url_base . $url_loc

. "&FcastType=text" ;

// send the alert

mail ( $email , $subject , $body ) ;

}

}