[Phphtmllib-devel] SF.net SVN: phphtmllib:[3518] trunk/open2300/lib/modules/api/ AviationWeather.in
Status: Beta
Brought to you by:
hemna
From: <he...@us...> - 2010-06-26 00:18:05
|
Revision: 3518 http://phphtmllib.svn.sourceforge.net/phphtmllib/?rev=3518&view=rev Author: hemna Date: 2010-06-26 00:17:58 +0000 (Sat, 26 Jun 2010) Log Message: ----------- stuff Modified Paths: -------------- trunk/open2300/lib/modules/api/AviationWeather.inc Modified: trunk/open2300/lib/modules/api/AviationWeather.inc =================================================================== --- trunk/open2300/lib/modules/api/AviationWeather.inc 2010-06-21 21:16:01 UTC (rev 3517) +++ trunk/open2300/lib/modules/api/AviationWeather.inc 2010-06-26 00:17:58 UTC (rev 3518) @@ -53,22 +53,24 @@ public function process_airport($icao) { - + $icao = strtoupper($icao); + $cache_key = "METAR_".$icao; $info = $this->cache->get($cache_key); - if ($info && false) { + if ($info) { $wxInfo = $info; return $wxInfo; } - $wxInfo = array("icao" => strtoupper($icao)); + $airport = $this->cache->get($icao); + + $wxInfo = array("icao" => $icao); $metar = $this->get_metar($icao, $wxInfo); if ($metar != null && strlen($metar) > 0) { $this->process_metar($metar, $wxInfo); $wxInfo["metar"] = $metar; - $airport = $this->cache->get($icao); if ($airport != null) { $wxInfo["runways"] = $this->get_runways($airport, $wxInfo); $wxInfo["altitude"] = $airport["elevation"]; @@ -86,6 +88,32 @@ //now do Pressure Altitude and Density Altitude $wxInfo["PressureAltitude"] = $this->pressure_altitude($wxInfo["BaroHPA"], $wxInfo["altitude"]); } + + if (is_null($wxInfo["images"]) || !isset($wxInfo["images"])) { + $images = $this->do_images($icao); + if ($images) { + $wxInfo["images"] = $images; + } + } + + if (!isset($airport["taf"]) || $airport["taf"] == 1) { + $taf = $this->get_taf($icao); + + if (is_null($taf)) { + //if we couldn't find the TAF + //no reason to try again. + //not many airports have taf reports + $airport["taf"] = 0; + } else { + $airport["taf"] = 1; + $wxInfo["taf"] = $taf; + } + + //we changed settings, save them to the cache. + $this->cache->set($icao,$airport); + } + + //$this->metars[] = $wxInfo; //need to combine this info w/ the airport data cache. } @@ -185,6 +213,39 @@ return $metar; } + public function get_taf($station) { + + // This function retrieves TAF information for a given station from the + // National Weather Service. It assumes that the station exists. + // A slower URL is "ftp://weather.noaa.gov/data/forecasts/taf/stations/$station.TXT" + $fileName = "http://weather.noaa.gov/pub/data/forecasts/taf/stations/$station.TXT"; + $taf = ''; + + + $proxy = $GLOBALS["config"]->get("proxy"); + if (!empty($proxy)) { + $context = stream_context_create(array("http" => array("proxy" => "tcp://".$proxy))); + } else { + $context = null; + } + + $fileData = @file($fileName, 0, $context); // or die('Data not available'); + if ($fileData != false) { + list($i, $date) = each($fileData); + $utc = strtotime(trim($date)); + $this->set_time_data($utc,$wxInfo); + while (list($i, $line) = each($fileData)) { + //$taf .= ' ' . trim($line); + $taf .= ' ' . $line; + } + $taf = trim(str_replace(' ', ' ', $taf)); + return $taf; + } else { + return null; + } + + } + protected function set_time_data($utc, &$wxInfo) { // This function formats observation time in the local time zone of server, the // current local time on server, and time difference since observation. $utc is a @@ -649,7 +710,90 @@ - + protected function do_images($icao) { + $airport_images = array(); + $airportdiagram = ""; + + //first lets see if we can find some airport images. + $flyagogo_url = 'http://flyagogo.net/uploads/'; + + for ($x=1; $x<=3; $x++) { + $result = $this->http_head_curl($flyagogo_url.$icao."/".$x.".jpg"); + if ($result !== false && $result["http_code"] == 200) { + $airport_images[] = $result["url"]; + } else { + //if n doesn't work then n+1 won't either + break; + } + } + + //lets see if we can snag an airport diagram + $page = $this->http_get_curl("http://airnav.com/airport/".$icao); + if ($page !== false) { + //first snag the airport image itself. + $str = strstr($page,"http://img.airnav.com/ap/"); + if ($str) { + $image_url = substr($str,0,strpos($str, '"')); + $airport_images[] = $image_url; + } + + + //now the airport diagram. + $str = strstr($page,"http://img.airnav.com/aptdiag"); + if ($str) { + $airportdiagram = substr($str,0,strpos($str, '"')); + } + } + + + $images = array("images" => $airport_images, "diagram" => $airportdiagram); + return $images; + } + + /** + * @return boolean false on error, content otherwise + * @param string $url + * @desc makes a HEAD request using cURL + * @author Svetoslav Marinov + * @link http://devquickref.com/ + */ + function http_head_curl($url) { + if (!extension_loaded('curl_init') || !function_exists('curl_init')) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 30s + curl_setopt($ch, CURLOPT_HEADER, 1); + curl_setopt($ch, CURLOPT_NOBODY, 1); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($ch); + $info = curl_getinfo($ch); + curl_close($ch); + return $info; + } + return false; + } + + + function http_get_curl($url) { + if (!extension_loaded('curl_init') || !function_exists('curl_init')) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 30s + curl_setopt($ch, CURLOPT_HEADER, 1); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $result = curl_exec($ch); + $info = curl_getinfo($ch); + curl_close($ch); + if ($info["http_code"] == 200) { + return $result; + } else { + return false; + } + } + return false; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |