Skip to content

Commit

Permalink
Do not treat Zarr fill_value as CF _FillValue for coordinate variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed May 7, 2024
1 parent 135506d commit a84ef0a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ CDM.name(v::ZarrVariable) = Zarr.zname(v.zarray)
CDM.dimnames(v::ZarrVariable) = Tuple(reverse(v.zarray.attrs["_ARRAY_DIMENSIONS"]))
CDM.dataset(v::ZarrVariable) = v.parentdataset

function _iscoordvar(v)
dn = dimnames(v)
if length(dn) == 0
return false
end
return name(v) == first(dn)
end

function CDM.attribnames(v::ZarrVariable)
names = filter(!=("_ARRAY_DIMENSIONS"),keys(v.zarray.attrs))
if !isnothing(v.zarray.metadata.fill_value)
if !isnothing(v.zarray.metadata.fill_value) && !_iscoordvar(v)
push!(names,"_FillValue")
end
return names
Expand Down Expand Up @@ -44,6 +52,17 @@ haschunks(v::ZarrVariable) = haschunks(v.zarray)
eachchunk(v::CFVariable{T,N,<:ZarrVariable}) where {T,N} = eachchunk(v.var)
haschunks(v::CFVariable{T,N,<:ZarrVariable}) where {T,N} = haschunks(v.var)

"""
defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensionnames; chunksizes=nothing, attrib = Dict(), fillvalue = nothing)
Create a variable `name` in the dataset `ds` with the type `vtype` and the dimension `dimensionnames`.
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.
See also `CommonDataModel.defVar` for more information.
"""
function CDM.defVar(ds::ZarrDataset,name::SymbolOrString,vtype::DataType,dimensionnames; chunksizes=nothing, attrib = Dict(), fillvalue = nothing, kwargs...)
@assert iswritable(ds)

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

return ZarrVariable{vtype,ndims(zarray),typeof(zarray),typeof(ds)}(
zv = ZarrVariable{vtype,ndims(zarray),typeof(zarray),typeof(ds)}(
zarray,ds)

return ds[name]
end
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zarr = "0a941bbe-ad1d-11e8-39d9-ab76183a1d99"

[compat]
Aqua = "0.8"
CommonDataModel = "0.3.6"
NCDatasets = "0.14"
julia = "1"
Zarr = "0.9.2"
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ using ZarrDatasets
include("test_multifile.jl")
include("test_write.jl")
include("test_groups.jl")
include("test_fillvalue.jl")
include("test_aqua.jl")
end
31 changes: 31 additions & 0 deletions test/test_fillvalue.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using ZarrDatasets
using Test
using Zarr

fname = tempname()
mkdir(fname)

# CF coordinate variable with Zarr fill_value set

store = Zarr.DirectoryStore(fname)
zg = zgroup(store, "")
zarray = zcreate(
Int, zg, "lon", 3;
fill_value = 9999,
attrs = Dict("_ARRAY_DIMENSIONS" => ("lon",)))

ds = ZarrDataset(fname)
@test eltype(ds["lon"]) == Int

fname = tempname()
mkdir(fname)

ds = ZarrDataset(fname,"c")

# variable which is not a CF coordinate variable
v2 = defVar(ds,"foo",Int,(),fillvalue=9999)
@test eltype(v2) == Union{Missing,Int}

v3 = defVar(ds,"bar",Int16[2,3,4],("time",),fillvalue=9999)
@test eltype(v3) == Union{Missing,Int16}
close(ds)

0 comments on commit a84ef0a

Please sign in to comment.