Class: WrfForecast::ForecastRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/wrf_forecast/data/forecast_repository.rb

Overview

This class contains all relevant forecast data that is required for generating a forecast text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrf_handler) ⇒ ForecastRepository

initialization

Parameters:

  • wrf_handler (WrfHandler)

    the wrf handler with the data



23
24
25
26
27
28
29
30
31
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 23

def initialize(wrf_handler)
  @extreme_values = Hash.new()
  @forecast_data = Hash.new()
  @time_data = wrf_handler.retrieve_data_set(:forecast_time)

  add_temperature_data(wrf_handler)
  add_windspeed_data(wrf_handler)
  add_rain_data(wrf_handler)
end

Instance Attribute Details

#direction_distributionHash (readonly)

Returns the wind direction distribution.

Returns:

  • (Hash)

    the wind direction distribution



15
16
17
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 15

def direction_distribution
  @direction_distribution
end

#extreme_valuesHash (readonly)

Returns the extreme values for the interval data.

Returns:

  • (Hash)

    the extreme values for the interval data



11
12
13
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 11

def extreme_values
  @extreme_values
end

#forecast_dataHash (readonly)

Returns the forecast data identified by a symbol.

Returns:

  • (Hash)

    the forecast data identified by a symbol



13
14
15
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 13

def forecast_data
  @forecast_data
end

#hourly_rainArray (readonly)

Returns the hourly rain sums.

Returns:

  • (Array)

    the hourly rain sums



19
20
21
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 19

def hourly_rain
  @hourly_rain
end

#prevalent_directionSymbol (readonly)

Returns the prevalent wind direction.

Returns:

  • (Symbol)

    the prevalent wind direction



17
18
19
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 17

def prevalent_direction
  @prevalent_direction
end

#time_dataArray (readonly, private)

Returns the time stamp data.

Returns:

  • (Array)

    the time stamp data



36
37
38
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 36

def time_data
  @time_data
end

Instance Method Details

#add_rain_data(wrf_handler) ⇒ Object (private)

method to add the rain data from the two different sources

Parameters:

  • wrf_handler (WrfHandler)

    the wrf handler with the data



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 77

def add_rain_data(wrf_handler)
  cumulus_rain = wrf_handler.retrieve_data_set(:cumulus_rainfall)
  explicit_rain = wrf_handler.retrieve_data_set(:explicit_rainfall)
  rain_data = Array.new()
  cumulus_rain.zip(explicit_rain).each { |c, e| 
    rain_data << c + e
  }
  @forecast_data[:rain] = rain_data
  calculate_hourly_rainsum
  nil
end

#add_temperature_data(wrf_handler) ⇒ Object (private)

method to add the temperature data und determine extreme values

Parameters:

  • wrf_handler (WrfHandler)

    the wrf handler with the data



40
41
42
43
44
45
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 40

def add_temperature_data(wrf_handler)
  temperature = wrf_handler.retrieve_data_set(:air_temperature)
  @forecast_data[:air_temperature] = temperature
  @extreme_values[:air_temperature] = RubyUtils::Statistic.extreme_values(temperature)    
  nil
end

#add_windspeed_data(wrf_handler) ⇒ Object (private)

method to add the wind data und determine extreme values for wind speed

Parameters:

  • wrf_handler (WrfHandler)

    the wrf handler with the data



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 49

def add_windspeed_data(wrf_handler)
  r2d = 180.0 / (Math.atan(1) * 4.0)
  u_component = wrf_handler.retrieve_data_set(:u_wind)
  v_component = wrf_handler.retrieve_data_set(:v_wind)
  wind_speed = Array.new()
  wind_direction = Array.new()
  u_component.zip(v_component).each { |u, v| 
    wind_speed << Math.sqrt(u**2+v**2)
    wind_direction << Math.atan2(u, v) * r2d + 180
  }
  @forecast_data[:wind_speed] = wind_speed
  @forecast_data[:wind_direction] = wind_direction
  @extreme_values[:wind_speed] = RubyUtils::Statistic.extreme_values(wind_speed)
  generate_wind_direction_statistic
  nil
end

#calculate_hourly_rainsumObject (private)

method to sum up the rain data into hourly rain sums for that calculate the difference from the rain value at the start and end of the currently checked hour



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 92

def calculate_hourly_rainsum
  rain_data = @forecast_data[:rain]
  @hourly_rain = Array.new()
  # when using an offset, start with the current value as delta
  previous_hour = rain_data[0]
  previous_timestamp = @time_data[0].hour
  rain_data.zip(@time_data).each { |rain, timestamp|
    # detect new hour, when the leading number increases by one
    if (timestamp.hour != previous_timestamp)
      @hourly_rain << rain - previous_hour
      previous_hour = rain
    end
    previous_timestamp = timestamp.hour
  }

  @hourly_rain << rain_data[rain_data.size-1] - previous_hour
  @extreme_values[:rain] = RubyUtils::Statistic.extreme_values(@hourly_rain)
  nil
end

#generate_wind_direction_statisticObject (private)

method to determine the wind direction distribution for the given data



67
68
69
70
71
72
73
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 67

def generate_wind_direction_statistic
  direction_repository = WrfLibrary::WindDirectionRepository.new()
  direction_repository.generate_direction_distribution(@forecast_data[:wind_direction])
  @direction_distribution = direction_repository.direction_distribution
  @prevalent_direction = direction_repository.determine_prevalent_direction
  nil
end