Skip to content

Commit 496874f

Browse files
authored
Merge pull request #42 from JuliaGeo/rs/variable_as_da
Variable as a disk array
2 parents ea6cf66 + 7494506 commit 496874f

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/variables.jl

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ abstract type AbstractGRIBVariable{T, N} <: AbstractVariable{T, N} end
22

33
"""
44
DiskValues{T, N, M} <: DA.AbstractDiskArray{T, N}
5+
56
Object that maps the dimensions lookup to GRIB messages offsets.
67
`message_dims` are the dimensions that are found in the GRIB message (namely longitudes and latitudes).
78
`other_dims` are the dimensions that have been infered from reading the GRIB file index.
@@ -90,7 +91,7 @@ function DA.readblock!(A::DiskValues, aout, i::AbstractUnitRange...)
9091
end
9192

9293
DA.eachchunk(A::DiskValues) = DA.GridChunks(A, size(A))
93-
DA.haschunks(A::DiskValues) = DA.Unchunked()
94+
DA.haschunks(A::DiskValues) = DA.Chunked() # Its basically one large chunk
9495

9596
"""
9697
Variable <: AbstractArray
@@ -105,7 +106,6 @@ struct Variable{T, N, TA <: Union{Array{T, N}, DA.AbstractDiskArray{T, N}}, TP}
105106
end
106107
Base.parent(var::Variable) = var.values
107108
Base.size(var::Variable) = _size_dims(var.dims)
108-
Base.getindex(var::Variable, I...) = getindex(parent(var), I...)
109109

110110
ndims(::AbstractGRIBVariable{T,N}) where {T,N} = N
111111
varname(var::Variable) = var.name
@@ -125,6 +125,19 @@ dataset(var::AbstractGRIBVariable) = var.ds
125125

126126
_get_dim(var::Variable, key::String) = _get_dim(var.dims, key)
127127

128+
DA.@implement_diskarray Variable
129+
# Avoid DiskArrays.jl indexing when the parent is an Array
130+
Base.getindex(var::Variable{T,N,Array{T,N}}, I::AbstractUnitRange...) where {T,N} =
131+
getindex(parent(var), I...)
132+
Base.getindex(var::Variable{T,N,Array{T,N}}, I...) where {T,N} =
133+
getindex(parent(var), I...)
134+
135+
function DA.readblock!(A::Variable, aout, i::AbstractUnitRange...)
136+
DA.readblock!(parent(A), aout, i...)
137+
end
138+
DA.eachchunk(A::Variable) = DA.eachchunk(parent(A))
139+
DA.haschunks(A::Variable) = DA.haschunks(parent(A))
140+
128141
function Variable(ds::GRIBDataset, key)
129142
dsdims = ds.dims
130143
if key in dsdims

test/Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
44
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
5+
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
56
GRIB = "b16dfd50-4035-11e9-28d4-9dfe17e6779b"
67
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
78

test/dataset.jl

+22-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ using GRIBDatasets: DATA_ATTRIBUTES_KEYS, GRID_TYPE_MAP
77
using GRIBDatasets: _to_datetime
88
using GRIBDatasets: DiskValues, Variable, CFVariable, cfvariable
99
using GRIBDatasets: CDM
10+
using DiskArrays
11+
12+
grib_path = joinpath(dir_testfiles, "era5-levels-members.grib")
13+
varstring = "z"
1014

1115
@testset "dataset and variables" begin
12-
grib_path = joinpath(dir_testfiles, "era5-levels-members.grib")
1316
ds = GRIBDataset(grib_path)
1417
dsmis = GRIBDataset(joinpath(dir_testfiles, "fields_with_missing_values.grib"))
1518
dsNaN = GRIBDataset(joinpath(dir_testfiles, "fields_with_missing_values.grib"),maskingvalue = NaN)
1619
index = ds.index
1720

18-
varstring = "z"
1921
@testset "CommonDataModel implementation" begin
2022
@test CDM.dim(ds, "number") == 10
2123
@test length(CDM.dimnames(ds)) == 5
@@ -196,3 +198,21 @@ end
196198
end
197199

198200
end
201+
202+
@testset "diskarrays" begin
203+
# No scalar indexing allowed
204+
DiskArrays.allowscalar(false)
205+
ds = GRIBDataset(grib_path)
206+
# CFVariable is not a disk array, so will be super slow here.
207+
# But the underlying variable is
208+
var = ds[varstring].var
209+
@test DiskArrays.isdisk(var)
210+
# Currently just one huge chunk
211+
@test length(DiskArrays.eachchunk(var)) == 1
212+
# Broadcasts are lazy
213+
B = var .* 10
214+
@test B isa DiskArrays.BroadcastDiskArray
215+
@test B[1:50, 1:50, 1, 1, 1] isa Matrix
216+
# Reduction is chunked
217+
@test sum(var) * 10 == sum(B)
218+
end

0 commit comments

Comments
 (0)