Metadata-Version: 2.5
Name: ducats
Version: 0.3.0
Summary: A DuckDB catalog of the files in a directory or bucket and their time coverage
Project-URL: Repository, https://github.com/TUW-GEO/ducats
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: duckdb>=1.1
Requires-Dist: numpy>=1.26
Description-Content-Type: text/markdown

# ducats

A DuckDB catalog of the files in a directory or bucket: one row per file, with
its group columns and start/end time, synced from a glob.

```bash
pip install ducats
```

## Usage

Register a product in a module:

```python
from ducats import ProductConfig, RegexExtractor, register_product

register_product(ProductConfig(
    name="my_files",
    base_dir="/data/my_product",
    glob="**/*.nc",
    extractor=RegexExtractor(r"my_product_(?P<start>\d{14})_(?P<end>\d{14})\.nc$"),
))
```

Each registered product becomes a table in the catalog with columns `file`,
the extractor's group columns (here none), `start_ts` and `end_ts`.

### sync

```bash
ducats sync --products-module myproject.products --catalog-path catalog.duckdb
```

Imports the given modules (repeat `--products-module` for several), globs each
registered product's files and rebuilds its table. Files that are gone from
disk drop out. `--product NAME` syncs only one. `ducats status` shows when
each table was last synced and how many rows it has.

Without `--catalog-path` the catalog is in memory and lost when the command
exits.

### query

```bash
ducats query --catalog-path catalog.duckdb --sql \
  "SELECT file FROM my_files WHERE start_ts <= '2026-03-24 10:00' AND end_ts >= '2026-03-24 10:00'"
```

Runs any SQL against the catalog and prints up to `--max-rows` rows (default
100). The catalog is a plain DuckDB file, so `duckdb catalog.duckdb` or
`duckdb.connect("catalog.duckdb")` work as well.

### gaps

Two checks, per group in `--group-cols` (e.g. `sat`), or over the whole table:

```bash
ducats gaps --catalog-path catalog.duckdb --table my_files --mode coverage --bucket 1h --tolerance 5s
```

`coverage` splits time into `--bucket`-sized buckets (aligned to 1970-01-01)
from the first file to the last and checks each against the union of the
files' `[start_ts, end_ts]`. A bucket is *covered* if the files span all of
it, *missing* if no file overlaps it, and *partial* otherwise; it reports the
missing and partial ones. `--tolerance` (default 0s) ignores breaks between
files and shortfalls at a bucket's edges up to that length; time stamps like
`10:00:00`–`10:59:59` need at least 1s. A file without `end_ts` covers the
bucket it starts in.

```bash
ducats gaps --catalog-path catalog.duckdb --table my_files --group-cols sat \
  --mode contiguity --min-gap 0s --max-gap 1s
```

`contiguity` is for files that should follow each other end to start (e.g.
orbit segments): it reports each pair of consecutive files where the next
`start_ts` minus this `end_ts` is below `--min-gap` (overlap) or above
`--max-gap` (gap). Files without an `end_ts` are skipped.

From Python: `ducats.queries.find_coverage_gaps` and `find_contiguity_gaps`
return dataclasses (`CoverageReport.covered()` lists the covered buckets);
`ducats.report` formats them as text.

## Extractors

- `RegexExtractor(pattern, ts_format="%Y%m%d%H%M%S")`: fields from the file
  name. `start` is required, `end` is optional, other named groups become
  columns.
- `PythonExtractor(fn, fields, cache_path=None)`: `fn(path)` returns a dict
  of `fields` (including `start_ts`, `end_ts`). With `cache_path`, results
  are kept between runs.

## Development

```bash
uv sync
uv run pytest
```
