Class: WrfForecast::Text::SuntimeText

Inherits:
Object
  • Object
show all
Defined in:
lib/wrf_forecast/forecast/suntime_text.rb

Overview

This class determines the time for sunrise and the sunset for a given location and a given day. The time is formatted in a human readable time. With that this class generates the text passage of the text forecast presenting these times

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta_data) ⇒ SuntimeText

initialization

Parameters:

  • meta_data (WrfLibrary::WrfMetaData)

    the meta data of the given weather station



17
18
19
20
21
# File 'lib/wrf_forecast/forecast/suntime_text.rb', line 17

def initialize()
  coordinate = .station.coordinate
  @text = generate_sunrise_text(.start_date, coordinate).concat(", ")
  @text.concat(generate_sunset_text(.start_date, coordinate))
end

Instance Attribute Details

#textString (readonly)

Returns the part of the forcast text containing sunrise and sunset.

Returns:

  • (String)

    the part of the forcast text containing sunrise and sunset



13
14
15
# File 'lib/wrf_forecast/forecast/suntime_text.rb', line 13

def text
  @text
end

Instance Method Details

#format_time(time) ⇒ String (private)

helper method to format the time from a Float to a timestamp with hh:mm

Parameters:

  • time (Float)

    the given time as a decimal number

Returns:

  • (String)

    the formatted, human readable timestamp



48
49
50
51
52
53
54
55
56
# File 'lib/wrf_forecast/forecast/suntime_text.rb', line 48

def format_time(time)
  hours = time.floor
  text = ""
  text.concat("0") if (hours < 10)
  text.concat("#{hours}").concat(":")
  minutes = (time.round(2) % 1 * 60).round(0)
  text.concat("0") if (minutes < 10)
  text.concat("#{minutes}")
end

#generate_sunrise_text(date, coordinate) ⇒ String (private)

method to calculate the sunrise time and the text part for the sunrise

Parameters:

  • date (Time)

    the day for the calculation of the sunrise

  • coordinate (WrfLibrary::Coordinate)

    the location of the weather station

Returns:

  • (String)

    the text part for the sunrise



29
30
31
32
33
# File 'lib/wrf_forecast/forecast/suntime_text.rb', line 29

def generate_sunrise_text(date, coordinate)
  sunrise = WrfLibrary::SunEquation.calculate_sunrise_time(date, coordinate.x, coordinate.y)
  text = I18n.t("forecast_text.suntime.sunrise")
  text.concat(format_time(sunrise))
end

#generate_sunset_text(date, coordinate) ⇒ String (private)

method to calculate the sunset time and the text part for the sunset

Parameters:

  • date (Time)

    the day for the calculation of the sunset

  • coordinate (WrfLibrary::Coordinate)

    the location of the weather station

Returns:

  • (String)

    the text part for the sunset



39
40
41
42
43
# File 'lib/wrf_forecast/forecast/suntime_text.rb', line 39

def generate_sunset_text(date, coordinate)
  sunset = WrfLibrary::SunEquation.calculate_sunset_time(date, coordinate.x, coordinate.y)
  text = I18n.t("forecast_text.suntime.sunset")
  text.concat(format_time(sunset))
end