Skip to content

Commit

Permalink
workaround for meggart/DiskArrays.jl#131
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Nov 24, 2023
1 parent a785336 commit e73ac54
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 45 deletions.
10 changes: 6 additions & 4 deletions src/NCDatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ import CommonDataModel: AbstractDataset, AbstractVariable,
chunking, deflate, checksum, fillmode,
iswritable, sync, CatArrays,
SubDataset,
@select, select, Near, coordinate_value, coordinate_names, split_by_and
@select, select, Near, coordinate_value, coordinate_names, split_by_and,
chunking, deflate, checksum


import DiskArrays
import DiskArrays: readblock!, writeblock!, eachchunk, haschunks
using DiskArrays: @implement_diskarray
import DiskArrays: readblock!, writeblock!, eachchunk, haschunks, batchgetindex

function __init__()
NetCDF_jll.is_available() && init_certificate_authority()
Expand All @@ -73,7 +74,8 @@ include("multifile.jl")
include("ncgen.jl")
include("precompile.jl")

@implement_diskarray NCDatasets.Variable
DiskArrays.@implement_diskarray NCDatasets.Variable


export CatArrays
export CFTime
Expand Down
17 changes: 7 additions & 10 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ end
end


#=

# indexing with vector of integers

Expand Down Expand Up @@ -549,7 +548,10 @@ function _range_indices_dest(of,v,rest...)
end
range_indices_dest(ri...) = _range_indices_dest((),ri...)

function Base.getindex(v::Union{MFVariable,SubVariable},indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{<:Integer}}...)
function _batchgetindex(
v::Variable{T},
indices::Union{<:Integer,Colon,AbstractRange{<:Integer},AbstractVector{<:Integer}}...) where T

@debug "transform vector of indices to ranges"

sz_source = size(v)
Expand All @@ -574,15 +576,10 @@ function Base.getindex(v::Union{MFVariable,SubVariable},indices::Union{Int,Colon
for R in CartesianIndices(length.(ri))
ind_source = ntuple(i -> ri[i][R[i]],N)
ind_dest = ntuple(i -> ri_dest[i][R[i]],length(ri_dest))
#dest[ind_dest...] = v[ind_source...]
if hasproperty(v,:var)
buffer = Array{eltype(v.var),length(ind_dest)}(undef,length.(ind_dest))
load!(v,view(dest,ind_dest...),buffer,ind_source...)
else
dest[ind_dest...] = v[ind_source...]
end
dest[ind_dest...] = v[ind_source...]
end
return dest
end

=#
DiskArrays.batchgetindex(v::Variable,indices::Union{<:Integer,Colon,AbstractRange{<:Integer},AbstractVector{<:Integer}}...) = _batchgetindex(v,indices...)
DiskArrays.batchgetindex(v::Variable,index::AbstractVector{Int}) = _batchgetindex(v,index)
2 changes: 1 addition & 1 deletion test/test_bounds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ nctime_bounds[:,:] = time_bounds
close(ds)


# issue 170
# NCDatasets issue 170

fname = tempname()
ds = NCDataset(fname,"c")
Expand Down
52 changes: 26 additions & 26 deletions test/test_select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,32 +247,32 @@ i = findmin(x -> abs.(DateTime(2000,1,3,1) - x),time)[2]
v = NCDatasets.@select(ds["SST"],30 <= lon <= 60 && 40 <= lat <= 90)

# # https://github.com/meggart/DiskArrays.jl/issues/131
# ilon = findall(x -> 30 <= x <= 60,ds["lon"])
# ilat = findall(x -> 40 <= x <= 90,ds["lat"])
# v2 = ds["SST"][ilon,ilat,:]
# @test_broken v == v2


# lonr = 30..60
# latr = ClosedInterval(40, 90) # latitude range
# v = NCDatasets.@select(ds["SST"], lon ∈ $lonr && lat in $latr)
# ilon = findall(x -> 30 <= x <= 60,ds["lon"])
# ilat = findall(x -> 40 <= x <= 90,ds["lat"])
# v2 = ds["SST"][ilon,ilat,:]
# @test_broken v == v2

# v = NCDatasets.@select(ds["SST"], lon ∈ $(30..60) && lat ∈ $(65 ± 25))
# ilon = findall(x -> 30 <= x <= 60,ds["lon"])
# ilat = findall(x -> 40 <= x <= 90,ds["lat"])
# v2 = ds["SST"][ilon,ilat,:]
# @test_broken v == v2


# v = NCDatasets.@select(ds["SST"], lon ∈ 30..60 && lat ∈ 65 ± 25)
# ilon = findall(x -> 30 <= x <= 60,ds["lon"])
# ilat = findall(x -> 40 <= x <= 90,ds["lat"])
# v2 = ds["SST"][ilon,ilat,:]
# @test_broken v == v2
ilon = findall(x -> 30 <= x <= 60,ds["lon"])
ilat = findall(x -> 40 <= x <= 90,ds["lat"])
v2 = ds["SST"][ilon,ilat,:]
@test v == v2


lonr = 30..60
latr = ClosedInterval(40, 90) # latitude range
v = NCDatasets.@select(ds["SST"], lon $lonr && lat in $latr)
ilon = findall(x -> 30 <= x <= 60,ds["lon"])
ilat = findall(x -> 40 <= x <= 90,ds["lat"])
v2 = ds["SST"][ilon,ilat,:]
@test v == v2

v = NCDatasets.@select(ds["SST"], lon $(30..60) && lat $(65 ± 25))
ilon = findall(x -> 30 <= x <= 60,ds["lon"])
ilat = findall(x -> 40 <= x <= 90,ds["lat"])
v2 = ds["SST"][ilon,ilat,:]
@test v == v2


v = NCDatasets.@select(ds["SST"], lon 30..60 && lat 65 ± 25)
ilon = findall(x -> 30 <= x <= 60,ds["lon"])
ilat = findall(x -> 40 <= x <= 90,ds["lat"])
v2 = ds["SST"][ilon,ilat,:]
@test v == v2


in_lon_range(lon) = 30 <= lon <= 60
Expand Down
5 changes: 1 addition & 4 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,11 @@ close(ds_src)
close(ds_dest)

# issue 209
# broken
# maybe related to
# https://github.com/meggart/DiskArrays.jl/issues/131
filename_src = tempname()
ds = NCDataset(filename_src, "c")
data = [1,2,3]
ncv = defVar(ds,"data",data,("data",))
@test_broken isempty(ncv[Int[]])
@test isempty(ncv[Int[]])
close(ds)

# issue 211
Expand Down

0 comments on commit e73ac54

Please sign in to comment.