You are here

function _weather_parse_condition in Weather 5.6

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

Extract the condition information

Parameters

string Raw METAR data to parse:

array Parsed METAR data, will be altered:

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

File

./weather_parser.inc, line 326

Code

function _weather_parse_condition($metar_raw, &$metar) {
  $ordering = array(
    1 => array(
      'CLR' => 'clear',
    ),
    2 => array(
      'FEW' => 'few',
    ),
    3 => array(
      'SCT' => 'scattered',
    ),
    4 => array(
      'BKN' => 'broken',
    ),
    5 => array(
      'OVC' => 'overcast',
    ),
  );
  if (preg_match('/^' . '(FEW|SCT|BKN|OVC)([0-9]{3})' . '(CB|TCU)?' . '$/', $metar_raw, $matches)) {
    foreach ($ordering as $order => $data) {
      if (key($data) == $matches[1]) {
        $metar['#condition_text'][] = $data[key($data)];
        $metar['#condition_order'][] = $order;
        break;
      }
    }
  }
  else {
    if (preg_match('/^' . '(CLR|SKC|CAVOK)' . '$/', $metar_raw, $matches)) {
      $metar['#condition_text'][] = 'clear';
      $metar['#condition_order'][] = 1;
      if ($matches[1] == 'CAVOK') {
        $metar['visibility']['kilometers'] = 10;
        $metar['visibility']['miles'] = round($metar['visibility']['kilometers'] / 1.609344, 1);
      }
    }
    else {
      if (preg_match('/^' . '(NSC|NCD)' . '$/', $metar_raw, $matches)) {

        // NSC means no significant clouds,
        // NCD is from automatic stations, no cloud detected
        $metar['#condition_text'][] = 'no-significant-clouds';
        $metar['#condition_order'][] = 1;
      }
      else {
        if (preg_match('/^' . 'VV[0-9]{3}' . '$/', $metar_raw, $matches)) {
          $metar['#condition_text'][] = 'overcast';
          $metar['#condition_order'][] = 5;
        }
      }
    }
  }
  if (isset($metar['#condition_order'])) {
    foreach ($metar['#condition_order'] as $index => $order) {
      if ($order > $metar['condition_order']) {
        $metar['condition_order'] = $order;
        $metar['condition_text'] = $metar['#condition_text'][$index];
      }
    }
  }
}