Skip to content

Commit 2ea8678

Browse files
test defVar for data
1 parent 3a93a59 commit 2ea8678

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

src/variable.jl

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function defVar(ds::AbstractDataset,
4343
name::SymbolOrString,
4444
data::AbstractArray{Union{Missing,T},N},
4545
dimnames;
46-
kwargs...) where T <: Union{Int8,UInt8,Int16,Int32,Int64,Float32,Float64} where N
46+
kwargs...) where T <: Union{Int8,UInt8,Int16,Int32,Int64,Float32,Float64,Char,String} where N
4747
_defVar(ds::AbstractDataset,name,data,T,dimnames; kwargs...)
4848
end
4949

@@ -55,7 +55,7 @@ function defVar(ds::AbstractDataset,
5555
data::AbstractArray{<:Union{Missing,T},N},
5656
dimnames;
5757
kwargs...) where T <: Union{DateTime,AbstractCFDateTime} where N
58-
_defVar(ds::AbstractDataset,name,data,Float64,dimnames; kwargs...)
58+
_defVar(ds,name,data,Float64,dimnames; kwargs...)
5959
end
6060

6161
function defVar(ds::AbstractDataset,name::SymbolOrString,data,dimnames; kwargs...)
@@ -65,15 +65,16 @@ function defVar(ds::AbstractDataset,name::SymbolOrString,data,dimnames; kwargs..
6565
else
6666
nctype = eltype(data)
6767
end
68-
_defVar(ds::AbstractDataset,name,data,nctype,dimnames; kwargs...)
68+
69+
_defVar(ds,name,data,nctype,dimnames; kwargs...)
6970
end
7071

7172
function _defVar(ds::AbstractDataset,name::SymbolOrString,data,nctype,vardimnames; attrib = [], kwargs...)
7273
# define the dimensions if necessary
7374
for (i,dimname) in enumerate(vardimnames)
7475
if !(dimname in dimnames(ds))
7576
defDim(ds,dimname,size(data,i))
76-
elseif !(dimname in unlimited(ds.dim))
77+
elseif !(dimname in unlimited(ds))
7778
dimlen = dim(ds,dimname)
7879

7980
if (dimlen != size(data,i))
@@ -84,14 +85,15 @@ function _defVar(ds::AbstractDataset,name::SymbolOrString,data,nctype,vardimname
8485
end
8586

8687
T = eltype(data)
87-
attrib = collect(attrib)
88+
# we should preserve the order
89+
# value type is promoted to Any as we add values of different type
90+
attrib = convert(OrderedDict{String,Any},OrderedDict(attrib))
8891

8992
if T <: Union{TimeType,Missing}
90-
dattrib = Dict(attrib)
91-
if !haskey(dattrib,"units")
93+
if !haskey(attrib,"units")
9294
push!(attrib,"units" => CFTime.DEFAULT_TIME_UNITS)
9395
end
94-
if !haskey(dattrib,"calendar")
96+
if !haskey(attrib,"calendar")
9597
# these dates cannot be converted to the standard calendar
9698
if T <: Union{DateTime360Day,Missing}
9799
push!(attrib,"calendar" => "360_day")
@@ -103,18 +105,14 @@ function _defVar(ds::AbstractDataset,name::SymbolOrString,data,nctype,vardimname
103105
end
104106
end
105107

106-
v =
107-
if Missing <: T
108-
# make sure a fill value is set (it might be overwritten by kwargs...)
109-
defVar(ds,name,nctype,vardimnames;
110-
fillvalue = fillvalue(nctype),
111-
attrib = attrib,
112-
kwargs...)
113-
else
114-
defVar(ds,name,nctype,vardimnames;
108+
# make sure a fill value is set
109+
if (Missing <: T) && !haskey(attrib,"_FillValue")
110+
push!(attrib,"_FillValue" => fillvalue(nctype))
111+
end
112+
113+
v = defVar(ds,name,nctype,vardimnames;
115114
attrib = attrib,
116115
kwargs...)
117-
end
118116

119117
v[:] = data
120118
return v

test/memory_dataset.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Base
22
import CommonDataModel as CDM
3+
#import CommonDataModel: SymbolOrString
34
using DataStructures
4-
5+
import CommonDataModel: defVar, unlimited, name, dimnames, dataset, variable, dim, attribnames, attrib, defDim, defAttrib
56

67
struct MemoryVariable{T,N,TP} <: CDM.AbstractVariable{T,N}
78
parent_dataset::TP
@@ -42,12 +43,13 @@ function CDM.defDim(md::MemoryDataset,name::AbstractString,len)
4243
end
4344
end
4445

45-
function CDM.defVar(md::MemoryDataset,name::AbstractString,T,dimnames;
46+
function CDM.defVar(md::MemoryDataset,name::AbstractString,T::DataType,dimnames;
4647
attrib = OrderedDict{String,Any}(),
4748
)
4849
sz = ntuple(i -> CDM.dim(md,dimnames[i]),length(dimnames))
4950
data = Array{T,length(dimnames)}(undef,sz...)
50-
mv = MemoryVariable(md,name,(dimnames...,), data, attrib)
51+
mv = MemoryVariable(md,name,(dimnames...,), data,
52+
convert(OrderedDict{String,Any},attrib))
5153
md.variables[name] = mv
5254

5355
return md[name] # return CFVariable

test/test_scaling.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ for sample_data = ( -100:100,
4747
local io, data, fill_value, mv, md, add_offset, scale_factor
4848

4949
fill_value = sample_data[1]
50-
data = rand(sample_data[2:end],30,31)
50+
data = rand(sample_data[2:end],3,4)
5151

5252
md = MemoryDataset()
5353
CDM.defDim(md,"lon",size(data,1))
@@ -56,7 +56,7 @@ for sample_data = ( -100:100,
5656
scale_factor = 10
5757

5858
mv = CDM.defVar(md,"data",eltype(data),("lon","lat"), attrib =
59-
OrderedDict{String,Any}(
59+
OrderedDict(
6060
"_FillValue" => fill_value,
6161
"add_offset" => add_offset,
6262
"scale_factor" => scale_factor,
@@ -89,13 +89,23 @@ for sample_data = ( -100:100,
8989
mv[3,3] = 'y'
9090
@test mv.var[3,3] == 'y'
9191
end
92+
93+
# defVar(ds,name,data,dimnames)
94+
95+
data2 = replace(data,fill_value => missing)
96+
mv = CDM.defVar(md,"data2",data2,("lon","lat"), attrib =
97+
OrderedDict(
98+
"_FillValue" => fill_value
99+
))
100+
101+
@test all(mv[:,:] .=== data2)
92102
end
93103

94104

95105
# time
96106

97107
sample_data = -100:100
98-
data = rand(sample_data,30,31)
108+
data = rand(sample_data,3,4)
99109

100110
md = MemoryDataset()
101111
CDM.defDim(md,"lon",size(data,1))
@@ -128,6 +138,4 @@ md["data"][1,2] = DateTime(2000,2,1)
128138

129139

130140
@test CDM.dataset(md["data"]) == md
131-
132-
133141
close(md)

0 commit comments

Comments
 (0)