Skip to content

Commit

Permalink
Incorporated latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Wietek committed Sep 10, 2024
1 parent 38f7845 commit c57256b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Alex Wietek <[email protected]> and contributors"]
version = "1.0.0-DEV"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

Expand Down
4 changes: 3 additions & 1 deletion src/Dumper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module Dumper
using HDF5
using Printf

export DumpFile, write_data!, dump!, read_data, FileCollection, filenames, read_collection_h5
import Base.keys, Base.read
export DumpFile, write_data!, dump!, read_data
export FileCollection, files

include("create_extensible.jl")
include("append_extensible.jl")
Expand Down
71 changes: 65 additions & 6 deletions src/file_collection.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import DataStructures: OrderedDict

struct FileCollection
directory::AbstractString
regex::Regex
end

function filenames(collection::FileCollection)
"""
files
Returns which files are matched by the file collection
# Arguments
- `collection::FileCollection`: the file collection to be matched
"""
function files(collection::FileCollection)
files = readdir(collection.directory)
matched_files = String[]
for file in files
Expand All @@ -15,9 +25,56 @@ function filenames(collection::FileCollection)
return map(fl -> joinpath(collection.directory, fl), matched_files)
end

function read_collection_h5(collection::FileCollection, tag::AbstractString)
"""
keys
Returns which keys are defined in the hdf5 files
# Arguments
- `collection::FileCollection`: the file collection for which to get the keys
"""
function keys(collection::FileCollection)
files = readdir(collection.directory)
data = OrderedDict()

for file in files
m = match(collection.regex, file)
if !isnothing(m)
if length(m) == 1
param = m[Base.keys(m)[1]]
else
param = ntuple(i -> m[Base.keys(m)[i]], length(m))
end

# read hdf5 data
file = joinpath(collection.directory, file)
try
f = h5open(file)
keys = Base.keys(f)
data[param] = keys
catch e
showerror(stdout, e)
println()
println("cannot read from file", file)
end
end
end
return data
end


"""
read
reads the data from a file collection given a key
# Arguments
- `collection::FileCollection`: the file collection to be matched
- `key::String`: key for which the data is supposed to be read
"""
function Base.read(collection::FileCollection, key::String)
files = readdir(collection.directory)
data = Dict()
data = OrderedDict()
for file in files
m = match(collection.regex, file)
if !isnothing(m)
Expand All @@ -30,13 +87,15 @@ function read_collection_h5(collection::FileCollection, tag::AbstractString)
# read hdf5 data
file = joinpath(collection.directory, file)
try
dset = h5read(file, tag)
dset = h5read(file, key)
ndims = length(size(dset))
data[param] = permutedims(dset, ndims:-1:1)
catch
catch e
showerror(stdout, e)
println()
println("cannot read from file", file)
end
end
end
return sort(collect(data), by = x->x[1])
return data
end

0 comments on commit c57256b

Please sign in to comment.