Skip to content

Commit

Permalink
fix issue #37
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Oct 2, 2019
1 parent 2b5aa89 commit 93b3165
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/netcdf_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ end
# check(ccall((:nc_inq_opaque,libnetcdf),Cint,(Cint,nc_type,Cstring,Ptr{Cint}),ncid,xtype,name,sizep))
# end

# can the NetCDF variable varid receive the data?
function _nc_shape_check(ncid,varid,data,start,count,stride)
@debug ncid,varid,data,start,count,stride
end

function nc_put_var(ncid::Integer,varid::Integer,data::Array{Char,N}) where N
nc_put_var(ncid,varid,convert(Array{UInt8,N},data))
end
Expand Down Expand Up @@ -744,7 +749,6 @@ function nc_put_vars(ncid::Integer,varid::Integer,startp,countp,stridep,
end

nc_put_vars(ncid,varid,startp,countp,stridep,tmp)
#convert(Array{UInt8,N},op))
end

function nc_put_vars(ncid::Integer,varid::Integer,startp,countp,stridep,
Expand All @@ -759,9 +763,66 @@ function nc_put_vars(ncid::Integer,varid::Integer,startp,countp,stridep,
convert(Array{nc_vlen_t{T},N},op))
end

function _nc_check_size_put_vars(ncid,varid,countp,op)
# dimension index for NetCDF variable
i1 = 1
# dimension index of data op
i2 = 1

# not_one(x) = x != 1
# if filter(not_one,reverse(countp)) != filter(not_one,[size(op)...])
# path = nc_inq_path(ncid)
# varname = nc_inq_varname(ncid,varid)
# throw(DimensionMismatch("size mismatch for variable '$(varname)' in file '$(path)'. Trying to write $(size(op)) elements while $(countp) are expected"))
# end

unlimdims = nc_inq_unlimdims(ncid)
dimids = nc_inq_vardimid(ncid,varid)

countp = reverse(countp)
dimids = reverse(dimids)

while true
if (i2 > ndims(op)) && (i1 > length(countp))
break
end

count_i1 =
if i1 <= length(countp)
countp[i1]
else
1
end

# ignore dimensions with only one element
if (count_i1 == 1) && (i1 <= length(countp))
i1 += 1
continue
end
if size(op,i2) == 1
i2 += 1
continue
end

# no test for unlimited dimensions
if (i1 <= length(countp)) && (dimids[i1] in unlimdims)
# ok
elseif (size(op,i2) !== count_i1)
path = nc_inq_path(ncid)
varname = nc_inq_varname(ncid,varid)

throw(NetCDFError(NC_EEDGE,"size mismatch for variable '$(varname)' in file '$(path)'. Trying to write $(size(op)) elements while $(countp) are expected"))
end

i1 += 1
i2 += 1
end
end

function nc_put_vars(ncid::Integer,varid::Integer,startp,countp,stridep,op)
@debug "nc_put_vars: $startp,$countp,$stridep"
@debug "shape $(size(op))"
_nc_check_size_put_vars(ncid,varid,countp,op)

check(ccall((:nc_put_vars,libnetcdf),Cint,
(Cint,Cint,Ptr{Cint},Ptr{Cint},
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ println("NetCDF version: ",NCDatasets.nc_inq_libvers())
include("test_attrib.jl")

include("test_writevar.jl")

include("test_check_size.jl")

include("test_scaling.jl")

include("test_fillvalue.jl")
Expand Down
42 changes: 42 additions & 0 deletions test/test_check_size.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NCDatasets
using Test

filename = tempname()
filename = "/tmp/foo.nc"
rm(filename)

ds = Dataset(filename,"c")
x = collect(1:10)
defVar(ds, "x", x, ("x",))
defDim(ds, "Time", Inf)
sync(ds)
defVar(ds, "Time", Float64, ("Time",))

defVar(ds, "a", Float64, ("x", "Time"))
defVar(ds, "u", Float64, ("x", "Time"))
defVar(ds, "v", Float64, ("x", "Time"))
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)

# ignore singleton dimension
ds["w"][:,i] = reshape(collect(1:10), 1, 1, 10, 1)
end

ds["w"][:,:] = ones(10,10)

# w should grow along the unlimited dimension
ds["w"][:,:] = 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)

# NetCDF: Index exceeds dimension bound
@test_throws NCDatasets.NetCDFError ds["u"][100,100]
close(ds)

0 comments on commit 93b3165

Please sign in to comment.