Skip to content
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

Passing a constructed Zarr store to a ZarrDataset #5

Closed
asinghvi17 opened this issue Sep 9, 2024 · 2 comments · Fixed by #7
Closed

Passing a constructed Zarr store to a ZarrDataset #5

asinghvi17 opened this issue Sep 9, 2024 · 2 comments · Fixed by #7

Comments

@asinghvi17
Copy link
Member

It would be nice to be able to wrap either a constructed Zarr.AbstractStore or a fully materialized Zarr.ZGroup in a ZarrDataset, so that CF conventions just work.

I'll push a PR shortly that exposes this API.

@asinghvi17
Copy link
Member Author

Here's some code that seems to work preliminarily:

function ZarrDatasets.ZarrDataset(store::Zarr.AbstractStore,mode = "r";
                     parentdataset = nothing,
                     _omitcode = [404,403],
                     maskingvalue = missing,
                     attrib = Dict(),
                     )
    return ZarrDatasets.ZarrDataset(zopen(store, mode); parent_dataset, _omitcode, maskingvalue, attrib)
end

function ZarrDatasets.ZarrDataset(zg::Zarr.ZGroup;
                     parentdataset = nothing,
                     _omitcode = [404,403],
                     maskingvalue = missing,
                     attrib = Dict(),
                     )

    dimensions = ZarrDatasets.OrderedDict{Symbol,Int}()
    iswritable = false
    if (zg.storage isa Zarr.HTTPStore) ||
        (zg.storage isa Zarr.ConsolidatedStore{Zarr.HTTPStore})
        @debug "omit chunks on HTTP error" _omitcode
        Zarr.missing_chunk_return_code!(zg.storage,_omitcode)
    end

    for (varname,zarray) in zg.arrays
        for (dimname,dimlen) in zip(reverse(zarray.attrs["_ARRAY_DIMENSIONS"]),size(zarray))

            dn = Symbol(dimname)
            if haskey(dimensions,dn)
                @assert dimensions[dn] == dimlen
            else
                dimensions[dn] = dimlen
            end
        end
    end
    ZarrDatasets.ZarrDataset(parentdataset,zg,dimensions,zg.writeable,maskingvalue)

end

@Alexander-Barth
Copy link
Member

Alexander-Barth commented Sep 10, 2024

This looks good to me. Can you make a PR (possibly with a test)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants