rumi

R

Write Rumi files from R arrays, or read selected windows, bands and time steps. The reader pairs each source, a path or the file's bytes, with its external header.

Install

install.packages("rumi", repos = "https://asterisk-labs.r-universe.dev")

# or from GitHub
remotes::install_github("asterisk-labs/rumi", subdir = "bindings/r")

The package compresses frames with the GeoZL it reads them with, so no other package is needed to write. A source install builds the bundled Rumi, GeoZL, OpenZL and Karu code with CMake 3.21 or newer. It needs C11 and C++23 compilers, and pkg-config to find libcurl 7.83 or newer and OpenSSL 3.0 or newer. No separate Rumi installation is needed.

library(rumi)

header <- readBin("scene.header", "raw", file.info("scene.header")$size)
chip <- rumi_read("scene.rumi", header, time = 1:3, bands = c(4, 8),
                  window = c(1, 1, 256, 256))
dim(chip)                                    # 256 256 2 3

Functions

Bad arguments stop with an R error, and native failures signal a classed condition, as Errors describes.

rumi_info

rumi_info(source = NULL, header = NULL)

Inspects a source, an external header or both, and returns a rumi_metadata list. A path or raw vector alone is read and its canonical external header rebuilt in header. A header alone opens nothing, so bands, time, time_kind, transform, crs and pixel_is_point are NULL. Given both, Rumi checks that they match.

sourceA path, URI or VSI path, the file's bytes as a raw vector, or NULL. A leading ~ is expanded.
headerThe external header as a raw vector, or NULL. At least one of the two is required.

The list holds these fields.

shapeRumi's canonical (Band, Y, X) for an Image or (Time, Band, Y, X) for a Cube.
time_countNumber of time steps, 1 for an Image.
dtypeThe sample type, such as "uint16".
tileTile height and width.
frame_layoutThe axes of one decoded frame, such as "b h w".
index_orderThe band and time axes the frame index walks, outermost first.
framesNumber of frames.
bandsBand descriptions.
timeDate values when every coordinate is a whole UTC day, and UTC POSIXct values otherwise. Intervals are a matrix with the columns start and end.
time_kind"instant", "interval" or NULL.
transform, crsThe six affine coefficients and the EPSG code, or NULL when the file has no CRS.
pixel_is_pointTRUE for a PixelIsPoint raster, FALSE for PixelIsArea.
headerThe header bytes.
info <- rumi_info("scene.rumi", header)
info$shape                                   # 2 3 6 8
info$bands                                   # "B02" "B03" "B04"

header <- rumi_info("scene.rumi")$header    # rebuild a lost header
rumi_info(header = header)$bands             # NULL

rumi_read

rumi_read(source, header, time = NULL, bands = NULL,
    window = NULL, pattern = NULL)

Reads a window and a selection of time steps and bands. Only the frames the read needs are fetched.

sourceA path, URI or VSI path, or the file's bytes as a raw vector. A leading ~ is expanded.
headerThe external header as a raw vector, as readBin() returns it.
timeWhole one-based time indices in output order, or NULL for all.
bandsWhole one-based band indices in output order, or NULL for all.
windowc(row, column, height, width) with a one-based origin, or NULL for the whole image. It must lie inside the image.
patternAn output pattern that arranges the result, as in Arrays. NULL keeps the native order.

Returns an array shaped (X, Y, Band) for an Image or (X, Y, Band, Time) for a Cube. A Cube keeps its time dimension when one step is selected.

Every read is stateless and uses the header alone, as Python's rumi.read does, so it fetches only the frames it needs. It does not read the index and trailer, so the result has no band or time dimnames and its rumi_metadata attribute holds only the header's fields. rumi_info(source, header) returns the band labels and time, and checks that the header belongs to the file.

chip <- rumi_read("scene.rumi", header, time = c(2, 1), bands = c(3, 1),
                  window = c(2, 3, 3, 4))
dim(chip)                                    # 4 3 2 2

band <- rumi_read("scene.rumi", header, bands = 1)
bytes <- readBin("scene.rumi", "raw", file.info("scene.rumi")$size)
same <- rumi_read(bytes, header, bands = 1)

rumi_read_many

rumi_read_many(sources, headers, windows = NULL, time = NULL,
    bands = NULL, pattern = NULL)

Reads one window from each of several sources in one call and stacks them along a last n dimension, as a training batch needs.

sourcesA list or character vector of paths and raw vectors.
headersA list with one raw external header per source. Each source is read with its header alone, as in rumi_read.
windowsOne c(row, column, height, width) per source, as a list or as the rows of a matrix. All windows have the same height and width. A NULL entry reads that whole image, and NULL reads every whole image, which must then share a size.
othersAs in rumi_read, applied to every source.

Returns (X, Y, Band, n), or (X, Y, Band, Time, n) when the first source is a Cube. Like rumi_read, the result has no band or time dimnames.

