Skip to content

Commit

Permalink
Add ZarrDataset extension
Browse files Browse the repository at this point in the history
This is a first draft for opening zarr data with ZarrDatasets in Rasters
  • Loading branch information
felixcremer committed Apr 24, 2024
1 parent 8fce53b commit 86417a1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
RasterDataSources = "3cb90ccd-e1b6-4867-9617-4276c8b2ca36"
ZarrDatasets = "519a4cdf-1362-424a-9ea1-b1d782dbb24b"

[extensions]
RastersArchGDALExt = "ArchGDAL"
Expand All @@ -41,6 +42,7 @@ RastersHDF5Ext = "HDF5"
RastersMakieExt = "Makie"
RastersNCDatasetsExt = "NCDatasets"
RastersRasterDataSourcesExt = "RasterDataSources"
RastersZarrDatasetsExt = "ZarrDatasets"

[compat]
Aqua = "0.8"
Expand Down Expand Up @@ -75,6 +77,7 @@ Setfield = "0.6, 0.7, 0.8, 1"
Shapefile = "0.10, 0.11"
Statistics = "1"
Test = "1"
ZarrDatasets = "0.1"
julia = "1.10"

[extras]
Expand Down
34 changes: 34 additions & 0 deletions ext/RastersZarrDatasetsExt/RastersZarrDatasetsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module RastersZarrDatasetsExt

@static if isdefined(Base, :get_extension) # julia < 1.9
using Rasters, ZarrDatasets, CommonDataModel
else
using ..Rasters, ..GRIBDatasets, ..CommonDataModel
end

import DiskArrays,
FillArrays,
Extents,
GeoInterface,
Missings

using Dates,
DimensionalData,
GeoFormatTypes

using Rasters.Lookups
using Rasters.Dimensions
using Rasters: Zarrsource

using ZarrDatasets
using CommonDataModel: AbstractDataset

const RA = Rasters
const DD = DimensionalData
const DA = DiskArrays
const GI = GeoInterface
const LA = Lookups

include("zarrdatasets_source.jl")

end
21 changes: 21 additions & 0 deletions ext/RastersZarrDatasetsExt/zarrdatasets_source.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const GDS = ZarrDatasets

function RA.OpenStack(fs::RA.FileStack{Zarrsource,K}) where K
RA.OpenStack{Zarrsource,K}(ZD.ZarrDataset(RA.filename(fs)))
end

# In ZarrDatasets, the file is open for reading the values and closed afterwards.
Base.close(os::RA.OpenStack{Zarrsource}) = nothing

function RA._open(f, ::Zarrsource, filename::AbstractString; write=false, kw...)
#isfile(filename) || RA._filenotfound_error(filename)
ds = ZarrDatasets.ZarrDataset(filename)
RA._open(f, Zarrsource(), ds; kw...)
end

# Hack to get the inner DiskArrays chunks as they are not exposed at the top level
RA._get_eachchunk(var::ZD.ZarrVariable) = DiskArrays.eachchunk(var.zarray)
RA._get_haschunks(var::ZD.ZarrVariable) = DiskArrays.haschunks(var.zarray)

RA._sourcetrait(::ZD.ZarrVariable) = Zarrsource()
RA._sourcetrait(::ZD.ZarrDataset) = Zarrsource()
5 changes: 3 additions & 2 deletions src/sources/commondatamodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ function _layers(ds::AbstractDataset, ::Nothing=nothing)
end
union(dimkeys, boundskeys)::Vector{String}
else
dimkeys::Vector{String}
collect(dimkeys)::Vector{String}
end
nondim = setdiff(keys(ds), toremove)
# Maybe this should be fixed in ZarrDatasets but it works with this patch.
nondim = collect(setdiff(keys(ds), toremove))
grid_mapping = String[]
vars = map(k -> ds[k], nondim)
attrs = map(CDM.attribs, vars)
Expand Down
5 changes: 5 additions & 0 deletions src/sources/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ abstract type Source end
abstract type CDMsource <: Source end
struct NCDsource <: CDMsource end
struct GRIBsource <: CDMsource end
struct Zarrsource <: CDMsource end
struct GRDsource <: Source end
struct GDALsource <: Source end
struct SMAPsource <: Source end
Expand All @@ -22,6 +23,7 @@ const SYMBOL2SOURCE = Dict(
:netcdf => NCDsource(),
:grib => GRIBsource(),
:smap => SMAPsource(),
:zarr => Zarrsource(),
)

const SOURCE2SYMBOL = Dict(map(reverse, collect(pairs(SYMBOL2SOURCE))))
Expand All @@ -32,12 +34,14 @@ const SOURCE2EXT = Dict(
NCDsource() => (".nc",),
GRIBsource() => (".grib",),
SMAPsource() => (".h5",),
Zarrsource() => (".zarr", ".zarr/"),
)
const SOURCE2PACKAGENAME = Dict(
GDALsource() => "ArchGDAL",
NCDsource() => "NCDatasets",
GRIBsource() => "GRIBDatasets",
SMAPsource() => "HDF5",
Zarrsource() => "ZarrDataset",
)

const EXT2SOURCE = Dict(
Expand All @@ -46,6 +50,7 @@ const EXT2SOURCE = Dict(
".nc" => NCDsource(),
".grib" => GRIBsource(),
".h5" => SMAPsource(),
".zarr" => Zarrsource(),
)

# exception to be raised when backend extension is not satisfied
Expand Down
2 changes: 2 additions & 0 deletions test/sources/zarr.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using Rasters, Zarr
using ZarrDatasets

0 comments on commit 86417a1

Please sign in to comment.