Skip to content

Commit 7f61797

Browse files
maintain type of unpacked variable when using defVar
1 parent 4c83753 commit 7f61797

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

src/subvariable.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ defAttrib(v::SubVariable,name::SymbolOrString,data) = defAttrib(v.parent,name,da
1717
function SubVariable(A::AbstractVariable,indices...)
1818
var = nothing
1919
if hasproperty(A,:var)
20-
if hasproperty(A.var,:attrib)
20+
if hasmethod(SubVariable,Tuple{typeof(A.var),typeof.(indices)...})
2121
var = SubVariable(A.var,indices...)
2222
end
2323
end
24+
2425
T = eltype(A)
2526
N = length(size_getindex(A,indices...))
2627
return SubVariable{T,N,typeof(A),typeof(indices),typeof(A.attrib),typeof(var)}(

src/variable.jl

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,65 @@ Defines and return the variable in the data set `ds`
203203
copied from the variable `src`. The dimension name, attributes
204204
and data are copied from `src` as well as the variable name (unless provide by `name`).
205205
"""
206-
function defVar(dest::AbstractDataset,name::SymbolOrString,srcvar::AbstractVariable; kwargs...)
207-
v = defVar(dest,name,
208-
Array(srcvar),
209-
dimnames(srcvar),
210-
attrib=attribs(srcvar); kwargs...)
211-
return v
206+
function defVar(dest::AbstractDataset,varname::SymbolOrString,srcvar::AbstractVariable; kwargs...)
207+
_ignore_checksum = false
208+
if haskey(kwargs,:checksum)
209+
_ignore_checksum = kwargs[:checksum] === nothing
210+
end
211+
212+
src = dataset(srcvar)
213+
214+
# dimensions
215+
unlimited_dims = unlimited(src)
216+
217+
for dimname in dimnames(srcvar)
218+
if dimname in _dimnames_recursive(dest)
219+
# dimension is already defined
220+
continue
221+
end
222+
223+
if dimname in unlimited_dims
224+
defDim(dest, dimname, Inf)
225+
else
226+
defDim(dest, dimname, dim(src,dimname))
227+
end
228+
end
229+
230+
var = srcvar.var
231+
dimension_names = dimnames(var)
232+
cfdestvar = defVar(dest, varname, eltype(var), dimension_names;
233+
attrib = attribs(var))
234+
destvar = variable(dest,varname)
235+
236+
storage,chunksizes = chunking(var)
237+
@debug "chunking " name(var) size(var) size(cfdestvar) storage chunksizes
238+
chunking(cfdestvar,storage,chunksizes)
239+
240+
isshuffled,isdeflated,deflate_level = deflate(var)
241+
@debug "compression" isshuffled isdeflated deflate_level
242+
deflate(cfdestvar,isshuffled,isdeflated,deflate_level)
243+
244+
if !_ignore_checksum
245+
checksummethod = checksum(var)
246+
@debug "check-sum" checksummethod
247+
checksum(cfdestvar,checksummethod)
248+
end
249+
250+
# copy data
251+
# TODO use DiskArrays.eachchunk
252+
# if hasmethod(eachchunk,Tuple{typeof(var)})
253+
# for indices in eachchunk(var)
254+
# destvar[indices...] = var[indices...]
255+
# end
256+
# else
257+
indices = ntuple(i -> axes(var,i),ndims(var))
258+
destvar[indices...] = var[indices...]
259+
#end
260+
261+
return cfdestvar
212262
end
213263

264+
214265
function defVar(dest::AbstractDataset,srcvar::AbstractVariable; kwargs...)
215266
defVar(dest,name(srcvar),srcvar; kwargs...)
216267
end
@@ -261,8 +312,13 @@ end
261312

262313

263314
chunking(v::AbstractVariable) = (:contiguous,size(v))
315+
chunking(v::AbstractVariable,storage,chunksizes) = nothing
316+
264317
deflate(v::AbstractVariable) = (false,false,0)
318+
deflate(v::AbstractVariable,isshuffled,isdeflated,deflate_level) = nothing
319+
265320
checksum(v::AbstractVariable) = :nochecksum
321+
checksum(v::AbstractVariable,checksummethod) = nothing
266322

267323
fillvalue(v::AbstractVariable{T}) where T = v.attrib["_FillValue"]::T
268324

test/test_multifile.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import CommonDataModel:
1010
Attributes,
1111
CatArrays,
1212
Dimensions,
13+
MemoryDataset,
1314
checksum,
1415
chunking,
1516
dataset,

0 commit comments

Comments
 (0)