Source for file Weather_class.php
Documentation is available at Weather_class.php
* PolarLava::Weather - A class to retrieve XML weather from the NOAA NWS.
* This class requires PHP >= 5.0.0
* @package PolarLavaWeather
* @copyright Copyright 2006-2007 Kevin L. Papendick
* @author Kevin L. Papendick <kevinp@polarlava.com>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* PolarLava::Weather - A class to retrieve XML weather from the NOAA NWS.
* This class requires PHP >= 5.0.0
* @package PolarLavaWeather
* @var string Local XML data cache directory
* @var int Local XML data cache valid time in minutes
* @var int Local XML station data cache valid time in minutes
* @var boolean Error flag
* @var string Error message for user
* @var string Error message for debugging
* @var string Local conditions image directory
* @var string The weather observation station
* @var array weather station data array
* @var string The base weather service URL
* @var array weather data array
* @var string XML weather data
* @link http://www.weather.gov/data/current_obs/
* Weather object constructor
* Accepts an array of configuration parameters as key/value pairs:
* <li>'url' => 'Weather service XML url' default NOAA/NWS</li>
* <li>'obs_station' => 'observation station code' default 'KIAG'</li>
* <li>'image_dir' => 'local image directory' default NULL</li>
* <li>'cache_dir' => 'local cache directory' default NULL</li>
* <li>'cache_time' => 'local cache time in minutes' default 60 minutes</li>
* <li>'cache_time_sid' => 'local cache time in minutes' for station data. Default 10080 (7 days)</li>
* <li>'fetch_weather' => boolean flag, TRUE fetches weather data (default)</li>
* <li>'fetch_stations' => boolean flag, TRUE fetches available weather stations data</li>
* <li>'cache_stations' => boolean flag, TRUE serializes station data to cache</li>
$url =
(array_key_exists('url', $params)) ?
$params['url'] :
'http://www.weather.gov/xml/current_obs/';
$loc =
(array_key_exists('obs_station', $params)) ?
$params['obs_station'] :
'KIAG';
$image_dir =
(array_key_exists('image_dir', $params)) ?
$params['image_dir'] :
NULL;
$cache_dir =
(array_key_exists('cache_dir', $params)) ?
$params['cache_dir'] :
NULL;
$cache_time =
(array_key_exists('cache_time', $params)) ? (int)
$params['cache_time'] :
60;
$cache_time_sid =
(array_key_exists('cache_time_sid', $params)) ? (int)
$params['cache_time_sid'] :
10080; //1 week
$fetch_weather =
(array_key_exists('fetch_weather', $params)) ?
$params['fetch_weather'] :
TRUE;
$fetch_stations =
(array_key_exists('fetch_stations', $params)) ?
$params['fetch_stations'] :
FALSE;
$cache_stations =
(array_key_exists('cache_stations', $params)) ?
$params['cache_stations'] :
FALSE;
$this->setCachetime($cache_time);
$this->setCachetimeSid($cache_time_sid);
if ($cache_stations &&
$cache_dir) {
/***********************/
/***** Set Methods *****/
/***********************/
* Set the local XML data cache directory.
* Set the local XML data cache valid time in minutes.
* Set the local XML station data cache valid time in minutes.
* Set the error flag and message.
* @param string $usr_msg The user error message
* @param string $dbg_msg The debug error message
protected function setError($err, $usr_msg =
NULL, $dbg_msg =
NULL) {
* Set the local conditions image directory.
* Checks and retrieves the local conditions image.
* Set the weather observation station.
* @link http://www.weather.gov/data/current_obs/
* Set the weather data base URL.
* Convert XML weather data to array
* @return boolean success status
foreach (new ArrayObject($values) as $xml_elem) {
foreach (new ArrayObject($xml_elem) as $sub_elem) {
if($xml_elem['type'] !==
'cdata') {
//--- Clean up in isle 3!
//Correct 301 errors for local image download
$this->setError(TRUE, 'Incomplete weather data retrieved.', "Only $size of 43 weather elements appear in the Weather::_weather_array.");
* Convert XML weather station data to array
* @return boolean success status
$cache_file =
$cache_dir .
'stations.dat';
foreach (new ArrayObject($values) as $xml_elem) {
foreach (new ArrayObject($xml_elem) as $sub_elem) {
if($xml_elem['tag'] ==
'STATION_ID') {
$sid =
$xml_elem['value'];
} elseif($xml_elem['tag'] ==
'STATE') {
$state =
$xml_elem['value'];
} elseif($xml_elem['tag'] ==
'STATION_NAME') {
* Serializes weather station data array and saves it to cache_dir
* @return boolean success status
@chmod($cache_file, 0666);
* Set the weather xml data.
protected function setXml($a) {
/***********************/
/***** Get Methods *****/
/***********************/
* Get the local XML data cache directory.
* @return string $this->_cache_dir
* Get the local cache valid time in minutes.
* @return int $this->_cache_time
* Get the local station cache valid time in minutes.
* @return int $this->_cache_time_sid
* Get the error status and messages.
* @return array $this->_error, $this->_error_msg_user, $this->_error_msg_debug
return array('error' =>
$this->_error,
* Get the local conditions image directory.
* @return string $this->_image_dir
* Get the weather observation station.
* @return string $this->_obs_station
* Get the weather service URL.
* @return string $this->_url
* Get the weather data array.
* @return string $this->_weather_array
* Get the weather station data array.
* @return string $this->_station_array
* Get the weather stations from the station data array.
* @param string $state The location's stations.
* @return array $this->_station_array[$state]
* Get a single element from the weather data array.
* @return string $this->_weather_array[$a]
* Get an array of all available data points from the weather data array.
* @return array Array keys from $this->_weather_array.
* Retrieve XML weather data
* @param string $station_id Optional station ID. Default is getObsStation().
* @param int $cache_time Optional cache_time override
* @return boolean success status
protected function _getWeatherXml($station_id =
NULL, $cache_time = -
1) {
$cache_file =
$cache_dir .
$station_id .
'.xml';
$cache_time =
($cache_time === -
1) ?
$this->getCacheTime() :
$cache_time;
if (filemtime($cache_file) +
($cache_time *
60) >=
$now) {
$u =
'An error has occurred retrieving the weather XML data.';
$d =
'cURL Error in Weather::_getXML: ' .
curl_error($curl);
@chmod($cache_file, 0666);
* Retrieve XML weather station data
* @return boolean success status
* Get the weather xml data.
* @return string $this->_xml
/*************************/
/***** Other Methods *****/
/*************************/
* Return an array dump of the (parsed XML) weather data
* Return a dump of the XML data
Documentation generated on Sun, 18 Mar 2007 09:38:32 -0400 by phpDocumentor 1.3.1