Skip to content

Instantly share code, notes, and snippets.

@zunda
Created December 19, 2009 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zunda/259984 to your computer and use it in GitHub Desktop.
Save zunda/259984 to your computer and use it in GitHub Desktop.
set term push
set output 'distribution2009.png'
set term png transparent small size 320,240
set xdata time
set timefmt "%H:%M:%S"
set xtics 7200
set mxtics 4
set xlabel 'Chip time (hh:mm)'
set ylabel 'Finishers (/sec)'
plot 'distribution2009.dat' using 1:2 with lines not
unset output
set term pop
#!/usr/bin/env ruby
#
# formats places and times of finishers
#
# usage: ruby distribution.rb honolulu.htm
#
# Copyright (C) 2009 zunda <zunda at freeshell.org>
#
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#
require 'honolulu'
r = Results.read(ARGF)
s = r.data.reject{|e| e['Chip Time'].empty?}.sort_by{|e| e['Chip Time'].hms_to_sec}
tstep = 1200.0
tmax = (s[0]['Chip Time'].hms_to_sec / tstep).ceil * tstep
puts "#middle-chip-time(sec) count(/sec)"
count = 0
s.each do |e|
if e['Chip Time'].hms_to_sec > tmax
puts "#{(tmax - tstep/2).sec_to_hms} #{count.to_f/tstep}"
count = 0
tmax += tstep
else
count += 1
end
end
if count > 0
puts "#{(tmax - tstep/2).sec_to_hms} #{count.to_f/tstep}"
end
#!/usr/bin/env ruby
#
# honolulu.rb a library to read results from JAL Honolulu Marathon at
# http://results.sportstats.ca/res2009/honolulu.htm
#
# usage:
# r = Result.read(stream)
# p r.data
#
# Copyright (C) 2009 zunda <zunda at freeshell.org>
#
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#
# Utilities
class String
def hms_to_sec
ary = scan(/[\d\.]+/)
unless ary.empty?
ary.inject(0.0){|r, i| r*60 + i.to_f}
else
nil
end
end
end
class Numeric
def sec_to_hms
x, s = self.divmod(60)
r = '%04.1f' % s.to_s
while x > 0
x, e = x.divmod(60)
r = '%02.0f:' % e + r
end
return r
end
end
# A column in input file
class Column
attr_accessor :pos, :width, :name
def initialize(pos, width)
@pos = pos
@width = width
@name = ''
end
end
# Parsed data from input file
class Results
attr_reader :data
def Results.read(stream)
r = Results.new
stream.each_line do |line|
r.parse(line)
end
r
end
def initialize
@stage = :pre_header
@data = Array.new
end
def parse(line)
line = line.chomp
case @stage
when :pre_header
if /\A-[- ]*-\Z/ =~ line
get_column_positions(line)
@stage = :in_header
end
when :in_header
if /\A-[- ]*-\Z/ =~ line
@stage = :post_header
@cols[11].width -= 1
@cols[12].pos -= 1
@cols[12].width += 1
else
get_column_names(line)
end
when :post_header
@data << get_datum(line)
end
end
def get_column_positions(line)
@cols = Array.new
pos = 0
line.split(/ /).each do |col|
width = col.size
@cols << Column.new(pos, width)
pos += width + 1
end
end
def split(line)
@cols.map{|col| line[col.pos, col.width]}
end
def get_column_names(line)
split(line).zip(@cols) do |str, col|
col.name += ' ' unless col.name.empty?
col.name += str.strip
end
end
def get_datum(line)
r = Hash.new
@cols.map{|e| e.name}.zip(split(line).map{|e| e.strip}).each do |n, d|
r[n] = d
end
r
end
end
#!/usr/bin/env ruby
#
# calculates statistics of finishers
#
# usage: ruby stat.rb honolulu.htm
#
# Copyright (C) 2009 zunda <zunda at freeshell.org>
#
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#
require 'honolulu'
r = Results.read(ARGF)
n = 0
sum = 0.0
sum2 = 0.0
r.data.map do |e|
sec = e['Chip Time'].hms_to_sec
if sec
sum += sec
sum2 += sec**2
n += 1
end
end
av = sum/n
si = Math::sqrt(sum2/n - av**2)
puts "#{av.sec_to_hms} +- #{si.sec_to_hms}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment