-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.rb
37 lines (31 loc) · 1.34 KB
/
stats.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'rexml/document'
require 'time'
require 'date'
kml = REXML::Document.new(File.read('earthquakes.kml'))
quakes = {1 => {},2 => {},3 => {},4 => {},4 => {},5 => {},6 => {},7 => {}}
mindate = maxdate = nil
totals = {}
# Create a hash of all shocks keyed by date
kml.elements.each('//Placemark') do |placemark|
date = Date.parse(Time.parse(placemark.elements['ExtendedData/Data[@name="date"]/value'].text).localtime.to_s)
mindate = date if mindate.nil? or date < mindate
maxdate = date if maxdate.nil? or date > maxdate
mag = placemark.elements['ExtendedData/Data[@name="mag"]/value'].text.to_f.floor
quakes[mag][date] = [] if quakes[mag][date].nil?
quakes[mag][date] << placemark
totals[mag] = 0 if totals[mag].nil?
totals[mag] += 1
end
chxl = "0:|#{(mindate..maxdate).collect{|d|d.strftime('%b+%d')}.join('|')}|"
chd = [2,3,4,5,6,7].collect do |mag|
(mindate..maxdate).collect do |date|
q = quakes[mag][date]
q.nil? ? 0 : q.count
end.join(',')
end.join('|')
puts "<img width=\"800\" height=\"200\" src=\"http://chart.apis.google.com/chart?cht=bvs&chs=800x200&chxt=x,y&chbh=30,10&chco=67dddd,6991fd,00e64d,fdf569,ff9900,fd7567&chm=N,000000,-1,,12&chtt=Quake+count+by+day&chd=t:#{chd}&chxl=#{chxl}\"/>"
puts '<ul class="totals">'
totals.sort.each do |mag,total|
puts "<li><strong>M#{mag} total:</strong> #{total}</li>"
end
puts "</ul>"