Class: Metar::Data::SkyCondition

Inherits:
Base
  • Object
show all
Defined in:
lib/metar/data/sky_condition.rb

Constant Summary collapse

QUANTITY =
{
  'BKN' => 'broken',
  'FEW' => 'few',
  'OVC' => 'overcast',
  'SCT' => 'scattered'
}.freeze
CONDITION =
{
  'CB' => 'cumulonimbus',
  'TCU' => 'towering cumulus',
  # /// - cloud type unknown as observed by automatic system (15.9.1.7)
  '///' => nil,
  '' => nil
}.freeze
CLEAR_SKIES =
[
  'NSC', # WMO
  'NCD', # WMO
  'CLR',
  'SKC'
].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#value

Constructor Details

#initialize(raw, quantity: nil, height: nil, type: nil) ⇒ SkyCondition

Returns a new instance of SkyCondition.



56
57
58
59
60
61
# File 'lib/metar/data/sky_condition.rb', line 56

def initialize(raw, quantity: nil, height: nil, type: nil)
  @raw = raw
  @quantity = quantity
  @height = height
  @type = type
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



54
55
56
# File 'lib/metar/data/sky_condition.rb', line 54

def height
  @height
end

#quantityObject (readonly)

Returns the value of attribute quantity.



54
55
56
# File 'lib/metar/data/sky_condition.rb', line 54

def quantity
  @quantity
end

#typeObject (readonly)

Returns the value of attribute type.



54
55
56
# File 'lib/metar/data/sky_condition.rb', line 54

def type
  @type
end

Class Method Details

.parse(raw) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/metar/data/sky_condition.rb', line 27

def self.parse(raw)
  return nil if !raw

  return new(raw) if CLEAR_SKIES.include?(raw)

  m1 = raw.match(%r{^(BKN|FEW|OVC|SCT)(\d+|/{3})(CB|TCU|/{3}|)?$})
  if m1
    quantity = QUANTITY[m1[1]]
    height   =
      if m1[2] == '///'
        nil
      else
        Metar::Data::Distance.new(m1[2].to_i * 30.48)
      end
    type = CONDITION[m1[3]]
    return new(raw, quantity: quantity, height: height, type: type)
  end

  m2 = raw.match(/^(CB|TCU)$/)
  if m2
    type = CONDITION[m2[1]]
    return new(raw, type: type)
  end

  nil
end

Instance Method Details

#to_sObject



63
64
65
66
67
68
69
# File 'lib/metar/data/sky_condition.rb', line 63

def to_s
  if @height.nil?
    to_summary
  else
    to_summary + ' ' + I18n.t('metar.altitude.at') + ' ' + height.to_s
  end
end

#to_summaryObject



71
72
73
74
75
76
77
78
# File 'lib/metar/data/sky_condition.rb', line 71

def to_summary
  if @quantity.nil? && @height.nil? && @type.nil?
    I18n.t('metar.sky_conditions.clear skies')
  else
    type = @type ? ' ' + @type : ''
    I18n.t("metar.sky_conditions.#{@quantity}#{type}")
  end
end