Skip to content

Commit

Permalink
simplify nc_put_att
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Feb 9, 2024
1 parent 4638a21 commit 142ee13
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/netcdf_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,8 @@ function nc_def_vlen(ncid::Integer,name,base_typeid::Integer)
return xtypep[]
end

"""
datum_size is sizeof(nc_vlen_t)
"""
function nc_inq_vlen(ncid::Integer,xtype::Integer)
# datum_size is sizeof(nc_vlen_t)
datum_sizep = Ref(Csize_t(0))
base_nc_typep = Ref(nc_type(0))
name = zeros(UInt8,NC_MAX_NAME+1)
Expand Down Expand Up @@ -546,13 +544,11 @@ function nc_inq_user_type(ncid::Integer,xtype::Integer)
return unsafe_string(pointer(name)),sizep[],base_nc_typep[],nfieldsp[],classp[]
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 Symbol(name) == :_FillValue
nc_put_att_string(ncid,varid,"_FillValue",[data])
Expand All @@ -566,7 +562,7 @@ function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vect
nc_put_att(ncid,varid,name,join(data))
end

# NetCDF does not necessarily support 64 bit attributes
# NetCDF does not necessarily support 64 bit integer attributes
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])
Expand All @@ -579,21 +575,17 @@ nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{Int64}
nc_put_att(ncid,varid,name,Int32.(data))

function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Number)
check(ccall((:nc_put_att,libnetcdf),Cint,(Cint,Cint,Cstring,nc_type,Csize_t,Ptr{Nothing}),
ncid,varid,name,ncType[typeof(data)],1,[data]))
nc_put_att(ncid,varid,name,ncType[typeof(data)],[data])
end

function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Char)
# UInt8('α')
# ERROR: InexactError: trunc(UInt8, 945)
check(ccall((:nc_put_att,libnetcdf),Cint,(Cint,Cint,Cstring,nc_type,Csize_t,Ptr{Nothing}),
ncid,varid,name,ncType[typeof(data)],1,[UInt8(data)]))
nc_put_att(ncid,varid,name,ncType[typeof(data)],[UInt8(data)])
end

function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{T}) where T <: AbstractString
check(ccall((:nc_put_att,libnetcdf),Cint,(Cint,Cint,Cstring,
nc_type,Csize_t,Ptr{Nothing}),
ncid,varid,name,ncType[eltype(data)],length(data),pointer.(data)))
nc_put_att(ncid,varid,name,ncType[String],pointer.(data))
end

function nc_put_att(ncid::Integer,varid::Integer,name::SymbolOrString,data::Vector{T}) where {T}
Expand Down

0 comments on commit 142ee13

Please sign in to comment.