You are here

function _weather_parse_visibility in Weather 5

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

Extract the visibility information

Parameters

string Raw METAR data to parse:

array Parsed METAR data, will 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 183

Code

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

    // Special case: A single digit, e.g. in 1 1/2SM
    $metar['visibility']['#visibility_miles'] = $matches[1];
  }
  else {
    if (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']['#visibility_miles'] + $matches[2] / $matches[4];
      }
      else {
        $visibility = $matches[2] . $matches[4];
      }
      $metar['visibility']['miles'] = $visibility;
      $metar['visibility']['kilometers'] = round($visibility * 1.609344, 1);
    }
    else {
      if (preg_match('/^([0-9]{4})$/', $metar_raw, $matches)) {
        $metar['visibility']['kilometers'] = round($matches[1] / 1000, 1);
        $metar['visibility']['miles'] = round($metar['visibility']['kilometers'] / 1.609344, 1);
      }
    }
  }
}