Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlela committed Sep 27, 2015
1 parent 273774c commit 0ab8253
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/NetCDF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ function create_var(nc,v,mode)

v.dimids=Int32[v.dim[i].dimid for i=1:length(v.dim)]
for i=1:v.ndim dumids[i]=v.dimids[v.ndim+1-i] end

nc_def_var(nc.ncid,v.name,v.nctype,v.ndim,dumids,vara)

v.varid=vara[1];
Expand All @@ -641,7 +641,7 @@ Possible optional arguments are:
- **t** variable type, currently supported types are: const NC_BYTE, NC_CHAR, NC_SHORT, NC_INT, NC_FLOAT, NC_LONG, NC_DOUBLE
- **mode** file creation mode, only valid when new file is created, choose one of: NC_NETCDF4, NC_CLASSIC_MODEL, NC_64BIT_OFFSET
"""
function nccreate(fil::AbstractString,varname::AbstractString,dims...;atts::Dict=Dict{Any,Any}(),gatts::Dict=Dict{Any,Any}(),compress::Integer=-1,t::Union(Integer,Type)=NC_DOUBLE,mode::UInt16=NC_NETCDF4)
function nccreate(fil::AbstractString,varname::AbstractString,dims...;atts::Dict=Dict{Any,Any}(),gatts::Dict=Dict{Any,Any}(),compress::Integer=-1,t::Union(DataType,Integer)=NC_DOUBLE,mode::UInt16=NC_NETCDF4)
# Checking dims argument for correctness
dim=parsedimargs(dims)
# open the file
Expand Down
14 changes: 13 additions & 1 deletion test/high.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end

nccreate(fn1,"v1","Dim1",[1,2],@Compat.AnyDict("units"=>"deg C"),"Dim2",collect(1:10),"Dim3",20,@Compat.AnyDict("max"=>10),
mode=NC_NETCDF4)
nccreate(fn1,"vstr","Dim2",collect(1:10),t=String)
nccreate(fn2,"v2","Dim1",[1,2,3],@Compat.AnyDict("units"=>"deg C"),"Dim2",collect(1:10),"Dim3",20,@Compat.AnyDict("max"=>10),
atts=@Compat.AnyDict("a1"=>"varatts"),gatts=@Compat.AnyDict("Some global attributes"=>2010),mode=NC_64BIT_OFFSET)
nccreate(fn3,"v3","Dim1",3,mode=NC_CLASSIC_MODEL)
Expand Down Expand Up @@ -36,6 +37,17 @@ xt=Array(Any,length(tlist))
for i=1:length(tlist)
xt[i]=rand(tlist[i],10)
end
xs=Array(AbstractString,10)
xs[1]="a"
xs[2]="bb"
xs[3]="ccc"
xs[4]="dddd"
xs[5]="eeeee"
xs[6]="ffffff"
xs[7]="ggggggg"
xs[8]="hhhhhhhh"
xs[9]="iiiiiiiii"
xs[10]="jjjjjjjjjj"
#
# And write it
#
Expand All @@ -44,6 +56,7 @@ ncwrite(x1,fn1,"v1")
for i=1:10
ncwrite(x2[:,i,:],fn2,"v2",start=[1,i,1],count=[-1,1,-1])
end
ncwrite(xs,fn1,"vstr")
#Test automatic type conversion
x4=[1,2,3]
ncwrite(x4,fn3,"v3")
Expand All @@ -52,6 +65,5 @@ for i=1:length(tlist)
ncwrite(xt[i],fn3,"vt$i")
end


ncclose()

18 changes: 16 additions & 2 deletions test/intermediate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ d3 = NcDim("Dim3",20;[email protected]("max"=>10));
# Test Variable creation
v1 = NcVar("v1",[d1,d2,d3],compress=5) # With several dims in an Array, and compressed
v2 = NcVar("v2",[d1,d2,d3],atts=@Compat.AnyDict("a1"=>"varatts")) # with given attributes
v3 = NcVar("v3",d1) # with a single dimension
v3 = NcVar("v3",d1)
vs = NcVar("vstr",d2,t=String) # with a single dimension
tlist = [Float64, Float32, Int32, Int16, Int8]
vt = Array(NcVar, length(tlist))
for i= 1:length(tlist)
vt[i]=NcVar("vt$i",d2,t = tlist[i])
end

# Creating Files
nc1 = NetCDF.create(fn1,v1,mode=NC_NETCDF4);
nc1 = NetCDF.create(fn1,v1,vs,mode=NC_NETCDF4);
nc2 = NetCDF.create(fn2,v2,v3,gatts=@Compat.AnyDict("Some global attributes"=>2010),mode=NC_64BIT_OFFSET);
nc3 = NetCDF.create(fn3,vt,mode=NC_CLASSIC_MODEL);

Expand Down Expand Up @@ -48,10 +49,22 @@ xt=Array(Any,length(tlist))
for i=1:length(tlist)
xt[i]=rand(tlist[i],10)
end
xs=Array(AbstractString,10)
xs[1]="a"
xs[2]="bb"
xs[3]="ccc"
xs[4]="dddd"
xs[5]="eeeee"
xs[6]="ffffff"
xs[7]="ggggggg"
xs[8]="hhhhhhhh"
xs[9]="iiiiiiiii"
xs[10]="jjjjjjjjjj"
#
# And write it
#
NetCDF.putvar(nc1,"v1",x1)
NetCDF.putvar(nc1,"vstr",xs)
#Test sequential writing along one dimension
for i=1:10
NetCDF.putvar(nc2,"v2",x2[:,i,:],start=[1,i,1],count=[-1,1,-1])
Expand All @@ -75,5 +88,6 @@ nc2 = NetCDF.open(fn2,mode=NC_NOWRITE);
nc3 = NetCDF.open(fn3,mode=NC_NOWRITE);

@test x1==NetCDF.readvar(nc1,"v1")
@test xs==NetCDF.readvar(nc1,"vstr")
@test x2==NetCDF.readvar(nc2,"v2")
@test x4==NetCDF.readvar(nc2,"v3")

0 comments on commit 0ab8253

Please sign in to comment.