@@ -16,7 +16,7 @@ using YAXArrays: Cube, YAXArray, to_dataset, savedataset, setchunks, open_datase
1616using Zarr: Zarr, zcreate, zopen, writeattrs
1717using DimensionalData: DimensionalData as DD
1818using DimensionalData. Dimensions: XDim, YDim
19- using Extents: Extent, extent
19+ using Extents: Extent, extent, intersects
2020using FillArrays: Zeros
2121using Proj
2222using OffsetArrays
@@ -39,7 +39,7 @@ struct Pyramid{T,N,D,A,B<:DD.AbstractDimArray{T,N,D,A},L, Me} <: DD.AbstractDimA
3939 metadata:: Me
4040end
4141
42- function Pyramid (data:: DD.AbstractDimArray ; resampling_method= mean ∘ skipmissing, kwargs... )
42+ function Pyramid (data:: DD.AbstractDimArray ; resampling_method= mean ∘ skipmissing, kwargs... )
4343 pyrdata, pyraxs = getpyramids (resampling_method, data; kwargs... )
4444 levels = DD. DimArray .(pyrdata, pyraxs)
4545 meta = Dict (deepcopy (DD. metadata (data)))
@@ -103,7 +103,7 @@ function DD.modify(f, pyr::Pyramid)
103103 Pyramid (pbase, plevels, pyr. metadata)
104104end
105105Base. read (pyr:: Pyramid ) = DD. modify (Array, pyr)
106- @inline function DD. rebuild (A:: Pyramid , data, dims:: Tuple = dims (A), refdims= refdims (A), name= name (A))
106+ @inline function DD. rebuild (A:: Pyramid , data, dims:: Tuple = DD . dims (A), refdims= DD . refdims (A), name= DD . name (A))
107107 Pyramid (DD. rebuild (parent (A), data, dims, refdims, name, nothing ), A. levels, A. metadata)
108108end
109109
@@ -271,9 +271,20 @@ Compute the number of levels for the aggregation based on the size of `data`.
271271compute_nlevels (data, tilesize= 256 ) = max (0 ,ceil (Int,log2 (maximum (size (data))/ tilesize)))
272272
273273function agg_axis (d,n)
274- # TODO this might be problematic for explicitly set axes
275- DD. set (d, LinRange (first (d), last (d), cld (length (d), n)))
274+ # TODO this might be problematic for Reversed axes
275+ # TODO this is only correct for points not intervals
276+ npoints = cld (length (d), n)
277+ half_stepsize = step (d) * (n- 1 ) / 2
278+ @show half_stepsize
279+ sgn = DD. isreverse (d) ? - 1 : 1
280+ DD. set (d, LinRange (first (d) + sgn * half_stepsize, last (d) - sgn * half_stepsize, npoints))
276281end
282+ #=
283+ . .
284+ . <--- new point
285+ . .
286+ =#
287+
277288"""
278289 gen_output(t,s)
279290
@@ -326,7 +337,11 @@ function buildpyramids(path::AbstractString; resampling_method=mean, recursive=t
326337 # Build a loop for all variables in a dataset?
327338 org = Cube (path)
328339 # We run the method once to derive the output type
329- t = typeof (resampling_method (zeros (eltype (org), 2 ,2 )))
340+ # tfunc = typeof(resampling_method(zeros(eltype(org), 2,2)))
341+ # t = Missing <: eltype(org) ? Union{Missing, tfunc} : tfunc
342+
343+ t = Base. infer_return_type (resampling_method, (Matrix{nonmissingtype (eltype (org))},))
344+
330345 n_level = compute_nlevels (org)
331346 input_axes = filter (x-> x isa SpatialDim, DD. dims (org))
332347 if length (input_axes) != 2
@@ -377,16 +392,17 @@ end
377392Compute the data of the pyramids of a given data cube `ras`.
378393This returns the data of the pyramids and the dimension values of the aggregated axes.
379394"""
380- function getpyramids (reducefunc, ras;recursive= true )
395+ function getpyramids (reducefunc, ras;recursive= true , tilesize = 256 )
381396 input_axes = DD. dims (ras)
382- n_level = compute_nlevels (ras)
397+ n_level = compute_nlevels (ras, tilesize)
383398 if iszero (n_level)
384- @info " Array is smaller than the tilesize no pyramids are computed"
385- [ras], [dims (ras)]
399+ @info " Array is smaller than the tilesize no pyramidlevels are computed"
400+ [ras], [DD . dims (ras)]
386401 end
387402 pyramid_sizes = [ceil .(Int, size (ras) ./ 2 ^ i) for i in 1 : n_level]
388403 pyramid_axes = [agg_axis .(input_axes,2 ^ i) for i in 1 : n_level]
389- outtype = typeof (reducefunc (rand (eltype (ras),2 ,2 )))
404+ outtype = Base. infer_return_type (reducefunc, (Matrix{eltype (ras)},))
405+ # outtype = Missing <: eltype(ras) ? Union{Missing, outtypefunc} : outtypefunc
390406 outmin = output_arrays (pyramid_sizes, outtype)
391407 fill_pyramids (ras,outmin,reducefunc,recursive; threaded= true )
392408
@@ -402,6 +418,7 @@ Internal function to select the raster data that should be plotted on screen.
402418"""
403419function selectlevel (pyramid, ext;target_imsize= (1024 , 512 ))
404420 pyrext = extent (pyramid)
421+ intersects (pyrext, ext) || return zero (pyramid. levels[end ])
405422 basepixels = map (keys (pyrext)) do bb
406423 pyrspan = pyrext[bb][2 ] - pyrext[bb][1 ]
407424 imsize = ext[bb][2 ] - ext[bb][1 ]
@@ -473,5 +490,15 @@ function tms_json(pyramid)
473490 push! (tms, " orderedAxes" => pyramidaxes ())
474491 return tms
475492end
493+ function Base. cat (A1:: Pyramid , As:: Pyramid... ;dims)
494+ println (" Inside pyr cat" )
495+ @show typeof (levels .(As, 1 ))
496+ catlevels = [cat (A1. levels[i], levels .(As, i)... ; dims) for i in eachindex (A1. levels)]
497+ catbase = cat (parent (A1), parent .(As)... ; dims)
498+ Pyramid (catbase, catlevels, merge (DD. metadata (A1), DD. metadata .(As)... ))
499+ end
500+
501+
502+
476503include (" broadcast.jl" )
477504end
0 commit comments