Icechunk and virtual stores¶
These need the cloud extra (pip install "disp_xr[cloud]").
Write and read DISP datasets in Icechunk repositories (cloud extra).
An Icechunk repository is a versioned Zarr v3 store: every write is a commit
with a snapshot id, and tags name snapshots that must not move (a Cal/Val
run). to_icechunk wraps :func:icechunk.xarray.to_icechunk, which, unlike
ds.to_zarr on a session store, keeps writes made under dask.distributed or
multiprocessing.
Provenance (disp-xr and package versions, product, frame, input granules and
the history of operations) goes in the commit metadata as well as in the
dataset attributes. A stack built by appending one ministack per commit uses
the "ministack" chunk preset, so an append writes new shards instead of
rewriting old ones. Library code never amends a commit or expires snapshots.
to_icechunk ¶
to_icechunk(ds: Dataset, repo: RepoLike, *, branch: str = 'main', message: Optional[str] = None, append_dim: Optional[str] = None, tag: Optional[str] = None, preset: Optional[Preset] = None, mode: Literal['w-', 'w'] = 'w-', keep_bits: Optional[int] = None, compression_level: int = 3, target_mb: Optional[float] = None) -> str
Write ds to an Icechunk repository as one commit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
DISP dataset with |
required |
repo
|
(Repository, str or Path)
|
Repository, or a local directory (created if missing). |
required |
branch
|
str
|
Branch to commit to. A missing branch is created from |
'main'
|
message
|
str
|
Commit message; defaults to a summary of the write. |
None
|
append_dim
|
'time'
|
Append along time. The dataset must be on the store's grid, with the
same variables and dtypes, and start after the store's last epoch. On
an empty branch this creates the store with the |
"time"
|
tag
|
str
|
Tag the new snapshot (for example a Cal/Val run). Tags never move. |
None
|
preset
|
('timeseries', 'maps', 'ministack')
|
Chunk layout of a new store; ignored when appending to an existing
one. Defaults to |
"timeseries"
|
mode
|
('w-', 'w')
|
Without |
"w-"
|
keep_bits
|
Optional[int]
|
As in :func: |
None
|
compression_level
|
Optional[int]
|
As in :func: |
None
|
target_mb
|
Optional[int]
|
As in :func: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Snapshot id of the commit. Record it in downstream provenance;
:func: |
Examples:
open_icechunk ¶
open_icechunk(repo: RepoLike, *, branch: Optional[str] = None, tag: Optional[str] = None, snapshot_id: Optional[str] = None, chunks: Any = 'shards', mask_and_scale: bool = False, group: Optional[str] = None) -> xr.Dataset
Open a DISP dataset from an Icechunk repository, read-only.
Give at most one of branch (default "main", its latest commit),
tag or snapshot_id. The snapshot read is recorded in
attrs["source_snapshot_id"], which exports carry as provenance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo
|
(Repository, str or Path)
|
Repository, or a local directory holding one. |
required |
branch
|
str
|
Version to read. |
None
|
tag
|
str
|
Version to read. |
None
|
snapshot_id
|
str
|
Version to read. |
None
|
chunks
|
"shards" or any :func:`xarray.open_zarr` value
|
|
'shards'
|
mask_and_scale
|
bool
|
Decode fill values to NaN. Off by default, so integer layers keep
their dtype, as :func: |
False
|
group
|
str
|
Group to open instead of the root (for example one production of a
virtual frame, |
None
|
Examples:
Virtual stores¶
Virtual DISP frames: Icechunk stores that point at the granules (cloud extra).
A virtual store holds no pixels. Each layer chunk is a reference to a byte
range in a granule, read through VirtualiZarr's HDF parser, so a whole frame
opens as one Zarr dataset without copying data. time, reference_time,
x, y and spatial_ref are loaded into the store, because their
encoding differs between granules; the /identification and /metadata
groups are not read.
Displacement stays relative to each ministack reference, as in the granules:
chain and re-reference on top of :func:open_virtual. A virtual store is an
index for ingest and exploration; for per-pixel work write a native store
(:func:disp_xr.to_zarr with the "timeseries" preset).
The store registers a virtual chunk container for the granule location (the S3 bucket, or the local directory). Credentials for it are given when the repository is opened and are never saved with it.
build_virtual_frame ¶
build_virtual_frame(granules: GranulesLike, repo: RepoLike, *, branch: str = 'main', group: Optional[str] = None, layers: Optional[Sequence[str]] = None, max_workers: Optional[int] = None, credentials: Optional[AWSCredentials] = None, reference_urls: Optional[Mapping[str, str]] = None) -> list[str]
Write a frame of granules to an Icechunk repository as virtual references.
One commit per ministack, in reference-date order. A branch that already holds a frame is extended: every new ministack must start after its last epoch and be on the same grid, with the same layers and dtypes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
granules
|
str, Path, iterable of paths, or DataFrame
|
Local or |
required |
repo
|
(Repository, Storage, str or Path)
|
Repository to write to. For a storage or a local path, the repository
is created if needed and a virtual chunk container for the granule
location is registered. A |
required |
branch
|
str
|
Branch to commit to. |
'main'
|
group
|
str
|
Group to write instead of the root, for example one group per
production ( |
None
|
layers
|
sequence of str
|
Layers to reference. Defaults to all displacement and quality layers. |
None
|
max_workers
|
int
|
Processes that parse granules; |
None
|
credentials
|
AWSCredentials
|
For |
None
|
reference_urls
|
mapping of str to str
|
Granule file name to the URL the references store, for every granule:
parse local copies of granules published elsewhere (for example ASF's
|
None
|
Returns:
| Type | Description |
|---|---|
list of str
|
Snapshot id of each commit, one per ministack. |
Raises:
| Type | Description |
|---|---|
MixedProductionError
|
If the granules mix productions (dtype or encoding differ). |
ValueError
|
If a ministack is on another grid or does not follow the branch's last epoch. |
Examples:
open_virtual ¶
open_virtual(repo: RepoLike, *, branch: Optional[str] = None, tag: Optional[str] = None, snapshot_id: Optional[str] = None, chunks: Any = 'native', credentials: Optional[AWSCredentials] = None, group: Optional[str] = None) -> xr.Dataset
Open a virtual frame read-only, with access to its granules.
As :func:disp_xr.open_icechunk, but the repository is opened with read
access to every virtual chunk container in its config: local directories,
or S3 with credentials (default: environment, then ASF).
chunks="native" (default) chunks the frame as :func:disp_xr.open_disp
does: one epoch by config["chunks.read_block"] pixels. The stored
chunks are the granules' own HDF5 chunks, too small to compute on, and
without dask (chunks=None) a chain or a fit loads the whole frame.
Any other value is passed to :func:xarray.open_zarr. group opens
one group (for example one production) instead of the root.
Examples:
validate_virtual ¶
validate_virtual(repo: RepoLike, granules: GranulesLike, *, n_samples: int = 10, seed: int = 0, branch: str = 'main', credentials: Optional[AWSCredentials] = None, group: Optional[str] = None) -> dict[str, Any]
Check a virtual frame against the granules, read with h5py.
Compares the time axis, n_samples random pixel series of every layer,
and one random epoch in full.
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any value or the time axis differs, naming what differs. |
Examples:
Frame repositories: virtual granules plus chain offsets¶
A DISP frame as one Icechunk repository: virtual granules plus chain offsets.
A virtual frame (:func:disp_xr.build_virtual_frame) references the granules
as they are: displacement relative to each ministack's reference date, and,
when the frame mixes productions that store layers in different dtypes, one
group per production ("production/<name>"). :func:add_chain_offsets
stores, once, the arithmetic :func:disp_xr.chain would redo on every read:
for each ministack, the chained displacement at its reference date (an
offset map), as a native float32 group chain of the same repository.
:func:open_frame then returns the continuous series, raw + offset of
each epoch's ministack with the zero epoch at the start date, equal to
chain(open_disp(granules)) bit for bit.
Every epoch reads two maps (its own and its ministack's offset) instead of
one plus a crossover per earlier ministack, and the chain group lets
plain xarray users rebase without disp-xr::
continuous = raw + chain.displacement_offset.sel(ministack=raw.reference_time)
Offsets are computed from the unmasked granules, so masks still apply after
opening, and nan_policy (chain's) is fixed per repository and
recorded in the group's attributes.
add_chain_offsets ¶
add_chain_offsets(repo: RepoLike, *, branch: str = 'main', nan_policy: str = 'propagate', message: Optional[str] = None, tag: Optional[str] = None, credentials: Optional[AWSCredentials] = None) -> Optional[str]
Store the chain offsets of a virtual frame, or extend them, in one commit.
For each ministack, the chained displacement at its reference date (the
first ministack's is zero), for every displacement layer, from the
unmasked granules of all production groups in time order. Ministacks
already stored are kept: after new granules are added with
:func:disp_xr.build_virtual_frame, a call computes only the new ones,
from the stored offsets (one crossover map read per new ministack).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo
|
(Repository, Storage, str or Path)
|
Repository of a virtual frame. |
required |
branch
|
str
|
Branch to read and commit to. |
'main'
|
nan_policy
|
('propagate', 'omit')
|
As in :func: |
"propagate"
|
message
|
str
|
Commit message; defaults to a summary. |
None
|
tag
|
str
|
Tag the new snapshot. Tags never move. |
None
|
credentials
|
AWSCredentials
|
For granules on S3, as in :func: |
None
|
Returns:
| Type | Description |
|---|---|
str or None
|
Snapshot id of the commit, or |
Examples:
open_frame ¶
open_frame(repo: RepoLike, *, branch: Optional[str] = None, tag: Optional[str] = None, snapshot_id: Optional[str] = None, zero_epoch: bool = True, chunks: Any = 'native', credentials: Optional[AWSCredentials] = None) -> xr.Dataset
Open a frame repository as one continuous, chained series.
The production groups in time order, plus the stored chain offsets
(:func:add_chain_offsets) of each epoch's ministack: equal to
chain(open_disp(granules), nan_policy=...), lazily, with every epoch
read as itself and one offset map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo
|
(Repository, Storage, str or Path)
|
Frame repository. |
required |
branch
|
str
|
Version to read (default: the latest commit of |
None
|
tag
|
str
|
Version to read (default: the latest commit of |
None
|
snapshot_id
|
str
|
Version to read (default: the latest commit of |
None
|
zero_epoch
|
bool
|
Prepend the start date as an epoch of zero displacement, as
:func: |
True
|
chunks
|
optional
|
As in :func: |
'native'
|
credentials
|
AWSCredentials
|
For granules on S3. |
None
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Chained displacement layers and the quality layers; |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the repository has no chain offsets, or not for every ministack. |
Examples: