📄 phpweather.php
字号:
<?php/////////////////////////////////////////////* Copied from PHP Weather version 1.62. *//* Line 109 was added for php timeclock. *//* Line 110 was edited for php timeclock. *//////////////////////////////////////////////* Unsets old language variables and loads new ones. */if (isset($strings)) { /* The strings array is loaded - assume the same for the rest. */ unset($strings); unset($wind_dir_text_short_array); unset($wind_dir_text_array); unset($weather_array); unset($cloud_condition_array);}/* Load the new strings */$wind_dir_text_short_array = array( 'N', 'N/NE', 'NE', 'E/NE', 'E', 'E/SE', 'SE', 'S/SE', 'S', 'S/SW', 'SW', 'W/SW', 'W', 'W/NW', 'NW', 'N/NW', 'N');$cloud_condition_array = array( 'SKC' => 'Clear', 'CLR' => 'Clear', 'VV' => 'Vertical Visibility', 'FEW' => 'Partly Cloudy', 'SCT' => 'Scattered Clouds', 'BKN' => 'Partly Cloudy', 'OVC' => 'Overcast');/* Offset in hours to add to the time of a report. If all your times * are 2 hours off, then set this to -2. */$weather_offset = 0; /* Make a connection to the MySQL database: */ if (mysql_pconnect($db_hostname, $db_username, $db_password)) { mysql_select_db($db_name); } else { echo "<p>Unable to connect to MySQL database!</p>"; }function store_speed($value, $windunit, &$meterspersec, &$knots, &$milesperhour) { /* * Helper function to convert and store speed based on unit. * &$meterspersec, &$knots and &$milesperhour are passed on * reference */ if ($windunit == 'KT') { /* The windspeed measured in knots: */ $knots = number_format($value); /* The windspeed measured in meters per second, rounded to one * decimal place: */ $meterspersec = number_format($value * 0.51444, 1); /* The windspeed measured in miles per hour, rounded to one * decimal place: */ $milesperhour = number_format($value * 1.1507695060844667, 1); } elseif ($windunit == 'MPS') { /* The windspeed measured in meters per second: */ $meterspersec = number_format($value); /* The windspeed measured in knots, rounded to one decimal * place: */ $knots = number_format($value / 0.51444, 1); /* The windspeed measured in miles per hour, rounded to one * decimal place: */ $milesperhour = number_format($value / 0.51444 * 1.1507695060844667, 1); } elseif ($windunit == 'KMH') { /* The windspeed measured in kilometers per hour: */ $meterspersec = number_format($value * 1000 / 3600, 1); $knots = number_format($value * 1000 / 3600 / 0.51444, 1); /* The windspeed measured in miles per hour, rounded to one * decimal place: */ $milesperhour = number_format($knots * 1.1507695060844667, 1); }}function get_metar($station, $always_use_cache = 0) { /* * Looks in the database, and fetches a new metar is nesceary. If * $always_use_cache is true, then it ignores the timestamp of the * METAR and just returns it. * * You should pass a ICAO station identifier, eg. 'EKYT' for * Aalborg, Denmark. */ global $conn, $dbmMetar, $dbmTimestamp; $query = "SELECT metar, UNIX_TIMESTAMP(timestamp) FROM ".$db_prefix."metars WHERE station = '$station'"; $result = mysql_query($query); @$metar_rows = mysql_num_rows($result); /* this suppresses a php error message if the metars db has not yet been created. */ if (isset($metar_rows)) { /* found station */ list($metar, $timestamp) = mysql_fetch_row($result); } if (isset($metar)) { /* found station */ if ($always_use_cache || $timestamp > time() - 3600) { /* We have asked explicit for a cached metar, or the metar is * still fresh. */ return $metar; } else { /* We looked in the cache, but the metar was too old. */ return fetch_metar($station, 0); } } else { /* The station is new - we fetch a new METAR */ return fetch_metar($station, 1); }}function fetch_metar($station, $new) { /* * Fetches a new METER from weather.noaa.gov. If the $new variable * is true, the metar is inserted, else it will replace the old * metar. The new METAR is returned. */ global $conn, $dbmMetar, $dbmTimestamp; $metar = ''; $station = strtoupper($station); /* We use the @ notation, because it might fail. */ $file = @file('http://weather.noaa.gov/pub/data/' . "observations/metar/stations/$station.TXT"); /* Here we test to see if we actually got a METAR. */ if (is_array($file)) { $date = trim($file[0]); $metar = trim($file[1]); for ($i = 2; $i < count($file); $i++) { $metar .= ' ' . trim($file[i]); } /* The date is in the form 2000/10/09 14:50 UTC. This seperates the different parts. */ $date_parts = explode(':', strtr($date, '/ ', '::')); $date_unixtime = gmmktime($date_parts[3], $date_parts[4], 0, $date_parts[1], $date_parts[2], $date_parts[0]); if (!ereg('[0-9]{6}Z', $metar)) { /* Some reports dont even have a time-part, so we insert the * current time. This might not be the time of the report, but * it was broken anyway :-) */ $metar = gmdate('dHi', $date_unixtime) . 'Z ' . $metar; } if ($date_unixtime < (time() - 3300)) { /* The timestamp in the metar is more than 55 minutes old. We * adjust the timestamp, so that we won't try to fetch a new * METAR within the next 5 minutes. After 5 minutes, the * timestamp will again be more than 1 hour old. */ $date_unixtime = time() - 3300; } } else { /* If we end up here, it means that there was no file, we then set * the metar to and empty string. We set the date to time() - 3000 * to give the server 10 minutes of peace. If the file is * unavailable, we don't want to stress the server. */ $metar = ''; $date_unixtime = time() - 3000; } /* It might seam strange, that we make a local date, but MySQL * expects a local when we insert the METAR. */ $date = date('Y/m/d H:i', $date_unixtime); if ($new) { /* Insert the new record */ $query = "INSERT INTO ".$db_prefix."metars SET station = '$station', " . "metar = '$metar', timestamp = '$date'"; } else { /* Update the old record */ $query = "UPDATE ".$db_prefix."metars SET metar = '$metar', " . "timestamp = '$date' WHERE station = '$station'"; } mysql_query($query); return $metar;}function process_metar($metar) { /* This function decodes a raw METAR. The result is an associative * array with entries like 'temp_c', 'visibility_miles' etc. */ global $strings, $wind_dir_text_short_array, $wind_dir_text_array, $cloud_condition_array, $weather_array, $weather_offset; $temp_visibility_miles = ''; $cloud_layers = 0; $decoded_metar['remarks'] = ''; $decoded_metar['weather'] = ''; $cloud_coverage = array('SKC' => '0', 'CLR' => '0', 'VV' => '8/8', 'FEW' => '1/8 - 2/8', 'SCT' => '3/8 - 4/8', 'BKN' => '5/8 - 7/8', 'OVC' => '8/8'); $decoded_metar['metar'] = $metar; $parts = split('[ ]+', $metar); $num_parts = count($parts); for ($i = 0; $i < $num_parts; $i++) { $part = $parts[$i]; if (ereg('RMK|TEMPO|BECMG', $part)) { /* The rest of the METAR is either a remark or temporary * information. We skip the rest of the METAR. */ $decoded_metar['remarks'] .= ' ' . $part; break; } elseif ($part == 'METAR') { /* * Type of Report: METAR */ $decoded_metar['type'] = 'METAR'; } elseif ($part == 'SPECI') { /* * Type of Report: SPECI */ $decoded_metar['type'] = 'SPECI'; } elseif (ereg('^[A-Z]{4}$', $part) && ! isset($decoded_metar['station'])) { /* * Station Identifier */ $decoded_metar['station'] = $part; } elseif (ereg('([0-9]{2})([0-9]{2})([0-9]{2})Z', $part, $regs)) { /* * Date and Time of Report * We return a standard Unix UTC/GMT timestamp suitable for * gmdate() */ $decoded_metar['time'] = gmmktime($regs[2] + $weather_offset, $regs[3], 0, gmdate('m'), $regs[1], gmdate('Y')); } elseif (ereg('(AUTO|COR|RTD|CC[A-Z]|RR[A-Z])', $part, $regs)) { /* * Report Modifier: AUTO, COR, CCx or RRx */ $decoded_metar['report_mod'] = $regs[1]; } elseif (ereg('([0-9]{3}|VRB)([0-9]{2,3}).*(KT|MPS|KMH)', $part, $regs)) { /* Wind Group */ $windunit = $regs[3]; /* do ereg in two parts to retrieve unit first */ /* now do ereg to get the actual values */ ereg("([0-9]{3}|VRB)([0-9]{2,3})(G([0-9]{2,3})?$windunit)", $part, $regs); if ($regs[1] == 'VRB') { $decoded_metar['wind_deg'] = $strings['wind_vrb_long']; $decoded_metar['wind_dir_text'] = $strings['wind_vrb_long']; $decoded_metar['wind_dir_text_short'] = $strings['wind_vrb_short']; } else { $decoded_metar['wind_deg'] = $regs[1]; $decoded_metar['wind_dir_text'] = $wind_dir_text_array[intval(round($regs[1]/22.5))]; $decoded_metar['wind_dir_text_short'] = $wind_dir_text_short_array[intval(round($regs[1]/22.5))]; } store_speed($regs[2], $windunit, $decoded_metar['wind_meters_per_second'], $decoded_metar['wind_knots'], $decoded_metar['wind_miles_per_hour']); if (isset($regs[4])) { /* We have a report with information about the gust. First we have the gust measured in knots: */ store_speed($regs[4],$windunit, $decoded_metar['wind_gust_meters_per_second'], $decoded_metar['wind_gust_knots'], $decoded_metar['wind_gust_miles_per_hour']); } } elseif (ereg('^([0-9]{3})V([0-9]{3})$', $part, $regs)) { /* * Variable wind-direction */ $decoded_metar['wind_var_beg'] = $regs[1]; $decoded_metar['wind_var_end'] = $regs[2]; } elseif ($part == 9999) { /* A strange value. When you look at other pages you see it interpreted like this (where I use > to signify 'Greater than'): */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -