Skip to content

Commit 48849ec

Browse files
Merge pull request #31 from JuliaGeo/rs/parent
define `Base.parent` and use it
2 parents 4a16ac6 + a83671c commit 48849ec

File tree

5 files changed

+69
-70
lines changed

5 files changed

+69
-70
lines changed

src/cfvariable.jl

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ Return a tuple of integers with the size of the variable `var`.
1111
Note that the size of a variable can change, i.e. for a variable with an
1212
unlimited dimension.
1313
"""
14-
Base.size(v::CFVariable) = size(v.var)
14+
Base.size(v::CFVariable) = size(parent(v))
15+
Base.parent(v::CFVariable) = v.var
1516

16-
name(v::CFVariable) = name(v.var)
17-
dataset(v::CFVariable) = dataset(v.var)
17+
name(v::CFVariable) = name(parent(v))
18+
dataset(v::CFVariable) = dataset(parent(v))
1819

1920

20-
# be aware that for GRIBDatasets v.attrib is different from v.var.attrib
21+
# be aware that for GRIBDatasets v.attrib is different from parent(v).attrib
2122
attribnames(v::CFVariable) = keys(v.attrib)
2223
attrib(v::CFVariable,name::SymbolOrString) = v.attrib[name]
2324
defAttrib(v::CFVariable,name,value) = v.attrib[name] = value
2425
delAttrib(v::CFVariable,name) = delete!(v,name)
2526

26-
dimnames(v::CFVariable) = dimnames(v.var)
27-
dim(v::CFVariable,name::SymbolOrString) = dim(v.var,name)
27+
dimnames(v::CFVariable) = dimnames(parent(v))
28+
dim(v::CFVariable,name::SymbolOrString) = dim(parent(v),name)
2829

2930
# necessary for IJulia if showing a variable from a closed file
3031
Base.show(io::IO,::MIME"text/plain",v::AbstractVariable) = show(io,v)
@@ -215,7 +216,7 @@ missing_values(v::CFVariable) = v._storage_attrib.missing_values
215216

216217
# collect all possible fill values
217218
function fill_and_missing_values(v::CFVariable)
218-
T = eltype(v.var)
219+
T = eltype(parent(v))
219220
fv = ()
220221
if !isnothing(fillvalue(v))
221222
fv = (fillvalue(v),)
@@ -448,29 +449,29 @@ end
448449
#@inline CFinvtransformdata(data::Char,fv,scale_factor,add_offset,time_origin,time_factor,DT) = CFtransform_replace_missing(data,fv)
449450

450451
function Base.getindex(v::CFVariable, indexes::TIndices...)
451-
data = v.var[indexes...]
452+
data = parent(v)[indexes...]
452453
return CFtransformdata(data,fill_and_missing_values(v),scale_factor(v),add_offset(v),
453454
time_origin(v),time_factor(v),maskingvalue(v),eltype(v))
454455
end
455456

456457
function Base.setindex!(v::CFVariable,data::Array{Missing,N},indexes::TIndices...) where N
457-
v.var[indexes...] = fill(fillvalue(v),size(data))
458+
parent(v)[indexes...] = fill(fillvalue(v),size(data))
458459
end
459460

460461
function Base.setindex!(v::CFVariable,data::Missing,indexes::TIndices...)
461-
v.var[indexes...] = fillvalue(v)
462+
parent(v)[indexes...] = fillvalue(v)
462463
end
463464

464465
function Base.setindex!(v::CFVariable,data::Union{T,Array{T}},indexes::TIndices...) where T <: Union{AbstractCFDateTime,DateTime,Missing}
465466

466467
if calendar(v) !== nothing
467468
# can throw an convertion error if calendar attribute already exists and
468469
# is incompatible with the provided data
469-
v.var[indexes...] = CFinvtransformdata(
470+
parent(v)[indexes...] = CFinvtransformdata(
470471
data,fill_and_missing_values(v),scale_factor(v),add_offset(v),
471472
time_origin(v),time_factor(v),
472473
maskingvalue(v),
473-
eltype(v.var))
474+
eltype(parent(v)))
474475
return data
475476
end
476477

@@ -479,12 +480,12 @@ end
479480

480481

481482
function Base.setindex!(v::CFVariable,data,indexes::TIndices...)
482-
v.var[indexes...] = CFinvtransformdata(
483+
parent(v)[indexes...] = CFinvtransformdata(
483484
data,fill_and_missing_values(v),
484485
scale_factor(v),add_offset(v),
485486
time_origin(v),time_factor(v),
486487
maskingvalue(v),
487-
eltype(v.var))
488+
eltype(parent(v)))
488489

489490
return data
490491
end
@@ -551,20 +552,20 @@ end
551552
552553
Return a tuple of strings with the dimension names of the variable `v`.
553554
"""
554-
dimnames(v::Union{CFVariable,MFCFVariable}) = dimnames(v.var)
555+
dimnames(v::Union{CFVariable,MFCFVariable}) = dimnames(parent(v))
555556

556-
name(v::Union{CFVariable,MFCFVariable}) = name(v.var)
557-
chunking(v::CFVariable,storage,chunksize) = chunking(v.var,storage,chunksize)
558-
chunking(v::CFVariable) = chunking(v.var)
557+
name(v::Union{CFVariable,MFCFVariable}) = name(parent(v))
558+
chunking(v::CFVariable,storage,chunksize) = chunking(parent(v),storage,chunksize)
559+
chunking(v::CFVariable) = chunking(parent(v))
559560

560-
deflate(v::CFVariable,shuffle,dodeflate,deflate_level) = deflate(v.var,shuffle,dodeflate,deflate_level)
561-
deflate(v::CFVariable) = deflate(v.var)
561+
deflate(v::CFVariable,shuffle,dodeflate,deflate_level) = deflate(parent(v),shuffle,dodeflate,deflate_level)
562+
deflate(v::CFVariable) = deflate(parent(v))
562563

563-
checksum(v::CFVariable,checksummethod) = checksum(v.var,checksummethod)
564-
checksum(v::CFVariable) = checksum(v.var)
564+
checksum(v::CFVariable,checksummethod) = checksum(parent(v),checksummethod)
565+
checksum(v::CFVariable) = checksum(parent(v))
565566

566567

567-
fillmode(v::CFVariable) = fillmode(v.var)
568+
fillmode(v::CFVariable) = fillmode(parent(v))
568569

569570

