You are here

protected function ThemeService::calculateBeaufort in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Service/ThemeService.php \Drupal\weather\Service\ThemeService::calculateBeaufort()

Calculate Beaufort wind scale for given wind speed.

@link http://en.wikipedia.org/wiki/Beaufort_scale

Parameters

int $wind_speed: Wind speed in m/s.

Return value

array Beaufort number and description.

2 calls to ThemeService::calculateBeaufort()
ThemeService::formatWind in src/Service/ThemeService.php
Convert wind.
ThemeService::formatWindSpeed in src/Service/ThemeService.php
Convert wind speed.

File

src/Service/ThemeService.php, line 714

Class

ThemeService
ThemeService service.

Namespace

Drupal\weather\Service

Code

protected function calculateBeaufort($wind_speed) {

  // Set up an array of wind descriptions according to Beaufort scale.
  $description = [
    $this
      ->t('Calm'),
    $this
      ->t('Light air'),
    $this
      ->t('Light breeze'),
    $this
      ->t('Gentle breeze'),
    $this
      ->t('Moderate breeze'),
    $this
      ->t('Fresh breeze'),
    $this
      ->t('Strong breeze'),
    $this
      ->t('Near gale'),
    $this
      ->t('Gale'),
    $this
      ->t('Strong gale'),
    $this
      ->t('Storm'),
    $this
      ->t('Violent storm'),
    $this
      ->t('Hurricane'),
  ];
  $number = 0;
  if ($wind_speed >= 0.3) {
    $number = 1;
  }
  if ($wind_speed >= 1.6) {
    $number = 2;
  }
  if ($wind_speed >= 3.5) {
    $number = 3;
  }
  if ($wind_speed >= 5.5) {
    $number = 4;
  }
  if ($wind_speed >= 8.0) {
    $number = 5;
  }
  if ($wind_speed >= 10.8) {
    $number = 6;
  }
  if ($wind_speed >= 13.9) {
    $number = 7;
  }
  if ($wind_speed >= 17.2) {
    $number = 8;
  }
  if ($wind_speed >= 20.8) {
    $number = 9;
  }
  if ($wind_speed >= 24.5) {
    $number = 10;
  }
  if ($wind_speed >= 28.5) {
    $number = 11;
  }
  if ($wind_speed >= 32.7) {
    $number = 12;
  }
  return [
    'number' => $number,
    'description' => $description[$number],
  ];
}