function weather_get_metar in Weather 6.5
Same name and namespace in other branches
- 5.6 weather.module \weather_get_metar()
- 5 weather.module \weather_get_metar()
- 7 weather.module \weather_get_metar()
Fetches the latest METAR data from the database or internet
2 calls to weather_get_metar()
- weather_block in ./
weather.module - Generate HTML for the weather block
- weather_search_location in ./
weather.module - Searches for the specified location, whether it is a place name or an ICAO code. For example, weather/fuhlsbüttel will display the weather for Hamburg-Fuhlsbüttel.
File
- ./
weather.module, line 2015 - Display <acronym title="METeorological Aerodrome Report">METAR</acronym> weather data from anywhere in the world
Code
function weather_get_metar($icao) {
// See if there's a report in the database
$icao = strtoupper($icao);
$sql = "SELECT * FROM {weather} WHERE icao='%s'";
$result = db_query($sql, $icao);
$data = db_fetch_array($result);
// If there is no report, initialize the array
if (!isset($data['next_update_on'])) {
$data['next_update_on'] = 0;
}
// If the time has come, try to download again
if ($data['next_update_on'] <= time()) {
$metar_raw = _weather_retrieve_data($icao);
if ($metar_raw) {
$metar = weather_parse_metar($metar_raw);
// Calculate the next scheduled update. Use 62 minutes after the
// reported timestamp, to allow the data to propagate to the server.
$data['next_update_on'] = $metar['reported_on'] + 62 * 60;
// However, if the current time is more than 62 minutes
// over the reported timestamp, do not download on every page request.
// Therefore, we use 3, 6, 12, and 24 hours to check for updates.
// From then on, we check once a day for updates.
if ($data['next_update_on'] < time()) {
$last_update = $metar['reported_on'];
$hours = 3 * 60 * 60;
while ($last_update + $hours + 120 < time()) {
if ($hours < 86400) {
$hours = $hours * 2;
}
else {
$hours = $hours + 86400;
}
}
// Add 2 minutes to allow the data to propagate to the server.
$data['next_update_on'] = $last_update + $hours + 120;
}
weather_store_metar($metar, $data['next_update_on']);
}
else {
// The download has not been successful. Calculate the time of next update
// according to last tries.
if (empty($data['metar_raw'])) {
// There is no entry yet, so this is the first download attempt.
// Create a new entry and store the current time in the metar_raw column.
$metar['icao'] = $icao;
$metar['#raw'] = gmdate("dHi") . "Z";
$next_update_on = time() + 10 * 60;
weather_store_metar($metar, $next_update_on);
}
else {
// There has been at least one download attempt. Increase the time of
// next update to not download every few minutes.
// If 24 hours are reached, we check once a day for updates.
// This way, we gracefully handle ICAO codes which do no longer exist.
// The time of the last download attempt is stored in the metar_raw column.
$metar = weather_parse_metar($data['metar_raw']);
$last_update = $metar['reported_on'];
$hours = 3 * 60 * 60;
while ($last_update + $hours + 120 < time()) {
if ($hours < 86400) {
$hours = $hours * 2;
}
else {
$hours = $hours + 86400;
}
}
// Add 2 minutes to allow the data to propagate to the server.
$next_update_on = $last_update + $hours + 120;
$metar['icao'] = $icao;
$metar['#raw'] = $data['metar_raw'];
weather_store_metar($metar, $next_update_on);
}
}
}
else {
$metar = weather_parse_metar($data['metar_raw']);
}
return $metar;
}