Skip to content
TUW-GEOPublic

About

Sync a queryable DuckDB catalog of file temporal coverage from a file archive (formerly ducat)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

Repository files navigation

ducats

Maintains a DuckDB catalog of file coverage — sat/product columns + a start/end timestamp per file — kept in sync with what's actually on disk. The job it does is sync; the job it deliberately does not do is implement every question you might ask ("which file covers this instant", "which METOP-B files fall in any January", "where are the gaps") as a bespoke function. Once a product is synced, those are plain SQL against a table.

Why

glob() lists files vectorized; regexp_extract pulls fields out of filenames vectorized. For archives with tens of thousands of files, syncing is fast and the resulting table is small — cheap enough to keep on disk and query directly, from this package, another Python process, a notebook, or the plain duckdb CLI.

Install

pip install ducats

(Formerly ducat; renamed because that name is taken on PyPI. The import package and the CLI are ducats too.)

Quickstart

Register a product (see Plugging in a new product below), then:

uv run ducats sync --products-module myproject.gap_products --catalog-path /data/catalog.duckdb
uv run ducats status --catalog-path /data/catalog.duckdb

Then just query:

# which file covers this instant?
uv run ducats query --catalog-path /data/catalog.duckdb --sql \
  "SELECT file FROM my_hourly_files WHERE start_ts <= '2026-03-24 10:00:00' AND end_ts >= '2026-03-24 10:00:00'"

# which files fall in any January?
uv run ducats query --catalog-path /data/catalog.duckdb --sql \
  "SELECT file FROM my_hourly_files WHERE month(start_ts) = 1"

Or skip the CLI and open the file directly — it's a normal DuckDB database:

duckdb /data/catalog.duckdb -c "SELECT file FROM my_hourly_files WHERE month(start_ts) = 1"

Two things worth a real function

Most questions are a one-line WHERE clause once a table's synced — not worth wrapping. Two aren't, because they involve real computation (a generated series anti-joined against what's present, or a window function over ordered intervals), so ducats.queries provides them:

query for archives where... flags...
find_grid_gaps files arrive at a fixed cadence (e.g. one per UTC hour) buckets with zero files
find_contiguity_gaps files are variable-length intervals that should chain end-to-start (e.g. orbit swaths) adjacent pairs whose next.start - this.end falls outside [min_gap, max_gap]
uv run ducats gaps --catalog-path /data/catalog.duckdb --table my_hourly_files \
  --mode grid --bucket 1h

uv run ducats gaps --catalog-path /data/catalog.duckdb --table my_hourly_files \
  --mode contiguity --min-gap 0s --max-gap 1h

Plugging in a new product

A product is a ProductConfig: where its files live, an Extractor that reads group columns + a start/end timestamp out of each file.

from ducats.extract import RegexExtractor
from ducats.registry import ProductConfig, register_product

register_product(ProductConfig(
    name="my_hourly_files",
    base_dir="/data/my_product",
    glob="**/*.nc",
    extractor=RegexExtractor(
        pattern=r"my_product_(\d{14})_(\d{14})\.nc$",
        group_cols={},          # no natural grouping here
        start_group=1,
        end_group=2,
    ),
    group_cols=[],
))

Put that in a module (say myproject/gap_products.py) and run:

uv run ducats sync --products-module myproject.gap_products --catalog-path /data/catalog.duckdb

Nothing is auto-discovered — a product only exists in a given run if its module was explicitly imported via --products-module. That keeps "what's registered" unambiguous instead of depending on import side effects from unrelated code.

Two ways to extract fields

  • RegexExtractor — everything needed (timestamps, grouping keys) is in the filename. Stays entirely inside SQL; fast even at tens of thousands of files. Use this whenever it applies.
  • PythonExtractor — the info is inside the file (a NetCDF/HDF5 global attribute, a sidecar JSON, ...). Registers a Python scalar UDF with DuckDB that opens one file and returns a STRUCT of the fields you need; DuckDB calls it once per row. Slower (one file open per matched file) but the same shape comes out, so nothing downstream cares. See the docstring in ducats/extract.py for a worked example.

Either extractor just needs to leave a temp table named items with columns file, <group_cols...>, start_ts, end_ts — write your own if neither fits.

PythonExtractor accepts cache_path: point it at a file and extracted rows persist across runs, so repeat runs over a mostly-unchanged archive only call fn for files not already cached. This is a different thing from catalog persistence below — it avoids re-opening files, not re-running Python. No staleness detection — if fn's output for a path or the fields schema changes, delete the cache file to force a rebuild.

Catalog persistence

ducats.catalog is the sync layer:

from ducats.catalog import open_catalog, sync_product
from myproject.gap_products import my_hourly_files  # a ProductConfig you registered

con = open_catalog("/data/catalog.duckdb")  # omit the path for in-memory, throwaway
sync_product(con, my_hourly_files)
con.sql("SELECT count(*) FROM my_hourly_files").show()

sync_product always leaves the table matching the current glob — files that disappeared from disk are dropped from it too. Point open_catalog at a real path and the sync work only has to happen again for files that weren't there last time; anything already in the table stays, and other processes/tools can open the same file and query it without touching this package at all.

Using the computed queries as a library

from datetime import timedelta
from ducats.queries import find_grid_gaps
from ducats.report import format_grid_reports

reports = find_grid_gaps(con, "resample", group_cols=["sat", "res"], bucket=timedelta(hours=1))
print(format_grid_reports(reports))

find_grid_gaps/find_contiguity_gaps return dataclasses (GridGapReport/ContiguityReport), not text — ducats.report is a separate, optional formatting layer, so you can consume the structured results directly (e.g. to page on-call, or feed a dashboard) without scraping printed text.

Examples

Product-specific config (filename patterns, group wiring, contiguity tolerances) lives in the consuming repo, not ducats. For a real-world example, see ascat_szf.yaml in eumdac-store.

Development

uv sync
uv run pytest

About

Sync a queryable DuckDB catalog of file temporal coverage from a file archive (formerly ducat)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages