@@ -4,6 +4,8 @@ import CommonDataModel as CDM
4
4
using DataStructures
5
5
using Dates
6
6
7
+ include (" memory_dataset.jl" )
8
+
7
9
fname = tempname ()
8
10
ds = NCDataset (fname," c" )
9
11
@@ -36,94 +38,82 @@ CDM.show(io,"text/plain",v)
36
38
37
39
v = @test_logs (:warn ,r" numeric" ) defVar (ds," temp2" ,data,(" lon" ," lat" ),attrib = Dict (" missing_value" => " bad_idea" ))
38
40
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
-
69
41
for sample_data = ( - 100 : 100 ,
70
42
' a' :' z' ,
71
43
[" AB" ," CD" ," EF" ],
72
44
[NaN ; 1 : 10 ],
73
45
)
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
77
48
78
49
fill_value = sample_data[1 ]
79
50
data = rand (sample_data[2 : end ],30 ,31 )
80
51
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
83
66
84
67
@test " lon" in CDM. dimnames (mv)
85
68
@test CDM. name (mv) == " data"
86
69
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
-
96
70
md[" data" ][1 ,1 ] = missing
97
71
@test ismissing (md[" data" ][1 ,1 ])
98
72
@test md[" data" ]. var[1 ,1 ] === fill_value
99
73
100
74
md[" data" ][1 : 2 ,1 : 2 ] .= missing
101
75
@test all (ismissing .(md[" data" ][1 : 2 ,1 : 2 ]))
102
76
@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
103
92
end
104
93
105
94
106
95
# time
107
96
108
97
sample_data = - 100 : 100
109
98
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} (
111
106
" units" => " days since 2000-01-01" ))
112
107
108
+ mv. var[:,:] .= data
113
109
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 )
116
112
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" )
126
114
115
+ @test " lon" in CDM. dimnames (mv)
116
+ @test CDM. name (mv) == " data"
127
117
128
118
time_origin = DateTime (2000 ,1 ,1 )
129
119
@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)
132
122
md[" data" ][1 ,2 ] = DateTime (2000 ,2 ,1 )
133
123
@test md[" data" ]. var[1 ,2 ] == Dates. value (md[" data" ][1 ,2 ] - time_origin) ÷ (24 * 60 * 60 * 1000 )
134
124
135
- # close(ds )
125
+ close (md )
0 commit comments