Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add map-reduce syntax to CellCubes and DGGS #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion src/gridsystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
9 changes: 9 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using DGGS
using Test

using Statistics

@testset verbose = true "DGGS.jl" begin
lon_range = -180:180
Expand All @@ -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)
Expand Down
Loading