Class: Metar::Station

Inherits:
Object
  • Object
show all
Defined in:
lib/metar/station.rb

Constant Summary collapse

NOAA_STATION_LIST_URL =
'https://tgftp.nws.noaa.gov/data/nsd_cccc.txt'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cccc, noaa_data) ⇒ Station

No check is made on the existence of the station



64
65
66
67
68
69
70
# File 'lib/metar/station.rb', line 64

def initialize(cccc, noaa_data)
  raise "Station identifier must not be nil"   if cccc.nil?
  raise "Station identifier must not be empty" if cccc.to_s == ''

  @cccc = cccc
  load! noaa_data
end

Instance Attribute Details

#ccccObject (readonly) Also known as: code

Returns the value of attribute cccc.



60
61
62
# File 'lib/metar/station.rb', line 60

def cccc
  @cccc
end

#countryObject (readonly)

Returns the value of attribute country.



60
61
62
# File 'lib/metar/station.rb', line 60

def country
  @country
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



60
61
62
# File 'lib/metar/station.rb', line 60

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



60
61
62
# File 'lib/metar/station.rb', line 60

def longitude
  @longitude
end

#nameObject (readonly)

Returns the value of attribute name.



60
61
62
# File 'lib/metar/station.rb', line 60

def name
  @name
end

#rawObject (readonly)

Returns the value of attribute raw.



60
61
62
# File 'lib/metar/station.rb', line 60

def raw
  @raw
end

#stateObject (readonly)

Returns the value of attribute state.



60
61
62
# File 'lib/metar/station.rb', line 60

def state
  @state
end

Class Method Details

.allObject



24
25
26
27
28
29
30
# File 'lib/metar/station.rb', line 24

def all
  all_structures.collect do |h|
    options = h.clone
    cccc    = options.delete(:cccc)
    new(cccc, options)
  end
end

.all_structuresObject



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

def all_structures
  return @structures if @structures

  @nsd_cccc ||= download_stations
  @structures = []

  @nsd_cccc.each_line do |station|
    fields = station.split(';')
    @structures << {
      cccc: fields[0],
      name: fields[3],
      state: fields[4],
      country: fields[5],
      latitude: fields[7],
      longitude: fields[8],
      raw: station.clone
    }
  end

  @structures
end

.countriesObject



20
21
22
# File 'lib/metar/station.rb', line 20

def countries
  all_structures.reduce(Set.new) { |a, s| a.add(s[:country]) }.to_a.sort
end

.download_stationsObject



86
87
88
89
90
# File 'lib/metar/station.rb', line 86

def download_stations
  uri = URI.parse(NOAA_STATION_LIST_URL)
  response = Net::HTTP.get_response(uri)
  response.body
end

.exist?(cccc) ⇒ Boolean

Does the given CCCC code exist?

Returns:

  • (Boolean)


37
38
39
# File 'lib/metar/station.rb', line 37

def exist?(cccc)
  !find_data_by_cccc(cccc).nil?
end

.find_all_by_country(country) ⇒ Object



41
42
43
# File 'lib/metar/station.rb', line 41

def find_all_by_country(country)
  all.select { |s| s.country == country }
end

.find_by_cccc(cccc) ⇒ Object



32
33
34
# File 'lib/metar/station.rb', line 32

def find_by_cccc(cccc)
  all.find { |station| station.cccc == cccc }
end

.find_data_by_cccc(cccc) ⇒ Object



114
115
116
# File 'lib/metar/station.rb', line 114

def find_data_by_cccc(cccc)
  all_structures.find { |station| station[:cccc] == cccc }
end

.to_latitude(latitude) ⇒ Object



52
53
54
55
56
57
# File 'lib/metar/station.rb', line 52

def to_latitude(latitude)
  m = latitude.match(/^(\d+)-(\d+)([SN])/)
  return nil if !m

  (m[3] == 'N' ? 1.0 : -1.0) * (m[1].to_f + m[2].to_f / 60.0)
end

.to_longitude(longitude) ⇒ Object



45
46
47
48
49
50
# File 'lib/metar/station.rb', line 45

def to_longitude(longitude)
  m = longitude.match(/^(\d+)-(\d+)([EW])/)
  return nil if !m

  (m[3] == 'E' ? 1.0 : -1.0) * (m[1].to_f + m[2].to_f / 60.0)
end

Instance Method Details

#parserObject



72
73
74
75
# File 'lib/metar/station.rb', line 72

def parser
  raw = Metar::Raw::Noaa.new(@cccc)
  Metar::Parser.new(raw)
end

#reportObject



77
78
79
# File 'lib/metar/station.rb', line 77

def report
  Metar::Report.new(parser)
end