Skip to content

Commit

Permalink
use DiskArrays.estimate_chunksize for contiguous arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Oct 17, 2023
1 parent e07f756 commit 34f48c6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ therefore [considers variables with unlimited dimension as `:contiguous`](https:
"""
function chunking(v::Variable)
storage,chunksizes = nc_inq_var_chunking(v.ds.ncid,v.varid)
# TODO: NCDatasets 0.14: return a tuple for chunksizes
return storage,reverse(chunksizes)
end

Expand All @@ -259,6 +260,7 @@ export chunking
# same as `chunking` except that for NetCDF3 file the unlimited dimension is considered
# as chunked
# https://github.com/Unidata/netcdf-c/discussions/2224
# Also the chunksizes is always a tuple.
function _chunking(v::Variable{T,N}) where {T,N}
ncid = v.ds.ncid
varid = v.varid
Expand Down Expand Up @@ -445,14 +447,20 @@ function _write_data_to_nc(v::Variable, data, indexes::Union{AbstractRange{<:Int
return _write_data_to_nc(v, data, ind...)
end

getchunksize(v::Variable) = getchunksize(haschunks(v),v)
getchunksize(::DiskArrays.Chunked, v::Variable) = _chunking(v)[2]
# getchunksize(::DiskArrays.Unchunked, v::Variable) = DiskArrays.estimate_chunksize(v)
getchunksize(::DiskArrays.Unchunked, v::Variable) = size(v)
function eachchunk(v::Variable)
# storage will be reported as chunked for variables with unlimited dimension
# by _chunking and chunksizes will 1 for the unlimited dimensions
storage, chunksizes = _chunking(v)
if storage == :contiguous
return DiskArrays.estimate_chunksize(v)
else
return DiskArrays.GridChunks(v, chunksizes)
end
end
haschunks(v::Variable) = (_chunking(v)[1] == :contiguous ? DiskArrays.Unchunked() : DiskArrays.Chunked())

eachchunk(v::CFVariable) = eachchunk(v.var)
haschunks(v::CFVariable) = haschunks(v.var)
eachchunk(v::Variable) = DiskArrays.GridChunks(v, Tuple(getchunksize(v)))
haschunks(v::Variable) = (_chunking(v)[1] == :contiguous ? DiskArrays.Unchunked() : DiskArrays.Chunked())

_normalizeindex(n,ind::Base.OneTo) = 1:1:ind.stop
_normalizeindex(n,ind::Colon) = 1:1:n
Expand Down

0 comments on commit 34f48c6

Please sign in to comment.