Class: WrfLibrary::WindDirectionRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/wrf_library/data/wind_direction_repository.rb

Overview

This class serves as the creation and storage component of a wind direction distribution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ WindDirectionRepository

initialization

Parameters:

  • data (Array) (defaults to: nil)

    optional array with wind directions, default is nil



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wrf_library/data/wind_direction_repository.rb', line 11

def initialize(data=nil)
  @directions = Hash.new()
  @directions[:NE] = WrfLibrary::WindDirection.new( 22.5,  67.5, :NE)
  @directions[:E]  = WrfLibrary::WindDirection.new( 67.5, 112.5, :E)
  @directions[:SE] = WrfLibrary::WindDirection.new(112.5, 157.5, :SE)
  @directions[:S]  = WrfLibrary::WindDirection.new(157.5, 202.5, :S)
  @directions[:SW] = WrfLibrary::WindDirection.new(202.5, 247.5, :SW)
  @directions[:W]  = WrfLibrary::WindDirection.new(247.5, 292.5, :W)
  @directions[:NW] = WrfLibrary::WindDirection.new(292.5, 337.5, :NW)
  
  @direction_distribution = Hash.new()
  @direction_distribution[:N] = 0
  @directions.each_key { |key|
    @direction_distribution[key] = 0
  }
  generate_direction_distribution(data) if (data != nil)
end

Instance Attribute Details

#direction_distributionHash (readonly)

Returns the wind direction distribution once it is calculated or an empty hash.

Returns:

  • (Hash)

    the wind direction distribution once it is calculated or an empty hash



7
8
9
# File 'lib/wrf_library/data/wind_direction_repository.rb', line 7

def direction_distribution
  @direction_distribution
end

#directionsHash (readonly, private)

Returns internal mapping from wind direction symbol to degree interval.

Returns:

  • (Hash)

    internal mapping from wind direction symbol to degree interval



70
71
72
# File 'lib/wrf_library/data/wind_direction_repository.rb', line 70

def directions
  @directions
end

Instance Method Details

#determine_prevalent_directionObject

method to determine the prevalent wind direction



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wrf_library/data/wind_direction_repository.rb', line 55

def determine_prevalent_direction
  prevalent_direction = nil
  count = 0
  @direction_distribution.each_pair { |key, value|
    if (value > count)
      prevalent_direction = key
      count = value
    end
  }
  return prevalent_direction
end

#determine_wind_sector(value) ⇒ Symbol

method to determine the wind section for a given value

Parameters:

  • value (Float)

    the wind direction

Returns:

  • (Symbol)

    the wind direction symbol of the corresponding sector or :NONE if no direction

Raises:

  • (ArgumentError)

    if the value lies outside the direction interval [0, 360]



34
35
36
37
38
39
40
41
# File 'lib/wrf_library/data/wind_direction_repository.rb', line 34

def determine_wind_sector(value)
  @directions.each_value { |direction|
    return direction.symbol if (value > direction.lower && value <= direction.upper)
    return :N if ((value > 337.5 && value <= 360.0) || (value <= 22.5 && value >= 0.0))
    return :NONE if (value == -1) # Edge case that wind components are zero, no direction
  }
  raise ArgumentError, "Given value #{value} does not represent a wind direction."
end

#generate_direction_distribution(data) ⇒ Hash

method to generate a wind direction distribution based on the given input data

Parameters:

  • data (Array)

    the array with the direction data

Returns:

  • (Hash)

    the calculated direction distribution



46
47
48
49
50
51
52
# File 'lib/wrf_library/data/wind_direction_repository.rb', line 46

def generate_direction_distribution(data)
  data.each { |value|
    sector = determine_wind_sector(value)
    @direction_distribution[sector] = @direction_distribution[sector] + 1
  }
  return @direction_distribution
end