batch <- rumi_read_many(list("scene.rumi", "scene.rumi"), list(header, header),
                        windows = list(c(1, 1, 2, 3), c(3, 4, 2, 3)),
                        bands = 2)
dim(batch)                                   # 3 2 1 2 2

batch <- rumi_read_many(c("a.rumi", "b.rumi"), list(header_a, header_b),
                        windows = rbind(c(1, 1, 256, 256), c(257, 1, 256, 256)))

rumi_frames

rumi_frames(x, pattern, tile_size = 512L, dtype = NULL)

Cuts an array into frames in file order, clipping edge tiles. Compress each frame with rumi_geozl_graph and rumi_geozl_compress before writing.

xAn array of samples. It holds the pattern's input axes in reverse, so "b (row h) (col w)" takes (X, Y, Band), the shape rumi_read returns.
patternThe frame pattern Python takes, in C order, such as "b (row h) (col w) -> row col b (h w)".
tile_sizeHeight and width of a square tile, from 1 to 65535.
dtypeThe sample type the payloads are compressed as, such as "uint16". NULL takes "int32" for an integer array, "float64" for double, "uint8" for raw, "bool" for logical and "complex128" for complex.

The returned rumi_frames list has data, compressed and layout fields. Fill each compressed entry with raw bytes. A b h w frame is an R array shaped (w, h, band), and every frame carries dtype as its rumi_dtype attribute, so rumi_geozl_graph compresses it as that type.

data, compressedOne array per frame in file order, and a list of NULL to fill with payloads.
layout, frame_unitThe axes of one frame, such as "h w", and the code the file records for them.
image_width, image_length, tile_sizeThe image size and the tile size.
bands, time_countThe number of bands and time steps.
dtype, patternThe sample type and the frame pattern text.
tiles_across, tiles_downThe tile grid.
row, col, band, timeOne-based integer positions, one per frame. band and time are NULL unless the frames are split by that axis.
frames <- rumi_frames(image, "b (row h) (col w) -> row col b (h w)",
                      tile_size = 256, dtype = "uint16")
for (i in seq_along(frames$data)) {
  tile <- frames$data[[i]]
  graph <- rumi_geozl_graph(tile, "planar>zigzag>pfor")
  frames$compressed[[i]] <- rumi_geozl_compress(tile, graph)
}
frames$band[1:3]                             # 1 2 3

rumi_frame_start

rumi_frame_start(shape, pattern, tile_size = 512L)

Returns the byte offset of the first frame without cutting an array. It is also the size of everything a file stores before its first frame, and it matches Python's rumi.frame_start.

shapeThe dimensions of the array rumi_frames would cut, in dim() order. That is the reverse of the pattern's input axes, so "b (row h) (col w)" takes c(X, Y, Band).
pattern, tile_sizeAs in rumi_frames.

Returns the offset in bytes as a double.

rumi_frame_start(c(70, 40, 2), "b (row h) (col w) -> row col b (h w)", 16)  # 812

rumi_geozl_graph, rumi_geozl_compress

rumi_geozl_graph(x, method, width = NULL, planes = NULL, error = NULL,
    nodata = NULL, dtype = NULL)
rumi_geozl_compress(x, graph)

Compress frames with the GeoZL linked into Rumi. These calls build a graph and return compressed bytes for rumi_write. rumi_geozl_* covers graph and compress only. Decompress, profile and coeffs live in the geozl R package, which reads the same frames.

xSamples as a double, integer, logical or raw vector or array, converted to dtype. A raw vector is taken as native samples already. For a bounded error the graph's x also fixes the domain later tiles must fall in.
methodA recipe such as "planar>zigzag>pfor".
width, planesRow width in samples and the number of stacked images. They default to dim(x)[1] and, for three or more dimensions, the last one, which is the planes of a b h w frame.
errorNULL or 0 for lossless, a positive number for an absolute bound, a percentage such as "1%" for a relative one, or a full LINEAR, LOG or SQRT recipe.
nodataA sentinel value. NA and NaN in x are stored as it, and NaN selects NaN handling, which float rasters with missing values get without asking.
dtypeThe storage type, as rumi_frames takes it, or terra's "INT2U" and GDAL's "UInt16" names. A frame from rumi_frames uses its own dtype and refuses another. Otherwise the default follows the type of x.

The graph is a rumi_geozl_graph list with the fields method, width, planes, dtype, itemsize, error and nodata, where itemsize is the bytes per sample. print shows them as geozl's geozl_graph does, for example <rumi_geozl_graph> planar>zigzag>pfor, uint16, row width 48, 1 plane, lossless. rumi_geozl_compress returns the frame as a raw vector for a compressed entry. A graph wraps native state, so its compress calls take turns, and it does not survive saving and reloading.

