Skip to content

Commit

Permalink
fix issue #264
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Feb 9, 2024
1 parent dbc547f commit 4638a21
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/netcdf_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,14 @@ function nc_inq_user_type(ncid::Integer,xtype::Integer)
end


function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,typeid::Integer,data::Vector)
check(ccall((:nc_put_att,libnetcdf),Cint,(Cint,Cint,Cstring,nc_type,Csize_t,Ptr{Nothing}),
ncid,varid,name,typeid,length(data),data))
end


function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::AbstractString)
if name == "_FillValue"
if Symbol(name) == :_FillValue
nc_put_att_string(ncid,varid,"_FillValue",[data])
else
check(ccall((:nc_put_att_text,libnetcdf),Cint,(Cint,Cint,Cstring,Csize_t,Cstring),
Expand All @@ -561,8 +567,13 @@ function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vect
end

# NetCDF does not necessarily support 64 bit attributes
nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Int64) =
nc_put_att(ncid,varid,name,Int32(data))
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Int64)
if Symbol(name) == :_FillValue
nc_put_att(ncid,varid,name,ncType[Int64],[data])
else
nc_put_att(ncid,varid,name,Int32(data))
end
end

nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{Int64}) =
nc_put_att(ncid,varid,name,Int32.(data))
Expand All @@ -589,11 +600,6 @@ function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vect
nc_put_att(ncid,varid,name,ncType[T],data)
end

function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,typeid::Integer,data::Vector)
check(ccall((:nc_put_att,libnetcdf),Cint,(Cint,Cint,Cstring,nc_type,Csize_t,Ptr{Nothing}),
ncid,varid,name,typeid,length(data),data))
end

# convert e.g. ranges to vectors
function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::AbstractVector)
nc_put_att(ncid,varid,name,Vector(data))
Expand All @@ -603,6 +609,7 @@ function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data)
error("attributes can only be scalars or vectors")
end


function nc_get_att(ncid::Integer,varid::Integer,name)
xtype,len = nc_inq_att(ncid,varid,name)

Expand Down
8 changes: 8 additions & 0 deletions test/test_fillvalue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,11 @@ ncv = ds["data"]

@test ncv[1,1] == data[1,1]
@test isnan(ncv[2,2])

# issue 264
fname = tempname()
ds = NCDataset(fname,"c")
defDim(ds,"mydim", 2)
defVar(ds,"test_array_i64_missing", [missing, 2], ("mydim", ))
close(ds)
rm(fname)

0 comments on commit 4638a21

Please sign in to comment.