You are here

function weather_format_wind in Weather 7

Same name and namespace in other branches
  1. 7.3 weather_theme.inc \weather_format_wind()
  2. 7.2 weather_theme.inc \weather_format_wind()

Convert wind.

Parameters

int $wind_speed: Wind speed in km/h.

string $wind_direction: Wind direction ('variable' or compass bearings, for example, '080')

int $wind_gusts: Speed of wind gusts

array $settings: Settings for displaying (abbreviated directions, with compass bearing)

string $unit: Unit to be returned (km/h, knots, meter/s, ...).

Return value

string Formatted representation in the desired unit.

1 call to weather_format_wind()
theme_weather_theming in ./weather_theme.inc
Custom theme function for preprocessing the weather display.

File

./weather_theme.inc, line 299
Prepare themed weather output.

Code

function weather_format_wind($wind_speed, $wind_direction, $wind_gusts, $settings, $unit) {

  // Settings for wind directions.
  $abbreviated = FALSE;
  if (!empty($settings['show_abbreviated_directions'])) {
    $abbreviated = TRUE;
  }
  $exact_degree = FALSE;
  if (!empty($settings['show_directions_degree'])) {
    $exact_degree = TRUE;
  }

  // Shortcut for a special case.
  if ($wind_speed == 0 and $wind_direction == 0) {
    return t('Calm');
  }

  // Handle variable directions
  if ($wind_direction == 'variable') {

    // TRANSLATORS: This is a 'variable' wind direction.
    $result[] = t('Variable');
  }
  elseif (drupal_strlen($wind_direction) > 3) {

    // The wind is variable with compass bearings, for example, 090-130
    list($start, $end) = explode('-', $wind_direction);
    $start_text = weather_bearing_to_text($start, $abbreviated);
    $end_text = weather_bearing_to_text($end, $abbreviated);
    if ($exact_degree) {
      $result[] = t('Variable from !direction_a (!degree_a°) to !direction_b (!degree_b°)', array(
        '!direction_a' => $start_text,
        '!degree_a' => $start,
        '!direction_b' => $end_text,
        '!degree_b' => $end,
      ));
    }
    else {
      $result[] = t('Variable from !direction_a to !direction_b', array(
        '!direction_a' => $start_text,
        '!direction_b' => $end_text,
      ));
    }
  }
  else {

    // The wind has only one exact direction.
    $text = weather_bearing_to_text($wind_direction, $abbreviated);
    if ($exact_degree) {
      $result[] = t('!direction (!degree°)', array(
        '!direction' => $text,
        '!degree' => $wind_direction,
      ));
    }
    else {
      $result[] = $text;
    }
  }

  // Set up the wind speed.
  if ($unit == 'mph') {

    // Convert into mph.
    $speed = round($wind_speed * 0.62137, 1);
    $result[] = t('!speed mph', array(
      '!speed' => $speed,
    ));
    $gusts_speed = t('!speed mph', array(
      '!speed' => round($wind_gusts * 0.62137, 1),
    ));
  }
  elseif ($unit == 'knots') {

    // Convert into knots.
    $speed = round($wind_speed * 0.53996, 1);
    $result[] = t('!speed knots', array(
      '!speed' => $speed,
    ));
    $gusts_speed = t('!speed knots', array(
      '!speed' => round($wind_gusts * 0.53996, 1),
    ));
  }
  elseif ($unit == 'mps') {

    // Convert into meter per second.
    $speed = round($wind_speed * 0.27778, 1);
    $result[] = t('!speed meter/s', array(
      '!speed' => $speed,
    ));
    $gusts_speed = t('!speed meter/s', array(
      '!speed' => round($wind_gusts * 0.27778, 1),
    ));
  }
  elseif ($unit == 'beaufort') {

    // Convert into Beaufort.
    $number = weather_calculate_beaufort($wind_speed);
    $result[] = t('Beaufort !number', array(
      '!number' => $number,
    ));
    $gusts_speed = t('Beaufort !number', array(
      '!number' => weather_calculate_beaufort($wind_gusts),
    ));
  }
  else {

    // Default to metric units.
    $result[] = t('!speed km/h', array(
      '!speed' => $wind_speed,
    ));
    $gusts_speed = t('!speed km/h', array(
      '!speed' => $wind_gusts,
    ));
  }

  // Set up gusts, if applicable.
  if (!empty($wind_gusts)) {
    $result[] = t('gusts up to !speed', array(
      '!speed' => $gusts_speed,
    ));
  }
  $tmp = preg_replace("/([^ ]*)&thinsp;([^ ]*)/", '<span style="white-space:nowrap;">\\1&thinsp;\\2</span>', $result);
  return implode(', ', $tmp);
}