570571
############################################################
@@ -585,7 +586,7 @@ end
585586
586587
Loads a NetCDF (or other format) variables `ncvar` in-place and puts the result in `data` (an
587588
array of `eltype(ncvar)`) along the specified `indices`. `buffer` is a temporary
588-
array of the same size as data but the type should be `eltype(ncv.var)`, i.e.
589+
array of the same size as data but the type should be `eltype(parent(ncv))`, i.e.
589590
the corresponding type in the files (before applying `scale_factor`,
590591
`add_offset` and masking fill values). Scaling and masking will be applied to
591592
the array `data`.
@@ -608,17 +609,17 @@ ds = Dataset("file.nc")
608609
ncv = ds["vgos"];
609610
# data and buffer must have the right shape and type
610611
data = zeros(eltype(ncv),size(ncv)); # here Vector{Float64}
611-
buffer = zeros(eltype(ncv.var),size(ncv)); # here Vector{Int16}
612+
buffer = zeros(eltype(parent(ncv)),size(ncv)); # here Vector{Int16}
612613
NCDatasets.load!(ncv,data,buffer,:,:,:)
613614
close(ds)
614615
```
615616
"""
616617
@inline function load!(v::Union{CFVariable{T,N},MFCFVariable{T,N},SubVariable{T,N}}, data, buffer, indices::TIndices...) where {T,N}
617618

618-
if v.var == nothing
619+
if parent(v) == nothing
619620
return load!(v,indices...)
620621
else
621-
load!(v.var,buffer,indices...)
622+
load!(parent(v),buffer,indices...)
622623
fmv = fill_and_missing_values(v)
623624
return CFtransformdata!(data,buffer,fmv,scale_factor(v),add_offset(v),
624625
time_origin(v),time_factor(v),

src/memory_dataset.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ function grow_unlimited_dimension(ds,dname,len)
6868
end
6969
end
7070

71-
Base.getindex(v::MemoryVariable,ij::TIndices...) = v.data[ij...]
72-
CDM.load!(v::MemoryVariable,buffer,ij...) = buffer .= view(v.data,ij...)
73-
71+
Base.parent(v::MemoryVariable) = v.data
72+
Base.size(v::MemoryVariable) = size(parent(v))
73+
Base.getindex(v::MemoryVariable,ij::TIndices...) = parent(v)[ij...]
7474
function Base.setindex!(v::MemoryVariable,data,ij...)
75-
sz = size(v.data)
76-
v.data[ij...] = data
75+
sz = size(v)
76+
parent(v)[ij...] = data
7777

7878
root = _root(v)
7979
for idim = findall(size(v) .> sz)
@@ -82,7 +82,8 @@ function Base.setindex!(v::MemoryVariable,data,ij...)
8282
end
8383
return data
8484
end
85-
Base.size(v::MemoryVariable) = size(v.data)
85+
86+
CDM.load!(v::MemoryVariable,buffer,ij...) = buffer .= view(parent(v),ij...)
8687
CDM.name(v::Union{MemoryVariable,MemoryDataset}) = v.name
8788
CDM.dimnames(v::MemoryVariable) = v.dimnames
8889
CDM.dataset(v::MemoryVariable) = v.parent_dataset

src/multifile.jl

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ function parentdataset(mfds::MFDataset)
7272
end
7373
end
7474

75-
Base.Array(v::MFVariable) = Array(v.var)
76-
7775
iswritable(mfds::MFDataset) = iswritable(mfds.ds[1])
7876

7977
function MFDataset(ds,aggdim,isnewdim,constvars)
@@ -176,18 +174,23 @@ function varnames(mfds::MFDataset)
176174
end
177175
end
178176

179-
Base.getindex(v::MFVariable,indexes::TIndices...) = getindex(v.var,indexes...)
180-
Base.setindex!(v::MFVariable,data,indexes::TIndices...) = setindex!(v.var,data,indexes...)
181-
182-
183-
load!(v::MFVariable,buffer,indexes...) = CatArrays.load!(v.var,buffer,indexes...)
177+
Base.parent(v::MFVariable) = v.var
178+
Base.parent(v::MFCFVariable) = v.var
179+
Base.Array(v::MFVariable) = Array(parent(v))
180+
Base.getindex(v::MFVariable,indexes::TIndices...) = getindex(parent(v),indexes...)
181+
Base.setindex!(v::MFVariable,data,indexes::TIndices...) = setindex!(parent(v),data,indexes...)
182+
Base.size(v::MFVariable) = size(parent(v))
183+
Base.size(v::MFCFVariable) = size(parent(v))
184+
Base.getindex(v::MFCFVariable,ind::TIndices...) = v.cfvar[ind...]
185+
Base.setindex!(v::MFCFVariable,data,ind::TIndices...) = v.cfvar[ind...] = data
186+
function Base.cat(vs::AbstractVariable...; dims::Integer)
187+
CatArrays.CatArray(dims,vs...)
188+
end
184189

185-
Base.size(v::MFVariable) = size(v.var)
186-
Base.size(v::MFCFVariable) = size(v.var)
190+
load!(v::MFVariable,buffer,indexes...) = CatArrays.load!(parent(v),buffer,indexes...)
187191
dimnames(v::MFVariable) = v.dimnames
188192
name(v::MFVariable) = v.varname
189193

190-
191194
function variable(mfds::MFDataset,varname::SymbolOrString)
192195
if mfds.isnewdim
193196
if Symbol(varname) in mfds.constvars
@@ -268,15 +271,6 @@ end
268271

269272
dataset(v::Union{MFVariable,MFCFVariable}) = v.ds
270273

271-
272-
Base.getindex(v::MFCFVariable,ind::TIndices...) = v.cfvar[ind...]
273-
Base.setindex!(v::MFCFVariable,data,ind::TIndices...) = v.cfvar[ind...] = data
274-
275-
276-
function Base.cat(vs::AbstractVariable...; dims::Integer)
277-
CatArrays.CatArray(dims,vs...)
278-
end
279-
280274
"""
281275
storage,chunksizes = chunking(v::MFVariable)
282276
storage,chunksizes = chunking(v::MFCFVariable)
@@ -299,6 +293,6 @@ end
299293
deflate(v::MFVariable) = deflate(v.ds.ds[1][name(v)])
300294
checksum(v::MFVariable) = checksum(v.ds.ds[1][name(v)])
301295

302-
chunking(v::MFCFVariable) = chunking(v.var)
303-
deflate(v::MFCFVariable) = deflate(v.var)
304-
checksum(v::MFCFVariable) = checksum(v.var)
296+
chunking(v::MFCFVariable) = chunking(parent(v))
297+
deflate(v::MFCFVariable) = deflate(parent(v))
298+
checksum(v::MFCFVariable) = checksum(parent(v))

src/subvariable.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11

22
Base.parent(v::SubVariable) = v.parent
33
Base.parentindices(v::SubVariable) = v.indices
4-
Base.size(v::SubVariable) = _shape_after_slice(size(v.parent),v.indices...)
4+
Base.size(v::SubVariable) = _shape_after_slice(size(parent(v)),v.indices...)
55

66
function dimnames(v::SubVariable)
7-
dimension_names = dimnames(v.parent)
7+
dimension_names = dimnames(parent(v))
88
return dimension_names[map(i -> !(i isa Integer),collect(v.indices))]
99
end
1010

11-
name(v::SubVariable) = name(v.parent)
11+
name(v::SubVariable) = name(parent(v))
1212

13-
attribnames(v::SubVariable) = attribnames(v.parent)
14-
attrib(v::SubVariable,name::SymbolOrString) = attrib(v.parent,name)
15-
defAttrib(v::SubVariable,name::SymbolOrString,data) = defAttrib(v.parent,name,data)
13+
attribnames(v::SubVariable) = attribnames(parent(v))
14+
attrib(v::SubVariable,name::SymbolOrString) = attrib(parent(v),name)
15+
defAttrib(v::SubVariable,name::SymbolOrString,data) = defAttrib(parent(v),name,data)
1616

1717
function SubVariable(A::AbstractVariable,indices...)
1818
var = nothing
@@ -46,7 +46,7 @@ valid indices `parentindices` and `indices`
4646
=#
4747
subsub(parentindices,indices) = _subsub((),indices,1,parentindices...)
4848

49-
materialize(v::SubVariable) = v.parent[v.indices...]
49+
materialize(v::SubVariable) = parent(v)[v.indices...]
5050

5151
"""
5252
collect always returns an array.
@@ -56,10 +56,10 @@ into a zero-dimensional array.
5656
function collect(v::SubVariable{T,N}) where T where N
5757
if N == 0
5858
A = Array{T,0}(undef,())
59-
A[] = v.parent[v.indices...]
59+
A[] = parent(v)[v.indices...]
6060
return A
6161
else
62-
return v.parent[v.indices...]
62+
return parent(v)[v.indices...]
6363
end
6464
end
6565

@@ -108,7 +108,7 @@ Base.getindex(v::SubVariable,indices::CartesianIndices) =
108108

109109
function Base.setindex!(v::SubVariable,data,indices...)
110110
sub_indices = subsub(v.indices,indices)
111-
v.parent[sub_indices...] = data
111+
parent(v)[sub_indices...] = data
112112
end
113113

114114
Base.setindex!(v::SubVariable,data,indices::CartesianIndex) =
@@ -176,14 +176,14 @@ groupname(ds::SubDataset) = groupname(ds.ds)
176176

177177

178178
function dataset(v::SubVariable)
179-
indices = (;((Symbol(d),i) for (d,i) in zip(dimnames(v.parent),v.indices))...)
180-
return SubDataset(dataset(v.parent),indices)
179+
indices = (;((Symbol(d),i) for (d,i) in zip(dimnames(parent(v)),v.indices))...)
180+
return SubDataset(dataset(parent(v)),indices)
181181
end
182182

183183
function chunking(v::SubVariable)
184-
storage, chunksizes = chunking(v.parent)
184+
storage, chunksizes = chunking(parent(v))
185185
return storage, min.(chunksizes,size(v))
186186
end
187187

188-
deflate(v::SubVariable) = deflate(v.parent)
189-
checksum(v::SubVariable) = checksum(v.parent)
188+
deflate(v::SubVariable) = deflate(parent(v))
189+
checksum(v::SubVariable) = checksum(parent(v))

test/test_variable.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using Printf
55
#using NCDatasets: NetCDFError, load!
66
using DataStructures
77
using CFTime
8+
using CommonDataModel
89
using CommonDataModel:
910
MemoryDataset,
1011
name,
@@ -27,6 +28,8 @@ TDS(filename,"c") do ds
2728
ds.dim["lat"] = sz[2]
2829

2930
v = defVar(ds,"small",Float64,("lon","lat"))
31+
@test parent(v) isa CommonDataModel.MemoryVariable
32+
@test parent(parent(v)) isa Array
3033
# @test_throws Union{NetCDFError,DimensionMismatch} v[:] = zeros(sz[1]+1,sz[2])
3134
@test_throws DimensionMismatch v[1:sz[1],1:sz[2]] = zeros(sz[1]+1,sz[2])
3235
@test_throws BoundsError v[sz[1]+1,1] = 1

0 commit comments

Comments
 (0)