Skip to content

Commit

Permalink
Merge pull request #18 from mauro3/m3/fix-open-file
Browse files Browse the repository at this point in the history
Left the file open in my last PR
  • Loading branch information
jguerber authored Jun 6, 2024
2 parents 4d0fa2c + 467cfe5 commit 5375b19
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,31 @@ Looks in `header` for a number of lines to ignore, then writes the following lin
"""
function _read_data(filename::AbstractString, header::Dict{String, Any})
# only store data lines in a variable
io = open(filename)
# read the header
[readline(io) for i=1:header["nlines"]]

# now read the rest of the file
raw = split(read(io, String))

if header["datatype"] == Any # if datatype is undetermined yet
ncheck = min(header["nrows"]*header["ncols"], 100) # check 100 numbers or less
found_float = false
for i in 1:ncheck
if match(r"[.]", raw[i]) !== nothing
found_float = true
break
out = open(filename) do io
# read the header and discard to move the file pointer to the right place
[readline(io) for i=1:header["nlines"]]

# now read the rest of the file
raw = split(read(io, String))

if header["datatype"] == Any # if datatype is undetermined yet
ncheck = min(header["nrows"]*header["ncols"], 100) # check 100 numbers or less
found_float = false
for i in 1:ncheck
if match(r"[.]", raw[i]) !== nothing
found_float = true
break
end
end

datatype = found_float ? Float32 : Int32
else
datatype = header["datatype"]
end

datatype = found_float ? Float32 : Int32
else
datatype = header["datatype"]
return parse.(datatype, raw)
end

out = parse.(datatype, raw)

return permutedims(reshape(out, header["ncols"], header["nrows"]))
end

Expand Down

0 comments on commit 5375b19

Please sign in to comment.