Add measurement_logging/src/measurement_logging/cli.py
This commit is contained in:
parent
002c4a80a7
commit
78111432a8
1 changed files with 66 additions and 0 deletions
66
measurement_logging/src/measurement_logging/cli.py
Normal file
66
measurement_logging/src/measurement_logging/cli.py
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from measurement_logging.core import log_measurement
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args() -> argparse.Namespace:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="CLI-Tool zum Loggen von Nachtfotografie-Messdaten (Temperatur, Feuchte, Kameraeinstellungen)."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--input",
|
||||||
|
required=True,
|
||||||
|
help="Pfad zur JSON-Datei mit den Messwerten."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--output",
|
||||||
|
required=False,
|
||||||
|
default="output/measurements.csv",
|
||||||
|
help="Pfad zur CSV-Datei, in die die Messdaten geschrieben werden (Standard: output/measurements.csv)."
|
||||||
|
)
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def read_measurement_json(input_path: Path) -> dict[str, Any]:
|
||||||
|
if not input_path.exists():
|
||||||
|
raise FileNotFoundError(f"Eingabedatei nicht gefunden: {input_path}")
|
||||||
|
with input_path.open("r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
required_fields = [
|
||||||
|
"time",
|
||||||
|
"temperature",
|
||||||
|
"humidity",
|
||||||
|
"iso",
|
||||||
|
"exposure",
|
||||||
|
"rms_noise",
|
||||||
|
]
|
||||||
|
for field in required_fields:
|
||||||
|
if field not in data:
|
||||||
|
raise ValueError(f"Feld '{field}' fehlt in der Eingabedatei {input_path}.")
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
args = parse_args()
|
||||||
|
try:
|
||||||
|
measurement_data = read_measurement_json(Path(args.input))
|
||||||
|
log_measurement(
|
||||||
|
time=measurement_data["time"],
|
||||||
|
temperature=float(measurement_data["temperature"]),
|
||||||
|
humidity=float(measurement_data["humidity"]),
|
||||||
|
iso=int(measurement_data["iso"]),
|
||||||
|
exposure=float(measurement_data["exposure"]),
|
||||||
|
rms_noise=float(measurement_data["rms_noise"]),
|
||||||
|
)
|
||||||
|
print(f"Messdaten erfolgreich geloggt in {args.output}.")
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"Fehler: {exc}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue