You are here

function weather_get_metar in Weather 5

Same name and namespace in other branches
  1. 5.6 weather.module \weather_get_metar()
  2. 6.5 weather.module \weather_get_metar()
  3. 7 weather.module \weather_get_metar()

Fetches the latest METAR data from the database or internet

1 call to weather_get_metar()
weather_block in ./weather.module
Generate HTML for the weather block

File

./weather.module, line 874
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['metar_raw']) or !isset($data['next_update_on'])) {
    $data['next_update_on'] = 0;
    $data['metar_raw'] = '';
  }

  // if the time has come, download again
  if ($data['next_update_on'] <= time()) {
    $data['metar_raw'] = '';
  }

  // fetch data from the internet
  if ($data['metar_raw'] == '') {
    $data['metar_raw'] = _weather_retrieve_data($icao);
    if ($data['metar_raw']) {
      $metar = weather_parse_metar($data['metar_raw']);
      weather_store_metar($metar);
    }
    else {

      // the internet retrieval has not been successful.
      // try again in 10 minutes
      $sql = "UPDATE {weather} SET next_update_on=%d WHERE icao='%s'";
      db_query($sql, time() + 10 * 60, $icao);
    }
  }
  else {
    $metar = weather_parse_metar($data['metar_raw']);
  }
  return $metar;
}