Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DiskArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ include("cat.jl")
include("generator.jl")
include("zip.jl")
include("show.jl")
include("chunktiledarray.jl")
include("cached.jl")
include("pad.jl")

Expand Down
31 changes: 2 additions & 29 deletions src/cached.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
import Mmap

"""
ChunkTiledDiskArray <: AbstractDiskArray

And abstract supertype for disk arrays that have fast indexing
of tiled chunks already stored as separate arrays, such as [`CachedDiskArray`](@ref).
"""
abstract type ChunkTiledDiskArray{T,N} <: AbstractDiskArray{T,N} end

Base.size(a::ChunkTiledDiskArray) = arraysize_from_chunksize.(eachchunk(a).chunks)

function readblock!(A::ChunkTiledDiskArray{T,N}, data, I...) where {T,N}
chunks = eachchunk(A)
chunk_indices = findchunk.(chunks.chunks, I)
data_offset = OffsetArray(data, map(i -> first(i) - 1, I)...)
foreach(CartesianIndices(chunk_indices)) do ci
chunkindex = ChunkIndex(ci; offset=true)
chunk = A[chunkindex]
# Find the overlapping indices
inner_indices = map(axes(chunk), axes(data_offset)) do ax1, ax2
max(first(ax1), first(ax2)):min(last(ax1), last(ax2))
end
for ii in CartesianIndices(inner_indices)
data_offset[ii] = chunk[ii]
end
end
end

"""
CachedDiskArray <: ChunkTiledDiskArray
CachedDiskArray <: AbstractChunkTiledDiskArray

CachedDiskArray(A::AbstractArray; maxsize=1000, mmap=false)

Expand All @@ -40,7 +13,7 @@ to temproray files.

Can also be called with `cache`, which can be extended for wrapper array types.
"""
struct CachedDiskArray{T,N,A<:AbstractArray{T,N},C} <: ChunkTiledDiskArray{T,N}
struct CachedDiskArray{T,N,A<:AbstractArray{T,N},C} <: AbstractChunkTiledDiskArray{T,N}
parent::A
cache::C
mmap::Bool
Expand Down
60 changes: 60 additions & 0 deletions src/chunktiledarray.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
AbstractChunkTiledDiskArray <: AbstractDiskArray

An abstract supertype for disk arrays that have fast indexing
of tiled chunks already stored as separate arrays, such as [`CachedDiskArray`](@ref).
"""
abstract type AbstractChunkTiledDiskArray{T,N} <: AbstractDiskArray{T,N} end

Base.size(a::AbstractChunkTiledDiskArray) = arraysize_from_chunksize.(eachchunk(a).chunks)

function readblock!(A::AbstractChunkTiledDiskArray{T,N}, data, I...) where {T,N}
chunks = eachchunk(A)
chunk_indices = findchunk.(chunks.chunks, I)
data_offset = OffsetArray(data, map(i -> first(i) - 1, I)...)
foreach(CartesianIndices(chunk_indices)) do ci
@show ci
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

chunkindex = ChunkIndex(ci; offset=true)
@show chunkindex
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

chunk = A[chunkindex]
# Find the overlapping indices
inner_indices = map(axes(chunk), axes(data_offset)) do ax1, ax2
max(first(ax1), first(ax2)):min(last(ax1), last(ax2))
end
for ii in CartesianIndices(inner_indices)
data_offset[ii] = chunk[ii]
end
end
end

"""
TiledDiskArray <: AbstractChunkTiledDiskArray

Construct an array from a collection of tiles.
This needs a function to find the tile given a tile position and the overall size of the array.
"""
struct TiledDiskArray{T,N} <: AbstractChunkTiledDiskArray{T,N}
tilefunction
tilenum
tilesizes
Comment on lines +37 to +39
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use this definition instead:

struct TiledDiskArray{T,N,F} <: AbstractChunkTiledDiskArray{T,N}
    tilefunction::F
    tilenum::NTuple{N,Int}
    tilesizes::NTuple{N,Int}
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth having the tile definition be GridChunks instead - for example the Copernicus DEM has irregular sizes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a link to that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is tilesizes just tilesize? What does the pluralisation mean?

end


Base.size(A::TiledDiskArray) = A.tilenum .* A.tilesizes
eachchunk(A::TiledDiskArray) = DiskArrays.GridChunks(size(A), A.tilesizes)
haschunks(A::TiledDiskArray) = Chunked()

function Base.getindex(A::TiledDiskArray, i::ChunkIndex{N,OffsetChunks}) where {N}
tile = _getchunk(A,i)
inds = eachchunk(A)[i.I]
wrapchunk(tile, inds)
end

Base.getindex(A::TiledDiskArray, i::ChunkIndex{N,OneBasedChunks}) where {N} =
_getchunk(A, i)



function _getchunk(A::TiledDiskArray, i::ChunkIndex)
A.tilefunction(i.I.I...)
end
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1157,3 +1157,9 @@ end
@test DiskArrays.haschunks(a_chunked_2) isa DiskArrays.Chunked
@test size(DiskArrays.eachchunk(a_chunked_2)) == (5,5)
end

@testset "TiledChunkArray" begin
tiles = [fill(10x+y, 10,10) for x in 1:9, y in 1:9]
a = DiskArrays.TiledDiskArray((x,y) -> tiles[x,y], (90,90))
@test a[1,1] == 11
end
Loading