Skip to content

Instantly share code, notes, and snippets.

@cockscomb
Created September 26, 2011 09:05
Show Gist options
  • Save cockscomb/1241900 to your computer and use it in GitHub Desktop.
Save cockscomb/1241900 to your computer and use it in GitHub Desktop.
Getting per 10 minutes wind data from Kishocho.
# encoding: UTF-8
require 'csv'
require 'date'
require_relative 'kishocho'
include Kishocho
# puts Kishocho.get_10_min_data(1192, Date.new(2010, 5, 1))
file_name = 'oota_2010-5.csv'
dates = []
(1..31).each do |day|
dates << Date.new(2010, 5, day)
end
CSV.open(file_name, 'w:UTF-8', headers: true) do |csv|
csv << Kishocho::HEADER
dates.each do |date|
Kishocho.get_10_min_data(1192, date) do |data|
csv << data
end
sleep 1
end
end
# encoding: UTF-8
module Kishocho
require 'nokogiri'
require 'open-uri'
require 'date'
HEADER = ['Time', 'Precipitation', 'Temperature',
'Average Wind Velocity', 'Average Wind Direction',
'Largest Wind Velocity', 'Largest Wind Direction', 'Sunshine']
def get_10_min_data(location, date)
column = []
uri = "http://www.data.jma.go.jp/obd/stats/etrn/view/10min_a1.php?block_no=#{location}&year=#{date.year}&month=#{date.month}&day=#{date.day}"
open(uri, 'r:Shift_JIS') do |data|
doc = Nokogiri::HTML(data)
doc.xpath('//tr[@class="mtx"][td]').each do |tr|
row = tr.xpath('td').map { |td| td.content }
datestring = date.strftime('%Y-%m-%dT') + row[0].to_s + ':00+09:00'
row[0] = DateTime.parse(datestring)
if block_given?
yield row
else
column << row
end
end
end
column
end
module_function :get_10_min_data
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment