気象庁(AMeDAS)の月降水量データ取得(3)
Rubyを用いてa(測候所なし)の降水量データの取得
エディターを立ち上げ以下のように入力します。ファイル名は「get_month_prec_a.rb」として保存してください。
out_file = "c:/ronbun/data/month_prec_all.txt"
out = open(out_file,"w")
v = []
out.printf "statioin_number,name,year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec,longitude,latitude,altitude\n"
printf "statioin_number,name,year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec,longitude,latitude,altitude\n"
b = "c:/ronbun/data/p_new_csv_all.txt"
open(b).each do |liner|
v = liner.chop.split(",")
b_n = v[1].to_s
i = 1976 # 気象庁の測候所なしは基本的に1976年から観測を開始しているため
while i < 2011
a = "http://www.data.jma.go.jp/obd/stats/etrn/view/monthly_a1.php?block_no=" + b_n + "&year="
file = a + i.to_s
printf "%s,%s,%s,",v[1],v[2],i.to_s
out.printf "%s,%s,%s,",v[1],v[2],i.to_s
require 'open-uri'
open(file) {|f|
f.each_line {|line|
if %r|</a></div></td><td class="data_0_0">\d+<| =~ line
line.scan(%r|</a></div></td><td class="data_0_0">\d+|){|matche|
matching = matche.delete('</a></div></td><td class="data__">')
print matching.to_f
out.printf "%s,",matching.to_f
}
elsif %r|</a></div></td><td class="data_0_0">\d+ .<| =~ line
line.scan(%r|</a></div></td><td class="data_0_0">\d+|){|matche|
matching = matche.delete('</a></div></td><td class="data__">')
print matching.to_f
out.printf "%s,",matching.to_f
}
elsif %r|</a></div></td><td class="data_1t_0">\d+<| =~ line
line.scan(%r|</a></div></td><td class="data_1t_0">\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_1t_0">\d+ .<| =~ line
line.scan(%r|</a></div></td><td class="data_1t_0">\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_0_1b">\d+<| =~ line
line.scan(%r|</a></div></td><td class="data_0_1b">\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_0_1b">\d+ .<| =~ line
line.scan(%r|</a></div></td><td class="data_0_1b">\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_1t_1b">\d+<| =~ line
line.scan(%r|</a></div></td><td class="data_1t_1b">\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_1t_1b">\d+ .<| =~ line
line.scan(%r|</a></div></td><td class="data_1t_1b">\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_0_0">\d+.\d+<| =~ line
line.scan(%r|</a></div></td><td class="data_0_0">\d+.\d+<|){|matche|
matching = matche.delete('</a></div></td><td class="data__">')
print matching.to_f
out.printf "%s,",matching.to_f
}
elsif %r|</a></div></td><td class="data_0_0">\d+.\d+ .<| =~ line
line.scan(%r|</a></div></td><td class="data_0_0">\d+.\d+|){|matche|
matching = matche.delete('</a></div></td><td class="data__">')
print matching.to_f
out.printf "%s,",matching.to_f
}
elsif %r|</a></div></td><td class="data_1t_0">\d+.\d+| =~ line
line.scan(%r|</a></div></td><td class="data_1t_0">\d+.\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_1t_0">\d+.\d+ .<| =~ line
line.scan(%r|</a></div></td><td class="data_1t_0">\d+.\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_0_1b">\d+.\d+| =~ line
line.scan(%r|</a></div></td><td class="data_0_1b">\d+.\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_0_1b">\d+.\d+ .<| =~ line
line.scan(%r|</a></div></td><td class="data_0_1b">\d+.\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_1t_1b">\d+.\d+ .<| =~ line
line.scan(%r|</a></div></td><td class="data_1t_1b">\d+.\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_1t_1b">\d+.\d+| =~ line
line.scan(%r|</a></div></td><td class="data_1t_1b">\d+.\d+|){|matche|
matche.scan(%r|>\d+|){|matching|
just_matching = matching.delete(">")
print just_matching.to_f
out.printf "%s,",just_matching.to_f
}
}
elsif %r|</a></div></td><td class="data_0_0">///| =~ line
line.scan(%r|</a></div></td><td class="data_0_0">///|){|matche|
matche = 0
print matche
out.printf "%s,",matche
}
elsif %r|</a></div></td><td class="data_1t_0">///| =~ line
line.scan(%r|</a></div></td><td class="data_1t_0">///|){|matche|
matche = 0
print matche
out.printf "%s,",matche
}
elsif %r|</a></div></td><td class="data_0_1b">///| =~ line
line.scan(%r|</a></div></td><td class="data_0_1b">///|){|matche|
matche = 0
print matche
out.printf "%s,",matche
}
elsif %r|</a></div></td><td class="data_1t_1b">///| =~ line
line.scan(%r|</a></div></td><td class="data_1t_1b">///|){|matche|
matche = 0
print matche
out.printf "%s,",matche
}
end
}
}
printf "%.5f,%.5f,%.1f\n",v[9].to_f,v[10].to_f,v[8].to_f
out.printf "%.5f,%.5f,%.1f\n",v[9].to_f,v[10].to_f,v[8].to_f
i += 1
end
end
##########配列の要素############################################################
=begin
v[0] = a or s
v[1] = block_no
v[2] = name
v[3] = longitude
v[4] = latitude
v[5] = altitude
##########再配列################################################################
[0] = station_number
[1] = name
[2] = year
[3] = jan
[4] = feb
[5] = mar
[6] = apr
[7] = may
[8] = jun
[9] = jul
[10] = aug
[11] = sept
[12] = oct
[13] = nov
[14] = dec
[15] = lon
[16] = lat
[17] = alt
=end
※非常に時間がかかるので注意してください
加工データの表示
コマンドプロンプトを立ち上げて下さい。
「get_month_prec_a.rb」が存在するディレクトリまで移動します。
ここで、
ruby get_month_prec_a.rb
と打つと以下の様になっています。
Rubyを用いてa(測候所なし)の年降水量の取得
エディターを立ち上げ以下のように入力します。ファイル名は「get_annual_prec_csv.rb」として保存してください。
out_file = "c:/ronbun/data/prec_all_a.csv"
out = open(out_file,"w")
out.printf "statioin_number,name,year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec,Annual,longitude,latitude,altitude\n"
printf "statioin_number,name,year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec,Annual,longitude,latitude,altitude\n"
file = "c:/ronbun/data/month_prec_all.txt"
v = []
i = 0
open(file).each do |line|
if /statioin_number/ =~ line
##
else
v = line.chop.split(",")
i = v[3].to_f + v[4].to_f + v[5].to_f + v[6].to_f + v[7].to_f + v[8].to_f + v[9].to_f + v[10].to_f + v[11].to_f + v[12].to_f + v[13].to_f + v[14].to_f
printf "%s,%s,%4s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%.5f,%.5f,%.1f\n",v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],i,v[15],v[16],v[17]
out.printf "%s,%s,%4s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%.5f,%.5f,%.1f\n",v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],i,v[15],v[16],v[17]
end
end
##########配列の要素############################################################
=begin
[0] = station_number
[1] = name
[2] = year
[3] = jan
[4] = feb
[5] = mar
[6] = apr
[7] = may
[8] = jun
[9] = jul
[10] = aug
[11] = sept
[12] = oct
[13] = nov
[14] = dec
[15] = lon
[16] = lat
[17] = alt
##########配列の要素(再配列)####################################################
v[0] = station_number
v[1] = name
v[2] = year
v[3] = Jan
v[4] = Feb
v[5] = Mar
v[6] = Apr
v[7] = May
v[8] = Jun
v[9] = Jul
v[10] = Aug
v[11] = Sept
v[12] = Oct
v[13] = Nov
v[14] = Dec
v[15] = Annual
v[16] = longitude
v[17] = latitude
v[18] = altitude
=end
加工データの表示
コマンドプロンプトを立ち上げて下さい。
「get_annual_prec_csv.rb」が存在するディレクトリまで移動します。
ここで、
ruby get_annual_prec_csv.rb
と打つと以下の様になっています。
今回はここまでになります。
お疲れ様でした。
最終更新:2011年05月18日 15:09