Metadata-Version: 2.5
Name: eumdac-store
Version: 0.5.0
Summary: Robust EUMDAC downloader with integrity validation, quarantine, and a ducats-backed catalog, to a directory or a bucket
Project-URL: Repository, https://github.com/TUW-GEO/eumdac-store
Author-email: TU Wien <remote.sensing@geo.tuwien.ac.at>
License-Expression: MIT
License-File: LICENSE.txt
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.12
Requires-Dist: ducats>=0.2
Requires-Dist: eumdac<5,>=4.0
Requires-Dist: pydantic-settings>=2.0
Requires-Dist: pydantic>=2.0
Requires-Dist: python-dotenv>=1.0
Requires-Dist: pyyaml>=6.0
Requires-Dist: requests>=2.31
Requires-Dist: s3fs>=2024.6
Requires-Dist: universal-pathlib>=0.2
Requires-Dist: urllib3>=2.0
Description-Content-Type: text/markdown

# eumdac-store

Robust EUMDAC downloader — Python API + CLI, to a directory or a bucket.

What it adds on top of plain `eumdac download`:

- **Rate-limit awareness** — parallel downloads clamped to EUMDAC's 10-connection ceiling.
- **Retries with backoff** — one budget shared across network errors and integrity failures.
- **Integrity validation** — each download checked against EUMDAC's size + ZIP magic before publishing. MD5 is skipped (EUMDAC publishes wrong MD5 metadata).
- **Atomic publish** — partials are unlinked on failure, so a reader never sees half-files.
- **Persistent-corruption quarantine** — a product that exhausts the retry budget gets a `.quarantine` sidecar (JSON `kind`: `missing` / `size` / `zip`) and is skipped until cooloff.
- **Skip what we already have** — a [ducats](https://github.com/TUW-GEO/ducats) catalog synced from the store (a directory or a bucket) decides presence, re-synced after each run.

## Install

```bash
pip install eumdac-store
```

For development: `uv sync`.

## CLI

```bash
# Download the last day, skipping what the catalog already has.
# Product wiring is required and comes from a config file — ASCAT SZF values ship in examples/ascat_szf.yaml:
eumdac-store download --config examples/ascat_szf.yaml --days 1 \
  --download-root-dir /data/eumdac

# Explicit window:
eumdac-store download --config examples/ascat_szf.yaml \
  --start 2026-01-01T00:00 --end 2026-01-02T00:00 ...

# Either bound is optional: --start alone fetches everything since then (up to
# now); --end alone fetches everything up to it (open lower bound).
# E.g. backfill the whole collection up to a date:
eumdac-store download --config examples/ascat_szf.yaml --end 2026-01-01T00:00 ...

# List without downloading:
eumdac-store list --config examples/ascat_szf.yaml --days 1 ...

# Re-sync the catalog from the store (no EUMDAC calls):
eumdac-store sync --config examples/ascat_szf.yaml ...

# Sync status + quarantine sidecar count:
eumdac-store status --config examples/ascat_szf.yaml ...
```

Credentials: `EUMDAC_KEY` / `EUMDAC_SECRET` in the environment or `.env`, or EUMETSAT's own `credentials.json` (`{"consumer_key": …, "consumer_secret": …}`) placed at `$XDG_CONFIG_HOME/eumdac-store/credentials.json` (default `~/.config/eumdac-store/credentials.json`). Env vars win over the file. Config also via `$EUMDAC_STORE_CONFIG` or `EUMDAC_STORE_*` env vars; per-flag options win.

## Object storage (S3)

`download_root_dir` may be a URL instead of a directory; the local filesystem stays the default.

```bash
eumdac-store download --config examples/ascat_szf.yaml --days 1 \
  --download-root-dir s3://tuw-ascat-l1b/eumdac
```

Credentials and endpoint come from the usual AWS sources (`AWS_PROFILE`, `AWS_SHARED_CREDENTIALS_FILE`, `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`, `AWS_ENDPOINT_URL`) or from `storage_options` (YAML, or `EUMDAC_STORE_STORAGE_OPTIONS` as JSON), passed to fsspec as is, e.g. `{"profile": "tuw-ascat-l1b", "endpoint_url": "https://objects.eodc.eu"}`.

What differs from a local root:

- **Publish** — the zip is downloaded and validated on local disk (a temp dir), then uploaded in one PUT; an object is only visible once its upload completes, so readers never see a half file.
- **Catalog** — local, as for every root (see Configuration); ducats's `glob()` lists the bucket through the same fsspec filesystem (registered with DuckDB), so presence still comes from what is really stored.
- **Quarantine** — sidecars are small objects next to the zips; their age is the object's LastModified (or the sidecar's own `quarantined_at`).
- **Retention** — set a lifecycle rule on the bucket; eumdac-store never deletes.

## Gaps

```bash
eumdac-store gaps --config examples/ascat_szf.yaml --days 2   # exit 4 if any
```

Lists the store and reports breaks in each group's time chain (groups: the pattern's named groups other than `start`/`end`, e.g. `sat`; or `gap_group_cols`). Consecutive files chain when the next starts `gap_min_seconds`..`gap_max_seconds` after the last one ends (defaults 0..1 s, as SZF). `--start`/`--end` keep the gaps that begin in that window.

## Python API

```python
from eumdac_store import EumdacStore, StoreConfig

store = EumdacStore(StoreConfig.from_yaml("examples/ascat_szf.yaml",
    download_root_dir="/data/eumdac",
))
result = store.download(days=1)          # or start=, end=, specific_dates=
print(result)   # downloaded=… skipped_present=… skipped_quarantined=… failed=…
```

Product fields are **required** (no defaults); pass them inline to `StoreConfig(...)` to skip the YAML.

## Configuration

| Field | Required | Default | Meaning |
|-------|----------|---------|---------|
| `collection_id` | **yes** | — | EUMDAC collection to search |
| `product_name` | **yes** | — | ducats catalog table name (must be a valid identifier)
| `filename_glob` | **yes** | — | Which files in `<product_name>/` the catalog globs |
| `filename_pattern` | **yes** | — | Regex for ducats's `RegexExtractor`; `(?P<start>...)` is required, `(?P<end>...)` optional (absent → `end_ts` NULL), other named groups become categorical columns |
| `download_root_dir` | **yes** | — | EUMDAC data root: `<product_name>/` per product lives here. A directory, or a URL such as `s3://bucket/prefix` (see Object storage) |
| `storage_options` | no | `{}` | fsspec options for a URL root |
| `catalog_file` | no | derived | DuckDB catalog path (see below) |
| `max_workers` | no | 10 | Parallel downloads (hard-clamped to 10) |
| `max_retries` | no | 3 | Retry budget per file |
| `quarantine_cooloff_hours` | no | 2.0 | How long a quarantine sidecar suppresses refetch |

`product_dir` is `<download_root_dir>/<product_name>/`. The catalog is a local cache of that listing, rebuilt from scratch on every sync, so it is kept out of the data: `catalog_path` is `catalog_file` if set, else `$XDG_CACHE_HOME/eumdac-store/<root>/catalog.duckdb` (`<root>` is the absolute directory without its leading `/`, or `bucket/prefix` for a URL). Losing it costs one sync. A catalog left at `<download_root_dir>/catalog.duckdb` by earlier versions is deleted after the first successful sync, so a copy of the root never carries a stale one.

Products under one `download_root_dir` share one catalog (one DuckDB file, one table per `product_name`).

Copy `examples/ascat_szf.yaml` for another product. A missing required field is a hard validation error.

## Tests

```bash
uv run pytest      # no network — a fake EUMDAC DataStore backs every test
```
