Skip to content

Convert YAXArrays

This section describes how to convert variables from types of other Julia packages into YAXArrays and vice versa.

WARNING

YAXArrays is designed to work with large datasets that are way larger than the memory. However, most types are designed to work in memory. Those conversions are only possible if the entire dataset fits into memory. In addition, metadata might be lost during conversion.

Convert Base.Array

Convert Base.Array to YAXArray:

julia
using YAXArrays

m = rand(5,10)
a = YAXArray(m)
╭──────────────────────────╮
│ 5×10 YAXArray{Float64,2} │
├──────────────────────────┴──────────────────────────────────── dims ┐
  ↓ Dim_1 Sampled{Int64} Base.OneTo(5) ForwardOrdered Regular Points,
  → Dim_2 Sampled{Int64} Base.OneTo(10) ForwardOrdered Regular Points
├─────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any}()
├────────────────────────────────────────────────────────── file size ┤ 
  file size: 400.0 bytes
└─────────────────────────────────────────────────────────────────────┘

Convert YAXArray to Base.Array:

julia
m2 = collect(a.data)
5×10 Matrix{Float64}:
 0.667057  0.728995  0.808061   0.786653  …  0.8419     0.791231  0.118624
 0.401954  0.382786  0.20153    0.806099     0.0568355  0.823764  0.0879675
 0.126426  0.980211  0.0992214  0.725325     0.724075   0.07426   0.449777
 0.074525  0.617036  0.330332   0.250216     0.709227   0.547389  0.737019
 0.280866  0.369564  0.600464   0.774483     0.694029   0.262042  0.576404

Convert Raster

A Raster as defined in Rasters.jl has a same supertype of a YAXArray, i.e. AbstractDimArray, allowing easy conversion between those types:

julia
using Rasters

lon, lat = X(25:1:30), Y(25:1:30)
time = Ti(2000:2024)
ras = Raster(rand(lon, lat, time))
a = YAXArray(dims(ras), ras.data)
╭────────────────────────────╮
│ 6×6×25 YAXArray{Float64,3} │
├────────────────────────────┴────────────────────────── dims ┐
  ↓ X  Sampled{Int64} 25:1:30 ForwardOrdered Regular Points,
  → Y  Sampled{Int64} 25:1:30 ForwardOrdered Regular Points,
  ↗ Ti Sampled{Int64} 2000:2024 ForwardOrdered Regular Points
├─────────────────────────────────────────────────── metadata ┤
  Dict{String, Any}()
├────────────────────────────────────────────────── file size ┤ 
  file size: 7.03 KB
└─────────────────────────────────────────────────────────────┘
julia
ras2 = Raster(a)
╭──────────────────────────╮
│ 6×6×25 Raster{Float64,3} │
├──────────────────────────┴──────────────────────────── dims ┐
  ↓ X  Sampled{Int64} 25:1:30 ForwardOrdered Regular Points,
  → Y  Sampled{Int64} 25:1:30 ForwardOrdered Regular Points,
  ↗ Ti Sampled{Int64} 2000:2024 ForwardOrdered Regular Points
├─────────────────────────────────────────────────── metadata ┤
  Dict{String, Any}()
├───────────────────────────────────────────────────── raster ┤
  extent: Extent(X = (25, 30), Y = (25, 30), Ti = (2000, 2024))

└─────────────────────────────────────────────────────────────┘
[:, :, 1]
  ↓ →  25         26         27         28         29          30
 25     0.758188   0.457139   0.349836   0.515499   0.792215    0.250473
 26     0.988388   0.833166   0.706876   0.41747    0.141744    0.849964
 27     0.1222     0.722171   0.633496   0.365494   0.582633    0.936302
 28     0.363343   0.54997    0.705936   0.964455   0.0309583   0.000954111
 29     0.660057   0.299326   0.892888   0.643371   0.95249     0.925379
 30     0.194827   0.720109   0.896135   0.92841    0.0158571   0.212187

Convert DimArray

A DimArray as defined in DimensionalData.jl has a same supertype of a YAXArray, i.e. AbstractDimArray, allowing easy conversion between those types.

Convert DimArray to YAXArray:

julia
using DimensionalData
using YAXArrayBase

dim_arr = rand(X(1:5), Y(10.0:15.0), metadata = Dict{String, Any}())
a = yaxconvert(YAXArray, dim_arr)
╭─────────────────────────╮
│ 5×6 YAXArray{Float64,2} │
├─────────────────────────┴────────────────────────────────── dims ┐
  ↓ X Sampled{Int64} 1:5 ForwardOrdered Regular Points,
  → Y Sampled{Float64} 10.0:1.0:15.0 ForwardOrdered Regular Points
├──────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any}()
├─────────────────────────────────────────────────────── file size ┤ 
  file size: 240.0 bytes
└──────────────────────────────────────────────────────────────────┘

Convert YAXArray to DimArray:

julia
dim_arr2 = yaxconvert(DimArray, a)
╭─────────────────────────╮
│ 5×6 DimArray{Float64,2} │
├─────────────────────────┴────────────────────────────────── dims ┐
  ↓ X Sampled{Int64} 1:5 ForwardOrdered Regular Points,
  → Y Sampled{Float64} 10.0:1.0:15.0 ForwardOrdered Regular Points
├──────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any}()
└──────────────────────────────────────────────────────────────────┘
 ↓ →  10.0       11.0       12.0       13.0        14.0        15.0
 1     0.152233   0.419393   0.890036   0.962418    0.0888288   0.166223
 2     0.209566   0.894676   0.725177   0.49142     0.190788    0.0700609
 3     0.414184   0.598623   0.81739    0.444331    0.510375    0.689918
 4     0.980926   0.971201   0.822465   0.290261    0.676299    0.314525
 5     0.692285   0.327415   0.993211   0.0337252   0.38971     0.534716

INFO

At the moment there is no support to save a DimArray directly into disk as a NetCDF or a Zarr file.