meggart / DiskArrays.jl

Other
73 stars 13 forks source link

1D variable with unlimited dimensions #123

Closed Alexander-Barth closed 11 months ago

Alexander-Barth commented 11 months ago

While debugging an issue in NCDatasets using DiskArrays v0.3.20 ( https://github.com/Alexander-Barth/NCDatasets.jl/issues/230 ) I found some behavior of NetCDF.jl with DiskArray with variables of unlimited dimensions that I could not understand. While the following code with a 2D array (one unlimited) works without error:

import NetCDF
fname2 = tempname()
time_dim = NetCDF.NcDim("time",Int[],unlimited=true);
lon_dim = NetCDF.NcDim("lon",1:3);
v = NetCDF.NcVar("obs",[lon_dim,time_dim]);
NetCDF.create(fname2,v) do nc
   nc["obs"][:,1:3] = rand(3,3)
end;

The following code with a single unlimited dimensions fails:

import NetCDF
var = collect(1:4)
fname2 = tempname()
time_dim = NetCDF.NcDim("time",Int[],unlimited=true);
v = NetCDF.NcVar("var",[time_dim]);
NetCDF.create(fname2,v) do nc
    nc["var"][1:4] = var
end;

with the following error message:

ERROR: BoundsError: attempt to access 0-element CartesianIndices{1, Tuple{Base.OneTo{Int64}}} at index [1:4]                                                       
Stacktrace:
  [1] throw_boundserror(A::CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, I::Tuple{UnitRange{Int64}})                                                                      
    @ Base ./abstractarray.jl:744
  [2] checkbounds
    @ ./abstractarray.jl:709 [inlined]
  [3] getindex
    @ ./multidimensional.jl:358 [inlined]
  [4] view
    @ ./multidimensional.jl:372 [inlined]
  [5] interpret_indices_disk
    @ ~/.julia/packages/DiskArrays/VLHvZ/src/diskarray.jl:137 [inlined]
  [6] setindex_disk!(a::NcVar{Float64, 1, 6}, v::UnitRange{Int64}, i::UnitRange{Int64})
    @ DiskArrays ~/.julia/packages/DiskArrays/VLHvZ/src/diskarray.jl:56
  [7] setindex!(a::NcVar{Float64, 1, 6}, v::UnitRange{Int64}, i::UnitRange{Int64})
    @ DiskArrays ~/.julia/packages/DiskArrays/VLHvZ/src/diskarray.jl:222
  [8] (::var"#15#16")(nc::NcFile)
    @ Main ./REPL[54]:2
  [9] create(::var"#15#16", ::String, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})                                     
    @ NetCDF ~/.julia/packages/NetCDF/7hOe9/src/NetCDF.jl:1022
 [10] create(::Function, ::String, ::NcVar{Float64, 1, 6})
    @ NetCDF ~/.julia/packages/NetCDF/7hOe9/src/NetCDF.jl:1019
 [11] top-level scope
    @ REPL[54]:1

NetCDF.jl and the equivalent NCDatasets.jl code both fail at the same line:

https://github.com/meggart/DiskArrays.jl/blob/v0.3.20/src/diskarray.jl#L137

Ref: https://github.com/meggart/DiskArrays.jl/issues/53

Alexander-Barth commented 11 months ago

If I comment-out the definition interpret_indices_disk(A, r::NTuple{1,AbstractVector}) so that the general method interpret_indices_disk(A, r::NTuple{N,Union{Integer,AbstractVector,Colon}}) where {N} is used instead, the example passes for NetCDF.jl and NCDatasets.jl but I see failure in this test:

https://github.com/meggart/DiskArrays.jl/blob/v0.3.20/test/runtests.jl#L105

rafaqz commented 11 months ago

Fixed on master