Skip to content

Commit

Permalink
use to_indices rather than own normalizeindexes for index normalizati…
Browse files Browse the repository at this point in the history
…on (#250)

Co-authored-by: Haakon Ludvig Langeland Ervik <[email protected]>
  • Loading branch information
Alexander-Barth and haakon-e committed Mar 6, 2024
1 parent 0284393 commit 0335d26
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,11 @@ end

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

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

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

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

_normalizeindex(n,ind::Base.OneTo) = 1:1:ind.stop
_normalizeindex(n,ind::Colon) = 1:1:n
_normalizeindex(n,ind::Integer) = ind:1:ind
_normalizeindex(n,ind::UnitRange) = StepRange(ind)
_normalizeindex(n,ind::StepRange) = ind
_normalizeindex(n,ind) = error("unsupported index")

# indexes can be longer than sz
function normalizeindexes(sz,indexes)
return ntuple(i -> _normalizeindex(sz[i],indexes[i]), length(sz))
end


# computes the size of the array `a` after applying the indexes
# size(a[indexes...]) == size_getindex(a,indexes...)

Expand Down

2 comments on commit 0335d26

@Alexander-Barth
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release notes:

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/102369

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.14.3 -m "<description of version>" 0335d266a854ab861e2fb7c6cb371170a397b463
git push origin v0.14.3

Please sign in to comment.