Module: WrfForecast

Defined in:
lib/wrf_forecast.rb,
lib/wrf_forecast/data.rb,
lib/wrf_forecast/text.rb,
lib/wrf_forecast/parameter.rb,
lib/wrf_forecast/threshold.rb,
lib/wrf_forecast/json_converter.rb,
lib/wrf_forecast/data/directions.rb,
lib/wrf_forecast/forecast/daytime.rb,
lib/wrf_forecast/help/help_output.rb,
lib/wrf_forecast/forecast/rain_text.rb,
lib/wrf_forecast/forecast/wind_text.rb,
lib/wrf_forecast/locale_configuration.rb,
lib/wrf_forecast/forecast/suntime_text.rb,
lib/wrf_forecast/forecast/forecast_text.rb,
lib/wrf_forecast/forecast/measurand_text.rb,
lib/wrf_forecast/data/forecast_repository.rb,
lib/wrf_forecast/threshold/base_threshold.rb,
lib/wrf_forecast/threshold/rain_threshold.rb,
lib/wrf_forecast/threshold/wind_threshold.rb,
lib/wrf_forecast/forecast/forecast_handler.rb,
lib/wrf_forecast/forecast/temperature_text.rb,
lib/wrf_forecast/threshold/threshold_value.rb,
lib/wrf_forecast/parameter/parameter_handler.rb,
lib/wrf_forecast/threshold/threshold_handler.rb,
lib/wrf_forecast/parameter/parameter_repository.rb,
lib/wrf_forecast/threshold/temperature_threshold.rb,
lib/wrf_forecast/json_converter/text_forecast_converter.rb,
lib/wrf_forecast/json_converter/hourly_forecast_converter.rb,
lib/wrf_forecast/json_converter/forecast_station_converter.rb

Overview

This module is the main entry point and will be called from the main forecast script

Defined Under Namespace

Modules: Daytime, JsonConverter, LocaleConfiguration, Parameter, Text, Threshold Classes: Directions, ForecastHandler, ForecastRepository, ForecastText, HelpOutput

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.forecast_handlerForecastHandler (readonly)

Returns the handler for the rehashed forecast data.

Returns:



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

def forecast_handler
  @forecast_handler
end

.parameter_handlerParameter::ParameterHandler (readonly)

Returns the handler controlling the parameters.

Returns:



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

def parameter_handler
  @parameter_handler
end

.wrf_handlerHandler (readonly)

Returns the repository storing the datasets.

Returns:

  • (Handler)

    the repository storing the datasets



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

def wrf_handler
  @wrf_handler
end

Class Method Details

.contains_parameter?(symbol) ⇒ Boolean (private)

method the check if the given parameter has been set

Returns:

  • (Boolean)


70
71
72
# File 'lib/wrf_forecast.rb', line 70

def contains_parameter?(symbol)
  @parameter_handler.repository.parameters[symbol] != nil
end

.determine_json_outputObject (private)

private method to determine which kind of json output needs to be created, based on the given script parameter json and aggregate



160
161
162
163
164
165
# File 'lib/wrf_forecast.rb', line 160

private_class_method def self.determine_json_output
  if (@parameter_handler.repository.parameters[:aggregate])
    return @forecast_handler.generate_hourly_json_output          
  end
  @forecast_handler.generate_text_json_output
end

.get_warningsHash

method to return the warnings of the current forecast

Returns:

  • (Hash)

    if initialized the warning hash, else nil



90
91
92
93
94
95
96
97
# File 'lib/wrf_forecast.rb', line 90

def self.get_warnings
  if (@forecast_handler != nil)
    return @forecast_handler.get_warnings
  else
    print_error("Error: Module not initialized. Run WrfForecast.new(ARGV)")
  end
  nil
end

.initialize(arguments) ⇒ Object

main entry point and initialization

Parameters:

  • arguments (Array)

    the input values from the terminal input ARGV



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

def initialize(arguments)
  @parameter_handler = Parameter::ParameterHandler.new(arguments)
  initialize_locale

  if (!parameter_handler.repository.parameters[:help] && 
      !parameter_handler.repository.parameters[:version])
    initialize_wrf_handler
    initialize_forecast
  else
    @wrf_handler = nil
    @forecast_handler = nil
  end
