You are here

function _weather_format_wind in Weather 5

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

Convert wind

Parameters

int Wind:

string The unit to be returned (metric, imperial):

Return value

string Formatted representation

1 call to _weather_format_wind()
theme_weather in ./weather.module
Custom theme function for the weather block output

File

./weather.module, line 726
Display <acronym title="METeorological Aerodrome Report">METAR</acronym> weather data from anywhere in the world

Code

function _weather_format_wind($wind, $unit) {
  if (!isset($wind)) {
    return t('No data');
  }

  // shortcut for a special case
  if ($wind['direction'] == 0 and $wind['speed_kmh'] == 0) {
    return t('Calm');
  }

  // translate the wind direction
  $directions = array(
    t('North') => array(
      'start' => -23,
      'end' => 22,
    ),
    t('Northeast') => array(
      'start' => 22,
      'end' => 67,
    ),
    t('East') => array(
      'start' => 67,
      'end' => 112,
    ),
    t('Southeast') => array(
      'start' => 112,
      'end' => 157,
    ),
    t('South') => array(
      'start' => 157,
      'end' => 202,
    ),
    t('Southwest') => array(
      'start' => 202,
      'end' => 247,
    ),
    t('West') => array(
      'start' => 247,
      'end' => 292,
    ),
    t('Northwest') => array(
      'start' => 292,
      'end' => 337,
    ),
  );

  // perform the wrap for North
  if ($wind['direction'] >= 337) {
    $wind['direction'] -= 360;
  }
  if ($wind['variable_start'] >= 337) {
    $wind['variable_start'] -= 360;
  }
  if ($wind['variable_end'] >= 337) {
    $wind['variable_end'] -= 360;
  }
  foreach ($directions as $description => $range) {
    if ($wind['direction'] >= $range['start'] and $wind['direction'] < $range['end']) {
      $wind['direction_text'] = $description;
    }
    if ($wind['variable_start'] >= $range['start'] and $wind['variable_start'] < $range['end']) {
      $wind['variable_start_text'] = $description;
    }
    if ($wind['variable_end'] >= $range['start'] and $wind['variable_end'] < $range['end']) {
      $wind['variable_end_text'] = $description;
    }
  }

  // handle variable wind directions
  if ($wind['direction'] == 'VRB' or isset($wind['variable_start'])) {
    if (isset($wind['variable_start'])) {
      $result[] = t('Variable from !direction_a to !direction_b', array(
        '!direction_a' => $wind['variable_start_text'],
        '!direction_b' => $wind['variable_end_text'],
      ));
    }
    else {
      $result[] = t('Variable');
    }
  }
  else {

    // no variable wind direction, but an exact one
    $result[] = $wind['direction_text'];
  }

  // set up the wind speed
  if ($unit == 'imperial') {
    $result[] = t('!speed mph', array(
      '!speed' => $wind['speed_mph'],
    ));
  }
  else {

    // default to metric units
    $result[] = t('!speed km/h', array(
      '!speed' => $wind['speed_kmh'],
    ));
  }

  // set up gusts, if applicable
  if ($wind['gusts_kmh'] > 0) {
    if ($unit == 'imperial') {
      $result[] = t('gusts up to !speed mph', array(
        '!speed' => $wind['gusts_mph'],
      ));
    }
    else {

      // default to metric units
      $result[] = t('gusts up to !speed km/h', array(
        '!speed' => $wind['gusts_kmh'],
      ));
    }
  }
  return join(', ', $result);
}