From d75c6cb20cf1c1aeea39b0c5a2e34ceed1f5aa60 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Fri, 14 Apr 2023 17:36:27 +0200 Subject: [PATCH 01/33] replace indexing with read/writeblock! --- Project.toml | 3 ++- src/NCDatasets.jl | 1 + src/cfvariable.jl | 6 +++--- src/variable.jl | 47 +++++++++++++++++++++++------------------------ 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Project.toml b/Project.toml index b1417878..b97d0c76 100644 --- a/Project.toml +++ b/Project.toml @@ -10,6 +10,7 @@ CFTime = "179af706-886a-5703-950a-314cd64e0468" CommonDataModel = "1fbeeb36-5f17-413c-809b-666fb144f157" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3" NetCDF_jll = "7243133f-43d8-5620-bbf4-c2c921802cf3" NetworkOptions = "ca575930-c2e3-43a9-ace4-1e988b2c1908" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" @@ -24,10 +25,10 @@ julia = "1.3" [extras] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" [targets] test = ["Dates", "Test", "Random", "Printf", "IntervalSets"] diff --git a/src/NCDatasets.jl b/src/NCDatasets.jl index 1616f7a2..2140f5ef 100644 --- a/src/NCDatasets.jl +++ b/src/NCDatasets.jl @@ -38,6 +38,7 @@ import CommonDataModel: AbstractDataset, AbstractVariable, groupnames, group, dimnames, dim, attribnames, attrib +import DiskArrays: readblock!, writeblock! function __init__() NetCDF_jll.is_available() && init_certificate_authority() diff --git a/src/cfvariable.jl b/src/cfvariable.jl index fb2eb03c..fbdfac0a 100644 --- a/src/cfvariable.jl +++ b/src/cfvariable.jl @@ -403,7 +403,7 @@ function _range_indices_dest(of,v,rest...) end range_indices_dest(ri...) = _range_indices_dest((),ri...) -function Base.getindex(v::Union{CFVariable,Variable,MFVariable,SubVariable},indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{Int}}...) +function readblock!(v::Union{CFVariable,Variable,MFVariable,SubVariable}, aout, indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{Int}}...) @debug "transform vector of indices to ranges" sz_source = size(v) @@ -421,7 +421,7 @@ function Base.getindex(v::Union{CFVariable,Variable,MFVariable,SubVariable},indi R = first(CartesianIndices(length.(ri))) ind_source = ntuple(i -> ri[i][R[i]],N) ind_dest = ntuple(i -> ri_dest[i][R[i]],length(ri_dest)) - return v[ind_source...] + return aout[indics...] .= v[ind_source...] end dest = Array{eltype(v),length(sz_dest)}(undef,sz_dest) @@ -432,7 +432,7 @@ function Base.getindex(v::Union{CFVariable,Variable,MFVariable,SubVariable},indi buffer = Array{eltype(v.var),length(ind_dest)}(undef,length.(ind_dest)) load!(v,view(dest,ind_dest...),buffer,ind_source...) end - return dest + return aout[indices...] .= dest end diff --git a/src/variable.jl b/src/variable.jl index ba77e2b6..425df704 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -310,12 +310,12 @@ nomissing(a::AbstractArray,value) = a export nomissing -function Base.getindex(v::Variable,indexes::Int...) +function readblock!(v::Variable, aout, indexes::Int...) datamode(v.ds) - return nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]]) + aout[indexes...] .= nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]]) end -function Base.setindex!(v::Variable{T,N},data,indexes::Int...) where N where T +function writeblock!(v::Variable{T,N},data,indexes::Int...) where N where T @debug "$(@__LINE__)" datamode(v.ds) # use zero-based indexes and reversed order @@ -323,20 +323,20 @@ function Base.setindex!(v::Variable{T,N},data,indexes::Int...) where N where T return data end -function Base.getindex(v::Variable{T,N},indexes::Colon...) where {T,N} +function readblock!(v::Variable{T,N}, aout, indexes::Colon...) where {T,N} datamode(v.ds) data = Array{T,N}(undef,size(v)) nc_get_var!(v.ds.ncid,v.varid,data) # special case for scalar NetCDF variable if N == 0 - return data[] + aout[indexes...] .= data[] else - return data + aout[indexes...] .= data end end -function Base.setindex!(v::Variable{T,N},data::T,indexes::Colon...) where {T,N} +function writeblock!(v::Variable{T,N},data::T,indexes::Colon...) where {T,N} @debug "setindex! colon $data" datamode(v.ds) # make sure that the file is in data mode tmp = fill(data,size(v)) @@ -348,7 +348,7 @@ end for data_type = [Number, String, Char] @eval begin # call to v .= 123 - function Base.setindex!(v::Variable{T,N},data::$data_type) where {T,N} + function writeblock!(v::Variable{T,N},data::$data_type) where {T,N} @debug "setindex! $data" datamode(v.ds) # make sure that the file is in data mode tmp = fill(convert(T,data),size(v)) @@ -356,9 +356,9 @@ for data_type = [Number, String, Char] return data end - Base.setindex!(v::Variable,data::$data_type,indexes::Colon...) = setindex!(v::Variable,data) + writeblock!(v::Variable,data::$data_type,indexes::Colon...) = setindex!(v::Variable,data) - function Base.setindex!(v::Variable{T,N},data::$data_type,indexes::StepRange{Int,Int}...) where {T,N} + function writeblock!(v::Variable{T,N},data::$data_type,indexes::StepRange{Int,Int}...) where {T,N} datamode(v.ds) # make sure that the file is in data mode start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) tmp = fill(convert(T,data),jlshape) @@ -368,14 +368,14 @@ for data_type = [Number, String, Char] end end -function Base.setindex!(v::Variable{T,N},data::AbstractArray{T,N},indexes::Colon...) where {T,N} +function writeblock!(v::Variable{T,N},data::AbstractArray{T,N},indexes::Colon...) where {T,N} datamode(v.ds) # make sure that the file is in data mode nc_put_var(v.ds.ncid,v.varid,data) return data end -function Base.setindex!(v::Variable{T,N},data::AbstractArray{T2,N},indexes::Colon...) where {T,T2,N} +function writeblock!(v::Variable{T,N},data::AbstractArray{T2,N},indexes::Colon...) where {T,T2,N} datamode(v.ds) # make sure that the file is in data mode tmp = if T <: Integer @@ -441,16 +441,16 @@ end return start,count,stride end -function Base.getindex(v::Variable{T,N},indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} +function readblock!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} start,count,stride,jlshape = ncsub(indexes[1:N]) data = Array{T,N}(undef,jlshape) datamode(v.ds) - nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) + aout[indexes...] .= nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) return data end -function Base.setindex!(v::Variable{T,N},data::T,indexes::StepRange{Int,Int}...) where {T,N} +function writeblock!(v::Variable{T,N},data::T,indexes::StepRange{Int,Int}...) where {T,N} datamode(v.ds) # make sure that the file is in data mode start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) tmp = fill(data,jlshape) @@ -458,7 +458,7 @@ function Base.setindex!(v::Variable{T,N},data::T,indexes::StepRange{Int,Int}...) return data end -function Base.setindex!(v::Variable{T,N},data::Array{T,N},indexes::StepRange{Int,Int}...) where {T,N} +function writeblock!(v::Variable{T,N},data::Array{T,N},indexes::StepRange{Int,Int}...) where {T,N} datamode(v.ds) # make sure that the file is in data mode start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) nc_put_vars(v.ds.ncid,v.varid,start,count,stride,data) @@ -466,7 +466,7 @@ function Base.setindex!(v::Variable{T,N},data::Array{T,N},indexes::StepRange{Int end # data can be Array{T2,N} or BitArray{N} -function Base.setindex!(v::Variable{T,N},data::AbstractArray,indexes::StepRange{Int,Int}...) where {T,N} +function writeblock!(v::Variable{T,N},data::AbstractArray,indexes::StepRange{Int,Int}...) where {T,N} datamode(v.ds) # make sure that the file is in data mode start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) @@ -479,7 +479,7 @@ end -function Base.getindex(v::Variable{T,N},indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where {T,N} +function readblock!(v::Variable{T,N}, aout, indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where {T,N} sz = size(v) start,count,stride = ncsub2(sz,indexes...) jlshape = _shape_after_slice(sz,indexes...) @@ -487,16 +487,15 @@ function Base.getindex(v::Variable{T,N},indexes::Union{Int,Colon,AbstractRange{< datamode(v.ds) nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) - - return data + aout[indexes...] .= data end # NetCDF scalars indexed as [] -Base.getindex(v::Variable{T, 0}) where T = v[1] +readblock!(v::Variable{T, 0}, aout) where T = aout[1] = v[1] -function Base.setindex!(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) +function writeblock!(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) ind = normalizeindexes(size(v),indexes) # make arrays out of scalars (arrays can have zero dimensions) @@ -508,7 +507,7 @@ function Base.setindex!(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{ end -Base.getindex(v::Union{MFVariable,DeferVariable,Variable},ci::CartesianIndices) = v[ci.indices...] -Base.setindex!(v::Union{MFVariable,DeferVariable,Variable},data,ci::CartesianIndices) = setindex!(v,data,ci.indices...) +readblock!(v::Union{MFVariable,DeferVariable,Variable}, aout, ci::CartesianIndices) = aout[ci.indices...] .= v[ci.indices...] +writeblock!(v::Union{MFVariable,DeferVariable,Variable},data,ci::CartesianIndices) = writeblock!(v,data,ci.indices...) From 378be0dbd0c4d55a54878a39aa5fa205e61d2376 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 17 Apr 2023 10:10:47 +0200 Subject: [PATCH 02/33] fix slicing issue when reading --- src/variable.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/variable.jl b/src/variable.jl index 425df704..2f12bc88 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -446,7 +446,8 @@ function readblock!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR data = Array{T,N}(undef,jlshape) datamode(v.ds) - aout[indexes...] .= nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) + nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) + aout .= data return data end From 75fcfa446e4e8bd6187f4c15c530993137911a6a Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 17 Apr 2023 13:36:28 +0200 Subject: [PATCH 03/33] fix other reading indexing --- src/variable.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 2f12bc88..4f7646b2 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -443,12 +443,12 @@ end function readblock!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} start,count,stride,jlshape = ncsub(indexes[1:N]) - data = Array{T,N}(undef,jlshape) - + # data = Array{T,N}(undef,jlshape) datamode(v.ds) - nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) - aout .= data - return data + # nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) + nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) + # aout .= data + return aout end function writeblock!(v::Variable{T,N},data::T,indexes::StepRange{Int,Int}...) where {T,N} @@ -488,7 +488,7 @@ function readblock!(v::Variable{T,N}, aout, indexes::Union{Int,Colon,AbstractRan datamode(v.ds) nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) - aout[indexes...] .= data + aout .= data end # NetCDF scalars indexed as [] @@ -508,7 +508,7 @@ function writeblock!(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{<:I end -readblock!(v::Union{MFVariable,DeferVariable,Variable}, aout, ci::CartesianIndices) = aout[ci.indices...] .= v[ci.indices...] +readblock!(v::Union{MFVariable,DeferVariable,Variable}, aout, ci::CartesianIndices) = aout .= v[ci.indices...] writeblock!(v::Union{MFVariable,DeferVariable,Variable},data,ci::CartesianIndices) = writeblock!(v,data,ci.indices...) From c3dbfbc6471c902bb200103559a461939f1e9a62 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 17 Apr 2023 15:09:54 +0200 Subject: [PATCH 04/33] pass more tests --- src/variable.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 4f7646b2..5f184c4a 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -443,11 +443,11 @@ end function readblock!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} start,count,stride,jlshape = ncsub(indexes[1:N]) - # data = Array{T,N}(undef,jlshape) + data = Array{T,N}(undef,jlshape) datamode(v.ds) - # nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) - nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) - # aout .= data + nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) + # nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) + aout .= data return aout end @@ -504,7 +504,7 @@ function writeblock!(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{<:I data = fill(data,length.(ind)) end - return v[ind...] = data + return writeblock!(v, data, ind...) end From 012678be3d65bdcaf70aeb9b3dff37fcba057624 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Tue, 18 Apr 2023 10:05:06 +0200 Subject: [PATCH 05/33] small typo --- src/cfvariable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cfvariable.jl b/src/cfvariable.jl index fbdfac0a..22e0c4df 100644 --- a/src/cfvariable.jl +++ b/src/cfvariable.jl @@ -421,7 +421,7 @@ function readblock!(v::Union{CFVariable,Variable,MFVariable,SubVariable}, aout, R = first(CartesianIndices(length.(ri))) ind_source = ntuple(i -> ri[i][R[i]],N) ind_dest = ntuple(i -> ri_dest[i][R[i]],length(ri_dest)) - return aout[indics...] .= v[ind_source...] + return aout[indices...] .= v[ind_source...] end dest = Array{eltype(v),length(sz_dest)}(undef,sz_dest) From 88f3a5f70ff288bc86db7875b3e2e624b4003ac5 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 24 Apr 2023 10:17:32 +0200 Subject: [PATCH 06/33] macro for diskarrays methods for Variable --- src/NCDatasets.jl | 3 +++ src/cfvariable.jl | 6 +++--- src/variable.jl | 37 +++++++++++++++++++++++++++++++++++-- test/runtests.jl | 2 +- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/NCDatasets.jl b/src/NCDatasets.jl index 2140f5ef..c80a65e3 100644 --- a/src/NCDatasets.jl +++ b/src/NCDatasets.jl @@ -39,6 +39,7 @@ import CommonDataModel: AbstractDataset, AbstractVariable, dimnames, dim, attribnames, attrib import DiskArrays: readblock!, writeblock! +using DiskArrays: @implement_diskarray function __init__() NetCDF_jll.is_available() && init_certificate_authority() @@ -66,6 +67,8 @@ include("ncgen.jl") include("select.jl") include("precompile.jl") +@implement_diskarray NCDatasets.Variable + export CatArrays export CFTime export daysinmonth, daysinyear, yearmonthday, yearmonth, monthday diff --git a/src/cfvariable.jl b/src/cfvariable.jl index 22e0c4df..e990f92c 100644 --- a/src/cfvariable.jl +++ b/src/cfvariable.jl @@ -403,7 +403,7 @@ function _range_indices_dest(of,v,rest...) end range_indices_dest(ri...) = _range_indices_dest((),ri...) -function readblock!(v::Union{CFVariable,Variable,MFVariable,SubVariable}, aout, indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{Int}}...) +function Base.getindex(v::Union{CFVariable,MFVariable,SubVariable},indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{Int}}...) @debug "transform vector of indices to ranges" sz_source = size(v) @@ -421,7 +421,7 @@ function readblock!(v::Union{CFVariable,Variable,MFVariable,SubVariable}, aout, R = first(CartesianIndices(length.(ri))) ind_source = ntuple(i -> ri[i][R[i]],N) ind_dest = ntuple(i -> ri_dest[i][R[i]],length(ri_dest)) - return aout[indices...] .= v[ind_source...] + return v[ind_source...] end dest = Array{eltype(v),length(sz_dest)}(undef,sz_dest) @@ -432,7 +432,7 @@ function readblock!(v::Union{CFVariable,Variable,MFVariable,SubVariable}, aout, buffer = Array{eltype(v.var),length(ind_dest)}(undef,length.(ind_dest)) load!(v,view(dest,ind_dest...),buffer,ind_source...) end - return aout[indices...] .= dest + return dest end diff --git a/src/variable.jl b/src/variable.jl index 5f184c4a..b079ba72 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -508,7 +508,40 @@ function writeblock!(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{<:I end -readblock!(v::Union{MFVariable,DeferVariable,Variable}, aout, ci::CartesianIndices) = aout .= v[ci.indices...] -writeblock!(v::Union{MFVariable,DeferVariable,Variable},data,ci::CartesianIndices) = writeblock!(v,data,ci.indices...) +readblock!(v::Variable, aout, ci::CartesianIndices) = aout .= v[ci.indices...] +writeblock!(v::Variable, data, ci::CartesianIndices) = writeblock!(v,data,ci.indices...) +Base.getindex(v::Union{MFVariable,DeferVariable},ci::CartesianIndices) = v[ci.indices...] +Base.setindex!(v::Union{MFVariable,DeferVariable},data,ci::CartesianIndices) = setindex!(v,data,ci.indices...) +function readblock!(v::Variable, aout, indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{Int}}...) + @debug "transform vector of indices to ranges" + + sz_source = size(v) + ri = to_range_list.(indices,sz_source) + sz_dest = NCDatasets._shape_after_slice(sz_source,indices...) + + N = length(indices) + + ri_dest = range_indices_dest(ri...) + @debug "ri_dest $ri_dest" + @debug "ri $ri" + + if all(==(1),length.(ri)) + # single chunk + R = first(CartesianIndices(length.(ri))) + ind_source = ntuple(i -> ri[i][R[i]],N) + ind_dest = ntuple(i -> ri_dest[i][R[i]],length(ri_dest)) + return aout[indices...] .= v[ind_source...] + end + + dest = Array{eltype(v),length(sz_dest)}(undef,sz_dest) + for R in CartesianIndices(length.(ri)) + ind_source = ntuple(i -> ri[i][R[i]],N) + ind_dest = ntuple(i -> ri_dest[i][R[i]],length(ri_dest)) + #dest[ind_dest...] = v[ind_source...] + buffer = Array{eltype(v.var),length(ind_dest)}(undef,length.(ind_dest)) + load!(v,view(dest,ind_dest...),buffer,ind_source...) + end + return aout[indices...] .= dest +end diff --git a/test/runtests.jl b/test/runtests.jl index 249c8633..618d20dc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,7 +9,7 @@ println("NetCDF version: ",NCDatasets.nc_inq_libvers()) @testset "NCDatasets" begin include("test_simple.jl") - include("test_scalar.jl") + # include("test_scalar.jl") include("test_append.jl") include("test_append2.jl") include("test_attrib.jl") From 1bac35e5598daba79b14ff910b417294fa6524a9 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 24 Apr 2023 11:00:27 +0200 Subject: [PATCH 07/33] avoid methods conflict with `view` on Variable --- src/subvariable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subvariable.jl b/src/subvariable.jl index aa6eeda9..7acbcdc3 100644 --- a/src/subvariable.jl +++ b/src/subvariable.jl @@ -91,7 +91,7 @@ close(ds) ``` """ -Base.view(v::AbstractVariable,indices::Union{Int,Colon,AbstractVector{Int}}...) = SubVariable(v,indices...) +# Base.view(v::AbstractVariable,indices::Union{Int,Colon,AbstractVector{Int}}...) = SubVariable(v,indices...) Base.view(v::SubVariable,indices::CartesianIndex) = view(v,indices.I...) Base.view(v::SubVariable,indices::CartesianIndices) = view(v,indices.indices...) From 33816fe810b85a15e24016711daf4cc1a710f826 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 24 Apr 2023 11:00:43 +0200 Subject: [PATCH 08/33] adapt some tests --- test/test_check_size.jl | 8 ++++---- test/test_writevar.jl | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/test_check_size.jl b/test/test_check_size.jl index 226f9fac..88ad2673 100644 --- a/test/test_check_size.jl +++ b/test/test_check_size.jl @@ -17,10 +17,10 @@ defVar(ds, "w", Float64, ("x", "Time")) for i in 1:10 ds["Time"][i] = i - ds["a"][:,i] = 1 - @test_throws NCDatasets.NetCDFError ds["u"][:,i] = collect(1:9) - @test_throws NCDatasets.NetCDFError ds["v"][:,i] = collect(1:11) - @test_throws NCDatasets.NetCDFError ds["w"][:,i] = reshape(collect(1:20), 10, 2) + ds["a"][:,i] .= 1 + @test_throws DimensionMismatch ds["u"][:,i] = collect(1:9) + @test_throws DimensionMismatch ds["v"][:,i] = collect(1:11) + @test_throws DimensionMismatch ds["w"][:,i] = reshape(collect(1:20), 10, 2) # ignore singleton dimension ds["w"][:,i] = reshape(collect(1:10), 1, 1, 10, 1) diff --git a/test/test_writevar.jl b/test/test_writevar.jl index 8e50c97f..b7cac335 100644 --- a/test/test_writevar.jl +++ b/test/test_writevar.jl @@ -26,7 +26,7 @@ for T in [UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64,Float32,Float64] @test all(v[:,:][:] .== 123) # write scalar - v[:,:] = T(100) + v[:,:] .= T(100) @test all(v[:,:][:] .== 100) # write array (different type) @@ -34,7 +34,7 @@ for T in [UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64,Float32,Float64] @test all(v[:,:][:] .== 123) # write scalar (different type) - v[:,:] = 100 + v[:,:] .= 100 @test all(v[:,:][:] .== 100) # using StepRange as index @@ -43,7 +43,7 @@ for T in [UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64,Float32,Float64] @test all(v[:,:][:] .== 123) # write scalar - v[1:end,1:end] = T(100) + v[1:end,1:end] .= T(100) @test all(v[:,:][:] .== 100) # write array (different type) @@ -51,21 +51,21 @@ for T in [UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64,Float32,Float64] @test all(v[:,:][:] .== 123) # write scalar (different type) - v[1:end,1:end] = 100 + v[1:end,1:end] .= 100 @test all(v[:,:][:] .== 100) # step range ref = zeros(sz) - v[:,:] = 0 + v[:,:] .= 0 ref[1:2:end,2:2:end] .= 1 - v[1:2:end,2:2:end] = 1 + v[1:2:end,2:2:end] .= 1 @test v[:,:] == ref # write scalar (different type) ref[1:2:end,2:2:end] .= UInt8(2) - v[1:2:end,2:2:end] = UInt8(2) + v[1:2:end,2:2:end] .= UInt8(2) @test v[:,:] == ref ref[1,1] = 3 From d11cc271e1968bbdf315efb022adffb680d979b4 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 24 Apr 2023 11:30:32 +0200 Subject: [PATCH 09/33] pass test_check_size --- test/test_check_size.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_check_size.jl b/test/test_check_size.jl index 88ad2673..b829feed 100644 --- a/test/test_check_size.jl +++ b/test/test_check_size.jl @@ -29,11 +29,11 @@ end ds["w"][:,:] = ones(10,10) # w should grow along the unlimited dimension -ds["w"][:,:] = ones(10,15) +ds["w"][:,1:15] = ones(10,15) @test size(ds["w"]) == (10,15) # w cannot grow along a fixed dimension -@test_throws NCDatasets.NetCDFError ds["w"][:,:] = ones(11,15) +@test_throws DimensionMismatch ds["w"][:,:] = ones(11,15) # NetCDF: Index exceeds dimension bound @test_throws NCDatasets.NetCDFError ds["u"][100,100] @@ -46,7 +46,7 @@ ds = NCDataset(filename, "c") ds.dim["z"] = 4 ds.dim["time"] = Inf defVar(ds, "temp", Float64, ("z", "time")) -ds["temp"][:, :, 1] = rand(4) +ds["temp"][:, 1] = rand(4) close(ds) rm(filename) From 821b67ff24f92774a52b3447c596b2687969bc2c Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 24 Apr 2023 15:32:27 +0200 Subject: [PATCH 10/33] fix read and write scalar variable --- src/variable.jl | 7 ++++--- test/runtests.jl | 2 +- test/test_scalar.jl | 2 +- test/test_variable.jl | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index b079ba72..6951edf9 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -312,7 +312,7 @@ export nomissing function readblock!(v::Variable, aout, indexes::Int...) datamode(v.ds) - aout[indexes...] .= nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]]) + aout[indexes...] = nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]]) end function writeblock!(v::Variable{T,N},data,indexes::Int...) where N where T @@ -356,7 +356,7 @@ for data_type = [Number, String, Char] return data end - writeblock!(v::Variable,data::$data_type,indexes::Colon...) = setindex!(v::Variable,data) + writeblock!(v::Variable,data::$data_type,indexes::Colon...) = writeblock!(v::Variable,data) function writeblock!(v::Variable{T,N},data::$data_type,indexes::StepRange{Int,Int}...) where {T,N} datamode(v.ds) # make sure that the file is in data mode @@ -492,7 +492,8 @@ function readblock!(v::Variable{T,N}, aout, indexes::Union{Int,Colon,AbstractRan end # NetCDF scalars indexed as [] -readblock!(v::Variable{T, 0}, aout) where T = aout[1] = v[1] +readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) +writeblock!(v::Variable{T, 0}, data::Array{T, 0}) where T = writeblock!(v, data[1]) diff --git a/test/runtests.jl b/test/runtests.jl index 618d20dc..249c8633 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,7 +9,7 @@ println("NetCDF version: ",NCDatasets.nc_inq_libvers()) @testset "NCDatasets" begin include("test_simple.jl") - # include("test_scalar.jl") + include("test_scalar.jl") include("test_append.jl") include("test_append2.jl") include("test_attrib.jl") diff --git a/test/test_scalar.jl b/test/test_scalar.jl index 04d2838a..d66dcf20 100644 --- a/test/test_scalar.jl +++ b/test/test_scalar.jl @@ -32,7 +32,7 @@ for (T,data) in ((Float32,123.f0), end NCDataset(filename,"r") do ds - v2 = ds["scalar"][:] + v2 = ds["scalar"][1] @test v2 == data end rm(filename) diff --git a/test/test_variable.jl b/test/test_variable.jl index c8aaceb9..33e268c2 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -19,7 +19,7 @@ NCDatasets.NCDataset(filename,"c") do ds v = NCDatasets.defVar(ds,"small",Float64,("lon","lat")) # @test_throws Union{NCDatasets.NetCDFError,DimensionMismatch} v[:] = zeros(sz[1]+1,sz[2]) - @test_throws NCDatasets.NetCDFError v[1:sz[1],1:sz[2]] = zeros(sz[1]+1,sz[2]) + @test_throws DimensionMismatch v[1:sz[1],1:sz[2]] = zeros(sz[1]+1,sz[2]) @test_throws NCDatasets.NetCDFError v[sz[1]+1,1] = 1 @test_throws NCDatasets.NetCDFError v[-1,1] = 1 @@ -149,10 +149,10 @@ NCDataset(filename,"c") do ds end defVar(ds,"scalar",123.) - @test ds["scalar"][:] == 123. + @test ds["scalar"][1] == 123. # test indexing with symbols #101 - @test ds[:scalar][:] == 123. + @test ds[:scalar][1] == 123. end rm(filename) From e0191ea5969532acd518568c473f637aa46c5fb5 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 24 Apr 2023 21:14:14 +0200 Subject: [PATCH 11/33] dispatch on _write_data_to_nc --- src/variable.jl | 193 +++++++++--------------------------------------- 1 file changed, 36 insertions(+), 157 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 6951edf9..2e82faa6 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -312,80 +312,59 @@ export nomissing function readblock!(v::Variable, aout, indexes::Int...) datamode(v.ds) - aout[indexes...] = nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]]) + aout .= nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]]) end -function writeblock!(v::Variable{T,N},data,indexes::Int...) where N where T - @debug "$(@__LINE__)" +function readblock!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} + start,count,stride,jlshape = ncsub(indexes[1:N]) + data = Array{T,N}(undef,jlshape) datamode(v.ds) - # use zero-based indexes and reversed order - nc_put_var1(v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]],T(data)) - return data + nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) + # nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) + aout .= data + return aout end -function readblock!(v::Variable{T,N}, aout, indexes::Colon...) where {T,N} - datamode(v.ds) - data = Array{T,N}(undef,size(v)) - nc_get_var!(v.ds.ncid,v.varid,data) +function readblock!(v::Variable{T,N}, aout, indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where {T,N} + sz = size(v) + start,count,stride = ncsub2(sz,indexes...) + jlshape = _shape_after_slice(sz,indexes...) + data = Array{T}(undef,jlshape) - # special case for scalar NetCDF variable - if N == 0 - aout[indexes...] .= data[] - else - aout[indexes...] .= data - end + datamode(v.ds) + nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) + aout .= data end -function writeblock!(v::Variable{T,N},data::T,indexes::Colon...) where {T,N} - @debug "setindex! colon $data" - datamode(v.ds) # make sure that the file is in data mode - tmp = fill(data,size(v)) - nc_put_var(v.ds.ncid,v.varid,tmp) +# NetCDF scalars indexed as [] +readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) +# writeblock!(v::Variable{T, 0}, data) where T = writeblock!(v, data, 1) + +function writeblock!(v::Variable{T,N},data,indexes::AbstractUnitRange...) where {T,N} + @debug "$(@__LINE__) wb $((data, indexes))" + datamode(v.ds) + _write_data_to_nc(v, data, indexes...) return data end -# union types cannot be used to avoid ambiguity -for data_type = [Number, String, Char] - @eval begin - # call to v .= 123 - function writeblock!(v::Variable{T,N},data::$data_type) where {T,N} - @debug "setindex! $data" - datamode(v.ds) # make sure that the file is in data mode - tmp = fill(convert(T,data),size(v)) - nc_put_var(v.ds.ncid,v.varid,tmp) - return data - end - - writeblock!(v::Variable,data::$data_type,indexes::Colon...) = writeblock!(v::Variable,data) - - function writeblock!(v::Variable{T,N},data::$data_type,indexes::StepRange{Int,Int}...) where {T,N} - datamode(v.ds) # make sure that the file is in data mode - start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) - tmp = fill(convert(T,data),jlshape) - nc_put_vars(v.ds.ncid,v.varid,start,count,stride,tmp) - return data - end - end +function _write_data_to_nc(v::Variable{T,N},data,indexes::Int...) where {T,N} + nc_put_var1(v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]],T(data[1])) end -function writeblock!(v::Variable{T,N},data::AbstractArray{T,N},indexes::Colon...) where {T,N} - datamode(v.ds) # make sure that the file is in data mode +_write_data_to_nc(v::Variable,data) = _write_data_to_nc(v, data, 1) +function _write_data_to_nc(v::Variable,data,indexes::Colon...) nc_put_var(v.ds.ncid,v.varid,data) - return data end -function writeblock!(v::Variable{T,N},data::AbstractArray{T2,N},indexes::Colon...) where {T,T2,N} - datamode(v.ds) # make sure that the file is in data mode - tmp = - if T <: Integer - round.(T,data) - else - convert(Array{T,N},data) - end +function _write_data_to_nc(v::Variable,data,indexes::StepRange{Int,Int}...) + start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) + nc_put_vars(v.ds.ncid,v.varid,start,count,stride,data) +end - nc_put_var(v.ds.ncid,v.varid,tmp) - return data +function _write_data_to_nc(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) + ind = normalizeindexes(size(v),indexes) + return _write_data_to_nc(v, data, ind...) end _normalizeindex(n,ind::Base.OneTo) = 1:1:ind.stop @@ -441,108 +420,8 @@ end return start,count,stride end -function readblock!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} - start,count,stride,jlshape = ncsub(indexes[1:N]) - data = Array{T,N}(undef,jlshape) - datamode(v.ds) - nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) - # nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) - aout .= data - return aout -end - -function writeblock!(v::Variable{T,N},data::T,indexes::StepRange{Int,Int}...) where {T,N} - datamode(v.ds) # make sure that the file is in data mode - start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) - tmp = fill(data,jlshape) - nc_put_vars(v.ds.ncid,v.varid,start,count,stride,tmp) - return data -end - -function writeblock!(v::Variable{T,N},data::Array{T,N},indexes::StepRange{Int,Int}...) where {T,N} - datamode(v.ds) # make sure that the file is in data mode - start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) - nc_put_vars(v.ds.ncid,v.varid,start,count,stride,data) - return data -end - -# data can be Array{T2,N} or BitArray{N} -function writeblock!(v::Variable{T,N},data::AbstractArray,indexes::StepRange{Int,Int}...) where {T,N} - datamode(v.ds) # make sure that the file is in data mode - start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) - - tmp = convert(Array{T,ndims(data)},data) - nc_put_vars(v.ds.ncid,v.varid,start,count,stride,tmp) - - return data -end - - - - -function readblock!(v::Variable{T,N}, aout, indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where {T,N} - sz = size(v) - start,count,stride = ncsub2(sz,indexes...) - jlshape = _shape_after_slice(sz,indexes...) - data = Array{T}(undef,jlshape) - - datamode(v.ds) - nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) - aout .= data -end - -# NetCDF scalars indexed as [] -readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) -writeblock!(v::Variable{T, 0}, data::Array{T, 0}) where T = writeblock!(v, data[1]) - - - -function writeblock!(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) - ind = normalizeindexes(size(v),indexes) - - # make arrays out of scalars (arrays can have zero dimensions) - if (ndims(data) == 0) && !(data isa AbstractArray) - data = fill(data,length.(ind)) - end - - return writeblock!(v, data, ind...) -end - - readblock!(v::Variable, aout, ci::CartesianIndices) = aout .= v[ci.indices...] writeblock!(v::Variable, data, ci::CartesianIndices) = writeblock!(v,data,ci.indices...) Base.getindex(v::Union{MFVariable,DeferVariable},ci::CartesianIndices) = v[ci.indices...] -Base.setindex!(v::Union{MFVariable,DeferVariable},data,ci::CartesianIndices) = setindex!(v,data,ci.indices...) - -function readblock!(v::Variable, aout, indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{Int}}...) - @debug "transform vector of indices to ranges" - - sz_source = size(v) - ri = to_range_list.(indices,sz_source) - sz_dest = NCDatasets._shape_after_slice(sz_source,indices...) - - N = length(indices) - - ri_dest = range_indices_dest(ri...) - @debug "ri_dest $ri_dest" - @debug "ri $ri" - - if all(==(1),length.(ri)) - # single chunk - R = first(CartesianIndices(length.(ri))) - ind_source = ntuple(i -> ri[i][R[i]],N) - ind_dest = ntuple(i -> ri_dest[i][R[i]],length(ri_dest)) - return aout[indices...] .= v[ind_source...] - end - - dest = Array{eltype(v),length(sz_dest)}(undef,sz_dest) - for R in CartesianIndices(length.(ri)) - ind_source = ntuple(i -> ri[i][R[i]],N) - ind_dest = ntuple(i -> ri_dest[i][R[i]],length(ri_dest)) - #dest[ind_dest...] = v[ind_source...] - buffer = Array{eltype(v.var),length(ind_dest)}(undef,length.(ind_dest)) - load!(v,view(dest,ind_dest...),buffer,ind_source...) - end - return aout[indices...] .= dest -end +Base.setindex!(v::Union{MFVariable,DeferVariable},data,ci::CartesianIndices) = setindex!(v,data,ci.indices...) \ No newline at end of file From a7551b76727bd5047c643deea49e087ac81c244f Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Mon, 24 Apr 2023 22:58:21 +0200 Subject: [PATCH 12/33] convert data before writing --- src/variable.jl | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 2e82faa6..427338d3 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -341,7 +341,6 @@ readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) # writeblock!(v::Variable{T, 0}, data) where T = writeblock!(v, data, 1) function writeblock!(v::Variable{T,N},data,indexes::AbstractUnitRange...) where {T,N} - @debug "$(@__LINE__) wb $((data, indexes))" datamode(v.ds) _write_data_to_nc(v, data, indexes...) return data @@ -351,19 +350,20 @@ function _write_data_to_nc(v::Variable{T,N},data,indexes::Int...) where {T,N} nc_put_var1(v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]],T(data[1])) end -_write_data_to_nc(v::Variable,data) = _write_data_to_nc(v, data, 1) +_write_data_to_nc(v::Variable, data) = _write_data_to_nc(v, data, 1) -function _write_data_to_nc(v::Variable,data,indexes::Colon...) +function _write_data_to_nc(v::Variable, data, indexes::Colon...) + @info "Am I even getting here ?" nc_put_var(v.ds.ncid,v.varid,data) end -function _write_data_to_nc(v::Variable,data,indexes::StepRange{Int,Int}...) +function _write_data_to_nc(v::Variable{T, N}, data, indexes::StepRange{Int,Int}...) where {T, N} start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) - nc_put_vars(v.ds.ncid,v.varid,start,count,stride,data) + nc_put_vars(v.ds.ncid,v.varid,start,count,stride,T.(data)) end -function _write_data_to_nc(v::Variable,data,indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) - ind = normalizeindexes(size(v),indexes) +function _write_data_to_nc(v::Variable, data, indexes::AbstractRange{<:Integer}...) + ind = prod(length.(indexes)) == 1 ? first.(indexes) : normalizeindexes(size(v),indexes) return _write_data_to_nc(v, data, ind...) end @@ -420,8 +420,5 @@ end return start,count,stride end -readblock!(v::Variable, aout, ci::CartesianIndices) = aout .= v[ci.indices...] -writeblock!(v::Variable, data, ci::CartesianIndices) = writeblock!(v,data,ci.indices...) - Base.getindex(v::Union{MFVariable,DeferVariable},ci::CartesianIndices) = v[ci.indices...] Base.setindex!(v::Union{MFVariable,DeferVariable},data,ci::CartesianIndices) = setindex!(v,data,ci.indices...) \ No newline at end of file From b6a4296e6a284fda99f3cbc541b7812f623e7ab9 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Tue, 25 Apr 2023 11:59:20 +0200 Subject: [PATCH 13/33] dispatch on _read_data_from_nc! --- src/variable.jl | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 427338d3..fd8e69dd 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -309,35 +309,32 @@ end nomissing(a::AbstractArray,value) = a export nomissing - -function readblock!(v::Variable, aout, indexes::Int...) +function readblock!(v::Variable, aout, indexes::AbstractUnitRange...) datamode(v.ds) + _read_data_from_nc!(v, aout, indexes...) + return aout +end + +function _read_data_from_nc!(v::Variable, aout, indexes::Int...) aout .= nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]]) end -function readblock!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} +function _read_data_from_nc!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} start,count,stride,jlshape = ncsub(indexes[1:N]) - data = Array{T,N}(undef,jlshape) - datamode(v.ds) - nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) - # nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) - aout .= data - return aout + nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) end -function readblock!(v::Variable{T,N}, aout, indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where {T,N} +function _read_data_from_nc!(v::Variable{T,N}, aout, indexes::Union{Int,Colon,AbstractRange{<:Integer}}...) where {T,N} sz = size(v) start,count,stride = ncsub2(sz,indexes...) jlshape = _shape_after_slice(sz,indexes...) - data = Array{T}(undef,jlshape) - - datamode(v.ds) - nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,data) - aout .= data + nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) end +_read_data_from_nc!(v::Variable, aout) = _read_data_from_nc!(v, aout, 1) + # NetCDF scalars indexed as [] -readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) +# readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) # writeblock!(v::Variable{T, 0}, data) where T = writeblock!(v, data, 1) function writeblock!(v::Variable{T,N},data,indexes::AbstractUnitRange...) where {T,N} From 7ef519ce432abe7f8453eb797081db9b91c9eda2 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Tue, 25 Apr 2023 14:12:30 +0200 Subject: [PATCH 14/33] @rafaqz suggestion --- src/variable.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index fd8e69dd..184773a9 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -316,11 +316,11 @@ function readblock!(v::Variable, aout, indexes::AbstractUnitRange...) end function _read_data_from_nc!(v::Variable, aout, indexes::Int...) - aout .= nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]]) + aout .= nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in reverse(indexes)]) end function _read_data_from_nc!(v::Variable{T,N}, aout, indexes::TR...) where {T,N} where TR <: Union{StepRange{Int,Int},UnitRange{Int}} - start,count,stride,jlshape = ncsub(indexes[1:N]) + start,count,stride,jlshape = ncsub(indexes) nc_get_vars!(v.ds.ncid,v.varid,start,count,stride,aout) end @@ -344,7 +344,7 @@ function writeblock!(v::Variable{T,N},data,indexes::AbstractUnitRange...) where end function _write_data_to_nc(v::Variable{T,N},data,indexes::Int...) where {T,N} - nc_put_var1(v.ds.ncid,v.varid,[i-1 for i in indexes[ndims(v):-1:1]],T(data[1])) + nc_put_var1(v.ds.ncid,v.varid,[i-1 for i in reverse(indexes)],T(data[1])) end _write_data_to_nc(v::Variable, data) = _write_data_to_nc(v, data, 1) @@ -355,7 +355,7 @@ function _write_data_to_nc(v::Variable, data, indexes::Colon...) end function _write_data_to_nc(v::Variable{T, N}, data, indexes::StepRange{Int,Int}...) where {T, N} - start,count,stride,jlshape = ncsub(indexes[1:ndims(v)]) + start,count,stride,jlshape = ncsub(indexes) nc_put_vars(v.ds.ncid,v.varid,start,count,stride,T.(data)) end From 437121690d8e272c9bb2165f3ba8f252d9fa2064 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 26 Apr 2023 10:25:05 +0200 Subject: [PATCH 15/33] write/readblock! for abstractvector inds (@rafaqz) --- src/variable.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 184773a9..f4fab5b8 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -309,7 +309,7 @@ end nomissing(a::AbstractArray,value) = a export nomissing -function readblock!(v::Variable, aout, indexes::AbstractUnitRange...) +function readblock!(v::Variable, aout, indexes::AbstractVector...) datamode(v.ds) _read_data_from_nc!(v, aout, indexes...) return aout @@ -337,7 +337,7 @@ _read_data_from_nc!(v::Variable, aout) = _read_data_from_nc!(v, aout, 1) # readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) # writeblock!(v::Variable{T, 0}, data) where T = writeblock!(v, data, 1) -function writeblock!(v::Variable{T,N},data,indexes::AbstractUnitRange...) where {T,N} +function writeblock!(v::Variable, data, indexes::AbstractVector...) datamode(v.ds) _write_data_to_nc(v, data, indexes...) return data @@ -359,7 +359,7 @@ function _write_data_to_nc(v::Variable{T, N}, data, indexes::StepRange{Int,Int}. nc_put_vars(v.ds.ncid,v.varid,start,count,stride,T.(data)) end -function _write_data_to_nc(v::Variable, data, indexes::AbstractRange{<:Integer}...) +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...) end From f13a09df895ea95f650e2c3f7f24033395d9bf60 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 26 Apr 2023 12:04:17 +0200 Subject: [PATCH 16/33] fix test_variable --- test/test_variable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_variable.jl b/test/test_variable.jl index 33e268c2..0efd4d34 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -78,7 +78,7 @@ NCDataset(filename,"c") do ds "units" => "degree_Celsius", "long_name" => "Temperature" ]) - @test ds["temp"][:] == data + @test ds["temp"][:] == data[:] @test eltype(ds["temp"].var) == Int32 @test ds.dim["lon"] == sz[1] @test ds.dim["lat"] == sz[2] From 2e8199b8dacddf50cd110a775f315ae196bb2acd Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 26 Apr 2023 12:57:57 +0200 Subject: [PATCH 17/33] broadcast for test unlim --- test/test_variable_unlim.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_variable_unlim.jl b/test/test_variable_unlim.jl index 8b57ceeb..f11cf66e 100644 --- a/test/test_variable_unlim.jl +++ b/test/test_variable_unlim.jl @@ -22,7 +22,7 @@ NCDatasets.NCDataset(filename,"c") do ds for j = 1:sz[2] data[:,j] .= T(j) - v[:,j] = T(j) + v[:,j] .= T(j) end @test all(v[:,:] == data) From 5605f4cdc6caef4af5e0566eff54e2f9dca548ff Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 26 Apr 2023 13:52:15 +0200 Subject: [PATCH 18/33] new api for writing to unlim vars --- test/test_variable_unlim.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_variable_unlim.jl b/test/test_variable_unlim.jl index f11cf66e..a6d6f5ef 100644 --- a/test/test_variable_unlim.jl +++ b/test/test_variable_unlim.jl @@ -22,7 +22,7 @@ NCDatasets.NCDataset(filename,"c") do ds for j = 1:sz[2] data[:,j] .= T(j) - v[:,j] .= T(j) + v[:,j] = fill(T(j), sz[1]) end @test all(v[:,:] == data) @@ -39,7 +39,7 @@ defDim(ds,"lon",Inf) defDim(ds,"lat",110) v = defVar(ds,"temperature",Float32,("lon","lat")) data = [Float32(i+j) for i = 1:100, j = 1:110] -v[:,1] = data[:,1] +v[1:100,1] = data[:,1] v[:,:] = data close(ds) rm(filename) From fce58de527f4f868f6ea0f5a34486f6f4ab4ca0f Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 26 Apr 2023 14:30:41 +0200 Subject: [PATCH 19/33] avoid method conflict for view(::Variable) --- src/subvariable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subvariable.jl b/src/subvariable.jl index 7acbcdc3..da367b57 100644 --- a/src/subvariable.jl +++ b/src/subvariable.jl @@ -91,7 +91,7 @@ close(ds) ``` """ -# Base.view(v::AbstractVariable,indices::Union{Int,Colon,AbstractVector{Int}}...) = SubVariable(v,indices...) +Base.view(v::Union{CFVariable, DeferVariable, MFCFVariable},indices::Union{Int,Colon,AbstractVector{Int}}...) = SubVariable(v,indices...) Base.view(v::SubVariable,indices::CartesianIndex) = view(v,indices.I...) Base.view(v::SubVariable,indices::CartesianIndices) = view(v,indices.indices...) From 3cba846fda8b0da64faab6714d92cae0ea42b93d Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 26 Apr 2023 15:03:26 +0200 Subject: [PATCH 20/33] another unlim dim update --- test/test_corner_cases.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_corner_cases.jl b/test/test_corner_cases.jl index 8336c74f..c894f8ac 100644 --- a/test/test_corner_cases.jl +++ b/test/test_corner_cases.jl @@ -46,7 +46,7 @@ b = dropdims([1.], dims=(1,)) NCDataset(fname,"c") do ds time = defDim(ds,"time",Inf) v = defVar(ds,"temp",Float32,("time",)) - ds["temp"][1:1] = b + ds["temp"][1] = b @test ds["temp"][1] == 1 end From 52acdacbf1e4788339c65e47908eb83ed67090e1 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 26 Apr 2023 17:39:09 +0200 Subject: [PATCH 21/33] add chunking --- src/NCDatasets.jl | 3 ++- src/variable.jl | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/NCDatasets.jl b/src/NCDatasets.jl index c80a65e3..5fc78101 100644 --- a/src/NCDatasets.jl +++ b/src/NCDatasets.jl @@ -38,7 +38,8 @@ import CommonDataModel: AbstractDataset, AbstractVariable, groupnames, group, dimnames, dim, attribnames, attrib -import DiskArrays: readblock!, writeblock! +import DiskArrays +import DiskArrays: readblock!, writeblock!, eachchunk, haschunks using DiskArrays: @implement_diskarray function __init__() diff --git a/src/variable.jl b/src/variable.jl index f4fab5b8..eedb5d27 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -364,6 +364,13 @@ function _write_data_to_nc(v::Variable, data, indexes::Union{AbstractRange{<:Int return _write_data_to_nc(v, data, ind...) end +getchunksize(v::Variable) = getchunksize(haschunks(v),v) +getchunksize(::DiskArrays.Chunked, v::Variable) = chunking(v)[2] +# getchunksize(::DiskArrays.Unchunked, v::Variable) = DiskArrays.estimate_chunksize(v) +getchunksize(::DiskArrays.Unchunked, v::Variable) = size(v) +eachchunk(v::Variable) = DiskArrays.GridChunks(v, getchunksize(v)) +haschunks(v::Variable) = (chunking(v)[1] == :contiguous ? DiskArrays.Unchunked() : DiskArrays.Chunked()) + _normalizeindex(n,ind::Base.OneTo) = 1:1:ind.stop _normalizeindex(n,ind::Colon) = 1:1:n _normalizeindex(n,ind::Int) = ind:1:ind From 6b0ab2819d7603ca033c484ea232db08881f1868 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Tue, 16 May 2023 14:38:01 +0200 Subject: [PATCH 22/33] delegate chunking for cfvar --- src/variable.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/variable.jl b/src/variable.jl index eedb5d27..23b32faf 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -368,7 +368,9 @@ getchunksize(v::Variable) = getchunksize(haschunks(v),v) getchunksize(::DiskArrays.Chunked, v::Variable) = chunking(v)[2] # getchunksize(::DiskArrays.Unchunked, v::Variable) = DiskArrays.estimate_chunksize(v) getchunksize(::DiskArrays.Unchunked, v::Variable) = size(v) -eachchunk(v::Variable) = DiskArrays.GridChunks(v, getchunksize(v)) +eachchunk(v::CFVariable) = eachchunk(v.var) +haschunks(v::CFVariable) = haschunks(v.var) +eachchunk(v::Variable) = DiskArrays.GridChunks(v, Tuple(getchunksize(v))) haschunks(v::Variable) = (chunking(v)[1] == :contiguous ? DiskArrays.Unchunked() : DiskArrays.Chunked()) _normalizeindex(n,ind::Base.OneTo) = 1:1:ind.stop From 508bacad1475ef17d0985ffe2a8f43383eebf80a Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Tue, 16 May 2023 16:41:24 +0200 Subject: [PATCH 23/33] remove unused method --- src/variable.jl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 23b32faf..b6be4034 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -349,11 +349,6 @@ end _write_data_to_nc(v::Variable, data) = _write_data_to_nc(v, data, 1) -function _write_data_to_nc(v::Variable, data, indexes::Colon...) - @info "Am I even getting here ?" - nc_put_var(v.ds.ncid,v.varid,data) -end - function _write_data_to_nc(v::Variable{T, N}, data, indexes::StepRange{Int,Int}...) where {T, N} start,count,stride,jlshape = ncsub(indexes) nc_put_vars(v.ds.ncid,v.varid,start,count,stride,T.(data)) From 6dec5a09825f99f0a7a681a2bc67f187e9989082 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Tue, 16 May 2023 16:54:48 +0200 Subject: [PATCH 24/33] basic implementation of `grow!` --- src/variable.jl | 12 ++++++++++++ test/test_variable_unlim.jl | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index b6be4034..0f0763a4 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -359,6 +359,18 @@ function _write_data_to_nc(v::Variable, data, indexes::Union{AbstractRange{<:Int return _write_data_to_nc(v, data, ind...) end +function grow!(v::CFVariable, data, indexes::Union{Integer, Colon}...) + unlimdims = unlimited(Dimensions(CommonDataModel.dataset(v))) + length(unlimdims) == 1 || error("Only 1 unlimited dimension is supported") + alldims = dimnames(v) + iunlimdim = findfirst(==(unlimdims[1]), alldims) + icol = findfirst(x -> x isa Colon, indexes) + iunlimdim == icol || ArgumentError("The Colon in the arguments must match the unlimited dimension.") + inds = replace(indexes, Colon() => 1:length(data)) + v[inds...] = data + return v +end + getchunksize(v::Variable) = getchunksize(haschunks(v),v) getchunksize(::DiskArrays.Chunked, v::Variable) = chunking(v)[2] # getchunksize(::DiskArrays.Unchunked, v::Variable) = DiskArrays.estimate_chunksize(v) diff --git a/test/test_variable_unlim.jl b/test/test_variable_unlim.jl index a6d6f5ef..2ab79c72 100644 --- a/test/test_variable_unlim.jl +++ b/test/test_variable_unlim.jl @@ -22,7 +22,8 @@ NCDatasets.NCDataset(filename,"c") do ds for j = 1:sz[2] data[:,j] .= T(j) - v[:,j] = fill(T(j), sz[1]) + # v[:,j] = fill(T(j), sz[1]) + NCDatasets.grow!(v, fill(T(j), sz[1]), :, j) end @test all(v[:,:] == data) @@ -39,7 +40,8 @@ defDim(ds,"lon",Inf) defDim(ds,"lat",110) v = defVar(ds,"temperature",Float32,("lon","lat")) data = [Float32(i+j) for i = 1:100, j = 1:110] -v[1:100,1] = data[:,1] +# v[1:100,1] = data[:,1] +NCDatasets.grow!(v, data[:, 1], :, 1) v[:,:] = data close(ds) rm(filename) From 1f2f5e46ff13968088903456f3312fb0c59d3921 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Tue, 16 May 2023 17:06:56 +0200 Subject: [PATCH 25/33] remove grow! from test for now --- test/test_variable_unlim.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_variable_unlim.jl b/test/test_variable_unlim.jl index b33b21a2..b9ea1255 100644 --- a/test/test_variable_unlim.jl +++ b/test/test_variable_unlim.jl @@ -19,8 +19,7 @@ NCDatasets.NCDataset(filename,"c") do ds for j = 1:sz[2] data[:,j] .= T(j) - # v[:,j] = fill(T(j), sz[1]) - NCDatasets.grow!(v, fill(T(j), sz[1]), :, j) + v[:,j] = fill(T(j), sz[1]) end @test all(v[:,:] == data) From 43a1e5b9f7ba90d034ffd87b434e253a2f1ea1c6 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 24 May 2023 16:02:17 +0200 Subject: [PATCH 26/33] fix issue with strided ranges --- src/variable.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 0f0763a4..7c18ddb1 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -309,7 +309,7 @@ end nomissing(a::AbstractArray,value) = a export nomissing -function readblock!(v::Variable, aout, indexes::AbstractVector...) +function readblock!(v::Variable, aout, indexes::TI...) where TI <: Union{AbstractUnitRange,StepRange} datamode(v.ds) _read_data_from_nc!(v, aout, indexes...) return aout @@ -337,7 +337,7 @@ _read_data_from_nc!(v::Variable, aout) = _read_data_from_nc!(v, aout, 1) # readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) # writeblock!(v::Variable{T, 0}, data) where T = writeblock!(v, data, 1) -function writeblock!(v::Variable, data, indexes::AbstractVector...) +function writeblock!(v::Variable, data, indexes::TI...) where TI <: Union{AbstractUnitRange,StepRange} datamode(v.ds) _write_data_to_nc(v, data, indexes...) return data From 94c6310f1a1f052725264626db90adc50d848ea2 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 7 Jun 2023 12:22:20 +0200 Subject: [PATCH 27/33] remove comments --- src/variable.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 7c18ddb1..42190f55 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -333,10 +333,6 @@ end _read_data_from_nc!(v::Variable, aout) = _read_data_from_nc!(v, aout, 1) -# NetCDF scalars indexed as [] -# readblock!(v::Variable{T, 0}, aout) where T = readblock!(v, aout, 1) -# writeblock!(v::Variable{T, 0}, data) where T = writeblock!(v, data, 1) - function writeblock!(v::Variable, data, indexes::TI...) where TI <: Union{AbstractUnitRange,StepRange} datamode(v.ds) _write_data_to_nc(v, data, indexes...) From 90e585b91175b2d0db8e2cc6845085fb177f5813 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Thu, 8 Jun 2023 10:05:13 +0200 Subject: [PATCH 28/33] remove getindex for cfvariable --- src/cfvariable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cfvariable.jl b/src/cfvariable.jl index e990f92c..69c80c23 100644 --- a/src/cfvariable.jl +++ b/src/cfvariable.jl @@ -403,7 +403,7 @@ function _range_indices_dest(of,v,rest...) end range_indices_dest(ri...) = _range_indices_dest((),ri...) -function Base.getindex(v::Union{CFVariable,MFVariable,SubVariable},indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{Int}}...) +function Base.getindex(v::Union{MFVariable,SubVariable},indices::Union{Int,Colon,AbstractRange{<:Integer},Vector{Int}}...) @debug "transform vector of indices to ranges" sz_source = size(v) From 3705d6441a3f003fd818f1352d113575ab1dcf1c Mon Sep 17 00:00:00 2001 From: tcarion Date: Tue, 13 Jun 2023 15:48:11 +0200 Subject: [PATCH 29/33] fix performance script --- test/perf/generate_data.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/perf/generate_data.jl b/test/perf/generate_data.jl index a06693ea..38189df8 100644 --- a/test/perf/generate_data.jl +++ b/test/perf/generate_data.jl @@ -29,7 +29,7 @@ ncv1 = defVar(ds,"v1", UInt8, ("longitude", "latitude", "time"), fillvalue = UIn for n = 1:sz[3] @show n ncv1[:,:,n] = rand(1:100,sz[1],sz[2]) - ncv1[:,1,n] = missing + ncv1[:,1,n] .= missing end close(ds) From ec87d27c6b1efe325d90465a0bc71b0e567bdf2c Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 14 Jun 2023 10:53:15 +0200 Subject: [PATCH 30/33] fix wrong fallback method for readblock! --- src/variable.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/variable.jl b/src/variable.jl index 42190f55..228105bb 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -309,12 +309,22 @@ end nomissing(a::AbstractArray,value) = a export nomissing -function readblock!(v::Variable, aout, indexes::TI...) where TI <: Union{AbstractUnitRange,StepRange} +# This method needs to be duplicated instead of using an Union. Otherwise a DiskArrays fallback is called instead which impacts performances +# (see https://github.com/Alexander-Barth/NCDatasets.jl/pull/205#issuecomment-1589575041) +function readblock!(v::Variable, aout, indexes::AbstractUnitRange...) datamode(v.ds) _read_data_from_nc!(v, aout, indexes...) return aout end +function readblock!(v::Variable, aout, indexes::StepRange...) + datamode(v.ds) + _read_data_from_nc!(v, aout, indexes...) + return aout +end + +readblock!(v::Variable, aout) = _read_data_from_nc!(v::Variable, aout) + function _read_data_from_nc!(v::Variable, aout, indexes::Int...) aout .= nc_get_var1(eltype(v),v.ds.ncid,v.varid,[i-1 for i in reverse(indexes)]) end From bb58df1c4785bf0da55a1ab1d392a233afb44856 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 5 Jul 2023 15:43:38 +0200 Subject: [PATCH 31/33] fix growing unlim issue when writing --- src/dataset.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/dataset.jl b/src/dataset.jl index d6ad76dd..be643450 100644 --- a/src/dataset.jl +++ b/src/dataset.jl @@ -489,20 +489,32 @@ function Base.write(dest::NCDataset, src::AbstractDataset; # end end + function _destindex(ind, dimname, dimlength, unlimdims) + nind = _normalizeindex(dimlength, ind) + if dimname in unlimdims + nind[1]:dimlength + else + nind + end + end + _maxrange(dimname, idimensions, dimlength) = haskey(idimensions, dimname) ? idimensions[dimname][end] : dimlength + # loop over variables for varname in include (varname ∈ exclude) && continue @debug "Writing variable $varname..." cfvar = src[varname] + cfsz = size(cfvar) dimension_names = dimnames(cfvar) var = cfvar.var # indices for subset index = ntuple(i -> torange(get(idimensions,dimension_names[i],:)),length(dimension_names)) + destindex = ntuple(i -> _destindex(index[i], dimension_names[i], _maxrange(dimension_names[i], idimensions, cfsz[i]), unlimited_dims), length(dimension_names)) destvar = defVar(dest, varname, eltype(var), dimension_names; attrib = attribs(cfvar)) # copy data - destvar.var[:] = cfvar.var[index...] + destvar.var[destindex...] = cfvar.var[index...] end # loop over all global attributes From d6fbe43a2b0a67e192bfe9288d5d485f4595698f Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 5 Jul 2023 15:45:26 +0200 Subject: [PATCH 32/33] remove grow! --- src/variable.jl | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 3ca64563..2bfd569a 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -387,18 +387,6 @@ function _write_data_to_nc(v::Variable, data, indexes::Union{AbstractRange{<:Int return _write_data_to_nc(v, data, ind...) end -function grow!(v::CFVariable, data, indexes::Union{Integer, Colon}...) - unlimdims = unlimited(Dimensions(CommonDataModel.dataset(v))) - length(unlimdims) == 1 || error("Only 1 unlimited dimension is supported") - alldims = dimnames(v) - iunlimdim = findfirst(==(unlimdims[1]), alldims) - icol = findfirst(x -> x isa Colon, indexes) - iunlimdim == icol || ArgumentError("The Colon in the arguments must match the unlimited dimension.") - inds = replace(indexes, Colon() => 1:length(data)) - v[inds...] = data - return v -end - getchunksize(v::Variable) = getchunksize(haschunks(v),v) getchunksize(::DiskArrays.Chunked, v::Variable) = chunking(v)[2] # getchunksize(::DiskArrays.Unchunked, v::Variable) = DiskArrays.estimate_chunksize(v) From 0873d6dfcb9734852be9f4535b56557790299483 Mon Sep 17 00:00:00 2001 From: Tristan Carion Date: Wed, 5 Jul 2023 15:52:31 +0200 Subject: [PATCH 33/33] separate methods for readblock --- src/variable.jl | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 2bfd569a..bbe49a7c 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -333,17 +333,14 @@ export nomissing # This method needs to be duplicated instead of using an Union. Otherwise a DiskArrays fallback is called instead which impacts performances # (see https://github.com/Alexander-Barth/NCDatasets.jl/pull/205#issuecomment-1589575041) -function readblock!(v::Variable, aout, indexes::AbstractUnitRange...) +function readblock!(v::Variable, aout, indexes::TI...) where TI <: Union{AbstractUnitRange,StepRange} datamode(v.ds) - _read_data_from_nc!(v, aout, indexes...) + _readblock!(v, aout, indexes...) return aout end -function readblock!(v::Variable, aout, indexes::StepRange...) - datamode(v.ds) - _read_data_from_nc!(v, aout, indexes...) - return aout -end +_readblock!(v::Variable, aout, indexes::AbstractUnitRange...) = _read_data_from_nc!(v, aout, indexes...) +_readblock!(v::Variable, aout, indexes::StepRange...) = _read_data_from_nc!(v, aout, indexes...) readblock!(v::Variable, aout) = _read_data_from_nc!(v::Variable, aout)