JuliaDataCubes / YAXArrays.jl

Yet Another XArray-like Julia package
https://juliadatacubes.github.io/YAXArrays.jl/
Other
99 stars 16 forks source link

pass variable name to Dataset as string or symbol #292

Closed lazarusA closed 1 year ago

lazarusA commented 1 year ago

currently we do

ds = Dataset(z = YAXArray(rand(10)))
YAXArray Dataset
Shared Axes: 
Dim{:Dim_1} Sampled{Int64} Base.OneTo(10) ForwardOrdered Regular Points
Variables: 
z, 

and the variable name is z, but is there a way to pass a name as an extra argument ? as a symbol or string? The following don't work :(

ds = Dataset(:z = YAXArray(rand(10)))
ds = Dataset("z" = YAXArray(rand(10)))

let's say we have >50 variables and we have a list for them, then using that list would be ideal.

felixcremer commented 1 year ago

You can construct a NamedTuple from your list of variables and list of variable names and then splat this inside the Dataset call.

keylist = (:a, :b,:c)
varlist = (YAXArray(rand(10)), YAXArray(rand(10)), YAXArray(rand(10)))
julia> YAXArrays.Dataset(;(;zip(keylist,varlist)...)...)
YAXArray Dataset
Shared Axes: 
Dim{:Dim_1} Sampled{Int64} Base.OneTo(10) ForwardOrdered Regular Points
Variables: 
a, b, c, 
lazarusA commented 1 year ago

Thanks, for one variable would be like this:

keylist = (:a, )
varlist = (YAXArray(rand(10)),)
YAXArrays.Dataset(;(;zip(keylist,varlist)...)...)

it works, but its really cumbersome, maybe having an enhancement to make this more user friendly would be a good, @meggart .

felixcremer commented 1 year ago

You can shorten that to the following, which is still obscure but a bit better:

julia> YAXArrays.Dataset(; (keylist .=> varlist)...)
YAXArray Dataset
Shared Axes: 
Dim{:Dim_1} Sampled{Int64} Base.OneTo(10) ForwardOrdered Regular Points
Variables: 
a, 
lazarusA commented 1 year ago

nice! final solution

 YAXArrays.Dataset(; (:a => YAXArray(rand(10)),)...)