Created
March 17, 2014 10:43
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *************************************** | |
# 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