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

Add option to skip keys when opening datasets #480

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ open_dataset(g; driver=:all)
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
Loading