-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgroupes.jl
69 lines (52 loc) · 1.8 KB
/
groupes.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
############################################################
# Groups
############################################################
"""
Base.keys(g::NCDatasets.Groups)
Return the names of all subgroubs of the group `g`.
"""
function Base.keys(g::Groups)
return String[nc_inq_grpname(ncid)
for ncid in nc_inq_grps(g.ds.ncid)]
end
"""
group = getindex(g::NCDatasets.Groups,groupname::AbstractString)
Return the NetCDF `group` with the name `groupname`.
For example:
```julia-repl
julia> ds = NCDataset("results.nc", "r");
julia> forecast_group = ds.group["forecast"]
julia> forecast_temp = forecast_group["temperature"]
```
"""
function Base.getindex(g::Groups,groupname::SymbolOrString)
grp_ncid = nc_inq_grp_ncid(g.ds.ncid,groupname)
ds = NCDataset(grp_ncid,g.ds.iswritable,g.ds.isdefmode; parentdataset = g.ds)
return ds
end
"""
defGroup(ds::NCDataset,groupname; attrib = []))
Create the group with the name `groupname` in the dataset `ds`.
`attrib` is a list of attribute name and attribute value pairs (see `NCDataset`).
"""
function defGroup(ds::NCDataset,groupname::SymbolOrString; attrib = [])
defmode(ds) # make sure that the file is in define mode
grp_ncid = nc_def_grp(ds.ncid,groupname)
ds = NCDataset(grp_ncid,ds.iswritable,ds.isdefmode; parentdataset = ds)
# set global attributes for group
for (attname,attval) in attrib
ds.attrib[attname] = attval
end
return ds
end
export defGroup
groupnames(ds::AbstractNCDataset) = keys(ds.group)
group(ds::AbstractNCDataset,groupname::AbstractString) = ds.group[groupname]
"""
name(ds::NCDataset)
Return the group name of the NCDataset `ds`
"""
name(ds::NCDataset) = nc_inq_grpname(ds.ncid)
groupname(ds::NCDataset) = name(ds)
export groupname
parentdataset(ds::NCDataset) = ds.parentdataset