Skip to content

Commit d419fa5

Browse files
test with in-memory dataset
1 parent 0871439 commit d419fa5

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

src/dimension.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ dimnames(ds::Union{AbstractDataset,AbstractVariable}) = ()
1212
1313
Return the length of the dimension `dimname` in the data set `ds`.
1414
"""
15-
function dim(ds::Union{AbstractDataset,AbstractVariable},dimname::AbstractString)
15+
function dim(v::AbstractVariable,dimname::AbstractString)
16+
error("no dimension $dimname in $(name(v))")
17+
end
18+
19+
function dim(ds::AbstractDataset,dimname::AbstractString)
1620
error("no dimension $dimname in $(path(ds))")
1721
end
1822

src/variable.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ function Base.show(io::IO,v::AbstractVariable)
6464
show_attrib(IOContext(io,:level=>level+3),attribs(v))
6565
end
6666
catch err
67+
@debug "error in show" err
6768
print(io,"Variable (dataset closed)")
6869
end
6970
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
23
GRIBDatasets = "82be9cdb-ee19-4151-bdb3-b400788d9abc"
34
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
45
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/test_scaling.jl

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using NCDatasets
22
using Test
3-
3+
import CommonDataModel as CDM
4+
using DataStructures
45

56
fname = tempname()
67
ds = NCDataset(fname,"c")
@@ -24,4 +25,65 @@ v.var[1,1] = 1
2425
@test ismissing(v[2,2])
2526
@test fillvalue(v) == fill_value
2627

27-
close(ds)
28+
@test collect(CDM.dimnames(v)) == ["lon","lat"]
29+
30+
#@test CDM.dim(v,"lon") == 10
31+
32+
io = IOBuffer()
33+
CDM.show(io,"text/plain",v)
34+
@test occursin("Attributes",String(take!(io)))
35+
36+
v = @test_warn "numeric" defVar(ds,"temp2",data,("lon","lat"),attrib = Dict(
37+
"missing_value" => "bad_idea"))
38+
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+
data = randn(30,31)
54+
mv = MemoryVariable("data",["lon","lat"], data, OrderedDict{String,Any}(
55+
"units" => "days since 2000-01-01"))
56+
57+
CDM.name(v::MemoryVariable) = v.name
58+
CDM.dimnames(v::MemoryVariable) = v.dimnames
59+
Base.size(v::MemoryVariable) = size(v.data)
60+
61+
@test "lon" in CDM.dimnames(mv)
62+
@test CDM.name(mv) == "data"
63+
64+
md = MemoryDataset(
65+
OrderedDict{String,Int}(
66+
"lon" => 30,
67+
"lat" => 31),
68+
OrderedDict{String,MemoryVariable}(
69+
"data" => mv),
70+
OrderedDict{String,Any}(
71+
"history" => "lala"),
72+
String[])
73+
74+
import Base
75+
Base.keys(md::MemoryDataset) = keys(md.variables)
76+
CDM.variable(md::MemoryDataset,varname::AbstractString) = md.variables[varname]
77+
Base.getindex(md::MemoryDataset,varname::AbstractString) = CDM.cfvariable(md,varname)
78+
CDM.dimnames(md::MemoryDataset) = keys(md.dim)
79+
CDM.dim(md::MemoryDataset,name::AbstractString) = md.dim[name]
80+
CDM.attribnames(md::Union{MemoryDataset,MemoryVariable}) = keys(md.attrib)
81+
CDM.attrib(md::Union{MemoryDataset,MemoryVariable},name::AbstractString) = md.attrib[name]
82+
83+
84+
io = IOBuffer()
85+
CDM.show(io,md)
86+
@test occursin("Attributes",String(take!(io)))
87+
88+
89+
#close(ds)

0 commit comments

Comments
 (0)