Add data_analysis/src/data_analysis/cli.py
This commit is contained in:
parent
df84494fc8
commit
d5a27e1ee5
1 changed files with 76 additions and 0 deletions
76
data_analysis/src/data_analysis/cli.py
Normal file
76
data_analysis/src/data_analysis/cli.py
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
from data_analysis.core import analyze_temperature_data
|
||||||
|
|
||||||
|
|
||||||
|
class CLIError(Exception):
|
||||||
|
"""Custom exception for CLI-related errors."""
|
||||||
|
|
||||||
|
|
||||||
|
def parse_arguments() -> argparse.Namespace:
|
||||||
|
"""Parses command-line arguments for input and output paths."""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Analyse von Infrarot-Temperaturdaten und Erzeugung einer JSON-Zusammenfassung."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--input",
|
||||||
|
required=True,
|
||||||
|
help="Pfad zur CSV-Datei mit Temperaturmessdaten."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--output",
|
||||||
|
required=True,
|
||||||
|
help="Pfad zur JSON-Datei mit Analyseergebnissen."
|
||||||
|
)
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def validate_paths(input_path: Path, output_path: Path) -> None:
|
||||||
|
"""Validates that input file exists and output directory is writable."""
|
||||||
|
if not input_path.exists():
|
||||||
|
raise CLIError(f"Eingabedatei nicht gefunden: {input_path}")
|
||||||
|
if not input_path.is_file():
|
||||||
|
raise CLIError(f"Eingabepfad ist keine Datei: {input_path}")
|
||||||
|
output_dir = output_path.parent
|
||||||
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""Main CLI entrypoint for the data analysis pipeline."""
|
||||||
|
try:
|
||||||
|
args = parse_arguments()
|
||||||
|
input_path = Path(args.input)
|
||||||
|
output_path = Path(args.output)
|
||||||
|
validate_paths(input_path, output_path)
|
||||||
|
|
||||||
|
# Load the CSV data
|
||||||
|
data = pd.read_csv(input_path)
|
||||||
|
assert not data.empty, "Die Eingabedatei enthält keine Daten."
|
||||||
|
required_columns = {"surface", "temperature_c", "emissivity", "timestamp"}
|
||||||
|
if not required_columns.issubset(data.columns):
|
||||||
|
missing = required_columns - set(data.columns)
|
||||||
|
raise CLIError(f"Fehlende erforderliche Spalten: {', '.join(missing)}")
|
||||||
|
|
||||||
|
# Perform analysis
|
||||||
|
result = analyze_temperature_data(data)
|
||||||
|
|
||||||
|
# Write output JSON
|
||||||
|
with open(output_path, 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(json.loads(result.to_json()), f, indent=2, ensure_ascii=False)
|
||||||
|
|
||||||
|
print(f"Analyse abgeschlossen. Ergebnisse gespeichert in: {output_path}")
|
||||||
|
|
||||||
|
except (CLIError, AssertionError) as e:
|
||||||
|
print(f"Fehler: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Unerwarteter Fehler: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue