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
:
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
:
m2 = collect(a.data)
5×10 Matrix{Float64}:
0.385566 0.893873 0.682451 0.808871 … 0.0619354 0.124175 0.515777
0.443632 0.353103 0.564634 0.704278 0.368022 0.145434 0.293256
0.575769 0.134637 0.839297 0.265568 0.92303 0.563123 0.886301
0.316638 0.233516 0.332875 0.474806 0.276348 0.631347 0.677238
0.0149217 0.229851 0.914996 0.135088 0.94013 0.346661 0.977482
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:
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
└─────────────────────────────────────────────────────────────┘
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.961001 0.00362772 0.31403 0.710498 0.283036 0.640441
26 0.995719 0.315687 0.723564 0.915848 0.499723 0.464818
27 0.0412041 0.47859 0.653071 0.766245 0.839447 0.379263
28 0.291882 0.46657 0.21924 0.119464 0.789704 0.889357
29 0.711002 0.971304 0.49964 0.998782 0.307888 0.125684
30 0.382774 0.92273 0.68083 0.473735 0.557147 0.507159
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
:
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
:
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.360273 0.0664705 0.429435 0.476739 0.36173 0.084243
2 0.775973 0.119542 0.290053 0.803549 0.436001 0.463916
3 0.0134931 0.796019 0.956738 0.497975 0.194278 0.610775
4 0.557964 0.60109 0.195567 0.938961 0.947251 0.149577
5 0.0338467 0.520137 0.849301 0.415606 0.600694 0.952859
::: info
At the moment there is no support to save a DimArray directly into disk as a NetCDF
or a Zarr
file.
:::