FWIW, a version using a mutable structure to collect the data (*edited with rocco’s append! suggestion):
using CSV, DataFrames
file = raw"C:\Users\...\LAMMPS_test.data.txt"
Base.@kwdef mutable struct MyData
timestep::Vector{Int} = Int[]
natoms::Int = 0
boxsize::Matrix{Float64} = Array{Float64}(undef, 0, 0)
positions::DataFrame = DataFrame()
end
M = MyData()
# Read repeated items only once:
open(file) do f
readuntil(f, "ITEM: NUMBER OF ATOMS")
M.natoms = parse(Int, readuntil(f, "ITEM: BOX BOUNDS pp pp pp"))
M.boxsize = Matrix{Float64}(CSV.read(IOBuffer(readuntil(f, "ITEM: ATOMS")), DataFrame, header=false))
end
# Read varying items:
open(file) do f
readline(f)
while !eof(f)
push!(M.timestep, parse(Int, readuntil(f, "ITEM: NUMBER OF ATOMS")))
readuntil(f, "ITEM: ATOMS")
append!(M.positions, CSV.read(IOBuffer(readuntil(f, "ITEM: TIMESTEP")), DataFrame; delim=' ', ignorerepeated=true))
end
end
# Access loaded data with:
M.timestep
M.natoms
M.boxsize
M.positions
M.positions.zs