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.275953   0.908733  0.945473  0.89082   …  0.506143  0.646264   0.484504
 0.931359   0.79898   0.492366  0.354019     0.380954  0.902789   0.775352
 0.195112   0.149747  0.587823  0.299036     0.952645  0.0481227  0.372878
 0.0126228  0.291247  0.417136  0.362148     0.834957  0.942395   0.855535
 0.957847   0.505572  0.075497  0.812221     0.678015  0.209058   0.845177

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.508968    0.201      0.228329    0.443921   0.0016043   0.744147
 26     0.426614    0.457045   0.360031    0.854556   0.873199    0.202457
 27     0.898824    0.295065   0.443947    0.689034   0.782729    0.236862
 28     0.233642    0.598441   0.928433    0.821712   0.428044    0.00341717
 29     0.249775    0.287776   0.0391094   0.998243   0.155019    0.835979
 30     0.0270115   0.952619   0.356225    0.983686   0.0318083   0.337743

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.375071    0.10778     0.136866    0.801031    0.945303   0.322938
 2     0.255362    0.475875    0.891795    0.146681    0.355027   0.747662
 3     0.883687    0.909186    0.604756    0.943467    0.5221     0.623636
 4     0.0809511   0.918395    0.0535527   0.908792    0.406759   0.345457
 5     0.54991     0.0401898   0.790472    0.0259303   0.383191   0.171133

INFO

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