-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code for realization of ZarrArray
- Loading branch information
Showing
3 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
### ========================================================================= | ||
### writeZarrArray() | ||
### ------------------------------------------------------------------------- | ||
### | ||
|
||
|
||
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
### ZarrRealizationSink objects | ||
### | ||
### The ZarrRealizationSink class is a concrete RealizationSink subclass that | ||
### implements an ZarrArray realization sink. | ||
### | ||
|
||
setClass("ZarrRealizationSink", | ||
contains = "RealizationSink", | ||
representation( | ||
## Slots that support the RealizationSink constructor contract. | ||
dim = "integer", | ||
type = "character", | ||
## Other slots. | ||
zarr_array_path = "character", # Single string. | ||
chunk_dim = "integer" # An integer vector parallel to the 'dim' slot | ||
) | ||
) | ||
|
||
#' @export | ||
setMethod("type", "ZarrRealizationSink", function(x) x@type) | ||
|
||
#' @export | ||
setMethod("chunkdim", "ZarrRealizationSink", function(x) x@chunk_dim) | ||
|
||
|
||
ZarrRealizationSink <- function(zarr_array_path = NULL, | ||
dim, | ||
type="double", | ||
chunkdim = NULL) { | ||
|
||
if (is.null(zarr_array_path)) { | ||
stop('must provide a path') | ||
} else { | ||
zarr_array_path <- .normalize_array_path(zarr_array_path) | ||
} | ||
|
||
if (is.null(chunkdim)) { | ||
stop('must provide chunk dimensions') | ||
} else { | ||
chunkdim <- as.integer(chunkdim) | ||
} | ||
|
||
create_empty_zarr_array(zarr_array_path, dim = dim, chunk_dim = chunkdim, | ||
data_type = type) | ||
|
||
new("ZarrRealizationSink", | ||
dim = dim, | ||
type = type, | ||
zarr_array_path = zarr_array_path, | ||
chunk_dim = chunkdim) | ||
} | ||
|
||
setMethod("write_block", "ZarrRealizationSink", function(sink, viewport, block) { | ||
|
||
starts <- start(viewport) - 1L | ||
index <- lapply(width(viewport), seq_len) | ||
index <- mapply(FUN="+", starts, index, SIMPLIFY=FALSE) | ||
|
||
update_zarr_array(sink@zarr_array_path, x = block, index = index) | ||
sink | ||
}) | ||
|
||
#' @export | ||
writeZarrArray <- function(x, zarr_array_path, | ||
chunk_dim = NULL) { | ||
|
||
sink <- ZarrRealizationSink(zarr_array_path = zarr_array_path, | ||
dim = dim(x), type = type(x), | ||
chunkdim = chunk_dim) | ||
sink <- BLOCK_write_to_sink(sink, x) | ||
as(sink, "ZarrArray") | ||
} | ||
|
||
setAs("ZarrRealizationSink", "ZarrArraySeed", | ||
function(from) ZarrArraySeed(from@zarr_array_path) | ||
) | ||
|
||
setAs("ZarrRealizationSink", "ZarrArray", | ||
function(from) DelayedArray(as(from, "ZarrArraySeed")) | ||
) | ||
|
||
setAs("ZarrRealizationSink", "DelayedArray", | ||
function(from) DelayedArray(as(from, "ZarrArraySeed")) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters