Module: WrfLibrary::Statistic

Defined in:
lib/wrf_library/statistic/hourly.rb,
lib/wrf_library/statistic.rb

Overview

module to aggregate statistic based logic applied to wrf data

Defined Under Namespace

Modules: Hourly

Class Method Summary collapse

Class Method Details

.add_rain_data(handler) ⇒ Array (private)

method to add the rain data from the two different sources

Parameters:

  • handler (WrfHandler)

    the wrf handler with the data

Returns:

  • (Array)

    the cumulated rain data of cumulus and explizit rainsums



77
78
79
80
81
82
83
84
85
# File 'lib/wrf_library/statistic.rb', line 77

private_class_method def self.add_rain_data(handler)
  cumulus_rain = handler.retrieve_data_set(:cumulus_rainfall)
  explicit_rain = handler.retrieve_data_set(:explicit_rainfall)
  rain_data = Array.new()
  cumulus_rain.zip(explicit_rain).each { |c, e| 
    rain_data << c + e
  }
  rain_data
end

.calculate_direction_means(timestamps, timespan, data) ⇒ Array (private)

method to determine the prevalent wind direction for the given data per timespan

Parameters:

  • timestamps (Array)

    the array with the timestamps of the data

  • timespan (Symbol)

    the time attribute for which the sum should be calculated

  • data (Array)

    the data values

Returns:

  • (Array)

    the hourly means of the input data



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/wrf_library/statistic.rb', line 120

private_class_method def self.calculate_direction_means(timestamps, timespan, data)
  results = Array.new()
  hourly_subset = Array.new()

  # use send to get the desired value to the time object, e.g. for hourly value get the current hour value
  previous_timestamp = timestamps[0].send(timespan)
  data.zip(timestamps).each { |value, timestamp|
    # detect new time interval value, when the leading number increases by one
    if (timestamp.send(timespan) != previous_timestamp)
      directions = WindDirectionRepository.new(hourly_subset)
      results << directions.determine_prevalent_direction
      previous_timestamp = timestamp.send(timespan)
      hourly_subset.clear
    end

    hourly_subset << value
  }
  directions = WindDirectionRepository.new(hourly_subset)
  results << directions.determine_prevalent_direction
end

.calculate_means_for_timespan(timestamps, timespan, data) ⇒ Array (private)

method to create mean values for the given data and timespan

Parameters:

  • timestamps (Array)

    the array with the timestamps of the data

  • timespan (Symbol)

    the time attribute for which the sum should be calculated

  • data (Array)

    the data values

Returns:

  • (Array)

    the hourly means of the input data



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

private_class_method def self.calculate_means_for_timespan(timestamps, timespan, data)
  results = Array.new()
  value_count = 0
  mean = 0.0

  # use send to get the desired value to the time object, e.g. for hourly value get the current hour value
  previous_timestamp = timestamps[0].send(timespan)
  data.zip(timestamps).each { |value, timestamp|
    # detect new time interval value, when the leading number increases by one
    if (timestamp.send(timespan) != previous_timestamp)
      results << (mean / value_count).round(3)
      mean = 0.0
      value_count = 0
    end

    previous_timestamp = timestamp.send(timespan)
    mean += value
    value_count += 1
  }
  results << (mean / value_count).round(3)
end

.calculate_timespan_means(measurand, handler, timespan) ⇒ Array

method to generate timespan mean values for the given measurand

Parameters:

  • measurand (Symbol)

    the given measurand

  • handler (WrfLibrary::Wrf::Handler)

    the wrf handler with the data

  • timespan (Symbol)

    the time attribute for which the sum should be calculated

Returns:

  • (Array)

    the array with the timespan means rounded to 3 significant digits



14
15
16
17
18
# File 'lib/wrf_library/statistic.rb', line 14

def self.calculate_timespan_means(measurand, handler, timespan)
  timestamps = handler.retrieve_data_set(:forecast_time)
  data = handler.retrieve_data_set(measurand)
  calculate_means_for_timespan(timestamps, timespan, data)
end

.calculate_timespan_rainsum(handler, timespan) ⇒ Array

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

Parameters:

  • handler (WrfLibrary::Wrf::Handler)

    the wrf handler with the data

  • timespan (Symbol)

    the time attribute for which the sum should be calculated

Returns:

  • (Array)

    the array with the timespan sums rounded to 3 significant digits



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wrf_library/statistic.rb', line 52

def self.calculate_timespan_rainsum(handler, timespan)
  timestamps = handler.retrieve_data_set(:forecast_time)
  rain_data = add_rain_data(handler)
  results = Array.new()

  # when using an offset, start with the current value as delta
  previous_timespan = rain_data[0]
  # use send to get the desired value to the time object, e.g. for hourly value get the current hour value
  previous_timestamp = timestamps[0].send(timespan)
  rain_data.zip(timestamps).each { |rain, timestamp|
    # detect new time interval value, when the leading number increases by one
    if (timestamp.send(timespan) != previous_timestamp)
      results << (rain - previous_timespan).round(3)
      previous_timespan = rain
    end
    previous_timestamp = timestamp.send(timespan)
  }

  # workaround to satisfy the current requirement for daily values
  results << (rain_data[rain_data.size-1] - previous_timespan).round(3)
end

.calculate_timespan_winddirection_means(handler, timespan) ⇒ Array

method to calculate the wind direction means for the given timespan

Parameters:

  • handler (WrfLibrary::Wrf::Handler)

    the wrf handler with the data

  • timespan (Symbol)

    the time attribute for which the sum should be calculated

Returns:

  • (Array)

    the array with the timespan means rounded to 3 significant digits



37
38
39
40
41
42
43
44
# File 'lib/wrf_library/statistic.rb', line 37

def self.calculate_timespan_winddirection_means(handler, timespan)
  timestamps = handler.retrieve_data_set(:forecast_time)
  u_component = handler.retrieve_data_set(:u_wind)
  v_component = handler.retrieve_data_set(:v_wind)
  wind_direction = Measurand::Wind.calculate_winddirection(u_component, v_component)

  calculate_direction_means(timestamps, timespan, wind_direction)
end

.calculate_timespan_windspeed_means(handler, timespan) ⇒ Array

method to calculate the windspeed means for the given timespan

Parameters:

  • handler (WrfLibrary::Wrf::Handler)

    the wrf handler with the data

  • timespan (Symbol)

    the time attribute for which the sum should be calculated

Returns:

  • (Array)

    the array with the timespan means rounded to 3 significant digits



24
25
26
27
28
29
30
31
# File 'lib/wrf_library/statistic.rb', line 24

def self.calculate_timespan_windspeed_means(handler, timespan)
  timestamps = handler.retrieve_data_set(:forecast_time)
  u_component = handler.retrieve_data_set(:u_wind)
  v_component = handler.retrieve_data_set(:v_wind)
  wind_speed = Measurand::Wind.calculate_windspeed(u_component, v_component)

  calculate_means_for_timespan(timestamps, timespan, wind_speed)
end