Skip to content

Commit e68e036

Browse files
authored
Merge pull request #548 from JuliaDataCubes/la/xmap_docs
2 parents ace0daa + 40ed924 commit e68e036

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/DAT/xmap.jl

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,39 @@ function DD._group_indices(dim::DD.Dimension, ::Whole; labels=nothing)
1717
look = DD.lookup(DD.format(DD.rebuild(dim,[first(dim) .. last(dim)])))
1818
DD.rebuild(dim,look), [1:length(dim)]
1919
end
20+
21+
"""
22+
⊘(a, b)
23+
24+
A convenience operator, used as `a ⊘ b`, for creating a windowed view of a `DimArrayOrStack` where one or more dimensions are treated as a single, complete window. This is equivalent to calling `windows(a, b => Whole())`.
25+
26+
This operator is particularly useful with `xmap`, where it specifies that a function should be applied over an entire dimension (or multiple dimensions) at once. For example, `xmap(mean, a ⊘ :time)` will compute the mean over the entire `:time` dimension.
27+
28+
# How to type
29+
30+
The `⊘` symbol can be typed in the Julia REPL or compatible editors by typing `\\oslash` and then pressing the Tab key.
31+
32+
# Arguments
33+
- `a`: A `YAXArray` or other `DimArrayOrStack`.
34+
- `b`: A `Symbol` or a `Tuple` of `Symbol`s representing the dimension(s) to be treated as a single window.
35+
36+
# Returns
37+
A `DimWindowArray` which can be passed to `xmap` for windowed processing.
38+
39+
# Examples
40+
```julia
41+
using YAXArrays, Dates, Statistics
42+
using YAXArrays: YAXArrays as YAX
43+
44+
a = YAXArray((YAX.time(1:5), lon(1:3)), rand(5,3))
45+
46+
# Create a windowed view where the `:time` dimension is a single window
47+
w = a ⊘ :time
48+
49+
# Use this view with xmap to calculate the mean over the time dimension.
50+
time_mean = xmap(mean, w, inplace=false)
51+
```
52+
"""
2053
(a,b::Tuple) = windows(a,map(Base.Fix2(Pair,Whole()),map(Symbol,b))...)
2154
(a,b) = (a,(b,))
2255
windows(A::DimArrayOrStack) = DimWindowArray(A,DD.dims(A),map(d->1:length(d),DD.dims(A)),DD.dims(A))
@@ -306,6 +339,72 @@ function xmap(f, ars::Union{YAXArrays.Cubes.YAXArray,DimWindowArray}...;
306339
end
307340
end
308341

342+
"""
343+
xmap(f, arrays::Union{YAXArray,DimWindowArray}...; output=XOutput(), inplace=default_inplace(f),
344+
function_args=(), function_kwargs=())
345+
346+
Apply a function `f` across multiple YAXArrays or DimWindowArrays, with support for windowed operations and
347+
dimension-aware broadcasting.
348+
349+
# Arguments
350+
- `f`: Function to apply. Can be a regular function or an `XFunction`
351+
- `arrays`: One or more YAXArrays or DimWindowArrays to operate on
352+
353+
# Keywords
354+
- `output`: Single `XOutput` or tuple of `XOutput` specifying output dimensions and properties
355+
- `inplace`: Whether the function `f` modifies its first argument (default depends on `f`)
356+
- `function_args`: Additional positional arguments to pass to `f`
357+
- `function_kwargs`: Additional keyword arguments to pass to `f`
358+
359+
# Returns
360+
- A single YAXArray if `output` is a single `XOutput`
361+
- A tuple of YAXArrays if `output` is a tuple of `XOutput`s
362+
363+
# Examples
364+
```julia
365+
using YAXArrays, Dates
366+
using YAXArrays: YAXArrays as YAX
367+
368+
# Create example arrays
369+
axlist = (
370+
YAX.time(Date("2022-01-01"):Day(1):Date("2022-01-05")),
371+
lon(1:3),
372+
lat(1:2)
373+
)
374+
array1 = YAXArray(axlist, rand(5,3,2))
375+
array2 = YAXArray(axlist, rand(5,3,2))
376+
377+
# Element-wise arithmetic using broadcasting
378+
result = array1 .+ array2
379+
380+
# Custom function with multiple outputs
381+
function myfunc(xout1, xout2, x, y)
382+
xout1 = x .+ y # Broadcasting with dot operator
383+
xout2 = x .* y # Broadcasting with dot operator
384+
return nothing
385+
end
386+
387+
out1 = XOutput(array1.time)
388+
out2 = XOutput(array1.time)
389+
sum_arr, prod_arr = xmap(myfunc, array1⊘:time, array2⊘:time,
390+
output=(out1, out2), inplace=true)
391+
392+
# Access computed values
393+
computed_sum = sum_arr[:,:,:] # Now works correctly
394+
computed_prod = prod_arr[:,:,:] # Now works correctly
395+
396+
# Time series operation
397+
using Statistics
398+
time_mean = xmap(Statistics.mean, array1⊘:time,
399+
output=XOutput(), inplace=false)
400+
401+
# Save result directly to disk
402+
ds = Dataset(result=time_mean)
403+
compute_to_zarr(ds, "output.zarr")
404+
```
405+
"""
406+
function xmap end
407+
309408
import Base.mapslices
310409
function mapslices(f, d::YAXArray, addargs...; dims, kwargs...)
311410
!isa(dims, Tuple) && (dims = (dims,))

0 commit comments

Comments
 (0)