Indexing, subsetting and selectors
All these operations are done via DimensionalData.jl
.
using YAXArrays, Dates
Define a toy cube
julia> t = Date("2020-01-01"):Month(1):Date("2022-12-31")
Date("2020-01-01"):Dates.Month(1):Date("2022-12-01")
julia> axes = (Dim{:lon}(-9:10), Dim{:lat}(-5:15), Dim{:time}(t))
↓ lon -9:10,
→ lat -5:15,
↗ time Date("2020-01-01"):Dates.Month(1):Date("2022-12-01")
julia> c = YAXArray(axes, reshape(1:20*21*36, (20, 21, 36)))
╭────────────────────────────╮
│ 20×21×36 YAXArray{Int64,3} │
├────────────────────────────┴─────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} -9:10 ForwardOrdered Regular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} Date("2020-01-01"):Dates.Month(1):Date("2022-12-01") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 118.12 KB
└──────────────────────────────────────────────────────────────────────────────┘
A very convinient selector is lookup
, getting for example the values for lon
and time
.
lookup
lon = lookup(c, :lon)
Sampled{Int64} ForwardOrdered Regular DimensionalData.Dimensions.Lookups.Points
wrapping: -9:10
tempo = lookup(c, :time)
Sampled{Date} ForwardOrdered Regular DimensionalData.Dimensions.Lookups.Points
wrapping: Date("2020-01-01"):Dates.Month(1):Date("2022-12-01")
Selectors
At
value
julia> c[time = At(DateTime("2021-05-01"))]
╭─────────────────────────╮
│ 20×21 YAXArray{Int64,2} │
├─────────────────────────┴─────────────────────────── dims ┐
↓ lon Sampled{Int64} -9:10 ForwardOrdered Regular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points
├───────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├──────────────────────────────────────────────── file size ┤
file size: 3.28 KB
└───────────────────────────────────────────────────────────┘
At
vector of values
julia> c[time = At([DateTime("2021-05-01"), DateTime("2021-06-01")])]
╭───────────────────────────╮
│ 20×21×2 YAXArray{Int64,3} │
├───────────────────────────┴──────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} -9:10 ForwardOrdered Regular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} [Date("2021-05-01"), Date("2021-06-01")] ForwardOrdered Irregular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 6.56 KB
└──────────────────────────────────────────────────────────────────────────────┘
similarly for any of the spatial dimensions:
julia> c[lon = At([-9,-5])]
╭───────────────────────────╮
│ 2×21×36 YAXArray{Int64,3} │
├───────────────────────────┴──────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} [-9, -5] ForwardOrdered Irregular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} Date("2020-01-01"):Dates.Month(1):Date("2022-12-01") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 11.81 KB
└──────────────────────────────────────────────────────────────────────────────┘
At
values with tolerance (atol
, rtol
)
julia> c[lon = At([-10, 11]; atol = 1)]
╭───────────────────────────╮
│ 2×21×36 YAXArray{Int64,3} │
├───────────────────────────┴──────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} [-9, 10] ForwardOrdered Irregular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} Date("2020-01-01"):Dates.Month(1):Date("2022-12-01") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 11.81 KB
└──────────────────────────────────────────────────────────────────────────────┘
Subsetting
This is also done with selectors, see the following examples
Between
Altought a Between(a,b)
function is available in DimensionalData
, is recommended to use instead the a .. b
notation:
julia> c[lon = -9 .. -7] # close interval, all points included.
╭───────────────────────────╮
│ 3×21×36 YAXArray{Int64,3} │
├───────────────────────────┴──────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} -9:-7 ForwardOrdered Regular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} Date("2020-01-01"):Dates.Month(1):Date("2022-12-01") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 17.72 KB
└──────────────────────────────────────────────────────────────────────────────┘
More selectors from DimensionalData are available, such as Touches
, Near
, Where
and Contains
.
Open/Close Intervals
using IntervalSets
julia> c[lon = OpenInterval(-9, -7)]
╭───────────────────────────╮
│ 1×21×36 YAXArray{Int64,3} │
├───────────────────────────┴──────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} -8:-8 ForwardOrdered Regular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} Date("2020-01-01"):Dates.Month(1):Date("2022-12-01") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 5.91 KB
└──────────────────────────────────────────────────────────────────────────────┘
julia> c[lon = ClosedInterval(-9, -7)]
╭───────────────────────────╮
│ 3×21×36 YAXArray{Int64,3} │
├───────────────────────────┴──────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} -9:-7 ForwardOrdered Regular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} Date("2020-01-01"):Dates.Month(1):Date("2022-12-01") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 17.72 KB
└──────────────────────────────────────────────────────────────────────────────┘
julia> c[lon =Interval{:open,:closed}(-9,-7)]
╭───────────────────────────╮
│ 2×21×36 YAXArray{Int64,3} │
├───────────────────────────┴──────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} -8:-7 ForwardOrdered Regular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} Date("2020-01-01"):Dates.Month(1):Date("2022-12-01") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 11.81 KB
└──────────────────────────────────────────────────────────────────────────────┘
julia> c[lon =Interval{:closed,:open}(-9,-7)]
╭───────────────────────────╮
│ 2×21×36 YAXArray{Int64,3} │
├───────────────────────────┴──────────────────────────────────────────── dims ┐
↓ lon Sampled{Int64} -9:-8 ForwardOrdered Regular Points,
→ lat Sampled{Int64} -5:15 ForwardOrdered Regular Points,
↗ time Sampled{Date} Date("2020-01-01"):Dates.Month(1):Date("2022-12-01") ForwardOrdered Regular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
Dict{String, Any}()
├─────────────────────────────────────────────────────────────────── file size ┤
file size: 11.81 KB
└──────────────────────────────────────────────────────────────────────────────┘
See tutorials for use cases.