-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataset.jl
409 lines (308 loc) · 11.1 KB
/
dataset.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""
CommonDatamodel.path(ds::AbstractDataset)
File path of the data set `ds`.
"""
path(ds::AbstractDataset) = ""
Base.close(ds::AbstractDataset) = nothing
"""
CommonDatamodel.name(ds::AbstractDataset)
Name of the group of the data set `ds`. For a data set containing
only a single group, this will be always the root group `"/"`.
"""
name(ds::AbstractDataset) = "/"
"""
pds = CommonDatamodel.parentdataset(ds::AbstractDataset)
The data set `pds` containing `ds` as a sub-group.
`pds` is nothing for the root group.
"""
parentdataset(ds::AbstractDataset) = nothing
sync(ds::AbstractDataset) = nothing
"""
CommonDatamodel.groupnames(ds::AbstractDataset)
All the subgroup names of the data set `ds`. For a data set containing
only a single group, this will be an empty vector of `String`.
"""
groupnames(ds::AbstractDataset) = ()
"""
CommonDatamodel.group(ds::AbstractDataset,groupname::SymbolOrString)
Return the sub-group data set with the name `groupname`.
"""
function group(ds::AbstractDataset,groupname::SymbolOrString)
error("no group $groupname in $(path(ds))")
end
"""
group = CommonDatamodel.defGroup(ds::AbstractDataset,name::SymbolOrString)
Create an empty sub-group with the name `name` in the data set `ds`.
The `group` is a sub-type of `AbstractDataset`.
"""
function defGroup(ds::AbstractDataset,name::SymbolOrString)
error("unimplemented for abstract type")
end
"""
CommonDatamodel.groups(ds::AbstractDataset)
Return all sub-group data as a dict-like object.
"""
groups(ds::AbstractDataset) =
OrderedDict((dn,group(ds,dn)) for dn in groupnames(ds))
"""
CommonDatamodel.unlimited(ds::AbstractDataset)
Iterator of strings with the name of the unlimited dimension.
"""
unlimited(ad::AbstractDataset) = ()
Base.isopen(ds::AbstractDataset) = true
function Base.show(io::IO,ds::AbstractDataset)
level = get(io, :level, 0)
indent = " " ^ level
if !isopen(ds)
print(io,"closed Dataset")
return
end
dspath = path(ds)
printstyled(io, indent, "Dataset: ",dspath,"\n", color=section_color[])
print(io,indent,"Group: ",name(ds),"\n")
print(io,"\n")
# show dimensions
if length(dimnames(ds)) > 0
show_dim(io, dims(ds))
print(io,"\n")
end
varnames = keys(ds)
if length(varnames) > 0
printstyled(io, indent, "Variables\n",color=section_color[])
for name in varnames
show(IOContext(io,:level=>level+2),ds[name])
print(io,"\n")
end
end
# global attribues
if length(attribnames(ds)) > 0
printstyled(io, indent, "Global attributes\n",color=section_color[])
show_attrib(IOContext(io,:level=>level+2),attribs(ds));
end
# groups
gnames = groupnames(ds)
if length(gnames) > 0
printstyled(io, indent, "Groups\n",color=section_color[])
for groupname in gnames
show(IOContext(io,:level=>level+2),group(ds,groupname))
end
end
end
maskingvalue(ds::AbstractDataset) = missing
"""
v = getindex(ds::AbstractDataset, varname::SymbolOrString)
Return the variable `varname` in the dataset `ds` as a
`CFVariable`. The following CF convention are honored when the
variable is indexed:
* `_FillValue` or `missing_value` (which can be a list) will be returned as `missing`.
* `scale_factor` and `add_offset` are applied (output = `scale_factor` * `data_in_file` + `add_offset`)
* time variables (recognized by the units attribute and possibly the calendar attribute) are returned usually as
`DateTime` object. Note that `CFTime.DateTimeAllLeap`, `CFTime.DateTimeNoLeap` and
`CF.TimeDateTime360Day` cannot be converted to the proleptic gregorian calendar used in
julia and are returned as such. (See [`CFTime.jl`](https://github.com/JuliaGeo/CFTime.jl)
for more information about those date types.) If a calendar is defined but not among the
ones specified in the CF convention, then the data in the file is not
converted into a date structure.
A call `getindex(ds, varname)` is usually written as `ds[varname]`.
If variable represents a cell boundary, the attributes `calendar` and `units` of the related variables are used, if they are not specified. For example:
```
dimensions:
time = UNLIMITED; // (5 currently)
nv = 2;
variables:
double time(time);
time:long_name = "time";
time:units = "hours since 1998-04-019 06:00:00";
time:bounds = "time_bnds";
double time_bnds(time,nv);
```
In this case, the variable `time_bnds` uses the units and calendar of `time`
because both variables are related thought the bounds attribute following the CF conventions.
See also [`cfvariable(ds, varname)`](@ref).
"""
function Base.getindex(ds::AbstractDataset,varname::SymbolOrString)
return cfvariable(ds, varname)
end
function Base.setindex!(ds::AbstractDataset,data::AbstractVariable,varname::SymbolOrString)
return defVar(ds, varname, data)
end
function Base.haskey(ds::AbstractDataset,varname)
return Symbol(varname) in Symbol.(keys(ds))
end
"""
varbyattrib(ds, attname = attval)
Returns a list of variable(s) which has the attribute `attname` matching the value `attval`
in the dataset `ds`.
The list is empty if the none of the variables has the match.
The output is a list of `CFVariable`s.
# Examples
Load all the data of the first variable with standard name "longitude" from the
NetCDF file `results.nc`.
```julia-repl
julia> ds = NCDataset("results.nc", "r");
julia> data = varbyattrib(ds, standard_name = "longitude")[1][:]
```
"""
function varbyattrib(ds::Union{AbstractDataset,AbstractVariable}; kwargs...)
# Start with an empty list of variables
varlist = []
# Loop on the variables
for v in keys(ds)
var = ds[v]
matchall = true
for (attsym,attval) in kwargs
attname = String(attsym)
# Check if the variable has the desired attribute
if attname in attribnames(var)
# Check if the attribute value is the selected one
if attrib(var,attname) != attval
matchall = false
break
end
else
matchall = false
break
end
end
if matchall
push!(varlist, var)
end
end
return varlist
end
"""
var = getindex(ds::Union{AbstractDataset,AbstractVariable},cfname::CFStdName)
Return the NetCDF variable `var` with the standard name `cfname` from a
dataset. If the first argument is a variable, then the search is limited to
all variables with the same dimension names.
"""
function Base.getindex(ds::Union{AbstractDataset,AbstractVariable},n::CFStdName)
ncvars = varbyattrib(ds, standard_name = String(n.name))
if length(ncvars) == 1
return ncvars[1]
else
throw(KeyError("$(length(ncvars)) matches while searching for a variable with standard_name attribute equal to $(n.name)"))
end
end
"""
names = keys(g::Groups)
Return the names of all subgroubs of the group `g`.
"""
Base.keys(groups::Groups) = groupnames(groups.ds)
"""
group = getindex(g::Groups,groupname::AbstractString)
Return the NetCDF `group` with the name `groupname` from the parent
group `g`.
For example:
```julia
ds = NCDataset("results.nc", "r");
forecast_group = ds.group["forecast"]
forecast_temp = forecast_group["temperature"]
```
"""
Base.getindex(groups::Groups,name) = group(groups.ds,name)
# Initialize the ds._boundsmap variable
function initboundsmap!(ds)
empty!(ds._boundsmap)
for vname in keys(ds)
v = variable(ds,vname)
bounds = get(v.attrib,"bounds",nothing)
if bounds !== nothing
ds._boundsmap[bounds] = vname
end
end
end
"""
write(dest::AbstractDataset, src::AbstractDataset; include = keys(src), exclude = [])
Write the variables of `src` dataset into an empty `dest` dataset (which must be opened in mode `"a"` or `"c"`).
The keywords `include` and `exclude` configure which variable of `src` should be included
(by default all), or which should be `excluded` (by default none).
If the first argument is a file name, then the dataset is open in create mode (`"c"`).
This function is useful when you want to save the dataset from a multi-file dataset.
To save a subset, one can use the view function `view` to virtually slice
a dataset:
## Example
```
NCDataset(fname_src) do ds
write(fname_slice,view(ds, lon = 2:3))
end
```
All variables in the source file `fname_src` with a dimension `lon` will be sliced
along the indices `2:3` for the `lon` dimension. All attributes (and variables
without a dimension `lon`) will be copied over unmodified.
"""
function Base.write(dest::AbstractDataset, src::AbstractDataset;
include = keys(src),
exclude = String[],
_ignore_checksum = false,
)
unlimited_dims = unlimited(src)
for (dimname,dimlength) in dims(src)
isunlimited = dimname in unlimited_dims
# if haskey(dest.dim,dimname)
# # check length
# if (dest.dim[dimname] !== src.dim[dimname]) && !isunlimited
# throw(DimensionMismatch("length of the dimensions $dimname are inconstitent in files $(path(dest)) and $(path(src))"))
# end
# else
if isunlimited
defDim(dest, dimname, Inf)
else
defDim(dest, dimname, dimlength)
end
# end
end
# loop over variables
for varname in include
(varname ∈ exclude) && continue
@debug "Writing variable $varname..."
kwargs =
if _ignore_checksum
(checksum = nothing,)
else
()
end
defVar(dest,src[varname]; kwargs...)
end
# loop over all global attributes
for (attribname,attribval) in attribs(src)
dest.attrib[attribname] = attribval
end
# loop over all groups
for (groupname,groupsrc) in groups(src)
groupdest = defGroup(dest,groupname)
write(groupdest,groupsrc)
end
return dest
end
@inline function Base.getproperty(ds::Union{AbstractDataset,AbstractVariable},name::Symbol)
if (name == :attrib) && !hasfield(typeof(ds),name)
return Attributes(ds)
elseif (name == :dim) && !hasfield(typeof(ds),name)
return Dimensions(ds)
elseif (name == :group) && !hasfield(typeof(ds),name) && (ds isa AbstractDataset)
return Groups(ds)
else
return getfield(ds,name)
end
end
for (item_color,default) in (
(:section_color, :red),
(:attribute_color, :cyan),
(:variable_color, :green),
)
item_color_str = String(item_color)
item_str = split(item_color_str,"_")[1]
default_str = String(default)
@eval begin
$item_color = Ref(Symbol(load_preference(CommonDataModel,$(item_color_str), $(QuoteNode(default)))))
"""
CommonDataModel.set_$($item_color_str)(color::Symbol)
Set the $($item_str) color. The default color is `$($default_str)`.
"""
function $(Symbol(:set_,item_color))(color::Symbol)
@set_preferences!($(item_color_str) => String(color))
$item_color[] = color
end
end
end