Skip to content
Merged
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ same request also works on a Dask cluster whose workers carry the secrets.
| `replace` | Kerchunk only: rewrite references in place (stable href). |
| `commit` | Icechunk only: commit the write (default `True`). |
| `repository_config` | Icechunk only: `icechunk.RepositoryConfig` applied when the repository is created (manifest splitting, inline threshold, ...). Ignored on append. |
| `dedupe` | Icechunk only: `"upsert"` (default; skip unchanged, overwrite reprocessed, append new), `"skip"` (never overwrite) or `"off"`. |
| `key` | Coordinate identifying a slot; defaults to `concat_dim`. Only the time coordinate is derivable from CMR metadata. |
| `on_error` | `"raise"` (default) or `"skip"`: record a failed granule, ingest the rest, end the job `PARTIAL`. |
| `verify` | `"metadata"` (default) or `"etag"`: also probe each granule's origin ETag (one HTTP HEAD per granule) to catch in-place replacement without a CMR revision bump. |
| `virtual_chunk_containers` | Icechunk only: extra `VirtualChunkContainer`s keyed by `url_prefix`, merged over the derived ones (entries win). |
| `access` | `"indirect"` (HTTPS, default) or `"direct"` (S3). |
| `parser` | VirtualiZarr parser name; `"DMRPPParser"` default with automatic `"HDFParser"` fallback. |
Expand All @@ -121,6 +125,16 @@ reader does not need to know the provider; other icechunk clients ignore it.
| no store, `load=False` | `virtual` | manifest-form `xarray.Dataset` / `DataTree` |
| no store, `load=True` | `xarray` | concrete lazily-loaded `xarray.Dataset` |

## Idempotent ingestion

`request()` is idempotent by default: each granule is keyed by its time step and
the ingested keys are recorded in the repository metadata. Re-running a batch
skips unchanged granules, overwrites a reprocessed one in place (a CMR revision
change is the signal), and appends only genuinely new steps. `dedupe="skip"`
never overwrites; `dedupe="off"` restores plain appends. With `on_error="skip"`
a failed granule is recorded, the rest are ingested, and the job ends `PARTIAL`
— see `job.info()["summary"]` and `job.info()["errors"]`.

## Development

Requires Python 3.12+ (icechunk, virtualizarr and earthaccess all do).
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module = ["pytest", "pytest.*"]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = ["kerchunk", "kerchunk.*", "fsspec", "fsspec.*", "fastparquet", "s3fs", "tqdm", "tqdm.*", "distributed", "distributed.*"]
module = ["kerchunk", "kerchunk.*", "fsspec", "fsspec.*", "fastparquet", "pandas", "s3fs", "tqdm", "tqdm.*", "distributed", "distributed.*"]
ignore_missing_imports = true

[[tool.mypy.overrides]]
Expand Down
167 changes: 141 additions & 26 deletions src/earthaccess_vds/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import tempfile
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, cast

from earthaccess_vds.credentials import build_obstore_registry
from earthaccess_vds.parser import get_urls_for_parser, resolve_parser
Expand Down Expand Up @@ -64,6 +64,7 @@ def virtualize( # noqa: PLR0913, C901
parser: ParserType = "DMRPPParser",
reference_dir: str | None = None,
tree: bool = False,
on_error: str = "raise",
auth: AuthHandle | None = None,
progress: Any = None,
registry: Any = None,
Expand Down Expand Up @@ -146,34 +147,76 @@ def _open_once(parser_obj: Any, obj_registry: Any) -> xr.Dataset | xr.DataTree:
**xr_combine_kwargs,
)

