Skip to content

Commit

Permalink
Merge pull request #480 from JuliaDataCubes/fg/skip_vars
Browse files Browse the repository at this point in the history
Add option to skip keys when opening datasets
  • Loading branch information
lazarusA authored Dec 16, 2024
2 parents 301ea46 + cf18071 commit b0da856
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/DatasetAPI/Datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ function open_mfdataset(vec::DD.DimVector{<:AbstractString};kwargs...)
alldatasets = open_dataset.(vec;kwargs...);
fi = first(alldatasets)
mergedim = DD.dims(alldatasets) |> only
ars = map(collect(keys(fi.cubes))) do var
vars_to_merge = collect(keys(fi.cubes))
ars = map(vars_to_merge) do var
cfi = fi.cubes[var]
mergedar = if DD.dims(cfi,mergedim) !== nothing
merge_existing_axis(alldatasets,cfi,var,mergedim)
Expand All @@ -411,14 +412,16 @@ end
Open the dataset at `g` with the given `driver`.
The default driver will search for available drivers and tries to detect the useable driver from the filename extension.
"""
function open_dataset(g; driver = :all)
function open_dataset(g; skip_keys=(), driver = :all)
str_skipkeys = string.(skip_keys)
dsopen = YAXArrayBase.to_dataset(g, driver = driver)
YAXArrayBase.open_dataset_handle(dsopen) do g
isempty(get_varnames(g)) && throw(ArgumentError("Group does not contain datasets."))
dimlist = collectdims(g)
dnames = string.(keys(dimlist))
varlist = filter(get_varnames(g)) do vn
upname = uppercase(vn)
!in(vn, str_skipkeys) &&
!occursin("BNDS", upname) &&
!occursin("BOUNDS", upname) &&
!any(i -> isequal(upname, uppercase(i)), dnames)
Expand Down
6 changes: 6 additions & 0 deletions test/Datasets/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ end
@test ds2.c.data[:, :] == ds.c.data
@test ds2.c.chunks == DiskArrays.GridChunks(size(a), (5, 10))

#Test if skip_keys works
ds3 = open_dataset(f, driver=:zarr, skip_keys = (:c,))
@test :a in keys(ds3.cubes)
@test :b in keys(ds3.cubes)
@test !in(:c,keys(ds3.cubes))

end

@testset "Saving, OutDims" begin
Expand Down

0 comments on commit b0da856

Please sign in to comment.