Read chips and queue band reads#

For a single pixel extent, follow Read and resize a raster window. For GPU batches or queued band reads, install the native codec feature and check the file’s supported profile. JPEG2000 requires nvjpeg2k; the Deflate TIFF batch route requires nvcomp.

Read batches of chips#

For fixed-size GPU chips, use iter_chips and consume each batch’s NCHW CuPy view. Views can retain a larger backing allocation; close the iterator when stopping early. Use read_many for custom windows and reusable outputs. The batching guide states budgets, ordering, retained-output lifetime and codec-specific limits. The runnable JPEG2000 chip example and Deflate TIFF chip example show application handoff. Model preprocessing and inference remain caller-owned.

Submit SAFE bands without waiting between reads#

Call submit on each band reader before completing the pending reads. Readers opened from one SAFE catalog share a queue with one host submitting thread per CUDA context, pool, and decoder-slot setting. The queue schedules individual JPEG2000 tiles and can start the next band while the preceding band finishes.

from contextlib import ExitStack
import cupy as cp
import cugeo.raster

with ExitStack() as owners:
    product = owners.enter_context(cugeo.raster.open("product.SAFE"))
    stream = owners.enter_context(cp.cuda.Stream(non_blocking=True))
    outputs, pending = {}, {}
    for name in ("B02", "B03", "B04", "B08"):
        band = owners.enter_context(product.open_band(name, resolution_metres=10))
        with stream:
            outputs[name] = cp.empty((band.height, band.width), dtype=cp.uint16)
        submitted = band.submit(out=outputs[name], stream=stream)
        if submitted is None:
            raise RuntimeError("band read queue is full")
        pending[name] = owners.enter_context(submitted)

    # CPU work that does not access these outputs can run here.
    for name, read in pending.items():
        result, receipt = read.complete()
        assert result is outputs[name]
        assert receipt.fallback is False

submit accepts a full native-resolution, single-component UInt8/UInt16 JPEG2000 band and caller-owned rank-two CUDA output with the source dtype. Row padding is accepted; addressed output spans must not overlap unfinished reads in the same catalog and CUDA context, including across pool and slot settings. This non-overlap is a caller precondition without runtime alias admission. It performs no overview selection, resampling, or radiometric conversion. Preparation may read compressed bytes and allocate on the calling thread. Producer events are recorded before return, so decode waits for writes already enqueued on the advertised producer and explicit caller streams without waiting on the calling thread.

decoder_slots="auto" uses the selected native pool’s state capacity; an explicit positive count cannot exceed it. jpeg2000_pool selects an explicit pool. The queue admits at most one pending band per decoder slot. Busy returns None without submitting the read; complete an earlier pending read before retrying. Standalone JPEG2000 readers own their own queue group.

After an execution error, observe and close the failed pending read. A later submission retires the failed queue and prepares a fresh one. If cleanup still fails, submission reports a coded error and retains the owners needed for safe cleanup; it does not start another decode in that queue.

query() checks readiness. complete() returns the output and its DatasetReadReceipt. Use complete()[0] when you only need the output. Errors propagate through query(), complete(), and close(). Closing a pending read waits for decoding to finish. Pending reads retain their output and native bindings after band readers or the catalog close. Keep the caller stream alive through pending close. The borrowed CUDA context must outlive the catalog, its readers, and their pending reads, because the shared queue caches context-bound resources. Close these owners before shutting down their native pools or CUDA contexts. Use ordinary read for windows or resampling. Request return_receipt=True when you need a completed read and its receipt.