Skip to content
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
56 changes: 53 additions & 3 deletions src/core/time_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,59 @@ function _get_ts_indices(ts_array::Array{TimestepValue{T}, 1}, times::Union{Tupl
return [_get_time_value_position(times, ts) for ts in ts_array]
end

# Base.firstindex and Base.lastindex
function Base.firstindex(arr::TimestepArray{T_TS, T, N, ti}) where {T_TS, T, N, ti}
if ti == 1
return Mimi.TimestepIndex(1)
else
return 1
end
end

function Base.lastindex(arr::TimestepArray{T_TS, T, N, ti}) where {T_TS, T, N, ti}
if ti == length(size(arr.data))
return Mimi.TimestepIndex(length(arr.data))
else
return length(arr.data)
end
end

function Base.lastindex(arr::TimestepArray{T_TS, T, N, ti}, dim::Int) where {T_TS, T, N, ti}
if ti == dim
return Mimi.TimestepIndex(size(arr.data, dim))
else
return size(arr.data, dim)
end
end

function Base.firstindex(arr::TimestepArray{T_TS, T, N, ti}, dim::Int) where {T_TS, T, N, ti}
if ti == dim
return Mimi.TimestepIndex(1)
else
return 1
end
end

# add axes methos copied from abstarctarray.jl:56
function Base.axes(A::TimestepArray{T_TS, T, N, ti}, d::Int) where {T_TS, T, N, ti}
_d_lessthan_N = d <= N;
if d == ti
if _d_lessthan_N
return Tuple(TimestepIndex.(1:size(A,d)))
else
return TimestepIndex(1)
end
else
if _d_lessthan_N
if _d_lessthan_N
return 1:size(A,d)
else
return 1
end
end
end
end

#
# b. TimestepVector
#
Expand Down Expand Up @@ -233,8 +286,6 @@ function Base.length(v::TimestepVector)
return length(v.data)
end

Base.lastindex(v::TimestepVector) = length(v)
Copy link
Collaborator Author

@lrennels lrennels Apr 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this line since it's a duplicate of the new method.


#
# c. TimestepMatrix
#
Expand Down Expand Up @@ -267,7 +318,6 @@ function Base.getindex(mat::TimestepMatrix{FixedTimestep{FIRST, STEP}, T, 2}, id
end

function Base.getindex(mat::TimestepMatrix{VariableTimestep{TIMES}, T, 2}, idx::AnyIndex, ts::VariableTimestep{TIMES}) where {T, TIMES}
# WAS THIS: data = mat.data[ts.t, idx, ts.t]
data = mat.data[idx, ts.t]
_missing_data_check(data, ts.t)
end
Expand Down
19 changes: 18 additions & 1 deletion test/test_timesteparrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ idx4 = TimestepIndex(4)

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

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

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

# TODO begin syntax is depreacated v1.0.0 - v1.3.0, so enable the tests after
# after we disable Julia versions below v1.4.0

# @test x_vec[begin] == time_dim_val[:,1,1][begin]
# @test x_mat[begin,1] == time_dim_val[:,:,1][begin,1]
# @test x_mat[begin,2] == time_dim_val[:,:,1][begin,2]
# @test y_vec[begin] == time_dim_val[:,2,2][begin]
# @test y_mat[begin,1] == time_dim_val[:,:,2][begin,1]
# @test y_mat[begin,2] == time_dim_val[:,:,2][begin,2]

@test x_vec[end] == time_dim_val[:,1,1][end]
@test x_mat[end,1] == time_dim_val[:,:,1][end,1]
@test x_mat[end,2] == time_dim_val[:,:,1][end,2]
@test y_vec[end] == time_dim_val[:,2,2][end]
@test y_mat[end,1] == time_dim_val[:,:,2][end,1]
@test y_mat[end,2] == time_dim_val[:,:,2][end,2]

#------------------------------------------------------------------------------
# 6. Test that getindex for TimestepArrays doesn't allow access to `missing`
# values during `run` that haven't been computed yet.
Expand Down