You are here

function _weather_parse_visibility in Weather 7

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

Extract visibility information.

Parameters

string $metar_raw: Raw METAR data to parse.

object $metar: METAR data object, may be altered.

1 call to _weather_parse_visibility()
weather_parse_metar in ./weather_parser.inc
Parses a raw METAR data string.

File

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

Code

function _weather_parse_visibility($metar_raw, &$metar) {
  if (preg_match('/^([0-9])$/', $metar_raw, $matches)) {

    // Special case: A single digit, for example, in 1 1/2SM
    $metar->visibility_miles = $matches[1];
  }
  elseif (preg_match('/^' . '(M?)([0-9])(\\/?)([0-9]*)' . 'SM' . '$/', $metar_raw, $matches)) {
    if ($matches[3] == '/') {

      // This is a fractional visibility, we need to convert this.
      $visibility = $metar->visibility_miles + $matches[2] / $matches[4];
    }
    else {
      $visibility = $matches[2] . $matches[4];
    }
    $metar->visibility = round($visibility * 1609.344, 0);
  }
  elseif (preg_match('/^([0-9]{4})(NDV)?$/', $metar_raw, $matches)) {

    // NDV means "no directional variation", used by automatic stations
    $metar->visibility = (int) $matches[1];
  }
}