Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Alexander-Barth/NCDatasets.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Oct 27, 2023
2 parents 3cb160f + a0ca027 commit d589989
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 deletions.
20 changes: 13 additions & 7 deletions src/netcdf_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,6 @@ function nc_inq_var_fletcher32(ncid::Integer,varid::Integer)
end

function nc_def_var_chunking(ncid::Integer,varid::Integer,storage,chunksizes)

check(ccall((:nc_def_var_chunking,libnetcdf),Cint,(Cint,Cint,Cint,Ptr{Csize_t}),ncid,varid,NCConstants[storage],chunksizes))
end

Expand Down Expand Up @@ -1236,13 +1235,20 @@ function nc_inq_unlimdims(ncid::Integer)
return unlimdimids
end

# function nc_inq_format(ncid::Integer,formatp)
# check(ccall((:nc_inq_format,libnetcdf),Cint,(Cint,Ptr{Cint}),ncid,formatp))
# end
function nc_inq_format(ncid::Integer)
formatp = Ref(Cint(0))
check(ccall((:nc_inq_format,libnetcdf),Cint,(Cint,Ptr{Cint}),ncid,formatp))
return formatp[]
end

# function nc_inq_format_extended(ncid::Integer,formatp,modep)
# check(ccall((:nc_inq_format_extended,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint}),ncid,formatp,modep))
# end
function nc_inq_format_extended(ncid::Integer)
formatp = Ref(Cint(0))
modep = Ref(Cint(0))

check(ccall((:nc_inq_format_extended,libnetcdf),Cint,(Cint,Ptr{Cint},Ptr{Cint}),ncid,formatp,modep))

return formatp[], modep[]
end

"""
Define the dimension with the name NAME and the length LEN in the
Expand Down
67 changes: 61 additions & 6 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,62 @@ chunking(v::Variable,storage,chunksizes) = nc_def_var_chunking(v.ds.ncid,v.varid
Return the storage type (`:contiguous` or `:chunked`) and the chunk sizes
of the varable `v`.
Note that `chunking` reports the same information as `nc_inq_var_chunking` and
therefore [considers variables with unlimited dimension as `:contiguous`](https://github.com/Unidata/netcdf-c/discussions/2224).
"""
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


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
sz = size(v)

format = nc_inq_format(ncid)

if format in (NC_FORMAT_NC3, NC_FORMAT_64BIT, NC_FORMAT_CDF5)
# normally there should be max. 1 unlimited dimension for NetCDF 3 files
unlimdim_ids = nc_inq_unlimdims(ncid)

if length(unlimdim_ids) > 0
dimids = reverse(nc_inq_vardimid(ncid,varid))

if !isempty(intersect(dimids,unlimdim_ids))

chunksizes = ntuple(N) do i
if dimids[i] in unlimdim_ids
1
else
sz[i]
end
end

return :chunked, chunksizes
end
end
end

storage,chunksizes = chunking(v)
return storage,NTuple{N}(chunksizes)
end

_chunking(v::CFVariable) = _chunking(v.var)

function _chunking(v)
storage,chunksizes = chunking(v)
return storage,Tuple(chunksizes)
end

"""
isshuffled,isdeflated,deflate_level = deflate(v::Variable)
Expand Down Expand Up @@ -398,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) = Tuple(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
10 changes: 9 additions & 1 deletion test/test_lowlevel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ for sampledata in samples
rm(filename;force=true)

# write data
ncid = NCDatasets.nc_create(filename,NCDatasets.NC_CLOBBER | NCDatasets.NC_NETCDF4)
mode = NCDatasets.NC_CLOBBER | NCDatasets.NC_NETCDF4
ncid = NCDatasets.nc_create(filename,mode)

format = NCDatasets.nc_inq_format(ncid)
@test format == NCDatasets.NC_FORMAT_NETCDF4

format,mode2 = NCDatasets.nc_inq_format_extended(ncid)
@test format == NCDatasets.NC_FORMATX_NC4
@test mode2 == mode

dimids = zeros(Cint,ndims(sampledata))
for i = 1:ndims(sampledata)
Expand Down
27 changes: 27 additions & 0 deletions test/test_variable_unlim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,30 @@ v[1:100,1] = data[:,1]
v[1:100,:] = data
close(ds)
rm(filename)


# issue 231

using NCDatasets
using Test
sz = (4,5,1)
filename = tempname()

for f = [:netcdf3_64bit_offset,:netcdf4]
ds = NCDataset(filename,"c",format=f)
ds.dim["lon"] = sz[1]
ds.dim["lat"] = sz[2]
ds.dim["time"] = Inf

T = Float32
v = defVar(ds,"var-$T",T,("lon","lat","time"))

j = 1
v[:,:,j] = fill(T(j), sz[1:2])

@test size(v) == sz

storage,chunksizes = NCDatasets._chunking(v)
@test storage == :chunked
@test chunksizes == sz
end

0 comments on commit d589989

Please sign in to comment.