danlooo / DGGS.jl

Discrete Global Grid System for Julia
GNU Affero General Public License v3.0
11 stars 2 forks source link

Add map-reduce syntax to CellCubes and DGGS #77

Closed danlooo closed 6 months ago

danlooo commented 9 months ago

Problem

One might want to reduce spatial dimensions e.g. to get a spatial average. However, the result is not a proper CellCube anymore due to the lack of spatial dimensions.

Solutions

  1. Return a plain YAXArray e.g. after mapslices(mean, dggs[6]; dims=["q2di_i", "q2di_j"]). Then, mapslice will change the type from input CellCube to output YAXArray
  2. Throw an error stating thata proper CellCube couldn't be thrown

What do you think? @meggart

julia> mapslices(mean, dggs[6]; dims=["q2di_i", "q2di_j"])
ERROR: Axis q2di_i is missing
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] CellCube(data::YAXArrays.Cubes.YAXArray{Union{Missing, Float32}, 2, Matrix{Union{Missing, Float32}}, Tuple{Dim{:q2di_n, DimensionalData.Dimensions.LookupArrays.Sampled{Int64, StepRange{Int64, Int64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Regular{Int64}, DimensionalData.Dimensions.LookupArrays.Points, DimensionalData.Dimensions.LookupArrays.NoMetadata}}, Ti{DimensionalData.Dimensions.LookupArrays.Sampled{Dates.DateTime, Vector{Dates.DateTime}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Irregular{Tuple{Nothing, Nothing}}, DimensionalData.Dimensions.LookupArrays.Points, DimensionalData.Dimensions.LookupArrays.NoMetadata}}}}, level::Int8)
   @ DGGS ~/prj/DGGS.jl/src/types.jl:19
 [3] mapslices(agg_func::Function, cell_cube::CellCube; dims::Vector{String}, kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ DGGS ~/prj/DGGS.jl/src/cubes.jl:99
 [4] top-level scope
   @ REPL[41]:1

julia> mapslices(mean, dggs[6].data; dims=["q2di_i", "q2di_j"])
12×12 YAXArray{Union{Missing, Float32},2} with dimensions: 
  Dim{:q2di_n} Sampled{Int64} 0:1:11 ForwardOrdered Regular Points,
  Ti Sampled{Dates.DateTime} Dates.DateTime[2001-01-01T00:00:00, …, 2001-12-01T00:00:00] ForwardOrdered Irregular Points
Total size: 576.0 bytes