Skip to content

Commit 73d598f

Browse files
in-place load! for multi-file datasets
1 parent e95dc06 commit 73d598f

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/CatArrays.jl

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,42 @@ function CatArray(dim::Int,arrays...)
7474
sz)
7575
end
7676

77-
78-
function Base.getindex(CA::CatArray{T,N},idx...) where {T,N}
79-
checkbounds(CA,idx...)
80-
81-
sz = _shape_after_slice(size(CA),idx...)
77+
function load!(CA::CatArray{T,N},B,idx...) where {T,N}
8278
idx_global_local = index_global_local(CA,idx)
83-
B = Array{T,length(sz)}(undef,sz...)
8479

8580
for (array,(idx_global,idx_local)) in zip(CA.arrays,idx_global_local)
8681
if valid_local_idx(idx_local...)
8782
# get subset from subarray
88-
subset = array[idx_local...]
89-
B[idx_global...] = subset
83+
subset = @view array[idx_local...]
84+
B[idx_global...] .= subset
9085
end
9186
end
9287

93-
if sz == ()
94-
# scalar
95-
return B[]
96-
else
97-
return B
88+
return B
89+
end
90+
91+
function Base.getindex(CA::CatArray{T,N},idx::Integer...) where {T,N}
92+
checkbounds(CA,idx...)
93+
idx_global_local = index_global_local(CA,idx)
94+
B = Ref{T}()
95+
96+
for (array,(idx_global,idx_local)) in zip(CA.arrays,idx_global_local)
97+
if valid_local_idx(idx_local...)
98+
B[] = array[idx_local...]
99+
end
98100
end
101+
102+
return B[]
103+
end
104+
105+
function Base.getindex(CA::CatArray{T,N},idx...) where {T,N}
106+
checkbounds(CA,idx...)
107+
108+
sz = _shape_after_slice(size(CA),idx...)
109+
B = Array{T,length(sz)}(undef,sz...)
110+
111+
load!(CA,B,idx...)
112+
return B
99113
end
100114

101115
Base.size(CA::CatArray) = CA.sz

src/multifile.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ end
160160
Base.getindex(v::MFVariable,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) = getindex(v.var,indexes...)
161161
Base.setindex!(v::MFVariable,data,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) = setindex!(v.var,data,indexes...)
162162

163+
164+
load!(v::MFVariable,buffer,indexes...) = CatArrays.load!(v.var,buffer,indexes...)
165+
163166
Base.size(v::MFVariable) = size(v.var)
164167
Base.size(v::MFCFVariable) = size(v.var)
165168
dimnames(v::MFVariable) = v.dimnames

test/test_multifile.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ fnames = example_file.(TDS,1:3,A)
132132

133133
varname = "var"
134134

135+
#deferopen = false
135136
for deferopen in (false,true)
136137
local mfds, data
137138
local lon
138-
local buf, ds_merged, fname_merged, var
139+
local buf, ds_merged, fname_merged, var, ncv
139140

140141
mfds = TDS(fnames, deferopen = deferopen);
141142

@@ -184,14 +185,13 @@ for deferopen in (false,true)
184185
@test mfds["lon"][1:1] == ds_merged["lon"][:]
185186
close(ds_merged)
186187

187-
#=
188-
# save subset of aggregated file (deprecated)
189-
fname_merged = tempname()
190-
write(fname_merged,mfds,idimensions = Dict("lon" => 1:1))
191-
ds_merged = TDS(fname_merged)
192-
@test mfds["lon"][1:1] == ds_merged["lon"][:]
193-
close(ds_merged)
194-
=#
188+
189+
# in-place load
190+
ncv = mfds[varname].var
191+
buffer = zeros(eltype(ncv),size(ncv))
192+
load!(ncv,buffer,:,:,:)
193+
@test buffer == C
194+
195195
# show
196196
buf = IOBuffer()
197197
show(buf,mfds)

0 commit comments

Comments
 (0)