Skip to content

Commit

Permalink
remove everything following a null terminating character in an string…
Browse files Browse the repository at this point in the history
… attribute
  • Loading branch information
Alexander-Barth committed May 25, 2018
1 parent 638fcfc commit d8b218b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
14 changes: 14 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,17 @@ In fact, `_FillValue` must have the same data type as the corresponding variable
tempvar.attrib["_FillValue"] = Float32(-9999.)
```


## Corner cases


* An attribute representing a vector with a single value (e.g. `[1]`) will be read back as a scalar (`1`) (same behavior in python netCDF4 1.3.1).

* NetCDF and Julia distinguishes between a vector of chars and a string, but both are returned as string for ease of use, in particular
an attribute representing a vector of chars `['u','n','i','t','s']` will be read back as the string `"units"`.

* An attribute representing a vector of chars `['u','n','i','t','s','\0']` will also be read back as the string `"units"` (issue #12).


<!-- LocalWords: NCDatasets jl Datasets Dataset netCDF
-->
11 changes: 10 additions & 1 deletion src/netcdf_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,16 @@ function nc_get_att(ncid::Integer,varid::Integer,name)
if xtype == NC_CHAR
val = Vector{UInt8}(len)
check(ccall((:nc_get_att,libnetcdf),Cint,(Cint,Cint,Cstring,Ptr{Void}),ncid,varid,name,val))
return join(Char.(val))

# remove everything following a null terminating character if present
# see issue #12
inull = findfirst(val .== 0)

if inull == 0
return join(Char.(val))
else
return join(Char.(view(val,1:inull-1)))
end
elseif xtype == NC_STRING
val = Vector{Ptr{UInt8}}(len)
check(ccall((:nc_get_att,libnetcdf),Cint,(Cint,Cint,Cstring,Ptr{Void}),ncid,varid,name,val))
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ println("NetCDF version: ",NCDatasets.nc_inq_libvers())
include("test_vlen.jl")

include("test_ncgen.jl")
include("test_varbyatt.jl")
include("test_varbyatt.jl")

include("test_corner_cases.jl")

# display
s = IOBuffer()
Expand Down

0 comments on commit d8b218b

Please sign in to comment.