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
Open
lazarusA
wants to merge
7
commits into
master
Choose a base branch
from
la/zarrcore
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+315
−130
Open
Changes from all commits
Commits
Show all changes
7 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 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 | ||
| import ZarrCore: zarr_format | ||
| export ZarrDataset | ||
|
|
||
| function __init__() | ||
| @debug "new driver key :zarr, updating backendlist." | ||
| YAB.backendlist[:zarr] = ZarrDataset | ||
| push!(YAB.backendregex, r"(.zarr$)|(.zarr/$)|(zarr.zip$)" => ZarrDataset) | ||
| end | ||
|
|
||
| struct ZarrDataset | ||
| g::ZGroup | ||
| end | ||
| function ZarrDataset(g::Union{String,ZGroup}; mode="r", path="", kwargs...) | ||
| if g isa ZGroup | ||
| return ZarrDataset(g) | ||
| end | ||
| 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, format=nothing, kwargs...) | ||
| # The format is set by the group in create_empty | ||
| if format !== nothing && format != zarr_format(p) | ||
| throw(ArgumentError("Can not create a Zarr v$format array in a Zarr v$(zarr_format(p)) group")) | ||
| end | ||
| 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 zarr_format(p) == 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(); format=2, kwargs...) = | ||
| ZarrDataset(zgroup(ZarrCore.storefromstring(path, true)..., format; attrs=gatts)) | ||
| zarr_format(ds::ZarrDataset) = zarr_format(ds.g.metadata) | ||
|
|
||
|
|
||
|
|
||
| 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
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.
as for the breaking thing, I'm not sure about this backend list, since now the dependency is in ZarrCore and not Zarr, do we still use the key
:zarrhere?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 still use the key zarr here, because this is about the format and I would say the existence of ZarrCore is an implementation detail.