You are here

function weather_parse_metar in Weather 7

Same name and namespace in other branches
  1. 5.6 weather_parser.inc \weather_parse_metar()
  2. 5 weather_parser.inc \weather_parse_metar()
  3. 6.5 weather_parser.inc \weather_parse_metar()

Parses a raw METAR data string.

Parameters

string $metar_raw_string: Raw METAR data string.

Return value

object METAR data object.

2 calls to weather_parse_metar()
WeatherParserTestCase::testNormalMETAR in tests/parser.test
Test the parser with normal data.
weather_refresh_data in ./weather_parser.inc
Parses raw METAR data string and stores results in database.

File

./weather_parser.inc, line 151
Retrieves and parses raw METAR data and stores result in database.

Code

function weather_parse_metar($metar_raw_string) {

  // Setup the METAR data object.
  $metar = new stdClass();
  $metar->raw = $metar_raw_string;

  // Initialize a helper property, see bug #1894646.
  $metar->visibility_miles = 0;

  // Extract the date and time in UTC.
  $metar->reported_on = _weather_parse_timestamp($metar_raw_string);

  // Some stations insert a space between the cloud conditions
  // and the altitude, for example, "FEW 025" instead of "FEW025".
  // Therefore, we scan for such occurences and remove the space.
  $metar_raw_string = preg_replace("/(FEW|SCT|BKN|OVC)\\s+([0-9]{3})/", '$1$2', $metar->raw);

  // Split string for parsing routines
  $raw_items = preg_split('/\\s+/', drupal_strtoupper($metar_raw_string));

  // Run the data through the METAR routines
  foreach ($raw_items as $metar_raw) {
    if (_weather_parse_stop($metar_raw)) {
      break;
    }
    _weather_parse_icao($metar_raw, $metar);
    _weather_parse_sky_condition($metar_raw, $metar);
    _weather_parse_phenomena($metar_raw, $metar);
    _weather_parse_temperature($metar_raw, $metar);
    _weather_parse_wind($metar_raw, $metar);
    _weather_parse_pressure($metar_raw, $metar);
    _weather_parse_visibility($metar_raw, $metar);
  }
  if (isset($metar->phenomena)) {
    $metar->phenomena = implode(', ', $metar->phenomena);
  }

  // Calculate sunrise and sunset times
  _weather_calculate_sunrise_sunset($metar);

  // Set up the image filename.
  _weather_construct_image_filename($metar);
  return $metar;
}