Codice PHP:
$stazioni= array('LIMG','LIEA','LIBA','LIPA','LIYW','LIBD','LIME','LIPE','LIPB','LIPL','LIPO','LIBR','LIEE','LIEB','LIEC','LIMU','LIQC','LICC','LICZ','LIPC','LICO','LIBC','LIMZ','LIED','LIVD','LIPY','LIPF','LIRQ','LIPK','LIVF','LIRH','LICL','LIMJ','LIBV','LIRM','LIRS','LIBG','LIRG','LICA','LICD','LIRL','LIBN','LIBH','LICF','LIML','LIMC','LIQO','LIVC','LIMY','LIBQ','LIRK','LIRN','LIMN','LIEO','LIVP','LICJ','LICP','LICG','LIMP','LIMV','LIMT','LIVR','LIRZ','LIBP','LIMS','LIMH','LIRP','LIRE','LIVM','LICR','LIVE','LIQN','LIPR','LIRA','LIRU','LIRF','LIPQ','LIBY','LIQW','LIVO','LIMK','LIMF','LICT','LIRT','LIPS','LIPH','LIVT','LIPI','LICU','LIPZ','LIPX','LIPT','LIRV');
foreach ($stazioni as $valore)
{
$stazione = $valore; /*$_GET['stazione']*/;
$wxInfo['STAZIONE'] = $stazione;
$metar = get_metar($stazione,$wxInfo);
process_metar($metar,$wxInfo);
// **** OUTPUT ****
echo "METAR = $metar
\n";
print_wxInfo($wxInfo); // Modify this function to fit your needs.
}
//FUNZIONI#############################################
function get_metar($stazione, &$wxInfo) {
$fileName="http://weather.noaa.gov/pub/data/observations/metar/stations/$stazione.TXT";
$metar = '';
$fileData = @file($fileName); // or die('Data not available');
if ($fileData != false) {
list($i, $date) = each($fileData);
$utc = strtotime(trim($date));
set_time_data($utc,$wxInfo);
while (list($i, $line) = each($fileData)) {
$metar .= ' ' . trim($line);
}
$metar = trim(str_replace(' ', ' ', $metar));
}
return $metar;
}
function set_time_data($utc, &$wxInfo) {
$timeZoneOffset = date('Z');
$local = $utc + $timeZoneOffset;
$wxInfo['OBSERVED'] = date('D M j, H:i T',$local);
$now = time();
$wxInfo['NOW'] = date('D M j, H:i T',$now);
$timeDiff = floor(($now - $local) / 60);
if ($timeDiff < 91) $wxInfo['AGE'] = "$timeDiff min ago";
else {
$min = $timeDiff % 60;
if ($min < 10) $min = '0' . $min;
$wxInfo['AGE'] = floor($timeDiff / 60) . ":$min hr ago";
}
}
function process_metar($metar, &$wxInfo) {
if ($metar != '') {
$metarParts = explode(' ',$metar);
$groupName = array('get_station','get_time','get_station_type','get_wind','get_var_wind','get_visibility','get_runway','get_conditions','get_cloud_cover','get_temperature','get_altimeter');
$metarPtr = 1; // get_station identity is ignored
$group = 1;
while ($group < count($groupName)) {
$part = $metarParts[$metarPtr];
$groupName[$group]($part,$metarPtr,$group,$wxInfo); // $groupName is a function variable
}
}
else $wxInfo['ERROR'] = 'Data not available';
}
function get_station($part, &$metarPtr, &$group, &$wxInfo) {
if (strlen($part) == 4 and $group == 0) {
$group++;
$metarPtr++;
}
}
function get_time($part, &$metarPtr, &$group, &$wxInfo) {
if (substr($part,-1) == 'Z') $metarPtr++;
$group++;
}
function get_station_type($part, &$metarPtr, &$group, &$wxInfo) {
if ($part == 'AUTO' || $part == 'COR') $metarPtr++;
$group++;
}
function get_wind($part, &$metarPtr, &$group, &$wxInfo) {
function speed($part, $unit) {
if ($unit == 'KT') $speed = round(1.1508 * $part); // from knots
elseif ($unit == 'MPS') $speed = round(2.23694 * $part); // from meters per second
else $speed = round(0.621371 * $part); // from km per hour
$speed = "$speed mph";
return $speed;
}
if (ereg('^([0-9G]{5,10}|VRB[0-9]{2,3})(KT|MPS|KMH)$',$part,$pieces)) {
$part = $pieces[1];
$unit = $pieces[2];
if ($part == '00000') {
$wxInfo['WIND'] = 'calm'; // no wind
}
else {
ereg('([0-9]{3}|VRB)([0-9]{2,3})G?([0-9]{2,3})?',$part,$pieces);
if ($pieces[1] == 'VRB') $direction = 'varies';
else {
$angle = (integer) $pieces[1];
$compass = array('N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW');
$direction = $compass[round($angle / 22.5) % 16];
}
if ($pieces[3] == 0) $gust = '';
else $gust = ', gusting to ' . speed($pieces[3], $unit);
$wxInfo['WIND'] = $direction . ' at ' . speed($pieces[2], $unit) . $gust;
}
$metarPtr++;
}
$group++;
}
function get_var_wind($part, &$metarPtr, &$group, &$wxInfo) {
if (ereg('([0-9]{3})V([0-9]{3})',$part,$pieces)) $metarPtr++;
$group++;
}
function get_runway($part, &$metarPtr, &$group, &$wxInfo) {
if (substr($part,0,1) == 'R') $metarPtr++;
else $group++;
}
function get_cloud_cover($part, &$metarPtr, &$group, &$wxInfo) {
static $cloudCode = array(
'SKC' => 'clear skies',
'CLR' => 'clear skies',
'FEW' => 'partly cloudy',
'SCT' => 'scattered clouds',
'BKN' => 'mostly cloudy',
'OVC' => 'overcast',
'VV' => 'vertical visibility');
if ($part == 'SKC' || $part == 'CLR') {
$wxInfo['CLOUDS'] = $cloudCode[$part];
$metarPtr++;
$group++;
}
else {
if (ereg('^([A-Z]{2,3})([0-9]{3})',$part,$pieces)) { // codes for CB and TCU are ignored
$wxInfo['CLOUDS'] = $cloudCode[$pieces[1]];
if ($pieces[1] == 'VV') {
$altitude = (integer) 100 * $pieces[2]; // units are feet
$wxInfo['CLOUDS'] .= " to $altitude ft";
}
$metarPtr++;
}
else {
$group++;
}
}
}
function print_wxInfo($wxInfo) {
$dots = '...............';
foreach($wxInfo As $wxIndex => $wx) {
if (strlen($wx) != 0) echo $wxIndex . substr($dots,0,strlen($dots)-strlen($wxIndex)) . " $wx
\n";
}
}