Skip to content

Commit

Permalink
Merge pull request #7 from JuliaGeo/as/dataset_from_store
Browse files Browse the repository at this point in the history
Add methods to construct a ZarrDataset from Zarr.jl objects
  • Loading branch information
Alexander-Barth authored Sep 11, 2024
2 parents 5dc7251 + 5e842a3 commit 894816d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ZarrDatasets"
uuid = "519a4cdf-1362-424a-9ea1-b1d782dbb24b"
authors = ["Alexander Barth <barth.alexander@gmail.com> and contributors"]
version = "0.1.2"
version = "0.1.3"

[deps]
CommonDataModel = "1fbeeb36-5f17-413c-809b-666fb144f157"
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,14 @@ NCDataset("$(dataset_id)_selection.nc","c") do ds_nc
write(ds_nc,ds_sub)
end
```

## Using a pre-existing Zarr array or store

It's also simple to wrap an existing Zarr array, or a manually constructed Zarr store, in a `ZarrDataset`:

```julia
zg = zopen("/path/to/zarr")
zd = ZarrDataset(zg)
```

and you can pass a constructed `Zarr.AbstractStore` similarly.
44 changes: 33 additions & 11 deletions src/dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ CDM.maskingvalue(ds::ZarrDataset) = ds.maskingvalue
ds = ZarrDataset(url::AbstractString,mode = "r";
_omitcode = [404,403],
maskingvalue = missing)
ZarrDataset(zg::Zarr.ZGroup; _omitcode, maskingvalue)
ZarrDataset(f::Function,url::AbstractString,mode = "r";
maskingvalue = missing)
Expand Down Expand Up @@ -135,33 +136,54 @@ function ZarrDataset(url::AbstractString,mode = "r";
)

dimensions = OrderedDict{Symbol,Int}()
iswritable = false

if mode == "r"
zg = if mode == "r"
zg = Zarr.zopen(url,mode)
if (zg.storage isa Zarr.HTTPStore) ||
(zg.storage isa Zarr.ConsolidatedStore{Zarr.HTTPStore})
elseif mode == "c"
store = Zarr.DirectoryStore(url)
zg = zgroup(store, "",attrs = Dict{String,Any}(attrib))
end
ZarrDataset(zg; mode, parentdataset, _omitcode, maskingvalue, attrib)
end

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

function ZarrDataset(zg::Zarr.ZGroup;
mode = "r",
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
elseif mode == "c"
store = Zarr.DirectoryStore(url)
zg = zgroup(store, "",attrs = Dict{String,Any}(attrib))
iswritable = true
end
ZarrDataset(parentdataset,zg,dimensions,iswritable,maskingvalue)

return ZarrDataset(parentdataset, zg, dimensions, mode == "r" ? false : zg.writeable, maskingvalue)

end


Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ using ZarrDatasets
include("test_write.jl")
include("test_groups.jl")
include("test_fillvalue.jl")
include("test_preexisting.jl")
include("test_aqua.jl")
end
49 changes: 49 additions & 0 deletions test/test_preexisting.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using ZarrDatasets
using Test
using Zarr

# Test opening a ZarrDataset using a pre-existing Zarr store or group

# First,
fname = tempname()
mkdir(fname)
gattrib = Dict("title" => "this is the title")
ds = ZarrDataset(fname,"c",attrib = gattrib)

ds.attrib["number"] = 1
defDim(ds,"lon",3)
defDim(ds,"lat",5)

attrib = Dict(
"units" => "m/s",
"long_name" => "test",
)


varname = "var2"
dimensionnames = ("lon","lat")
vtype = Int32

zv = defVar(ds,varname,vtype,dimensionnames, attrib = attrib)
zv[:,:] = data = rand(Int32,3,5)

zv.attrib["number"] = 12
zv.attrib["standard_name"] = "test"
ds.attrib["history"] = "test"
close(ds)

for ds in ZarrDataset.((Zarr.storefromstring(fname)[1], Zarr.zopen(fname), ))

zv = ds[varname]

@test zv.attrib["number"] == 12
@test zv.attrib["standard_name"] == "test"
@test ds.attrib["history"] == "test"

@test zv[:,:] == data

io = IOBuffer()
show(io,ds)
str = String(take!(io))
@test occursin("Global",str)
end

2 comments on commit 894816d

@Alexander-Barth
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/114976

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.3 -m "<description of version>" 894816d94229ca7a6fe3aaa5218dec1385e950eb
git push origin v0.1.3

Also, note the warning: Version 0.1.3 skips over 0.1.2
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

Please sign in to comment.