From 1e13b6ec66e24afa1a2b2577217240cd21700a87 Mon Sep 17 00:00:00 2001 From: Fabian Gans Date: Mon, 16 May 2022 17:37:33 +0200 Subject: [PATCH] Add AxisKeys --- Project.toml | 12 ------------ src/YAXArrayBase.jl | 2 ++ src/axisarrays/axiskeys.jl | 10 ++++++++++ test/Project.toml | 11 +++++++++++ test/arrays.jl | 11 +++++++++++ 5 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 src/axisarrays/axiskeys.jl create mode 100644 test/Project.toml diff --git a/Project.toml b/Project.toml index 373ef47..9d3cad0 100644 --- a/Project.toml +++ b/Project.toml @@ -14,15 +14,3 @@ DataStructures = "0.17, 0.18" Requires = "1" julia = "1.6" -[extras] -ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3" -AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9" -AxisIndices = "f52c9ee2-1b1c-4fd8-8546-6350938c7f11" -DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0" -NamedDims = "356022a1-0364-5f58-8944-0da4b18d706f" -NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -Zarr = "0a941bbe-ad1d-11e8-39d9-ab76183a1d99" - -[targets] -test = ["Test", "DimensionalData", "AxisArrays", "ArchGDAL", "AxisIndices", "NetCDF", "Zarr", "NamedDims"] diff --git a/src/YAXArrayBase.jl b/src/YAXArrayBase.jl index b359c5d..8763272 100644 --- a/src/YAXArrayBase.jl +++ b/src/YAXArrayBase.jl @@ -31,6 +31,8 @@ function __init__() @require AxisIndices="f52c9ee2-1b1c-4fd8-8546-6350938c7f11" include("axisarrays/axisindices.jl") + @require AxisKeys="94b1ba4f-4ee9-5380-92f1-94cde586c3c5" include("axisarrays/axiskeys.jl") + @require ArchGDAL="c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3" begin include("axisarrays/archgdal.jl") include("datasets/archgdal.jl") diff --git a/src/axisarrays/axiskeys.jl b/src/axisarrays/axiskeys.jl new file mode 100644 index 0000000..d39437f --- /dev/null +++ b/src/axisarrays/axiskeys.jl @@ -0,0 +1,10 @@ +import .AxisKeys + +dimnames(a::AxisKeys.KeyedArray) = AxisKeys.dimnames(a) + +dimvals(a::AxisKeys.KeyedArray,i) = AxisKeys.getproperty(a,AxisKeys.dimnames(a,i)) + +getdata(a::AxisKeys.KeyedArray) = parent(parent(a)) + +yaxcreate(::Type{<:AxisKeys.KeyedArray}, data, dnames, dvals, atts) = + AxisKeys.KeyedArray(data; map(i->dnames[i]=>dvals[i],1:ndims(data))...) diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..d2d88ac --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,11 @@ +[deps] +ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3" +AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9" +AxisIndices = "f52c9ee2-1b1c-4fd8-8546-6350938c7f11" +AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5" +DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0" +Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6" +NamedDims = "356022a1-0364-5f58-8944-0da4b18d706f" +NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Zarr = "0a941bbe-ad1d-11e8-39d9-ab76183a1d99" diff --git a/test/arrays.jl b/test/arrays.jl index c47c145..8b1984b 100644 --- a/test/arrays.jl +++ b/test/arrays.jl @@ -18,6 +18,17 @@ YAXArrayBase.getattributes(::M) = Dict{String,Any}("a1"=>5, "a2"=>"att") @test dimvals(d,2) == 1.5:0.5:3.0 end +@testset "AxisKeys" begin + using AxisKeys: KeyedArray + d = yaxconvert(KeyedArray,M()) + @test d isa KeyedArray + @test getdata(d) == reshape(1:12,3,4) + @test YAXArrayBase.dimnames(d) == (:x, :y) + @test dimvals(d,1) == 0.5:1.0:2.5 + @test dimvals(d,2) == 1.5:0.5:3.0 +end + + @testset "AxisArrays" begin using AxisArrays: AxisArrays d = yaxconvert(AxisArrays.AxisArray,M())