modularize: typedef, parser
This commit is contained in:
parent
658c15cb80
commit
881d542a30
7 changed files with 122 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
Manifest.toml
|
10
Project.toml
Normal file
10
Project.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
name = "CoordVisualize"
|
||||
uuid = "4c41ebcf-33aa-4478-9aac-83d12758d145"
|
||||
authors = ["qwjyh <urataw421@gmail.com>"]
|
||||
version = "0.1.0"
|
||||
|
||||
[deps]
|
||||
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
|
||||
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
|
||||
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
|
||||
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
|
6
README.adoc
Normal file
6
README.adoc
Normal file
|
@ -0,0 +1,6 @@
|
|||
= CoordVisualize
|
||||
|
||||
== Parsing log, taggin and exporting
|
||||
|
||||
== Reading data & Visualization, inspecting
|
||||
|
8
src/CoordVisualize.jl
Normal file
8
src/CoordVisualize.jl
Normal file
|
@ -0,0 +1,8 @@
|
|||
module CoordVisualize
|
||||
|
||||
using Dates
|
||||
|
||||
include("typedef.jl")
|
||||
include("parser.jl")
|
||||
|
||||
end # end module CoordVisualize
|
90
src/parser.jl
Normal file
90
src/parser.jl
Normal file
|
@ -0,0 +1,90 @@
|
|||
using CoordVisualize: CoordLog
|
||||
using Dates
|
||||
|
||||
"""
|
||||
parse_log(filepath::AbstractString; interactive=false)::Vector{CoordLog}
|
||||
|
||||
Parse raw coordinates log from tracecoords to vector of `CoordLog`.
|
||||
Each logging is converted to one `CoordLog`.
|
||||
|
||||
If keyword argument `interactive` is set to `true`, prompts will be shown to
|
||||
receive custom notes for each `CoordLog`.
|
||||
Otherwise the note is set to ""(empty string).
|
||||
"""
|
||||
function parse_log(filepath::AbstractString; interactive=false)::Vector{CoordLog}
|
||||
istracing::Bool = false
|
||||
coords_trace = Vector{Vector{Float64}}(undef, 0) # SVector ?
|
||||
ret = Vector{CoordLog}()
|
||||
log_date = DateTime(0)
|
||||
for (i, l) in enumerate(readlines(filepath))
|
||||
# skip logs not from tracecoord
|
||||
if match(r"^\[TRACECOORDS\]", l) |> isnothing
|
||||
continue
|
||||
end
|
||||
if !istracing
|
||||
# not tracing
|
||||
|
||||
# if starting
|
||||
if match(r"Logging starting", l) |> !isnothing
|
||||
@debug "Logging started at l:$(i)"
|
||||
istracing = true
|
||||
coords_trace = Vector{Vector{Float64}}(undef, 0) # SVector ?
|
||||
log_date = try
|
||||
s = match(r"\".+\"").match
|
||||
parse(DateTime, s[2:end-1])
|
||||
catch e
|
||||
@error "Failed to parse date at line $(i), file $(filepath)"
|
||||
DateTime(0)
|
||||
end
|
||||
end
|
||||
else
|
||||
if match(r"Logging stopping", l) |> !isnothing
|
||||
@debug "Logging stopped at l:$(i)"
|
||||
istracing = false
|
||||
|
||||
# get note in interactive parsing
|
||||
if interactive
|
||||
println("type note for the log at $(log_date)UTC")
|
||||
note = readline()
|
||||
else
|
||||
note = ""
|
||||
end
|
||||
|
||||
coords_trace = mapreduce(permutedims, vcat, coords_trace)
|
||||
push!(
|
||||
ret,
|
||||
CoordLog(coords_trace, log_date, note)
|
||||
)
|
||||
|
||||
continue
|
||||
end
|
||||
|
||||
# skip non coordinates
|
||||
if match(r"Coordinate:", l) |> isnothing
|
||||
continue
|
||||
end
|
||||
|
||||
# parse coordinates
|
||||
coord = try
|
||||
match(r"\(.*\)", l).match |>
|
||||
(s -> split(s[2:end-1], ',')) .|>
|
||||
(x -> parse(Float64, x))
|
||||
catch e
|
||||
error("Failed to parse coordinate at line $(i)")
|
||||
end
|
||||
push!(coords_trace, coord)
|
||||
end
|
||||
end
|
||||
ret
|
||||
end
|
||||
|
||||
function parse_log(filepaths::Vector{T}; interactive=false)::Vector{CoordLog} where {T <: AbstractString}
|
||||
map(filepaths) do filepath
|
||||
parse_log(filepath; interactive=interactive)
|
||||
end |> Iterators.flatten |> collect
|
||||
end
|
||||
CoordLog(
|
||||
[1.0 2.0 3.0; 4.0 5.0 6.0],
|
||||
Dates.now(),
|
||||
"a"
|
||||
)
|
7
src/typedef.jl
Normal file
7
src/typedef.jl
Normal file
|
@ -0,0 +1,7 @@
|
|||
using Dates
|
||||
struct CoordLog{T <: AbstractFloat}
|
||||
coords::Matrix{T}
|
||||
logdate::DateTime
|
||||
note::String
|
||||
end
|
||||
|
0
test/runtests.jl
Normal file
0
test/runtests.jl
Normal file
Loading…
Reference in a new issue