-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration_reader.jl
77 lines (63 loc) · 1.98 KB
/
configuration_reader.jl
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#__precompile__()
module ConfigurationReader
import Base.read
import Base.eof
import Base.close
export SplitInput, read, eof, close, proceed, lookup, read_configuration
export snapshot_reader
type SplitInput <: IO
istream::IO
line
buffer::IOBuffer
split_fun::Function
SplitInput(istream::IO, line, buffer::IOBuffer, split_fun) = new(istream, line,buffer,split_fun)
SplitInput(istream::IO, line,split_fun) = SplitInput(istream, line, IOBuffer(line),split_fun)
SplitInput(istream::IO,split_fun) = SplitInput(istream, readline(istream), split_fun)
end
function read(lbio::SplitInput, dtype::Type{UInt8})
ret = read(lbio.buffer,dtype)
if eof(lbio.buffer)
lbio.line = readline(lbio.istream)
lbio.buffer = IOBuffer(lbio.line)
end
return ret
end
function eof(lbio::SplitInput)
return lbio.split_fun(lbio.line) || (eof(lbio.buffer) && eof(lbio.istream))
end
function proceed(lbio::SplitInput)
lbio.line = readline(lbio.istream)
lbio.buffer = IOBuffer(lbio.line)
end
function lookup(lbio::SplitInput)
return lbio.line
end
function close(lbio::SplitInput)
end
function parse_timestamp_header(line)
minimal_len = length("time = 0")
if length(line) <= minimal_len
error("could not parse timestamp: ``$line''")
end
return parse(Float64,line[minimal_len:end])
end
function read_configuration(istream::IO)
arr = readcsv(istream)
#TODO: change output format
#arr = arr[:,1:end-1] #remove last column
header = Array{AbstractString,1}(arr[1,:][:])
data = arr = Array{Float64,2}(arr[2:end,:])
return header,data
end
function snapshot_reader(ch::Channel, istream::IO)
time_header = readline(istream)
split_on_time_header(line) = length(line) >= 4 && "time" == line[1:4]
while !eof(istream)
splitted_istream = SplitInput(istream, split_on_time_header)
data_header, data = read_configuration(splitted_istream)
timestamp = parse_timestamp_header(time_header)
put!(ch, (timestamp,data_header,data))
time_header = lookup(splitted_istream)
end
end
end