Skip to content

Commit c8c59d5

Browse files
more tests on packing/unpacking
1 parent 0e37bc2 commit c8c59d5

File tree

5 files changed

+119
-59
lines changed

5 files changed

+119
-59
lines changed

src/CommonDataModel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using CFTime
44
using Dates
55
using Printf
66
using Preferences
7-
import Base: isopen, show, display
7+
import Base: isopen, show, display, close
88
using DataStructures
99

1010
"""

src/dataset.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ File path of the data set `ds`.
77
path(ds::AbstractDataset) = ""
88

99

10+
Base.close(ds::AbstractDataset) = nothing
11+
1012
"""
1113
CommonDatamodel.name(ds::AbstractDataset)
1214

src/dimension.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function dim(ds::AbstractDataset,dimname::AbstractString)
2020
error("no dimension $dimname in $(path(ds))")
2121
end
2222

23-
function defDim(ds::AbstractDataset,name::AbstractString,len::Integer)
23+
function defDim(ds::AbstractDataset,name::AbstractString,len)
2424
error("unimplemnted for abstract type")
2525
end
2626

test/memory_dataset.jl

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Base
2+
import CommonDataModel as CDM
3+
using DataStructures
4+
5+
6+
struct MemoryVariable{T,N,TP} <: CDM.AbstractVariable{T,N}
7+
parent_dataset::TP
8+
name::String
9+
dimnames::NTuple{N,String}
10+
data::Array{T,N}
11+
attrib::OrderedDict{String,Any}
12+
end
13+
14+
struct MemoryDataset <: CDM.AbstractDataset
15+
dim::OrderedDict{String,Int}
16+
variables::OrderedDict{String,MemoryVariable}
17+
attrib::OrderedDict{String,Any}
18+
unlimited::Vector{String}
19+
end
20+
21+
Base.getindex(v::MemoryVariable,ij...) = v.data[ij...]
22+
Base.setindex!(v::MemoryVariable,data,ij...) = v.data[ij...] = data
23+
CDM.name(v::MemoryVariable) = v.name
24+
CDM.dimnames(v::MemoryVariable) = v.dimnames
25+
Base.size(v::MemoryVariable) = size(v.data)
26+
CDM.dim(v::MemoryVariable,name::AbstractString) = v.parent_dataset.dim[name]
27+
28+
Base.keys(md::MemoryDataset) = keys(md.variables)
29+
CDM.variable(md::MemoryDataset,varname::AbstractString) = md.variables[varname]
30+
Base.getindex(md::MemoryDataset,varname::AbstractString) = CDM.cfvariable(md,varname)
31+
CDM.dimnames(md::MemoryDataset) = keys(md.dim)
32+
CDM.dim(md::MemoryDataset,name::AbstractString) = md.dim[name]
33+
CDM.attribnames(md::Union{MemoryDataset,MemoryVariable}) = keys(md.attrib)
34+
CDM.attrib(md::Union{MemoryDataset,MemoryVariable},name::AbstractString) = md.attrib[name]
35+
36+
function CDM.defDim(md::MemoryDataset,name::AbstractString,len)
37+
if isinf(len)
38+
@warn "unlimited dimensions are not supported yet"
39+
md.dim[name] = 0
40+
push!(unlimited,name)
41+
else
42+
md.dim[name] = len
43+
end
44+
end
45+
46+
function CDM.defVar(md::MemoryDataset,name::AbstractString,T,dimnames;
47+
attrib = OrderedDict{String,Any}(),
48+
)
49+
sz = ntuple(i -> CDM.dim(md,dimnames[i]),length(dimnames))
50+
data = Array{T,length(dimnames)}(undef,sz...)
51+
mv = MemoryVariable(md,name,(dimnames...,), data, attrib)
52+
md.variables[name] = mv
53+
54+
return md[name] # return CFVariable
55+
end
56+
57+
function CDM.defAttrib(md::Union{MemoryVariable,MemoryDataset},name::AbstractString,data);
58+
md.attrib[name] = data
59+
end
60+
61+
function MemoryDataset()
62+
return MemoryDataset(
63+
OrderedDict{String,Int}(),
64+
OrderedDict{String,MemoryVariable}(),
65+
OrderedDict{String,Any}(),
66+
String[])
67+
end
68+

test/test_scaling.jl

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import CommonDataModel as CDM
44
using DataStructures
55
using Dates
66

7+
include("memory_dataset.jl")
8+
79
fname = tempname()
810
ds = NCDataset(fname,"c")
911

@@ -36,94 +38,82 @@ CDM.show(io,"text/plain",v)
3638

3739
v = @test_logs (:warn,r"numeric") defVar(ds,"temp2",data,("lon","lat"),attrib = Dict("missing_value" => "bad_idea"))
3840

