cvs: pear /Services_Weather/examples metar-extensive.php
| From: | Alexander Wirtz | Date: | Fri, 09 Jul 2004 13:00:13 +0000 |
| Subject: | cvs: pear /Services_Weather/examples metar-extensive.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-21677@lists.php.net to get a copy of this message | ||
eru Fri Jul 9 09:00:13 2004 EDT
Added files:
/pear/Services_Weather/examples metar-extensive.php
Log:
Initial version
http://cvs.php.net/co.php/pear/Services_Weather/examples/metar-extensive.php?r=1.1&p=1 Index: pear/Services_Weather/examples/metar-extensive.php +++ pear/Services_Weather/examples/metar-extensive.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Alexander Wirtz <alex@pc4p.net> | // +----------------------------------------------------------------------+ // // $Id$ /* * Well, this is a more elaborate example how to create a neat little page * with fetching METAR/TAF data from noaa.org and putting it into a design. * I'm not too proud of my design-skills, most of the stuff here is taken * from the metar-block which can be found within the Horde Framework, * courtesy of Rick Emery - creative pixelshoving isn't my domain :-P * I've used a Firefox for checking the design, so don't be too * disappointed, if the page looks shabby with with the IE, not that I care * very much anyway ;-) * Have fun! */ //------------------------------------------------------------------------- // This is the area, where you can customize the script //------------------------------------------------------------------------- $location = "Kennedy International Airport"; // The city we want to fetch the data for. // Where the search function will look for // the ICAO database (generated with the // buildMetarDB.php script) $dsn = "sqlite://localhost/D:/php/pear/data/Services_Weather/servicesWeatherDB"; $sourceMetar = "file"; // This script will pull the data via http $sourceTaf = "file"; // This script will pull the data via http $sourcePathMetar = "E:/noaa/metar/"; $sourcePathTaf = "E:/noaa/taf/"; $cacheType = ""; // Set a type (file, db, mdb, ...) to // enable caching. $cacheOpt = array(); // Cache needs various options, depending // on the container-type - please consult // the Cache manual / sourcecode! $unitsFormat = "metric"; // The format the units are displayed in - // metric, standard or some customization. $dateFormat = "j. M Y"; // Set the format the date is displayed in $timeFormat = "H:i"; // Set the format the time is displayed in //------------------------------------------------------------------------- // Load the Weather class require_once "Services/Weather.php"; // Object initialization - error checking is important, because of // handling exceptions such as missing PEAR modules $metar = &Services_Weather::service("Metar"); if (Services_Weather::isError($metar)) { die("Error: ".$metar->getMessage()."\n"); } // Set parameters for DB access, needed for location searches $metar->setMetarDB($dsn); if (Services_Weather::isError($metar)) { echo "Error: ".$metar->getMessage()."\n"; } // Initialize caching if (strlen($cacheType)) { $status = $metar->setCache($cacheType, $cacheOpt); if (Services_Weather::isError($status)) { echo "Error: ".$status->getMessage()."\n"; } } // Define the units format, bring the retrieved format into // something more common... $metar->setUnitsFormat($unitsFormat); $units = $metar->getUnitsFormat(); $units["temp"] = "°".strtoupper($units["temp"]); $units["wind"] = " ".str_replace("kmh", "km/h", $units["wind"]); $units["vis"] = " ".$units["vis"]; $units["height"] = " ".$units["height"]; $units["pres"] = " ".$units["pres"]; $units["rain"] = " ".$units["rain"]; $metar->setMetarSource($sourceMetar, $sourcePathMetar, $sourceTaf, $sourcePathTaf); // Set date-/time-format $metar->setDateTimeFormat($dateFormat, $timeFormat); // Search for defined location and fetch the first item found. // Bail out if something bad happens... $search = $metar->searchLocation($location, true); if (Services_Weather::isError($search)) { die("Error: ".$search->getMessage()."\n"); } // Retrieve data, store in variables, bail out on error $fetch = array( "location" => "getLocation", "weather" => "getWeather", "forecast" => "getForecast" ); foreach ($fetch as $variable => $function) { $$variable = $metar->$function($search); if (Services_Weather::isError($$variable)) { echo "Error: ".$$variable->getMessage()."\n"; continue; } } // Now we output all the data, please don't expect extensive comments here, this is basic // HTML/CSS stuff. Also this isn't a very fancy design, it's just to show you, what // the script is able to do (and more ;-))... ?> <html> <head> <title>Services_Weather::Weatherdotcom</title> <style type="text/css"> .normal { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal } .italic { font-weight: normal; font-style: italic } .bold { font-weight: bold; font-style: normal } .bolditalic { font-weight: bold; font-style: italic } .redbold { font-weight: bold; font-style: normal; color: #ff0000 } .bluebold { font-weight: bold; font-style: normal; color: #0000ff } .bggrey { background-color: #e9e9e9 } .bgkhaki { background-color: #d8d8c0 } .reg { font-size: 7pt; vertical-align: super } img { vertical-align: middle; border-style: none; border-width: 0px } a { font-weight: bold; font-style: italic; color: #993300; text-decoration: none } a:visited { font-weight: bold; font-style: italic; color: #993300; text-decoration: none } a:hover { font-weight: bold; font-style: italic; color: #cc3300; text-decoration: underline } table { border: 0px none black; border-spacing: 0px } td { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal } </style> </head> <body class="normal"> <?php // Debug outputs the raw data fetched by the foreach-loop above, just for checking... if (isset($_GET["debug"])) { echo "<pre>\n"; var_dump($location, $weather, $forecast); echo "</pre>\n"; } ?> <span class="bluebold" style="font-size: 13pt">Weather Forecast</span> created with <a style="font-size: 13pt" href="http://pear.php.net/">PEARs</a> <a style="font-size: 13pt" href="http://pear.php.net/package/Services_Weather/">Services_Weather</a><br> <table width="100%"> <tr> <td> <table border="1" style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0" width="100%"> <tr class="bgkhaki"> <td colspan="4" style="border-bottom: 2px solid #abada2"><span class="bold"><?=$location["name"]?> (<?=$search?>)</span></td> </tr> <tr> <td colspan="2" width="270"><span class="bold">Last updated:</span> <?=$weather["update"]?><br> </td> <td width="170"> </td> <td> </td> </tr> <tr> <td><span class="bold">Temperature:</span> <?=round($weather["temperature"], 1).$units["temp"]?></td> <td><span class="bold">Dew point:</span> <?=round($weather["dewPoint"], 1).$units["temp"]?></td> <td><span class="bold">Felt temperature:</span> <?=round($weather["feltTemperature"], 1).$units["temp"]?></td> <td rowspan="4" valign="top"> <?php if (isset($weather["remark"]) && sizeof($weather["remark"])) { ?> <span class="bold">Remarks:</span><br> <?php foreach($weather["remark"] as $key => $val) { switch ($key) { case "autostation": $string = ""; $value = $val; break; case "seapressure": $string = "Pressure at sealevel:"; $value = round($val, 1).$units["pres"]; break; case "presschg": $string = ""; $value = $val; break; case "1htemp": $string = "Temperature for last hour:"; $value = round($val, 1).$units["temp"]; break; case "1hdew": $string = "Dew Point for last hour:"; $value = round($val, 1).$units["temp"]; break; default: var_dump($key, $val); break; } ?> <?=$string?> <?=$value?><br> <?php } ?> </td> </tr> <tr> <td colspan="2"><span class="bold">Pressure:</span> <?=round($weather["pressure"], 1).$units["pres"]?></td> <td><span class="bold">Humidity:</span> <?=$weather["humidity"]?>%</td> </tr> <tr> <td colspan="2"><span class="bold">Wind:</span> <?=strtolower($weather["windDirection"]) == "calm" ? "Calm" : "From the ".$weather["windDirection"]." (".$weather["windDegrees"]."°) at ".round($weather["wind"], 1).$units["wind"]?></td> <td><span class="bold">Visibility:</span> <?=strtolower($weather["visQualifier"])?> <?=round($weather["visibility"], 1).$units["vis"]?></td> </tr> <tr> <td colspan="2" valign="top"> <span class="bold">Current condition:</span><br> <?=isset($weather["condition"]) ? ucwords($weather["condition"]) : "No Significant Weather"?> <?php if (isset($weather["precipitation"]) && sizeof($weather["precipitation"])) { ?> <br><span class="bold">Precipitation:</span><br> <?php $precip = "last"; for ($i = 0; $i < sizeof($weather["precipitation"]); $i++) { $precip .= " ".$weather["precipitation"][$i]["hours"]."h: ".$weather["precipitation"][$i]["amount"].$units["rain"]; ?> <?=$precip?> <?php } } ?> </td> <td valign="top"> <span class="bold">Clouds:</span><br> <?php if (isset($weather["clouds"]) && sizeof($weather["clouds"])) { for ($i = 0; $i < sizeof($weather["clouds"]); $i++) { $cloud = ucwords($weather["clouds"][$i]["amount"]); if (isset($weather["clouds"][$i]["type"])) { $cloud .= " ".$weather["clouds"][$i]["type"]; } if (isset($weather["clouds"][$i]["height"])) { $cloud .= " at ".$weather["clouds"][$i]["height"].$units["height"]; } ?> <?=$cloud?><br> <?php } } else { ?> Clear Below <?=$metar->convertDistance(12000, "ft", $units["height"]).$units["height"]?> <?php } ?> </td> </tr> </table> </td> </tr> <tr> <td> <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0" width="100%"> <tr class="bgkhaki"> <td align="center" style="border-bottom: 2px solid #abada2"><span class="bold">Forecast (TAF)</span></td> </tr> <tr valign="top"> <td> <table width="100%" class="bgkhaki" style="border-top: 2px solid #d8d8c0; border-bottom: 2px solid #d8d8c0; border-left: 2px solid #d8d8c0; border-right: 2px solid #8b87a0"> <tr> <td align="center" style="height:15px"> </td> <tr> <td align="center" style="height:45px"><span class="bold">Temperature</span> <span class="redbold">High</span> / <span class="bluebold">Low</span></td> </tr> <tr> <td align="center" style="height:15px"> </td> </tr> <tr> <td align="center" style="height:75px"><span class="bold">Condition</span></td> </tr> <tr> <td align="center" style="height:45px"><span class="bold">Precipitation probability</span></td> </tr> <tr> <td align="center" style="height:45px"><span class="bold">Wind</span></td> </tr> <tr> <td align="center" style="height:15px"><span class="bold">Humidity</span></td> </tr> </table> </td> </tr> <tr class="bgkhaki"> <td style="border-top: 2px solid #abada2"> </td> </tr> </table> </td> </tr> </table> <a href="javascript:history.back()">back</a> </body> </html>
http://cvs.php.net/co.php/pear/Services_Weather/examples/metar-extensive.php?r=1.1&p=1 Index: pear/Services_Weather/examples/metar-extensive.php +++ pear/Services_Weather/examples/metar-extensive.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Alexander Wirtz <alex@pc4p.net> | // +----------------------------------------------------------------------+ // // $Id$ /* * Well, this is a more elaborate example how to create a neat little page * with fetching METAR/TAF data from noaa.org and putting it into a design. * I'm not too proud of my design-skills, most of the stuff here is taken * from the metar-block which can be found within the Horde Framework, * courtesy of Rick Emery - creative pixelshoving isn't my domain :-P * I've used a Firefox for checking the design, so don't be too * disappointed, if the page looks shabby with with the IE, not that I care * very much anyway ;-) * Have fun! */ //------------------------------------------------------------------------- // This is the area, where you can customize the script //------------------------------------------------------------------------- $location = "Kennedy International Airport"; // The city we want to fetch the data for. // Where the search function will look for // the ICAO database (generated with the // buildMetarDB.php script) $dsn = "sqlite://localhost/D:/php/pear/data/Services_Weather/servicesWeatherDB"; $sourceMetar = "file"; // This script will pull the data via http $sourceTaf = "file"; // This script will pull the data via http $sourcePathMetar = "E:/noaa/metar/"; $sourcePathTaf = "E:/noaa/taf/"; $cacheType = ""; // Set a type (file, db, mdb, ...) to // enable caching. $cacheOpt = array(); // Cache needs various options, depending // on the container-type - please consult // the Cache manual / sourcecode! $unitsFormat = "metric"; // The format the units are displayed in - // metric, standard or some customization. $dateFormat = "j. M Y"; // Set the format the date is displayed in $timeFormat = "H:i"; // Set the format the time is displayed in //------------------------------------------------------------------------- // Load the Weather class require_once "Services/Weather.php"; // Object initialization - error checking is important, because of // handling exceptions such as missing PEAR modules $metar = &Services_Weather::service("Metar"); if (Services_Weather::isError($metar)) { die("Error: ".$metar->getMessage()."\n"); } // Set parameters for DB access, needed for location searches $metar->setMetarDB($dsn); if (Services_Weather::isError($metar)) { echo "Error: ".$metar->getMessage()."\n"; } // Initialize caching if (strlen($cacheType)) { $status = $metar->setCache($cacheType, $cacheOpt); if (Services_Weather::isError($status)) { echo "Error: ".$status->getMessage()."\n"; } } // Define the units format, bring the retrieved format into // something more common... $metar->setUnitsFormat($unitsFormat); $units = $metar->getUnitsFormat(); $units["temp"] = "°".strtoupper($units["temp"]); $units["wind"] = " ".str_replace("kmh", "km/h", $units["wind"]); $units["vis"] = " ".$units["vis"]; $units["height"] = " ".$units["height"]; $units["pres"] = " ".$units["pres"]; $units["rain"] = " ".$units["rain"]; $metar->setMetarSource($sourceMetar, $sourcePathMetar, $sourceTaf, $sourcePathTaf); // Set date-/time-format $metar->setDateTimeFormat($dateFormat, $timeFormat); // Search for defined location and fetch the first item found. // Bail out if something bad happens... $search = $metar->searchLocation($location, true); if (Services_Weather::isError($search)) { die("Error: ".$search->getMessage()."\n"); } // Retrieve data, store in variables, bail out on error $fetch = array( "location" => "getLocation", "weather" => "getWeather", "forecast" => "getForecast" ); foreach ($fetch as $variable => $function) { $$variable = $metar->$function($search); if (Services_Weather::isError($$variable)) { echo "Error: ".$$variable->getMessage()."\n"; continue; } } // Now we output all the data, please don't expect extensive comments here, this is basic // HTML/CSS stuff. Also this isn't a very fancy design, it's just to show you, what // the script is able to do (and more ;-))... ?> <html> <head> <title>Services_Weather::Weatherdotcom</title> <style type="text/css"> .normal { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal } .italic { font-weight: normal; font-style: italic } .bold { font-weight: bold; font-style: normal } .bolditalic { font-weight: bold; font-style: italic } .redbold { font-weight: bold; font-style: normal; color: #ff0000 } .bluebold { font-weight: bold; font-style: normal; color: #0000ff } .bggrey { background-color: #e9e9e9 } .bgkhaki { background-color: #d8d8c0 } .reg { font-size: 7pt; vertical-align: super } img { vertical-align: middle; border-style: none; border-width: 0px } a { font-weight: bold; font-style: italic; color: #993300; text-decoration: none } a:visited { font-weight: bold; font-style: italic; color: #993300; text-decoration: none } a:hover { font-weight: bold; font-style: italic; color: #cc3300; text-decoration: underline } table { border: 0px none black; border-spacing: 0px } td { font-family: Arial, Helvetica, sans-serif; font-size: 11pt; font-weight: normal; font-style: normal } </style> </head> <body class="normal"> <?php // Debug outputs the raw data fetched by the foreach-loop above, just for checking... if (isset($_GET["debug"])) { echo "<pre>\n"; var_dump($location, $weather, $forecast); echo "</pre>\n"; } ?> <span class="bluebold" style="font-size: 13pt">Weather Forecast</span> created with <a style="font-size: 13pt" href="http://pear.php.net/">PEARs</a> <a style="font-size: 13pt" href="http://pear.php.net/package/Services_Weather/">Services_Weather</a><br> <table width="100%"> <tr> <td> <table border="1" style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0" width="100%"> <tr class="bgkhaki"> <td colspan="4" style="border-bottom: 2px solid #abada2"><span class="bold"><?=$location["name"]?> (<?=$search?>)</span></td> </tr> <tr> <td colspan="2" width="270"><span class="bold">Last updated:</span> <?=$weather["update"]?><br> </td> <td width="170"> </td> <td> </td> </tr> <tr> <td><span class="bold">Temperature:</span> <?=round($weather["temperature"], 1).$units["temp"]?></td> <td><span class="bold">Dew point:</span> <?=round($weather["dewPoint"], 1).$units["temp"]?></td> <td><span class="bold">Felt temperature:</span> <?=round($weather["feltTemperature"], 1).$units["temp"]?></td> <td rowspan="4" valign="top"> <?php if (isset($weather["remark"]) && sizeof($weather["remark"])) { ?> <span class="bold">Remarks:</span><br> <?php foreach($weather["remark"] as $key => $val) { switch ($key) { case "autostation": $string = ""; $value = $val; break; case "seapressure": $string = "Pressure at sealevel:"; $value = round($val, 1).$units["pres"]; break; case "presschg": $string = ""; $value = $val; break; case "1htemp": $string = "Temperature for last hour:"; $value = round($val, 1).$units["temp"]; break; case "1hdew": $string = "Dew Point for last hour:"; $value = round($val, 1).$units["temp"]; break; default: var_dump($key, $val); break; } ?> <?=$string?> <?=$value?><br> <?php } ?> </td> </tr> <tr> <td colspan="2"><span class="bold">Pressure:</span> <?=round($weather["pressure"], 1).$units["pres"]?></td> <td><span class="bold">Humidity:</span> <?=$weather["humidity"]?>%</td> </tr> <tr> <td colspan="2"><span class="bold">Wind:</span> <?=strtolower($weather["windDirection"]) == "calm" ? "Calm" : "From the ".$weather["windDirection"]." (".$weather["windDegrees"]."°) at ".round($weather["wind"], 1).$units["wind"]?></td> <td><span class="bold">Visibility:</span> <?=strtolower($weather["visQualifier"])?> <?=round($weather["visibility"], 1).$units["vis"]?></td> </tr> <tr> <td colspan="2" valign="top"> <span class="bold">Current condition:</span><br> <?=isset($weather["condition"]) ? ucwords($weather["condition"]) : "No Significant Weather"?> <?php if (isset($weather["precipitation"]) && sizeof($weather["precipitation"])) { ?> <br><span class="bold">Precipitation:</span><br> <?php $precip = "last"; for ($i = 0; $i < sizeof($weather["precipitation"]); $i++) { $precip .= " ".$weather["precipitation"][$i]["hours"]."h: ".$weather["precipitation"][$i]["amount"].$units["rain"]; ?> <?=$precip?> <?php } } ?> </td> <td valign="top"> <span class="bold">Clouds:</span><br> <?php if (isset($weather["clouds"]) && sizeof($weather["clouds"])) { for ($i = 0; $i < sizeof($weather["clouds"]); $i++) { $cloud = ucwords($weather["clouds"][$i]["amount"]); if (isset($weather["clouds"][$i]["type"])) { $cloud .= " ".$weather["clouds"][$i]["type"]; } if (isset($weather["clouds"][$i]["height"])) { $cloud .= " at ".$weather["clouds"][$i]["height"].$units["height"]; } ?> <?=$cloud?><br> <?php } } else { ?> Clear Below <?=$metar->convertDistance(12000, "ft", $units["height"]).$units["height"]?> <?php } ?> </td> </tr> </table> </td> </tr> <tr> <td> <table style="border-top: 2px solid #524b98; border-bottom: 2px solid #e0e3ce; border-left: 2px solid #b8b6c1; border-right: 2px solid #8b87a0" width="100%"> <tr class="bgkhaki"> <td align="center" style="border-bottom: 2px solid #abada2"><span class="bold">Forecast (TAF)</span></td> </tr> <tr valign="top"> <td> <table width="100%" class="bgkhaki" style="border-top: 2px solid #d8d8c0; border-bottom: 2px solid #d8d8c0; border-left: 2px solid #d8d8c0; border-right: 2px solid #8b87a0"> <tr> <td align="center" style="height:15px"> </td> <tr> <td align="center" style="height:45px"><span class="bold">Temperature</span> <span class="redbold">High</span> / <span class="bluebold">Low</span></td> </tr> <tr> <td align="center" style="height:15px"> </td> </tr> <tr> <td align="center" style="height:75px"><span class="bold">Condition</span></td> </tr> <tr> <td align="center" style="height:45px"><span class="bold">Precipitation probability</span></td> </tr> <tr> <td align="center" style="height:45px"><span class="bold">Wind</span></td> </tr> <tr> <td align="center" style="height:15px"><span class="bold">Humidity</span></td> </tr> </table> </td> </tr> <tr class="bgkhaki"> <td style="border-top: 2px solid #abada2"> </td> </tr> </table> </td> </tr> </table> <a href="javascript:history.back()">back</a> </body> </html>