filename = 
total = {  => 0,  => 0,  => 0.0 }
invalid_count = 0
max_invalid_count = 0
invalid_run_end = 

File.new(filename).each do |line|
  num_readings = 0
  num_good_readings = 0
  sum_readings = 0.0

  fields = line.split
  fields[1..-1].each_slice(2) do |reading, flag|
    num_readings += 1
    if Integer(flag) > 0
      num_good_readings += 1
      sum_readings += Float(reading)
      invalid_count = 0
    else
      invalid_count += 1
      if invalid_count > max_invalid_count
        max_invalid_count = invalid_count
        invalid_run_end = fields[0]
      end
    end
  end

  printf ,
    fields[0], num_readings - num_good_readings, num_good_readings, sum_readings,
    num_good_readings > 0? sum_readings/num_good_readings: 0.0

  total[] += num_readings
  total[] += num_good_readings
  total[] += sum_readings
end

puts 
puts 
printf , total['sum_readings']
puts 
printf , total['sum_readings']/total['num_good_readings']
puts 
puts