Skip to content

Commit 0335d26

Browse files
use to_indices rather than own normalizeindexes for index normalization (#250)
Co-authored-by: Haakon Ludvig Langeland Ervik <[email protected]>
1 parent 0284393 commit 0335d26

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

src/variable.jl

+4-20
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,11 @@ end
426426

427427
_write_data_to_nc(v::Variable, data) = _write_data_to_nc(v, data, 1)
428428

429-
function _write_data_to_nc(v::Variable{T, N}, data, indexes::StepRange{<:Integer,<:Integer}...) where {T, N}
430-
start,count,stride,jlshape = ncsub(v,indexes)
431-
nc_put_vars(v.ds.ncid,v.varid,start,count,stride,T.(data))
432-
end
429+
function _write_data_to_nc(v::Variable{T}, data, indexes::AbstractRange{<:Integer}...) where T
430+
ind = prod(length.(indexes)) == 1 ? first.(indexes) : to_indices(v,indexes)
433431

434-
function _write_data_to_nc(v::Variable, data, indexes::Union{AbstractRange{<:Integer}}...)
435-
ind = prod(length.(indexes)) == 1 ? first.(indexes) : normalizeindexes(size(v),indexes)
436-
return _write_data_to_nc(v, data, ind...)
432+
start,count,stride,jlshape = ncsub(v,indexes)
433+
return nc_put_vars(v.ds.ncid,v.varid,start,count,stride,T.(data))
437434
end
438435

439436
function eachchunk(v::Variable)
@@ -451,19 +448,6 @@ haschunks(v::Variable) = (_chunking(v)[1] == :contiguous ? DiskArrays.Unchunked(
451448
eachchunk(v::CFVariable{T,N,<:Variable}) where {T,N} = eachchunk(v.var)
452449
haschunks(v::CFVariable{T,N,<:Variable}) where {T,N} = haschunks(v.var)
453450

454-
_normalizeindex(n,ind::Base.OneTo) = 1:1:ind.stop
455-
_normalizeindex(n,ind::Colon) = 1:1:n
456-
_normalizeindex(n,ind::Integer) = ind:1:ind
457-
_normalizeindex(n,ind::UnitRange) = StepRange(ind)
458-
_normalizeindex(n,ind::StepRange) = ind
459-
_normalizeindex(n,ind) = error("unsupported index")
460-
461-
# indexes can be longer than sz
462-
function normalizeindexes(sz,indexes)
463-
return ntuple(i -> _normalizeindex(sz[i],indexes[i]), length(sz))
464-
end
465-
466-
467451
# computes the size of the array `a` after applying the indexes
468452
# size(a[indexes...]) == size_getindex(a,indexes...)
469453

0 commit comments

Comments
 (0)