A bad argument stops with a plain error and a NULL call. R checks what the core cannot see, such as a logical error or a nodata outside the dtype, in the words of the geozl R package, with rumi_geozl_ in place of geozl_ where it names a call. The core checks the error text, the plane split and the dtype in words that follow the geozl packages. A refusal by the native library follows the Errors rule, and names the call and the method, as in GeoZL graph failed (method = "m").

rumi_write

rumi_write(path, frames, bands, time, transform = NULL, crs = NULL,
    pixel_is_point = FALSE)

Writes the compressed frames to a local file.

pathOne local path. A leading ~ is expanded, and an existing file is replaced.
framesA rumi_frames list with a non-empty raw payload in every compressed entry. A missing payload stops with an error that names the first one.
bandsOne unique, non-empty text per band, in band order, such as "B4, Red, 665nm".
timeOne entry per time step. An instant is a Date, a POSIXct or an ISO string such as "2025-08-08". Steps that cover an interval take a two-column matrix of starts and ends, or a list of start and end pairs. A Date marks midnight UTC.
transformSix affine coefficients in rasterio order, (x_res, row_rot, x_origin, col_rot, y_res, y_origin). Give it together with crs.
crsAn EPSG code from 1 to 4294967295, such as 32631 or "EPSG:32631", or an sf crs object, whose crs$epsg is used. Zero is refused.
pixel_is_pointTRUE when coordinates refer to pixel centres. The default refers to pixel corners.

Returns the external header as raw bytes. Keep it with the path, for example with writeBin(). A failed write removes the partial file.

header <- rumi_write("scene.rumi", frames, bands = c("B02", "B03", "B04", "B08"),
                     time = "2025-08-08",
                     transform = c(10, 0, 500000, 0, -10, 8000000), crs = 32618)
writeBin(header, "scene.header")

rumi_set_num_threads, rumi_set_checksum_verification

rumi_set_num_threads(n)
rumi_get_num_threads()
rumi_set_checksum_verification(on)
rumi_get_checksum_verification()

Set and read the process-wide read thread count and OpenZL checksum verification, as Python's calls of the same names do. Set them before the first read. The first parallel read fixes the thread count and the first decoded frame fixes checksum verification. A later change to another value signals a rumi_error_state condition.

nA whole number of threads from 1 to 1024.
onTRUE to verify checksums, FALSE to skip them.

The setters return the accepted value invisibly. The getters return an integer and a logical.

rumi_set_num_threads(8)
rumi_get_num_threads()                       # 8

Errors

A bad argument, or a request the native library calls invalid, stops with a plain R error. Every other native failure signals a condition of class c("rumi_error_<kind>", "rumi_error", "error", "condition") with the fields message, call, which is NULL, and kind. The kind is one of "io", "parse", "format", "decode", "oom", "unsupported", "state" and "internal", the same names Python's RumiError.kind and Julia's RumiError carry.

chip <- tryCatch(
  rumi_read("s3://bucket/scene.rumi", header, bands = 1),
  rumi_error_io = function(e) NULL           # retry later
)

Arrays

R is column-major, so the axes run in reverse of Python. An Image reads as (X, Y, Band) and a Cube as (X, Y, Band, Time), with the same memory as Python's (Band, Y, X) and (Time, Band, Y, X). Indices and window origins are one-based, and window keeps the (row, column, height, width) order.

The result carries dimnames for bands and time steps, a rumi_axes attribute naming each dimension and a rumi_metadata attribute with the file's metadata. Time names are dates when every coordinate is a whole UTC day and UTC timestamps otherwise, and an interval is written as start/end. A read from a path or raw vector has the header's metadata alone, so it has no band or time names. rumi_read_many names its last dimension n.

A pattern uses the axes n, t, b, y and x as in Python, and R gets them in reverse. "b x y" gives a (Y, X, Band) array, the order of terra::as.array(). An axis left out must have length one after selection. A result read with a pattern has no dimnames and no Rumi attributes.

A frame pattern in rumi_frames reads the same way. "b (row h) (col w)" takes an (X, Y, Band) array, so an array from terra::as.array() needs aperm(x, c(2, 1, 3)) first.

Sample types

bool reads as logical, and uint8, int8, uint16 and int16 as integer. complex32, complex64 and complex128 read as complex. The other integer and float types read as double, and float64 decodes straight into the R array. 64-bit integers above 253 lose precision and raise a warning.

R has no 8-bit float, so the five float8 types return their native bytes as a raw array of class rumi_float8_array.

What it covers

The R binding writes and reads on the CPU. It has no GPU read.

Reads use one decode thread and skip OpenZL checksums unless rumi_set_num_threads and rumi_set_checksum_verification change them, or RUMI_NUM_THREADS or RUMI_VERIFY=1 is set before the first read. RUMI_NUM_THREADS takes a count or ALL_CPUS.