end

.initialize_forecastObject (private)

method to initialize the forecast handler



64
65
66
67
# File 'lib/wrf_forecast.rb', line 64

def initialize_forecast
  @forecast_handler = WrfForecast::ForecastHandler.new(@wrf_handler)
  nil
end

.initialize_localeObject (private)

method to laod the available locale files and set a specific locale if the locale parameter is set



76
77
78
79
80
81
82
83
84
# File 'lib/wrf_forecast.rb', line 76

def initialize_locale
  WrfForecast::LocaleConfiguration.initialize_locale(
    Pathname.new(__dir__).join("../config/locales").expand_path)
  if (contains_parameter?(:locale))
    locale_string = @parameter_handler.repository.parameters[:locale]
    WrfForecast::LocaleConfiguration.determine_locale(locale_string)
  end
  nil
end

.initialize_wrf_handlerObject (private)

method to initialize the wrf handler based on the available parameter



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wrf_forecast.rb', line 42

def initialize_wrf_handler
  filename = @parameter_handler.repository.parameters[:file]
  time = Time.parse(@parameter_handler.repository.parameters[:date])
  # use 24 hours for a forecast right now to create a forecast text for a day

  if (contains_parameter?(:offset) && contains_parameter?(:period))
    period = RubyUtils::ParameterConverter.convert_float_parameter(
           @parameter_handler.repository.parameters[:period])
    offset = RubyUtils::ParameterConverter.convert_float_parameter(
           @parameter_handler.repository.parameters[:offset])
    @wrf_handler = WrfLibrary::Wrf::Handler.new(filename, time, period, offset)
  elsif (@parameter_handler.repository.parameters[:period] != nil)
    period = RubyUtils::ParameterConverter.convert_float_parameter(
           @parameter_handler.repository.parameters[:period])
    @wrf_handler = WrfLibrary::Wrf::Handler.new(filename, time, period)
  else
    @wrf_handler = WrfLibrary::Wrf::Handler.new(filename, time)
  end
  nil
end

.output_forecastString

singleton method check for the forecast handler and return the forecast based on the given parameters

Returns:

  • (String)

    if initialized the created forecast text, else nil



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/wrf_forecast.rb', line 102

def self.output_forecast
  if (@forecast_handler != nil)
    if (@parameter_handler.repository.parameters[:json])
      output = determine_json_output
    else
      output = @forecast_handler.text.get_complete_text
      output.concat("\n\n").concat(@forecast_handler.text.warnings)
    end
    save_forecast(output)
    return output
  else
    print_error("Error: Module not initialized. Run WrfForecast.new(ARGV)")
  end
  nil
end

call for standard error output

Parameters:

  • message (String)

    message string with error message



152
153
154
155
156
# File 'lib/wrf_forecast.rb', line 152

def self.print_error(message)
  puts "#{message}".red
  puts "For help type: ruby <script> --help".green
  nil
end

call to print the help text



134
135
136
137
138
139
140
141
# File 'lib/wrf_forecast.rb', line 134

def self.print_help
  if (@parameter_handler != nil && @parameter_handler.repository.parameters[:help] != nil)
    WrfForecast::HelpOutput.print_help_for(@parameter_handler.repository.parameters[:help])
  else
    print_error("Error: Module not initialized. Run WrfForecast.new(ARGV)")
  end
  nil
end

call to print version number and author



144
145
146
147
148
# File 'lib/wrf_forecast.rb', line 144

def self.print_version
  puts "wrf_forecast version 0.3.0".yellow
  puts "Created by Benjamin Held (March 2019)".yellow
  nil
end

.save_forecast(output) ⇒ Object

singleton method to save the forecast output if the given parameter is set

Parameters:

  • output (String)

    the output that should be saved



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wrf_forecast.rb', line 120

def self.save_forecast(output)
  if (contains_parameter?(:save))
    if (output != nil)
      file = File.open(@parameter_handler.repository.parameters[:save], "w")
      file.write(output)
      file.close
    else
      print_error("Error: No output was generated or it is nil.")
    end
  end
  nil
end