Skip to content

Commit a84ef0a

Browse files
Do not treat Zarr fill_value as CF _FillValue for coordinate variables
1 parent 135506d commit a84ef0a

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

src/variable.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ CDM.name(v::ZarrVariable) = Zarr.zname(v.zarray)
1010
CDM.dimnames(v::ZarrVariable) = Tuple(reverse(v.zarray.attrs["_ARRAY_DIMENSIONS"]))
1111
CDM.dataset(v::ZarrVariable) = v.parentdataset
1212

13+
function _iscoordvar(v)
14+
dn = dimnames(v)
15+
if length(dn) == 0
16+
return false
17+
end
18+
return name(v) == first(dn)
19+
end
20+
1321
function CDM.attribnames(v::ZarrVariable)
1422
names = filter(!=("_ARRAY_DIMENSIONS"),keys(v.zarray.attrs))
15-
if !isnothing(v.zarray.metadata.fill_value)
23+
if !isnothing(v.zarray.metadata.fill_value) && !_iscoordvar(v)
1624
push!(names,"_FillValue")
1725
end
1826
return names
@@ -44,6 +52,17 @@ haschunks(v::ZarrVariable) = haschunks(v.zarray)
4452
eachchunk(v::CFVariable{T,N,<:ZarrVariable}) where {T,N} = eachchunk(v.var)
4553
haschunks(v::CFVariable{T,N,<:ZarrVariable}) where {T,N} = haschunks(v.var)
4654

55+
"""
56+
57+
defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensionnames; chunksizes=nothing, attrib = Dict(), fillvalue = nothing)
58+
59+
60+
Create a variable `name` in the dataset `ds` with the type `vtype` and the dimension `dimensionnames`.
61+
62+
For coordinate variables, fill values will be used a background value of undefined chunks and not as missing value as coordinate variables cannot have the `_FillValues` in the CF convension as in Zarr v2 format a `fill_value` does not necessarily indicate a missing value.
63+
64+
See also `CommonDataModel.defVar` for more information.
65+
"""
4766
function CDM.defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensionnames; chunksizes=nothing, attrib = Dict(), fillvalue = nothing, kwargs...)
4867
@assert iswritable(ds)
4968

@@ -70,6 +89,8 @@ function CDM.defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensi
7089
kwargs...
7190
)
7291

73-
return ZarrVariable{vtype,ndims(zarray),typeof(zarray),typeof(ds)}(
92+
zv = ZarrVariable{vtype,ndims(zarray),typeof(zarray),typeof(ds)}(
7493
zarray,ds)
94+
95+
return ds[name]
7596
end

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
55
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
66
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
77
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
8+
Zarr = "0a941bbe-ad1d-11e8-39d9-ab76183a1d99"
89

910
[compat]
1011
Aqua = "0.8"
1112
CommonDataModel = "0.3.6"
1213
NCDatasets = "0.14"
1314
julia = "1"
15+
Zarr = "0.9.2"

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ using ZarrDatasets
66
include("test_multifile.jl")
77
include("test_write.jl")
88
include("test_groups.jl")
9+
include("test_fillvalue.jl")
910
include("test_aqua.jl")
1011
end

test/test_fillvalue.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using ZarrDatasets
2+
using Test
3+
using Zarr
4+
5+
fname = tempname()
6+
mkdir(fname)
7+
8+
# CF coordinate variable with Zarr fill_value set
9+
10+
store = Zarr.DirectoryStore(fname)
11+
zg = zgroup(store, "")
12+
zarray = zcreate(
13+
Int, zg, "lon", 3;
14+
fill_value = 9999,
15+
attrs = Dict("_ARRAY_DIMENSIONS" => ("lon",)))
16+
17+
ds = ZarrDataset(fname)
18+
@test eltype(ds["lon"]) == Int
19+
20+
fname = tempname()
21+
mkdir(fname)
22+
23+
ds = ZarrDataset(fname,"c")
24+
25+
# variable which is not a CF coordinate variable
26+
v2 = defVar(ds,"foo",Int,(),fillvalue=9999)
27+
@test eltype(v2) == Union{Missing,Int}
28+
29+
v3 = defVar(ds,"bar",Int16[2,3,4],("time",),fillvalue=9999)
30+
@test eltype(v3) == Union{Missing,Int16}
31+
close(ds)

0 commit comments

Comments
 (0)