# Attempt to open with the requested parser; fall back to HDFParser if
# DMR++ sidecars are not present.
def _attempt() -> xr.Dataset | xr.DataTree:
# Attempt to open with the requested parser; fall back to HDFParser if
# DMR++ sidecars are not present.
try:
opened = _open_once(resolved_parser, registry)
if progress is not None and (tree or len(granules) == 1):
# A single open does not go through the executor.
progress.opened_one()
except FileNotFoundError:
if type(resolved_parser).__name__ != "DMRPPParser":
raise
warnings.warn(
"DMR++ sidecar files were not found for one or more granules. "
"Falling back to HDFParser. "
"Set parser='HDFParser' to silence this warning.",
UserWarning,
stacklevel=2,
)
fallback = resolve_parser(
"HDFParser",
group=group if group != "/" else None,
)
fallback_registry = (
build_obstore_registry(granules, access=access, auth=auth)
if own_registry
else registry
)
if progress is not None:
progress.start("virtualize", total=len(granules))
opened = _open_virtual_dispatch(
granules=granules,
parser=fallback,
registry=fallback_registry,
access=access,
concat_dim=concat_dim,
combine=combine,
join=join,
preprocess=preprocess,
loadable_variables=loadable_variables,
drop_variables=drop_variables,
parallel=parallel,
data_vars=data_vars,
coords=coords,
compat=compat,
combine_attrs=combine_attrs,
tree=tree,
**xr_combine_kwargs,
)
if progress is not None and (tree or len(granules) == 1):
progress.opened_one()
return opened

try:
vds = _open_once(resolved_parser, registry)
if progress is not None and (tree or len(granules) == 1):
# A single open does not go through the executor.
progress.opened_one()
except FileNotFoundError:
if type(resolved_parser).__name__ != "DMRPPParser":
vds = _attempt()
except Exception as exc:
if on_error != "skip" or tree:
raise
warnings.warn(
"DMR++ sidecar files were not found for one or more granules. "
"Falling back to HDFParser. "
"Set parser='HDFParser' to silence this warning.",
UserWarning,
stacklevel=2,
)
resolved_parser = resolve_parser(
"HDFParser",
group=group if group != "/" else None,
vds = _open_lenient(
granules,
parser=resolved_parser,
registry=registry,
access=access,
concat_dim=concat_dim,
combine=combine,
preprocess=preprocess,
loadable_variables=loadable_variables,
drop_variables=drop_variables,
progress=progress,
original=exc,
)
if own_registry:
registry = build_obstore_registry(granules, access=access, auth=auth)
if progress is not None:
progress.start("virtualize", total=len(granules))
vds = _open_once(resolved_parser, registry)
if progress is not None and (tree or len(granules) == 1):
progress.opened_one()

if not load:
return vds
Expand Down Expand Up @@ -437,6 +480,78 @@ def open_refs_store(
return _open_kerchunk_refs(href, access=access)


def _open_lenient(
granules: Sequence[Any],
*,
parser: Any,
registry: Any,
access: AccessType,
concat_dim: str | None,
combine: CombineType,
preprocess: Callable | None,
loadable_variables: list[str] | None,
drop_variables: list[str] | None,
progress: Any,
original: BaseException,
) -> xr.Dataset:
"""Open granules one by one, skipping (and recording) the failures.

Used for ``on_error="skip"``: the batch-level open aborts on the first bad
granule, so we fall back to per-granule opens and combine the survivors.
"""

import xarray as xr

urls = get_urls_for_parser(granules, parser, access=access)
pieces: list[xr.Dataset] = []
errors: list[dict[str, Any]] = []
for granule, url in zip(granules, urls, strict=False):
try:
pieces.append(
_open_virtual_dataset_single(
url,
parser=parser,
registry=registry,
preprocess=preprocess,
drop_variables=drop_variables,
loadable_variables=loadable_variables,
)
)
except Exception as exc: # noqa: BLE001 - recorded, not raised
errors.append(
{
"granule": _granule_id(granule),
"phase": "open",
"error": repr(exc),
}
)
else:
if progress is not None:
progress.opened_one()

if progress is not None:
for error in errors:
progress.failed_one(**error)

if not pieces:
raise original

if combine == "nested":
return combine_virtual(pieces, concat_dim=concat_dim)
return cast("xr.Dataset", xr.combine_by_coords(pieces))


def _granule_id(granule: Any) -> Any:
"""Best-effort granule identifier for error reports."""

try:
return granule["meta"].get("concept-id") or granule.get("meta", {}).get(
"concept-id"
)
except (TypeError, KeyError):
return None


def combine_virtual(
pieces: Sequence[xr.Dataset | None],
*,
Expand Down
Loading
Loading