Class: NOAA::Station

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

Overview

Data about an NOAA observation station. When accessing current conditions, the NOAA XML API takes a station ID as input; thus, to find the current conditions for an arbitrary location, one must first determine the closest weather station to that location. The NOAA.current_conditions method performs this task implicitly; however, for applications that need to find conditions for the same location(s) repeatedly, it is far more efficient to find the closest weather station once, store that information, and then query directly against the weather station when updated conditions are needed.

Station data is stored in a YAML file that is created using the noaa-update-stations executable. Be sure to run this command at least once when you first install the NOAA library, and any time thereafter that you would like to get the latest list of stations. I don’t imagine the list changes very often but I don’t really have any idea.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties) ⇒ Station

Returns a new instance of Station.



91
92
93
94
95
96
# File 'lib/noaa/station.rb', line 91

def initialize(properties)
  @id, @name, @state, @xml_url = %w(id name state xml_url).map do |p|
    properties[p]
  end
  @coordinates = GeoKit::LatLng.new(properties['latitude'], properties['longitude'])
end

Class Attribute Details

.stations_file=(value) ⇒ Object

:nodoc:



18
19
20
# File 'lib/noaa/station.rb', line 18

def stations_file=(value)
  @stations_file = value
end

Instance Attribute Details

#coordinatesObject (readonly)

GeoKit::LatLng containing the station’s coordinates



78
79
80
# File 'lib/noaa/station.rb', line 78

def coordinates
  @coordinates
end

#idObject (readonly)

Station ID (e.g., “KNYC”)



81
82
83
# File 'lib/noaa/station.rb', line 81

def id
  @id
end

#nameObject (readonly)

Station name (e.g., “New York City, Central Park”)



84
85
86
# File 'lib/noaa/station.rb', line 84

def name
  @name
end

#stateObject (readonly)

Two-digit abbreviation for state in which station resides (e.g., “NY”)



87
88
89
# File 'lib/noaa/station.rb', line 87

def state
  @state
end

#xml_urlObject (readonly)

:nodoc:



89
90
91
# File 'lib/noaa/station.rb', line 89

def xml_url
  @xml_url
end

Class Method Details

.closest_to(*args) ⇒ Object

Find the station closest to a given location. Can accept arguments in any of the following three forms (all are equivalent):

NOAA::Station.closest_to(37.989, -77.507)
NOAA::Station.closest_to([37.989, -77.507])
NOAA::Station.closest_to(GeoKit::LatLng.new(37.989, -77.507))


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/noaa/station.rb', line 35

def closest_to(*args)
  if args.length == 1
    if args.first.respond_to?(:distance_to)
      closest_to_coordinates(args.first)
    elsif %w(first last).all? { |m| args.first.respond_to?(m) }
      closest_to_lat_lng(args.first)
    else
      raise ArgumentError, "expected two-element Array or GeoKit::LatLng"
    end
  elsif args.length == 2
    closest_to_lat_lng(args)
  else
    raise ArgumentError, "closest_to() will accept one Array argument, one GeoKit::LatLng argument, or two FixNum arguments"
  end
end

.find(id) ⇒ Object

Retrieve information about a station given a station ID

NOAA::Station.find('KNYC')  #=> NOAA::Station object for the Central Park station


24
25
26
# File 'lib/noaa/station.rb', line 24

def find(id)
  stations.find { |station| station.id == id }
end

Instance Method Details

#latitudeObject Also known as: lat

Latitude of station



99
100
101
# File 'lib/noaa/station.rb', line 99

def latitude
  @coordinates.lat
end

#longitudeObject Also known as: lng, lon

Longitude of station



105
106
107
# File 'lib/noaa/station.rb', line 105

def longitude
  @coordinates.lng
end