Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created March 17, 2014 10:43
Show Gist options
  • Save komasaru/9597192 to your computer and use it in GitHub Desktop.
Save komasaru/9597192 to your computer and use it in GitHub Desktop.
Ruby script to calculate a summary of the list of NOAA's weather stations.
# ***************************************
# Ruby script to calculate a summary of
# the list of NOAA's weather stations.
# ***************************************
require 'open-uri'
class NoaaStations
FILE = "http://www.aviationweather.gov/static/adds/metars/stations.txt"
def initialize
@hash = Hash.new(0)
@cnt_c = 0
end
# メイン処理
def exec_main
# ファイル読込&集計
compile
# 結果出力
display
rescue => e
puts "[#{e.class}] #{e.message}"
e.backtrace.each{|trace| puts "\t#{trace}"}
exit 1
end
private
# ファイル読込&集計
def compile
open(FILE) do |f|
while l = f.gets
l.chomp!
next if l =~ /^\!/
next if l.split(//).size < 80
country_code = l[(-2)..(-1)]
@hash[country_code] += 1
end
end
rescue => e
raise
end
# 結果出力
def display
@hash.sort_by{|k, v| k}.sort_by{|k, v| -v}.each do |k, v|
print k == "JP" ? " *" : " "
printf("#{k}:%4d", v.to_i)
puts if (@cnt_c += 1) % 10 == 0
end
puts "\nCount of countries: #{@cnt_c}"
rescue => e
raise
end
end
NoaaStations.new.exec_main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment