You could use YAXArrays.jl for that, but I am not sure, whether this is pushing a square peg through a round hole.
using YAXArrays
function innerapplymask(xout, xin, xmask, outvec)
if only(xmask)
push!(outvec, deepcopy(xin))
return nothing
else
return nothing
end
end
function outerapplymask(cube, mask)
maskaxs = caxes(mask)
applyaxs = setdiff(caxes(cube), caxes(mask))
indims = InDims(applyaxs..., window_oob_value=-99, artype=YAXArray)
#inmask = InDims(MovingWindow.(maskaxs, 0,0)..., window_oob_value=0)
outdims=OutDims()
outvec = []
mapCube(innerapplymask, (cube, mask), outvec; indims=(indims, InDims()), outdims)
return outvec
end
foo=rand(10,10,100)
maskarr = falses(10,10)
maskarr[1,2] = true
maskarr[3,4] = true
outerapplymask(YAXArray(foo), YAXArray(maskarr))
This gives you a list of YAXArray but you could change the artype in the InDims
constructor to Array
to get a list of plain arrays.
For my example YAXArray with a Zarr backend of this size:
YAXArray with the following dimensions
Lon Axis with 465 Elements from 672639.15 to 686559.15
Lat Axis with 444 Elements from 9.45343066e6 to 9.44014066e6
Time Axis with 100 Elements from 2016-10-03T10:12:28 to 2020-02-09T10:12:44
Polarisation Axis with 2 elements: VH VV
name: layer
Total size: 157.52 MB
this takes
2.401743 seconds (2.15 M allocations: 859.379 MiB, 60.18% gc time)
compared to the list comprehension:
julia> @time [freqcube[ind.I...,:,1] for ind in CartesianIndices(mask.data) if mask.data[ind]]
1361.023080 seconds (3.35 M allocations: 1.973 TiB, 17.06% gc time, 0.01% compilation time)