-
Notifications
You must be signed in to change notification settings - Fork 33
/
filestack.jl
51 lines (41 loc) · 1.72 KB
/
filestack.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
"""
FileStack{S,Na}
FileStack{S,Na}(filename, types, sizes, eachchunk, haschunks, write)
A wrapper object that holds file pointer and size/chunking
metadata for a multi-layered stack stored in a single file,
typically netcdf or hdf5.
`S` is a backend type like `NCDsource`, and `Na` is a tuple of `Symbol` keys.
"""
struct FileStack{S,Na,T,SZ,G<:Union{AbstractString,Symbol,Nothing},EC,HC}
filename::String
sizes::SZ
group::G
eachchunk::EC
haschunks::HC
write::Bool
end
function FileStack{S,Na,T}(
filename::AbstractString, sizes::SZ, group::G, eachchunk::EC, haschunks::HC, write::Bool
) where {S,Na,T,SZ,G,EC,HC}
FileStack{S,Na,T,SZ,G,EC,HC}(String(filename), sizes, group, eachchunk, haschunks, write)
end
# FileStack has `S,Na,T` parameters that are not recoverable from fields.
ConstructionBase.constructorof(::Type{<:FileStack{S,Na,T}}) where {S,Na,T} = FileStack{S,Na,T}
filename(fs::FileStack) = fs.filename
DD.name(::FileStack{<:Any,Na}) where Na = Na
DD.data_eltype(::FileStack{<:Any,<:Any,T}) where T = T
Base.eltype(::FileStack{<:Any,<:Any,T}) where T = T
Base.keys(::FileStack{<:Any,Na}) where Na = Na
Base.values(fs::FileStack{<:Any,Na}) where Na = (fs[n] for n in Na)
# Indexing FileStack returns a FileArray,
# referencing a specific name in the same file.
function Base.getindex(fs::FileStack{S,Na,T}, name::Symbol) where {S,Na,T}
is = NamedTuple{Na}(ntuple(identity, length(Na)))
i = is[name]
size = fs.sizes[i]
eachchunk = fs.eachchunk[i]
haschunks = fs.haschunks[i]
N = length(size)
return FileArray{S,_itype(T, i),N}(filename(fs), size, name, fs.group, eachchunk, haschunks, fs.write)
end
@inline _itype(::Type{<:NamedTuple{<:Any,T}}, i) where T = T.parameters[i]