@@ -30,10 +30,118 @@ function variable(ds::AbstractDataset,variablename::SymbolOrString)
30
30
error (" no variable $variablename in $(path (ds)) (abstract method)" )
31
31
end
32
32
33
- function defVar (ds:: AbstractDataset ,name:: SymbolOrString ,type,dimnames)
33
+ function defVar (ds:: AbstractDataset ,name:: SymbolOrString ,type:: DataType ,
34
+ dimnames)
34
35
error (" unimplemented for abstract type" )
35
36
end
36
37
38
+
39
+
40
+
41
+ # data has the type e.g. Array{Union{Missing,Float64},3}
42
+ function defVar (ds:: AbstractDataset ,
43
+ name:: SymbolOrString ,
44
+ data:: AbstractArray{Union{Missing,T},N} ,
45
+ dimnames;
46
+ kwargs... ) where T <: Union{Int8,UInt8,Int16,Int32,Int64,Float32,Float64} where N
47
+ _defVar (ds:: AbstractDataset ,name,data,T,dimnames; kwargs... )
48
+ end
49
+
50
+ # data has the type e.g. Vector{DateTime}, Array{Union{Missing,DateTime},3} or
51
+ # Vector{DateTime360Day}
52
+ # Data is always stored as Float64 in the NetCDF file
53
+ function defVar (ds:: AbstractDataset ,
54
+ name:: SymbolOrString ,
55
+ data:: AbstractArray{<:Union{Missing,T},N} ,
56
+ dimnames;
57
+ kwargs... ) where T <: Union{DateTime,AbstractCFDateTime} where N
58
+ _defVar (ds:: AbstractDataset ,name,data,Float64,dimnames; kwargs... )
59
+ end
60
+
61
+ function defVar (ds:: AbstractDataset ,name:: SymbolOrString ,data,dimnames; kwargs... )
62
+ # eltype of a String would be Char
63
+ if data isa String
64
+ nctype = String
65
+ else
66
+ nctype = eltype (data)
67
+ end
68
+ _defVar (ds:: AbstractDataset ,name,data,nctype,dimnames; kwargs... )
69
+ end
70
+
71
+ function _defVar (ds:: AbstractDataset ,name:: SymbolOrString ,data,nctype,vardimnames; attrib = [], kwargs... )
72
+ # define the dimensions if necessary
73
+ for (i,dimname) in enumerate (vardimnames)
74
+ if ! (dimname in dimnames (ds))
75
+ defDim (ds,dimname,size (data,i))
76
+ elseif ! (dimname in unlimited (ds. dim))
77
+ dimlen = dim (ds,dimname)
78
+
79
+ if (dimlen != size (data,i))
80
+ error (" dimension $(dimname) is already defined with the " *
81
+ " length $dimlen . It cannot be redefined with a length of $(size (data,i)) ." )
82
+ end
83
+ end
84
+ end
85
+
86
+ T = eltype (data)
87
+ attrib = collect (attrib)
88
+
89
+ if T <: Union{TimeType,Missing}
90
+ dattrib = Dict (attrib)
91
+ if ! haskey (dattrib," units" )
92
+ push! (attrib," units" => CFTime. DEFAULT_TIME_UNITS)
93
+ end
94
+ if ! haskey (dattrib," calendar" )
95
+ # these dates cannot be converted to the standard calendar
96
+ if T <: Union{DateTime360Day,Missing}
97
+ push! (attrib," calendar" => " 360_day" )
98
+ elseif T <: Union{DateTimeNoLeap,Missing}
99
+ push! (attrib," calendar" => " 365_day" )
100
+ elseif T <: Union{DateTimeAllLeap,Missing}
101
+ push! (attrib," calendar" => " 366_day" )
102
+ end
103
+ end
104
+ end
105
+
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;
115
+ attrib = attrib,
116
+ kwargs... )
117
+ end
118
+
119
+ v[:] = data
120
+ return v
121
+ end
122
+
123
+
124
+ function defVar (ds:: AbstractDataset ,name,data:: T ; kwargs... ) where T <: Union{Number,String,Char}
125
+ v = defVar (ds,name,T,(); kwargs... )
126
+ v[:] = data
127
+ return v
128
+ end
129
+
130
+ """
131
+ v = CommonDataModel.defVar(ds::AbstractDataset,src::AbstractVariable)
132
+
133
+ Defines and return the variable in the data set `ds`
134
+ copied from the variable `src`. The variable name, dimension name, attributes
135
+ and data are copied from `src`.
136
+ """
137
+ function defVar (ds:: AbstractDataset ,src:: AbstractVariable )
138
+ v = defVar (ds,name (src),
139
+ Array (src),
140
+ dimnames (src),
141
+ attrib= attribs (src))
142
+ return v
143
+ end
144
+
37
145
"""
38
146
ds = CommonDataModel.dataset(v::AbstractVariable)
39
147
0 commit comments