diff --git a/src/cubes.jl b/src/cubes.jl index 16e1d11..910c7ae 100644 --- a/src/cubes.jl +++ b/src/cubes.jl @@ -94,7 +94,9 @@ function query(cell_cube::CellCube, query_str::String="all") return CellCube(data, cell_cube.level) end -function reduce(agg_func, cell_cube::CellCube; dims) +function Base.mapslices(agg_func, cell_cube::CellCube; dims, kw...) + data = mapslices(agg_func, cell_cube.data; dims, kw...) + return CellCube(data, cell_cube.level) end "maximial i or j value in Q2DI index given a level" diff --git a/src/gridsystems.jl b/src/gridsystems.jl index 6db5327..20e2620 100644 --- a/src/gridsystems.jl +++ b/src/gridsystems.jl @@ -99,4 +99,9 @@ Base.getindex(dggs::GridSystem, level::Int) = dggs.data[level] Base.setindex!(dggs::GridSystem, cell_cube::CellCube, level::Int) = dggs.data[level] = cell_cube CellCube(dggs::GridSystem) = dggs[dggs.data|>keys|>maximum] -plot(dggs::GridSystem) = dggs |> CellCube |> plot \ No newline at end of file +plot(dggs::GridSystem) = dggs |> CellCube |> plot + +function Base.mapslices(f, dggs::GridSystem; dims, kw...) + data = dggs |> CellCube |> x -> mapslices(f, x; dims=dims, kw...) + return GridSystem(data) +end \ No newline at end of file diff --git a/src/types.jl b/src/types.jl index c4e208f..863ea84 100644 --- a/src/types.jl +++ b/src/types.jl @@ -13,6 +13,15 @@ end struct CellCube data::YAXArray level::Int8 + + function CellCube(data, level) + level > 0 || error("Level must be positive") + :q2di_i in name.(data.axes) || error("Axis q2di_i is missing") + :q2di_j in name.(data.axes) || error("Axis q2di_j is missing") + :q2di_n in name.(data.axes) || error("Axis q2di_n is missing") + + new(data, level) + end end struct GeoCube diff --git a/test/runtests.jl b/test/runtests.jl index a703107..b43aad5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,6 @@ using DGGS using Test - +using Statistics @testset verbose = true "DGGS.jl" begin lon_range = -180:180 @@ -18,6 +18,9 @@ using Test @test geo_cube |> x -> CellCube(x, 8) |> GeoCube |> typeof == GeoCube @test keys(dggs.data) |> maximum == 6 + @test_throws ErrorException mapslices(mean, dggs[6]; dims=["q2di_i", "q2di_j"]) + @test + dggs_path = tempname() write(dggs_path, dggs) dggs2 = GridSystem(dggs_path)