Repository navigation
migrate Zarr backend to ZarrCore and add ZarrZip extension support #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c40381d
migrate Zarr backend to ZarrCore and add ZarrZip extension support
lazarusA c529523
add support for Zarr v3 format and dimension names in ZarrCore extension
lazarusA 86d4858
Apply compression settings to coordinate arrays and fix NetCDF iscomp…
lazarusA 9040952
Allow Zarr v3 scalar arrays without dimension_names
lazarusA c7b446d
Rename zarr_format keyword to format and use zarr_format function fro…
felixcremer 0dbc009
Import zarr_format to extend it
felixcremer 0fdbe4e
Access metadata of wrapped group for zarr_format
felixcremer 471e49d
Read a group's Zarr format from its field
alex-s-gardner cca5859
Pass zarr_format to ZarrCore.zcreate in tests
alex-s-gardner 3e76868
Check the zarr_format key of v3 metadata
alex-s-gardner 6e19ee4
Merge pull request #74 from alex-s-gardner/fix-pr73-ci
lazarusA f5d8740
Use ZarrCore.zarr_format and a single zarr_format keyword
lazarusA 5545ec3
Revert some behavior changes
meggart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| module ZarrCoreExt | ||
| using YAXArrayBase | ||
| using ZarrCore: ZarrCore, ZArray, ZGroup, zgroup, zcreate, zopen, NoCompressor, CRC32cV3Codec | ||
| import YAXArrayBase: YAXArrayBase as YAB | ||
| export ZarrDataset | ||
|
|
||
| function __init__() | ||
| @debug "new driver key :zarr, updating backendlist." | ||
| YAB.backendlist[:zarr] = ZarrDataset{Int(ZarrCore.DV)} | ||
| YAB.backendlist[:zarr2] = ZarrDataset{2} | ||
| YAB.backendlist[:zarr3] = ZarrDataset{3} | ||
| push!(YAB.backendregex, r"(.zarr$)|(.zarr/$)|(zarr.zip$)" => ZarrDataset{Int(ZarrCore.DV)}) | ||
| end | ||
|
|
||
| struct ZarrDataset{ZF} | ||
| g::ZGroup | ||
| zarr_format::ZarrCore.ZarrFormat{ZF} | ||
| end | ||
| ZarrDataset(g::ZGroup; kwargs...) = ZarrDataset(g, g.zarr_format) | ||
| function YAB.to_dataset(T::Type{<:ZarrDataset}, g::String; mode="r", path="", kwargs...) | ||
| # The returned ZarrDataset{ZF} is derived from the actual format of the store | ||
| # (g.zarr_format), not from T. The requested T/driver only selects the backend | ||
| # class, so reading a v2 store with driver=:zarr3 yields ZarrDataset{2}. | ||
| store = if endswith(g, "zip") | ||
| YAB.zarr_zipstore(g) | ||
| else | ||
| g | ||
| end | ||
| ZarrDataset(zopen(store, mode, fill_as_missing=false, path=path)) | ||
| end | ||
|
|
||
| function YAB.get_var_dims(ds::ZarrDataset, name) | ||
| a = ds[name] | ||
| # Zarr v3 stores dimension names in the array metadata, already in Julia order | ||
| dn = ZarrCore.dimension_names(a) | ||
| if dn !== nothing && all(!isnothing, dn) | ||
| return collect(String, dn) | ||
| end | ||
| if haskey(a.attrs, "_ARRAY_DIMENSIONS") | ||
| return reverse(a.attrs["_ARRAY_DIMENSIONS"]) | ||
| end | ||
| # Scalars have no dimensions to name, Zarr v3 allows omitting dimension_names for them | ||
| ndims(a) == 0 && return String[] | ||
| throw(ArgumentError("Zarr array $name has no dimension names")) | ||
| end | ||
| YAB.get_varnames(ds::ZarrDataset) = collect(keys(ds.g.arrays)) | ||
| function YAB.get_var_attrs(ds::ZarrDataset, name) | ||
| #We add the fill value to the attributes to be consistent with NetCDF | ||
| a = ds[name] | ||
| if a.metadata.fill_value !== nothing | ||
| merge(ds[name].attrs, Dict("_FillValue" => a.metadata.fill_value)) | ||
| else | ||
| ds[name].attrs | ||
| end | ||
| end | ||
| YAB.get_global_attrs(ds::ZarrDataset) = ds.g.attrs | ||
| Base.getindex(ds::ZarrDataset, i) = ds.g[i] | ||
| Base.haskey(ds::ZarrDataset, k) = haskey(ds.g, k) | ||
|
|
||
| # function add_var(p::ZarrDataset, T::Type{>:Missing}, varname, s, dimnames, attr; kwargs...) | ||
| # S = Base.nonmissingtype(T) | ||
| # add_var(p,S, varname, s, dimnames, attr; fill_value = defaultfillval(S), fill_as_missing=true, kwargs...) | ||
| # end | ||
|
|
||
| function YAB.add_var(p::ZarrDataset, T::Type, varname, s, dimnames, attr; | ||
| chunksize=s, fill_as_missing=false, kwargs...) | ||
| if !haskey(kwargs, :compressor) && ZarrCore.default_compressor() isa NoCompressor | ||
| @info "No Zarr compressor package is loaded, so data will be written uncompressed. Load e.g. ZarrBlosc or Zarr to enable compression." maxlog = 1 | ||
| end | ||
| if p.zarr_format == ZarrCore.ZarrFormat(2) | ||
| attr2 = merge(attr, Dict("_ARRAY_DIMENSIONS" => reverse(collect(String, dimnames)))) | ||
| else | ||
| attr2 = attr | ||
| kwargs = (; kwargs..., dimension_names=Tuple(collect(String, dimnames))) | ||
| end | ||
| fv = get(attr, "_FillValue", get(attr, "missing_value", YAB.defaultfillval(T))) | ||
| attr3 = filter(attr2) do (k, v) | ||
| !isa(v, AbstractFloat) || !isnan(v) | ||
| end | ||
| za = zcreate(T, p.g, varname, s...; fill_value=fv, fill_as_missing, attrs=attr3, chunks=chunksize, kwargs...) | ||
| za | ||
| end | ||
|
|
||
| #Special case for init with Arrays | ||
| function YAB.add_var(p::ZarrDataset, a::AbstractArray, varname, dimnames, attr; | ||
| kwargs...) | ||
| # to_zarrtype is not public in ZarrCore | ||
| T = ZarrCore.to_zarrtype(a) | ||
| b = add_var(p, T, varname, size(a), dimnames, attr; kwargs...) | ||
| b .= a | ||
| a | ||
| end | ||
|
|
||
| YAB.create_empty(::Type{ZarrDataset}, path, gatts=Dict()) = | ||
| ZarrDataset(zgroup(ZarrCore.storefromstring(path, true)..., Int(ZarrCore.DV); attrs=gatts)) | ||
| YAB.create_empty(::Type{ZarrDataset{ZF}}, path, gatts=Dict()) where ZF = | ||
| ZarrDataset(zgroup(ZarrCore.storefromstring(path, true)..., ZF; attrs=gatts)) | ||
|
|
||
|
|
||
|
|
||
| YAB.allow_parallel_write(::ZarrDataset) = true | ||
| YAB.allow_missings(::ZarrDataset) = false | ||
| YAB.to_dataset(g::ZGroup; kwargs...) = ZarrDataset(g; kwargs...) | ||
| # get_pipeline, V2Pipeline and V3Pipeline are not public in ZarrCore | ||
| YAB.iscompressed(a::ZArray) = _iscompressed(ZarrCore.get_pipeline(a.metadata)) | ||
| _iscompressed(p::ZarrCore.V2Pipeline) = !isa(p.compressor, NoCompressor) | ||
| # Checksum codecs are bytes-to-bytes codecs too, but do not compress | ||
| _iscompressed(p::ZarrCore.V3Pipeline) = any(c -> !isa(c, CRC32cV3Codec), p.bytes_bytes) | ||
|
|
||
| end | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| module ZarrZipExt | ||
| using ZarrZip: ZipStore | ||
| import DiskArrays: AbstractDiskArray, DiskArrays, Unchunked, Chunked, GridChunks | ||
| import YAXArrayBase: YAXArrayBase as YAB | ||
|
|
||
| #Add ability to read zipped zarrs | ||
| YAB.zarr_zipstore(path::AbstractString) = ZipStore(SimpleFileDiskArray(path)) | ||
|
|
||
| struct SimpleFileDiskArray{C<:Union{Int,Nothing}} <: AbstractDiskArray{UInt8,1} | ||
| file::String | ||
| s::Int | ||
| chunksize::C | ||
| end | ||
| Base.size(s::SimpleFileDiskArray) = (s.s,) | ||
| function SimpleFileDiskArray(filename; chunksize=nothing) | ||
| isfile(filename) || throw(ArgumentError("File $filename does not exist")) | ||
| s = filesize(filename) | ||
| SimpleFileDiskArray(filename, s, chunksize) | ||
| end | ||
| function DiskArrays.readblock!(a::SimpleFileDiskArray, aout, i::AbstractUnitRange) | ||
| open(a.file) do f | ||
| seek(f, first(i) - 1) | ||
| read!(f, aout) | ||
| end | ||
| end | ||
| DiskArrays.haschunks(a::SimpleFileDiskArray) = a.chunksize === nothing ? Unchunked() : Chunked() | ||
| function DiskArrays.eachchunk(a::SimpleFileDiskArray) | ||
| if a.chunksize === nothing | ||
| DiskArrays.estimate_chunksize(a) | ||
| else | ||
| GridChunks((a.s,), (a.chunksize,)) | ||
| end | ||
| end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this makes a lot of sense. The backend should be only
:zarr, hence the additional args in the interface that were introduced elsewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would a user specify the zarr version in your implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version allows the user to specify driver=:zarr2 and then with driver=:zarr or driver = :all we get the default zarr version as specified in Zarr.jl or set via Preferences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you just pass the keyword argument
zarr_format=3,orzarr_format=2, which is consistent with ZarrCore API naming.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might have differing opinions here. I personally think this is a good workaround to avoid interface changes. You can still put :zarr as a backend and then the default backend will be chosen, but if you explicitly want v2 or v3 you are explicit. What is wrong with that approach?