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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 25

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

  add_pressure_data(wrf_handler)
  add_temperature_data(wrf_handler)
  add_windspeed_data(wrf_handler)
  add_rain_data(wrf_handler)
  calculate_apparent_temperature(wrf_handler)
end

Instance Attribute Details

#direction_distributionHash (readonly)

Returns the wind direction distribution.

Returns:

  • (Hash)

    the wind direction distribution



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

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



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

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



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

def forecast_data
  @forecast_data
end

#hourly_rainArray (readonly)

Returns the hourly rain sums.

Returns:

  • (Array)

    the hourly rain sums



21
22
23
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 21

def hourly_rain
  @hourly_rain
end

#prevalent_directionSymbol (readonly)

Returns the prevalent wind direction.

Returns:

  • (Symbol)

    the prevalent wind direction



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

def prevalent_direction
  @prevalent_direction
end

#time_dataArray (readonly, private)

Returns the time stamp data.

Returns:

  • (Array)

    the time stamp data



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

def time_data
  @time_data
end

Instance Method Details

#add_pressure_data(wrf_handler) ⇒ Object (private)

method to add the pressure data und determine extreme values

Parameters:

  • wrf_handler (WrfHandler)

    the wrf handler with the data



44
45
46
47
48
49
50
51
52
53
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 44

def add_pressure_data(wrf_handler)
  pressure = wrf_handler.retrieve_data_set(:pressure)
  temperature = wrf_handler.retrieve_data_set(:air_temperature)
  elevation = wrf_handler.data_repository..station.elevation
  reduced_pressure = WrfLibrary::Measurand::Pressure.reduce_pressure_to_sealevel(pressure, temperature, elevation)
  @forecast_data[:pressure] = reduced_pressure

  @extreme_values[:pressure] = RubyUtils::Statistic.extreme_values(reduced_pressure)
  nil
end

#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



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 94

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



57
58
59
60
61
62
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 57

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 66

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_apparent_temperature(wrf_handler) ⇒ Object (private)

method to calculate the apparant temperature from the given forecast data set

Parameters:

  • wrf_handler (WrfHandler)

    the wrf handler with the data



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 131

def calculate_apparent_temperature(wrf_handler)
  pressure = @forecast_data[:pressure]
  temperature = @forecast_data[:air_temperature]
  wind_speed = @forecast_data[:wind_speed]
  humidity = wrf_handler.retrieve_data_set(:mixing_ratio)
  apparent_temperature = Array.new()
  humidity.each_with_index { |value, i|
    temperature_value = temperature[i] - 273.15
    pressure_value = pressure[i] / 100
    apparent_temperature << WrfLibrary::ApparentTemperature.calculate_apparent_temperature(
                            temperature_value, wind_speed[i], value, pressure_value)
  }
  @extreme_values[:apparent_temperature] = RubyUtils::Statistic.extreme_values(apparent_temperature)
  @forecast_data[:apparent_temperature] = apparent_temperature
  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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 109

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



84
85
86
87
88
89
90
# File 'lib/wrf_forecast/data/forecast_repository.rb', line 84

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