39-
struct MemoryVariable{T,N} <: CDM.AbstractVariable{T,N}
40-
name::String
41-
dimnames::Vector{String}
42-
data::Array{T,N}
43-
attrib::OrderedDict{String,Any}
44-
end
45-
46-
struct MemoryDataset <: CDM.AbstractDataset
47-
dim::OrderedDict{String,Int}
48-
variables::OrderedDict{String,MemoryVariable}
49-
attrib::OrderedDict{String,Any}
50-
unlimited::Vector{String}
51-
end
52-
53-
Base.getindex(v::MemoryVariable,ij...) = v.data[ij...]
54-
Base.setindex!(v::MemoryVariable,data,ij...) = v.data[ij...] = data
55-
CDM.name(v::MemoryVariable) = v.name
56-
CDM.dimnames(v::MemoryVariable) = v.dimnames
57-
Base.size(v::MemoryVariable) = size(v.data)
58-
59-
import Base
60-
Base.keys(md::MemoryDataset) = keys(md.variables)
61-
CDM.variable(md::MemoryDataset,varname::AbstractString) = md.variables[varname]
62-
Base.getindex(md::MemoryDataset,varname::AbstractString) = CDM.cfvariable(md,varname)
63-
CDM.dimnames(md::MemoryDataset) = keys(md.dim)
64-
CDM.dim(md::MemoryDataset,name::AbstractString) = md.dim[name]
65-
CDM.attribnames(md::Union{MemoryDataset,MemoryVariable}) = keys(md.attrib)
66-
CDM.attrib(md::Union{MemoryDataset,MemoryVariable},name::AbstractString) = md.attrib[name]
67-
68-
6941
for sample_data = ( -100:100,
7042
'a':'z',
7143
["AB","CD","EF"],
7244
[NaN; 1:10],
7345
)
74-
local io
75-
local data
76-
local fill_value, mv, md
46+
47+
local io, data, fill_value, mv, md, add_offset, scale_factor
7748

7849
fill_value = sample_data[1]
7950
data = rand(sample_data[2:end],30,31)
8051

81-
mv = MemoryVariable("data",["lon","lat"], data, OrderedDict{String,Any}(
82-
"_FillValue" => fill_value))
52+
md = MemoryDataset()
53+
CDM.defDim(md,"lon",size(data,1))
54+
CDM.defDim(md,"lat",size(data,2))
55+
add_offset = 1
56+
scale_factor = 10
57+
58+
mv = CDM.defVar(md,"data",eltype(data),("lon","lat"), attrib =
59+
OrderedDict{String,Any}(
60+
"_FillValue" => fill_value,
61+
"add_offset" => add_offset,
62+
"scale_factor" => scale_factor,
63+
))
64+
65+
mv.var[:,:] .= data
8366

8467
@test "lon" in CDM.dimnames(mv)
8568
@test CDM.name(mv) == "data"
8669

87-
md = MemoryDataset(
88-
OrderedDict{String,Int}(
89-
"lon" => 30,
90-
"lat" => 31),
91-
OrderedDict{String,MemoryVariable}(
92-
"data" => mv),
93-
OrderedDict{String,Any}(),
94-
String[])
95-
9670
md["data"][1,1] = missing
9771
@test ismissing(md["data"][1,1])
9872
@test md["data"].var[1,1] === fill_value
9973

10074
md["data"][1:2,1:2] .= missing
10175
@test all(ismissing.(md["data"][1:2,1:2]))
10276
@test all(md["data"].var[1:2,1:2] .=== fill_value)
77+
78+
if eltype(data) <: Number
79+
mv.var[3,3] = 3
80+
@test mv[3,3] == scale_factor * 3 + add_offset
81+
82+
mv[3,3] = scale_factor * 4 + add_offset
83+
@test mv.var[3,3] == 4
84+
elseif eltype(data) == Char
85+
# ignore scale_factor and add_offset
86+
mv.var[3,3] = 'z'
87+
@test mv[3,3] == 'z'
88+
89+
mv[3,3] = 'y'
90+
@test mv.var[3,3] == 'y'
91+
end
10392
end
10493

10594

10695
# time
10796

10897
sample_data = -100:100
10998
data = rand(sample_data,30,31)
110-
mv = MemoryVariable("data",["lon","lat"], data, OrderedDict{String,Any}(
99+
100+
md = MemoryDataset()
101+
CDM.defDim(md,"lon",size(data,1))
102+
CDM.defDim(md,"lat",size(data,2))
103+
104+
105+
mv = CDM.defVar(md,"data",eltype(data),("lon","lat"), attrib = OrderedDict{String,Any}(
111106
"units" => "days since 2000-01-01"))
112107

108+
mv.var[:,:] .= data
113109

114-
@test "lon" in CDM.dimnames(mv)
115-
@test CDM.name(mv) == "data"
110+
@test CDM.dim(md,"lon") == size(data,1)
111+
@test CDM.dim(mv,"lon") == size(data,1)
116112

117-
md = MemoryDataset(
118-
OrderedDict{String,Int}(
119-
"lon" => 30,
120-
"lat" => 31),
121-
OrderedDict{String,MemoryVariable}(
122-
"data" => mv),
123-
OrderedDict{String,Any}(
124-
"history" => "lala"),
125-
String[])
113+
CDM.defAttrib(md,"history", "lala")
126114

115+
@test "lon" in CDM.dimnames(mv)
116+
@test CDM.name(mv) == "data"
127117

128118
time_origin = DateTime(2000,1,1)
129119
@test md["data"][1,1] == time_origin + Dates.Millisecond(data[1,1]*24*60*60*1000)
@@ -132,4 +122,4 @@ time_origin = DateTime(2000,1,1)
132122
md["data"][1,2] = DateTime(2000,2,1)
133123
@test md["data"].var[1,2] == Dates.value(md["data"][1,2] - time_origin) ÷ (24*60*60*1000)
134124

135-
#close(ds)
125+
close(md)

0 commit comments

Comments
 (0)