summaryrefslogtreecommitdiff
path: root/xport2json.rb
blob: aa1508d0a1d8b0e2ef12b8a0f1d035ac56e22d25 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'nokogiri'
require 'json'

FN = ARGV.shift.chomp

doc = File.open("#{FN}.xml") { |f| Nokogiri::XML(f) }

tstart = doc.css("xport>meta>start").text.to_i
tstep = doc.css("xport>meta>step").text.to_i

rcvd = []
rmax = 0
sent = []
smax = 0

doc.css("xport>data>row").each do |row|
  t = (row.css("t").text.to_i - tstart) / tstep
  r = row.css("v")[0].text.to_f
  s = row.css("v")[1].text.to_f

  rmax = r if r > rmax
  smax = s if s > smax

  rcvd << { t: t, v: r }
  sent << { t: t, v: s }
end

rmax *= 1.10
smax *= 1.10

rcvd.each do |r|
  r[:v] /= rmax
end

sent.each do |s|
  s[:v] /= smax
end

File.open("#{FN}.rcvd.json", "w") do |f|
  f.puts({
    max: rmax,
    data: rcvd
  }.to_json)
end

File.open("#{FN}.sent.json", "w") do |f|
  f.puts({
    max: smax,
    data: sent
  }.to_json)
end