Skip to content

Commit c6647a6

Browse files
authored
Merge pull request #695 from mimiframework/index-interfaces
begin and end: Indexing Interface for TimestepArrays
2 parents dbec056 + 2bea569 commit c6647a6

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

src/core/time_arrays.jl

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,59 @@ function _get_ts_indices(ts_array::Array{TimestepValue{T}, 1}, times::Union{Tupl
127127
return [_get_time_value_position(times, ts) for ts in ts_array]
128128
end
129129

130+
# Base.firstindex and Base.lastindex
131+
function Base.firstindex(arr::TimestepArray{T_TS, T, N, ti}) where {T_TS, T, N, ti}
132+
if ti == 1
133+
return Mimi.TimestepIndex(1)
134+
else
135+
return 1
136+
end
137+
end
138+
139+
function Base.lastindex(arr::TimestepArray{T_TS, T, N, ti}) where {T_TS, T, N, ti}
140+
if ti == length(size(arr.data))
141+
return Mimi.TimestepIndex(length(arr.data))
142+
else
143+
return length(arr.data)
144+
end
145+
end
146+
147+
function Base.lastindex(arr::TimestepArray{T_TS, T, N, ti}, dim::Int) where {T_TS, T, N, ti}
148+
if ti == dim
149+
return Mimi.TimestepIndex(size(arr.data, dim))
150+
else
151+
return size(arr.data, dim)
152+
end
153+
end
154+
155+
function Base.firstindex(arr::TimestepArray{T_TS, T, N, ti}, dim::Int) where {T_TS, T, N, ti}
156+
if ti == dim
157+
return Mimi.TimestepIndex(1)
158+
else
159+
return 1
160+
end
161+
end
162+
163+
# add axes methos copied from abstarctarray.jl:56
164+
function Base.axes(A::TimestepArray{T_TS, T, N, ti}, d::Int) where {T_TS, T, N, ti}
165+
_d_lessthan_N = d <= N;
166+
if d == ti
167+
if _d_lessthan_N
168+
return Tuple(TimestepIndex.(1:size(A,d)))
169+
else
170+
return TimestepIndex(1)
171+
end
172+
else
173+
if _d_lessthan_N
174+
if _d_lessthan_N
175+
return 1:size(A,d)
176+
else
177+
return 1
178+
end
179+
end
180+
end
181+
end
182+
130183
#
131184
# b. TimestepVector
132185
#
@@ -233,8 +286,6 @@ function Base.length(v::TimestepVector)
233286
return length(v.data)
234287
end
235288

236-
Base.lastindex(v::TimestepVector) = length(v)
237-
238289
#
239290
# c. TimestepMatrix
240291
#
@@ -267,7 +318,6 @@ function Base.getindex(mat::TimestepMatrix{FixedTimestep{FIRST, STEP}, T, 2}, id
267318
end
268319

269320
function Base.getindex(mat::TimestepMatrix{VariableTimestep{TIMES}, T, 2}, idx::AnyIndex, ts::VariableTimestep{TIMES}) where {T, TIMES}
270-
# WAS THIS: data = mat.data[ts.t, idx, ts.t]
271321
data = mat.data[idx, ts.t]
272322
_missing_data_check(data, ts.t)
273323
end

test/test_timesteparrays.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ idx4 = TimestepIndex(4)
4444

4545
x = TimestepVector{FixedTimestep{2000, 1}, Int}([9, 10, 11, 12])
4646
@test length(x) == 4
47-
@test lastindex(x) == 4
47+
@test lastindex(x) == TimestepIndex(4)
4848

4949
time_dim_val = [9, 10, 11, 12]
5050
temp_dim_val = [100, 101, 102, 103]
@@ -611,6 +611,23 @@ y_mat = TimestepMatrix{VariableTimestep{y_years}, Int, 1}(time_dim_val[:,:,2])
611611

612612
@test eltype(x_vec) == eltype(y_vec) == eltype(y_vec) == eltype(y_mat) == eltype(time_dim_val)
613613

614+
# TODO begin syntax is depreacated v1.0.0 - v1.3.0, so enable the tests after
615+
# after we disable Julia versions below v1.4.0
616+
617+
# @test x_vec[begin] == time_dim_val[:,1,1][begin]
618+
# @test x_mat[begin,1] == time_dim_val[:,:,1][begin,1]
619+
# @test x_mat[begin,2] == time_dim_val[:,:,1][begin,2]
620+
# @test y_vec[begin] == time_dim_val[:,2,2][begin]
621+
# @test y_mat[begin,1] == time_dim_val[:,:,2][begin,1]
622+
# @test y_mat[begin,2] == time_dim_val[:,:,2][begin,2]
623+
624+
@test x_vec[end] == time_dim_val[:,1,1][end]
625+
@test x_mat[end,1] == time_dim_val[:,:,1][end,1]
626+
@test x_mat[end,2] == time_dim_val[:,:,1][end,2]
627+
@test y_vec[end] == time_dim_val[:,2,2][end]
628+
@test y_mat[end,1] == time_dim_val[:,:,2][end,1]
629+
@test y_mat[end,2] == time_dim_val[:,:,2][end,2]
630+
614631
#------------------------------------------------------------------------------
615632
# 6. Test that getindex for TimestepArrays doesn't allow access to `missing`
616633
# values during `run` that haven't been computed yet.

0 commit comments

Comments
 (0)