Skip to content

Commit 004e596

Browse files
allow fill value for defVar
1 parent 0d427c9 commit 004e596

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/variable.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ eachchunk(v::CFVariable{T,N,<:ZarrVariable}) where {T,N} = eachchunk(v.var)
4545
haschunks(v::CFVariable{T,N,<:ZarrVariable}) where {T,N} = haschunks(v.var)
4646

4747

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

51+
if isnothing(fillvalue)
52+
fillvalue = get(attrib,"_FillValue",nothing)
53+
end
54+
5155
_attrib = Dict(attrib)
5256
_attrib["_ARRAY_DIMENSIONS"] = reverse(dimensionnames)
5357

@@ -62,6 +66,7 @@ function CDM.defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensi
6266
vtype, ds.zgroup, name, _size...;
6367
chunks = chunksizes,
6468
attrs = _attrib,
69+
fill_value = fillvalue,
6570
kwargs...
6671
)
6772

test/test_write.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ io = IOBuffer()
4444
show(io,ds)
4545
str = String(take!(io))
4646
@test occursin("Global",str)
47+
48+
49+
# fill value
50+
51+
fname = tempname()
52+
ds = ZarrDataset(fname,"c")
53+
defDim(ds,"lon",100)
54+
v = defVar(ds,"lon",Float32,("lon",),fillvalue = 9999.)
55+
v .= 1
56+
close(ds)
57+
58+
ds = ZarrDataset(fname)
59+
@test eltype(ds["lon"]) == Union{Missing, Float32}
60+
@test eltype(cfvariable(ds,"lon",fillvalue=nothing)) == Float32

0 commit comments

Comments
 (0)