class NDBC::Station

Attributes

active[R]
active?[R]
connection[RW]
forecast[R]
hull[R]
id[RW]
location[R]
name[R]
note[R]
owner[R]
payload[R]
tide_station_id[R]
timezone[R]
ttype[R]

Public Class Methods

all() click to toggle source
# File lib/ndbc/station.rb, line 9
def all
  NDBC::StationTable.station_table_data.map { |data| new(data[:id].upcase, data) }
end
new(id, station_data = {}) click to toggle source
# File lib/ndbc/station.rb, line 20
def initialize(id, station_data = {})
  @id = id.to_s
  @owner = station_data[:owner]
  @ttype = station_data[:ttype]
  @hull = station_data[:hull]
  @name = station_data[:name]
  @payload = station_data[:payload]
  @location = station_data[:location]
  @timezone = station_data[:timezone]
  @forecast = station_data[:forecast]
  @note = station_data[:note]
  @active = station_data[:active]
  @tide_station_id = station_data[:tide_station_id]
  @connection = Connection.new
end

Public Instance Methods

continuous_winds_data() click to toggle source
# File lib/ndbc/station.rb, line 50
def continuous_winds_data
  @continuous_winds_data ||= parse_observation_response(
    get_data(NDBC.config[:urls][:observations] + id + ".cwind")
  )
end
inspect() click to toggle source
# File lib/ndbc/station.rb, line 36
def inspect
  "#{id} (lat: #{@location[:latitude]}, lon: #{@location[:longitude]})"
end
latest_continuous_winds_data() click to toggle source
# File lib/ndbc/station.rb, line 56
def latest_continuous_winds_data
  latest_data(:continuous_winds_data)
end
latest_spectral_wave_forecasts() click to toggle source
# File lib/ndbc/station.rb, line 76
def latest_spectral_wave_forecasts
  latest_data(:spectral_wave_forecasts)
end
latest_spectral_wave_summaries() click to toggle source
# File lib/ndbc/station.rb, line 66
def latest_spectral_wave_summaries
  latest_data(:spectral_wave_summaries)
end
latest_standard_meteorological_data() click to toggle source
# File lib/ndbc/station.rb, line 46
def latest_standard_meteorological_data
  latest_data(:standard_meteorological_data)
end
method_missing(method_sym, *arguments, &block) click to toggle source
Calls superclass method
# File lib/ndbc/station.rb, line 80
def method_missing(method_sym, *arguments, &block)
  upcased_method_name = method_sym.to_s.upcase
  case method_sym
  when :wdir, :wspd, :gst, :wvht, :dpd, :apd, :mwd,
       :pres, :atmp, :wtmp, :dewp, :vis, :ptdy, :tide
    latest_standard_meteorological_data[upcased_method_name]
  when :dir, :spd, :gdr, :gsp, :gtime
    latest_continuous_winds_data[upcased_method_name]
  when :h0, :wwh, :wwp, :wwd, :steepness, :avp
    latest_spectral_wave_summaries[upcased_method_name]
  when :swh
    latest_spectral_wave_summaries['SwH']
  when :swp
    latest_spectral_wave_summaries['SwP']
  when :swd
    latest_spectral_wave_summaries['SwD']
  else
    super
  end
end
respond_to_missing?(method_sym, include_private = false) click to toggle source
Calls superclass method
# File lib/ndbc/station.rb, line 101
def respond_to_missing?(method_sym, include_private = false)
  case method_sym
  when :wdir, :wspd, :gst, :wvht, :dpd, :apd, :mwd, :pres, :atmp, :wtmp, :dewp, :vis, :ptdy,
       :tide, :dir, :spd, :gdr, :gsp, :gtime, :h0, :wwh, :wwp, :wwd, :steepness, :avp, :swh,
       :swp, :swd, :swd
    true
  else
    super
  end
end
spectral_wave_forecasts() click to toggle source
# File lib/ndbc/station.rb, line 70
def spectral_wave_forecasts
  @spectral_wave_forecasts ||= parse_prediction_response(
    get_data(NDBC.config[:urls][:predictions] + "multi_1.#{id}.bull")
  )
end
spectral_wave_summaries() click to toggle source
# File lib/ndbc/station.rb, line 60
def spectral_wave_summaries
  @spectral_wave_summaries ||= parse_observation_response(
    get_data(NDBC.config[:urls][:observations] + id + ".spec")
  )
end
standard_meteorological_data() click to toggle source
# File lib/ndbc/station.rb, line 40
def standard_meteorological_data
  @standard_meteorological_data ||= parse_observation_response(
    get_data(NDBC.config[:urls][:observations] + id + ".txt")
  )
end

Private Instance Methods

get_data(path) click to toggle source
# File lib/ndbc/station.rb, line 120
def get_data(path)
  connection.get(path)
rescue NotFound => error
  raise NDBC::StationNotFound, "Could not find station #{id}"
end
get_hst(split_line) click to toggle source
# File lib/ndbc/station.rb, line 178
def get_hst(split_line)
  split_line[2].strip.split(/\s+/)[0].to_f
end
latest_data(dataset) click to toggle source
# File lib/ndbc/station.rb, line 114
def latest_data(dataset)
  send(dataset)[:values].sort_by do |row|
    "#{row['YY']}#{row['MM']}#{row['DD']}#{row['hh']}#{row['mm']}"
  end.last || {}
end
parse_cycle_line(line) click to toggle source
# File lib/ndbc/station.rb, line 169
def parse_cycle_line(line)
  time_string = line[-15..-1] # "20140809  6 UTC"
  year =  time_string[0..3].to_i
  month = time_string[4..5].to_i
  day =   time_string[6..7].to_i
  hour =  time_string[9..10].to_i
  DateTime.new(year, month, day, hour) - 9.hours
end
parse_observation_response(response) click to toggle source
# File lib/ndbc/station.rb, line 126
def parse_observation_response(response)
  data = {
    units: {},
    values: []
  }

  return data if response.nil?

  response = response.split("\n")

  labels = response[0][1..-1].split(/\s+/)
  units = response[1][1..-1].split(/\s+/)

  data[:units] = Hash[ labels.zip(units) ]

  response[2..-1].each do |line|
    values = line.split(/\s+/).collect { |item| (item == "MM") ? nil : item }
    data[:values] << Hash[ labels.zip(values) ]
  end

  data
end
parse_prediction_response(response) click to toggle source
# File lib/ndbc/station.rb, line 149
def parse_prediction_response(response)
  return if response.nil?
  lines = response.split("\n")
  first_time = parse_cycle_line(lines[2])

  n = 0

  [].tap do |array|
    lines[7..196].each do |line|
      split_line = line.split('|')
      array << {
        time: first_time + n.hours,
        hst: get_hst(split_line),
        swells: parse_swells(split_line)
      }
      n = n+1
    end
  end
end
parse_swell_block(swell_block) click to toggle source
# File lib/ndbc/station.rb, line 191
def parse_swell_block(swell_block)
  pieces = swell_block.gsub('*', '').strip.split(/\s+/)
  {
    hs: pieces[0].to_f,
    tp: pieces[1].to_f,
    dir: pieces[2].to_i
  }
end
parse_swells(split_line) click to toggle source
# File lib/ndbc/station.rb, line 183
def parse_swells(split_line)
  swells = []
  split_line.slice(3..8).each do |swell_block|
    swells << parse_swell_block(swell_block)
  end
  return swells
end