Frequently Asked Questions (FAQ)
The purpose of this section is to do a collection of small convinient pieces of code on how to do simple things.
Extract the axes names from a Cube
using YAXArrays
using DimensionalData
::: warning
To get the axes of a YAXArray use the dims
function instead of the caxes
function
:::
::: info
Also, use DD.rebuild(ax, values)
instead of axcopy(ax, values)
to copy an axes with the same name but different values.
:::
Obtain values from axes and data from the cube
There are two options to collect values from axes. In this examples the axis ranges from 1 to 10.
These two examples bring the same result
collect(getAxis("Dim_1", c).val)
collect(c.axes[1].val)
10-element Vector{Int64}:
1
2
3
4
5
6
7
8
9
10
to collect data from a cube works exactly the same as doing it from an array
How do I concatenate cubes
It is possible to concatenate several cubes that shared the same dimensions using the [concatenatecubes
]@ref function.
Let's create two dummy cubes
using YAXArrays
axlist = (
Dim{:time}(range(1, 20, length=20)),
Dim{:lon}(range(1, 10, length=10)),
Dim{:lat}(range(1, 5, length=15))
)
data1 = rand(20, 10, 15)
ds1 = YAXArray(axlist, data1)
data2 = rand(20, 10, 15)
ds2 = YAXArray(axlist, data2)
Now we can concatenate ds1
and ds2
:
How do I subset a Cube?
Let's start by creating a dummy cube. Define the time span of the cube
using Dates
t = Date("2020-01-01"):Month(1):Date("2022-12-31")
Date("2020-01-01"):Dates.Month(1):Date("2022-12-01")
create cube axes
axes = (Dim{:Lon}(1:10), Dim{:Lat}(1:10), Dim{:Time}(t))
↓ Lon 1:10,
→ Lat 1:10,
↗ Time Date("2020-01-01"):Dates.Month(1):Date("2022-12-01")
assign values to a cube
Now we subset the cube by any dimension.
Subset cube by years
Subset cube by a specific date and date range
Subset cube by longitude and latitude
How do I apply map algebra?
Our next step is map algebra computations. This can be done effectively using the 'map' function. For example:
Multiplying cubes with only spatio-temporal dimensions
Cubes with more than 3 dimensions
To add some complexity, we will multiply each value for π and then divided for the sum of each time step. We will use the ds1
cube for this purpose.
How do I use the CubeTable function?
The function "CubeTable" creates an iterable table and the result is a DataCube. It is therefore very handy for grouping data and computing statistics by class. It uses OnlineStats.jl
to calculate statistics, and weighted statistics can be calculated as well.
Here we will use the ds1
Cube defined previously and we create a mask for data classification.
Cube containing a mask with classes 1, 2 and 3.
using GLMakie
GLMakie.activate!()
# This is how our classification map looks like
fig, ax, obj = heatmap(classes;
colormap=Makie.Categorical(cgrad([:grey15, :orangered, :snow3])))
cbar = Colorbar(fig[1,2], obj)
fig
Now we define the input cubes that will be considered for the iterable table
t = CubeTable(values=ds1, classes=classes)
Datacube iterator with 1 subtables with fields: (:values, :classes, :time, :lon, :lat)
using DataFrames
using OnlineStats
## visualization of the CubeTable
c_tbl = DataFrame(t[1])
first(c_tbl, 5)
Row | values | classes | time | lon | lat |
---|---|---|---|---|---|
Float64 | Int64 | Float64 | Float64 | Float64 | |
1 | 0.265912 | 2 | 1.0 | 1.0 | 1.0 |
2 | 0.913508 | 2 | 2.0 | 1.0 | 1.0 |
3 | 0.145011 | 2 | 3.0 | 1.0 | 1.0 |
4 | 0.45093 | 2 | 4.0 | 1.0 | 1.0 |
5 | 0.368653 | 2 | 5.0 | 1.0 | 1.0 |
In this line we calculate the Mean
for each class
We can also use more than one criteria for grouping the values. In the next example, the mean is calculated for each class and timestep.
How do I assing variable names to YAXArrays
in a Dataset
One variable name
Multiple variable names
keylist = (:a, :b, :c)
varlist = (YAXArray(rand(10)), YAXArray(rand(10,5)), YAXArray(rand(2,5)))