Open felixcremer opened 8 months ago
I think it used to error, we mustn't have actually tested that it did.
But now Im not sure if it should? A zero length range is allowed in indexing.
Yes, a zero length range is allowed, but this is a range which is not empty but has no overlap to the range of the time axis of the DimArray. Sorry I was not clear. In this example the time axis of the array is the month from 1996 but I am trying to subset with a interval from 1995. One gets an Array back with one dimension zero which can't be used for anything afterwards.
julia> dims(r,Ti)
Ti Sampled{DateTime} ForwardOrdered Explicit DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}
wrapping: 12-element Vector{DateTime}:
1996-01-16T12:00:00
1996-02-15T12:00:00
1996-03-16T12:00:00
1996-04-16T00:00:00
1996-05-16T12:00:00
1996-06-16T00:00:00
1996-07-16T12:00:00
1996-08-16T12:00:00
1996-09-16T00:00:00
1996-10-16T12:00:00
1996-11-16T00:00:00
1996-12-16T12:00:00
The other case would be to have a zero length interval and then the subsetting should work and return a slice of the data. This also works for an MWE but it fails as well for the example data with a time axis:
julia> summer = rger[Ti=Date(1996,4,1)..Date(1996, 4,1)]
104×142×0 Raster{Union{Missing, Float32},3} tas with dimensions:
X Sampled{Float64} Float64[-7.557552218296486, -7.502551037136502, …, -1.9474317399783132, -1.8924305588183294] ForwardOrdered Explicit Intervals{Center},
Y Sampled{Float64} Float64[-3.1625523990153646, -3.107551183040055, …, 4.537617837528135, 4.592619053503444] ForwardOrdered Explicit Intervals{Center},
Ti Sampled{DateTime} DateTime[] ForwardOrdered Explicit Intervals{Center}
extent: Error showing value of type Raster{Union{Missing, Float32}, 3, Tuple{X{DimensionalData.Dimensions.LookupArrays.Sampled{Float64, Vector{Float64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{Float64}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}, Y{DimensionalData.Dimensions.LookupArrays.Sampled{Float64, Vector{Float64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{Float64}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}, Ti{DimensionalData.Dimensions.LookupArrays.Sampled{DateTime, Vector{DateTime}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{DateTime}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}}, Tuple{}, Array{Union{Missing, Float32}, 3}, Symbol, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}, Missing}:
ERROR: BoundsError: attempt to access 2×0 Matrix{DateTime} at index [1, 1]
Stacktrace:
[1] getindex
@ ./essentials.jl:14 [inlined]
[2] _bounds
@ ~/.julia/packages/DimensionalData/lbGZS/src/LookupArrays/lookup_arrays.jl:204 [inlined]
[3] _bounds
@ ~/.julia/packages/DimensionalData/lbGZS/src/LookupArrays/lookup_arrays.jl:202 [inlined]
[4] _bounds
@ ~/.julia/packages/DimensionalData/lbGZS/src/LookupArrays/lookup_arrays.jl:198 [inlined]
[5] bounds
@ ~/.julia/packages/DimensionalData/lbGZS/src/LookupArrays/lookup_arrays.jl:194 [inlined]
[6] bounds
@ ~/.julia/packages/DimensionalData/lbGZS/src/Dimensions/dimension.jl:193 [inlined]
[7] map
@ ./tuple.jl:275 [inlined]
[8] bounds
@ ~/.julia/packages/DimensionalData/lbGZS/src/Dimensions/dimension.jl:222 [inlined]
[9] extent
@ ~/.julia/packages/DimensionalData/lbGZS/src/Dimensions/dimension.jl:277 [inlined]
[10] extent
@ ~/.julia/packages/DimensionalData/lbGZS/src/array/array.jl:65 [inlined]
[11] show_after(io::IOContext{Base.TTY}, mime::MIME{Symbol("text/plain")}, A::Raster{Union{Missing, Float32}, 3, Tuple{X{DimensionalData.Dimensions.LookupArrays.Sampled{Float64, Vector{Float64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{Float64}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}, Y{DimensionalData.Dimensions.LookupArrays.Sampled{Float64, Vector{Float64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{Float64}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}, Ti{DimensionalData.Dimensions.LookupArrays.Sampled{DateTime, Vector{DateTime}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{DateTime}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}}, Tuple{}, Array{Union{Missing, Float32}, 3}, Symbol, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}, Missing})
@ Rasters ~/.julia/packages/Rasters/k5UxK/src/show.jl:5
[12] show(io::IOContext{Base.TTY}, mime::MIME{Symbol("text/plain")}, A::Raster{Union{Missing, Float32}, 3, Tuple{X{DimensionalData.Dimensions.LookupArrays.Sampled{Float64, Vector{Float64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{Float64}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}, Y{DimensionalData.Dimensions.LookupArrays.Sampled{Float64, Vector{Float64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{Float64}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}, Ti{DimensionalData.Dimensions.LookupArrays.Sampled{DateTime, Vector{DateTime}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Explicit{Matrix{DateTime}}, DimensionalData.Dimensions.LookupArrays.Intervals{DimensionalData.Dimensions.LookupArrays.Center}, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}}}}, Tuple{}, Array{Union{Missing, Float32}, 3}, Symbol, DimensionalData.Dimensions.LookupArrays.Metadata{Rasters.NCDsource, Dict{String, Any}}, Missing})
@ DimensionalData ~/.julia/packages/DimensionalData/lbGZS/src/array/show.jl:27
[13] (::REPL.var"#55#56"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:276
[14] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:557
[15] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:262
[16] display(d::REPL.REPLDisplay, x::Any)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:281
[17] display(x::Any)
@ Base.Multimedia ./multimedia.jl:340
[18] #invokelatest#2
@ ./essentials.jl:819 [inlined]
[19] invokelatest
@ ./essentials.jl:816 [inlined]
[20] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:305
[21] (::REPL.var"#57#58"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:287
[22] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:557
[23] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:285
[24] (::REPL.var"#do_respond#80"{Bool, Bool, REPL.var"#93#103"{REPL.LineEditREPL, REPL.REPLHistoryProvider}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:899
[25] (::VSCodeServer.var"#101#104"{REPL.var"#do_respond#80"{Bool, Bool, REPL.var"#93#103"{REPL.LineEditREPL, REPL.REPLHistoryProvider}, REPL.LineEditREPL, REPL.LineEdit.Prompt}})(mi::REPL.LineEdit.MIState, buf::IOBuffer, ok::Bool)
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.61.2/scripts/packages/VSCodeServer/src/repl.jl:122
[26] #invokelatest#2
@ ./essentials.jl:819 [inlined]
[27] invokelatest
@ ./essentials.jl:816 [inlined]
[28] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
@ REPL.LineEdit ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/LineEdit.jl:2647
[29] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
@ REPL ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/REPL/src/REPL.jl:1300
[30] (::REPL.var"#62#68"{REPL.LineEditREPL, REPL.REPLBackendRef})()
@ REPL ./task.jl:514
For a plain DimArray subsetting with a zero width interval works as expected but I still would expect the out of bounds case to throw a BoundsError:
julia> arr = DimArray(reshape(1:100,10,10), (X(1:10), Y(1:10)))
10×10 DimArray{Int64,2} with dimensions:
X Sampled{Int64} 1:10 ForwardOrdered Regular Points,
Y Sampled{Int64} 1:10 ForwardOrdered Regular Points
1 2 3 4 5 6 7 8 9 10
1 1 11 21 31 41 51 61 71 81 91
2 2 12 22 32 42 52 62 72 82 92
⋮ ⋮ ⋮
9 9 19 29 39 49 59 69 79 89 99
10 10 20 30 40 50 60 70 80 90 100
julia> arr[X=1..1]
1×10 DimArray{Int64,2} with dimensions:
X Sampled{Int64} 1:1 ForwardOrdered Regular Points,
Y Sampled{Int64} 1:10 ForwardOrdered Regular Points
1 2 3 4 5 6 7 8 9 10
1 1 11 21 31 41 51 61 71 81 91
julia> arr[X=100..103] # This should throw a BoundsError
0×10 DimArray{Int64,2} with dimensions:
X Sampled{Int64} 11:10 ForwardOrdered Regular Points,
Y Sampled{Int64} 1:10 ForwardOrdered Regular Points
1 2 3 4 5 6 7 8 9 10
Ah ok that makes sense. Yeah it should be out if bounds.
When I subset a Raster with the wrong year I get the error below. I tried to reduce this to a artificially created DimArray but failed. I would have expected this to fail already during the subsetting but maybe there is a use case for it to return a zero dimension.