Skip to content

Commit

Permalink
allow fill value for defVar
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed May 3, 2024
1 parent 0d427c9 commit 004e596
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ eachchunk(v::CFVariable{T,N,<:ZarrVariable}) where {T,N} = eachchunk(v.var)
haschunks(v::CFVariable{T,N,<:ZarrVariable}) where {T,N} = haschunks(v.var)


function CDM.defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensionnames; chunksizes=nothing, attrib = Dict(), kwargs...)
function CDM.defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensionnames; chunksizes=nothing, attrib = Dict(), fillvalue = nothing, kwargs...)
@assert iswritable(ds)

if isnothing(fillvalue)
fillvalue = get(attrib,"_FillValue",nothing)
end

_attrib = Dict(attrib)
_attrib["_ARRAY_DIMENSIONS"] = reverse(dimensionnames)

Expand All @@ -62,6 +66,7 @@ function CDM.defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensi
vtype, ds.zgroup, name, _size...;
chunks = chunksizes,
attrs = _attrib,
fill_value = fillvalue,
kwargs...
)

Expand Down
14 changes: 14 additions & 0 deletions test/test_write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ io = IOBuffer()
show(io,ds)
str = String(take!(io))
@test occursin("Global",str)


# fill value

fname = tempname()
ds = ZarrDataset(fname,"c")
defDim(ds,"lon",100)
v = defVar(ds,"lon",Float32,("lon",),fillvalue = 9999.)
v .= 1
close(ds)

ds = ZarrDataset(fname)
@test eltype(ds["lon"]) == Union{Missing, Float32}
@test eltype(cfvariable(ds,"lon",fillvalue=nothing)) == Float32

0 comments on commit 004e596

Please sign in to comment.