Skip to content

Create cube / YAXArray from function

julia
using YAXArrays, Zarr
using Dates

Define function in space and time

julia
f(lo, la, t) = (lo + la + Dates.dayofyear(t))
f (generic function with 1 method)

Wrap function for mapCube output

julia
function g(xout,lo,la,t)
    xout .= f.(lo,la,t)
end
g (generic function with 1 method)

Note the applied . after f, this is because we will slice/broadcasted across time.

Create Cube's Axes

We wrap the dimensions of every axis into a YAXArray to use them in the mapCube function.

julia
julia> lon = YAXArray(Dim{:lon}(range(1, 15)))
╭──────────────────────────────╮
15-element YAXArray{Int64,1}
├──────────────────────────────┴──────────────────── dims ┐
lon Sampled{Int64} 1:15 ForwardOrdered Regular Points
├─────────────────────────────────────────────── metadata ┤
  Dict{String, Any}()
├────────────────────────────────────────────── file size ┤
  file size: 120.0 bytes
└─────────────────────────────────────────────────────────┘
julia
julia> lat = YAXArray(Dim{:lat}(range(1, 10)))
╭──────────────────────────────╮
10-element YAXArray{Int64,1}
├──────────────────────────────┴──────────────────── dims ┐
lat Sampled{Int64} 1:10 ForwardOrdered Regular Points
├─────────────────────────────────────────────── metadata ┤
  Dict{String, Any}()
├────────────────────────────────────────────── file size ┤
  file size: 80.0 bytes
└─────────────────────────────────────────────────────────┘

And a time axis

julia
julia> tspan =  Date("2022-01-01"):Day(1):Date("2022-01-30")
Date("2022-01-01"):Dates.Day(1):Date("2022-01-30")
julia
julia> time = YAXArray(Dim{:time}( tspan))
╭─────────────────────────────╮
30-element YAXArray{Date,1}
├─────────────────────────────┴────────────────────────────────────────── dims ┐
time Sampled{Date} Date("2022-01-01"):Dates.Day(1):Date("2022-01-30") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
  file size: 240.0 bytes
└──────────────────────────────────────────────────────────────────────────────┘

Generate Cube

The following generates a new cube using mapCube and saving the output directly to disk.

julia
gen_cube = mapCube(g, (lon, lat, time);
    indims = (InDims(), InDims(), InDims("time")),
    outdims = OutDims("time", overwrite=true,
    path = "my_gen_cube.zarr", backend=:zarr, outtype=Float32),
    ## max_cache=1e9
    )
"Running nonthreaded" = "Running nonthreaded"

time axis is first

Note that currently the time axis in the output cube goes first.

Check that it is working

julia
julia> gen_cube.data[1,:,:]
15×10 reshape(::Array{Union{Missing, Float32}, 3}, 15, 10) with eltype Union{Missing, Float32}:
  3.0   4.0   5.0   6.0   7.0   8.0   9.0  10.0  11.0  12.0
  4.0   5.0   6.0   7.0   8.0   9.0  10.0  11.0  12.0  13.0
  5.0   6.0   7.0   8.0   9.0  10.0  11.0  12.0  13.0  14.0
  6.0   7.0   8.0   9.0  10.0  11.0  12.0  13.0  14.0  15.0
  7.0   8.0   9.0  10.0  11.0  12.0  13.0  14.0  15.0  16.0
  8.0   9.0  10.0  11.0  12.0  13.0  14.0  15.0  16.0  17.0
  9.0  10.0  11.0  12.0  13.0  14.0  15.0  16.0  17.0  18.0
 10.0  11.0  12.0  13.0  14.0  15.0  16.0  17.0  18.0  19.0
 11.0  12.0  13.0  14.0  15.0  16.0  17.0  18.0  19.0  20.0
 12.0  13.0  14.0  15.0  16.0  17.0  18.0  19.0  20.0  21.0
 13.0  14.0  15.0  16.0  17.0  18.0  19.0  20.0  21.0  22.0
 14.0  15.0  16.0  17.0  18.0  19.0  20.0  21.0  22.0  23.0
 15.0  16.0  17.0  18.0  19.0  20.0  21.0  22.0  23.0  24.0
 16.0  17.0  18.0  19.0  20.0  21.0  22.0  23.0  24.0  25.0
 17.0  18.0  19.0  20.0  21.0  22.0  23.0  24.0  25.0  26.0

Change output order

The following generates a new cube using mapCube and saving the output directly to disk.

julia
gen_cube = mapCube(g, (lon, lat, time);
    indims = (InDims("lon"), InDims(), InDims()),
    outdims = OutDims("lon", overwrite=true,
    path = "my_gen_cube.zarr", backend=:zarr, outtype=Float32),
    ## max_cache=1e9
    )
"Running nonthreaded" = "Running nonthreaded"

slicing dim

Note that now the broadcasted dimension is lon.

julia
julia> gen_cube.data[:, :, 1]
15×10 reshape(::Array{Union{Missing, Float32}, 3}, 15, 10) with eltype Union{Missing, Float32}:
  3.0   4.0   5.0   6.0   7.0   8.0   9.0  10.0  11.0  12.0
  4.0   5.0   6.0   7.0   8.0   9.0  10.0  11.0  12.0  13.0
  5.0   6.0   7.0   8.0   9.0  10.0  11.0  12.0  13.0  14.0
  6.0   7.0   8.0   9.0  10.0  11.0  12.0  13.0  14.0  15.0
  7.0   8.0   9.0  10.0  11.0  12.0  13.0  14.0  15.0  16.0
  8.0   9.0  10.0  11.0  12.0  13.0  14.0  15.0  16.0  17.0
  9.0  10.0  11.0  12.0  13.0  14.0  15.0  16.0  17.0  18.0
 10.0  11.0  12.0  13.0  14.0  15.0  16.0  17.0  18.0  19.0
 11.0  12.0  13.0  14.0  15.0  16.0  17.0  18.0  19.0  20.0
 12.0  13.0  14.0  15.0  16.0  17.0  18.0  19.0  20.0  21.0
 13.0  14.0  15.0  16.0  17.0  18.0  19.0  20.0  21.0  22.0
 14.0  15.0  16.0  17.0  18.0  19.0  20.0  21.0  22.0  23.0
 15.0  16.0  17.0  18.0  19.0  20.0  21.0  22.0  23.0  24.0
 16.0  17.0  18.0  19.0  20.0  21.0  22.0  23.0  24.0  25.0
 17.0  18.0  19.0  20.0  21.0  22.0  23.0  24.0  25.0  26.0