Created
April 19, 2012 18:36
-
-
Save jweir/2422889 to your computer and use it in GitHub Desktop.
Daniel's pollution and position converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Script to load GPS and CO2 data and normalize the data | |
# the position data in a file "run.xml" | |
# pollution in a file "pollution.txt" | |
require 'nokogiri' | |
require 'fastercsv' | |
require 'active_support' | |
run_csv = [] | |
run_data = Nokogiri::XML File.read('run.xml') | |
run_data.remove_namespaces! | |
run_data.xpath('//trkpt').each do |point| | |
row = [] | |
row << Time.parse(point.xpath('time').text).utc | |
row << point.attr('lat') | |
row << point.attr('lon') | |
row << point.xpath('ele').text | |
run_csv << row | |
end | |
# puts run_csv.join("\n") | |
pollution_table = [] | |
pollution_data = FasterCSV.parse File.read('pollution.txt') | |
pollution_data.shift # loose the header | |
pollution_data.each do |row| | |
date, time = row[1].split(" ") | |
timestamp = Time.parse "#{date.split("/").reverse.join("/")} #{time}" | |
pollution_table << [(timestamp+4.hours).utc, row[2]] | |
end | |
# puts pollution_table.join("\n") | |
start = run_csv[0][0] | |
finish = run_csv[-1][0] | |
range = start..finish | |
final = [["timestamp","lat","lon","ele","co2"].join(",")] | |
range.each do |p| | |
row = [p,nil,nil,nil,nil] | |
run = run_csv.detect {|r| r[0] == p} || [] | |
run.each_with_index do |c,i| | |
row[i] = c | |
end | |
pol = pollution_table.detect {|r| r[0] == p} || [] | |
pol.shift | |
pol.each_with_index do |c,i| | |
row[i+4] = c | |
end | |
final << row.join(",") | |
end | |
puts final.join("\n") | |
File.open("out.csv","w") {|f| f << final.join("\n")} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment