Skip to content

API reference / Reading and ingest

tsdive.init_meta

init_meta(
    source: str | Path,
    *,
    out_dir: str | Path,
    source_id: str,
    timestamp_col: str = "timestamp",
    tags: Sequence[str] | None = None,
    quality_suffix: str | None = None,
    overwrite: bool = False,
    sep: str | None = None,
    decimal: str | None = None,
    encoding: str | None = None,
) -> list[Path]

Write one metadata template per tag column of a wide export.

Each template at out_dir / f"{safe_filename(tag)}.json" carries identity (source_id and the column name as point_id), name (the column name) and every optional key of tsdive.meta set to null. Nothing about units, ranges or sample rate is read off the data. With quality_suffix, quality_codes lists every distinct raw value of the tag's quality column, mapped to a severity only where the value spells GOOD, UNCERTAIN or BAD itself; every other code stays null for the reader to fill, and read_meta_json refuses the file until they are. A tag column that holds numbers and strings, such as PI digital states, adds each string to quality_codes as null. The file is read as ingest reads it, sep, decimal and encoding included.

Raises:

Type Description
SchemaError

unreadable input, or a missing timestamp, tag or quality column.

ValueError

two tags that share one file name.

FileExistsError

a template exists and overwrite is False.

Examples:

>>> import pandas as pd
>>> import tsdive
>>> fic = pd.read_parquet("data/demo/fic101_demo.parquet")
>>> tic = pd.read_parquet("data/demo/tic101_demo.parquet")
>>> wide = pd.DataFrame({"ts": fic["timestamp"], "FIC101.PV": fic["value"],
...                      "TIC101.PV": tic["value"]})
>>> wide.to_csv("export.csv", index=False)
>>> paths = tsdive.init_meta("export.csv", out_dir="meta", source_id="plant1",
...                          timestamp_col="ts")
>>> [path.as_posix() for path in paths]
['meta/FIC101.PV.json', 'meta/TIC101.PV.json']
>>> template = tsdive.read_meta_json(paths[0])
>>> str(template.identity), template.name, template.unit_raw
('plant1:FIC101.PV', 'FIC101.PV', None)