Add temperature_data_analysis/src/temperature_data_analysis/cli.py
This commit is contained in:
parent
ebe506aa22
commit
a921493d89
1 changed files with 69 additions and 0 deletions
|
|
@ -0,0 +1,69 @@
|
||||||
|
import argparse
|
||||||
|
import csv
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from statistics import StatisticsError
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from temperature_data_analysis.core import analyze_temperature_data
|
||||||
|
|
||||||
|
|
||||||
|
def _read_temperature_data(csv_path: Path) -> List[float]:
|
||||||
|
"""Reads temperature values (°C) from CSV input file."""
|
||||||
|
temperatures: List[float] = []
|
||||||
|
with csv_path.open(mode="r", encoding="utf-8") as csvfile:
|
||||||
|
reader = csv.DictReader(csvfile)
|
||||||
|
if "temperature_c" not in reader.fieldnames:
|
||||||
|
raise ValueError("CSV file must contain a 'temperature_c' column.")
|
||||||
|
for row in reader:
|
||||||
|
try:
|
||||||
|
value = float(row["temperature_c"])
|
||||||
|
temperatures.append(value)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
continue # skip invalid entries
|
||||||
|
if not temperatures:
|
||||||
|
raise ValueError("No valid temperature values found in CSV file.")
|
||||||
|
return temperatures
|
||||||
|
|
||||||
|
|
||||||
|
def _write_json_output(output_path: Path, result_dict: dict) -> None:
|
||||||
|
"""Writes the analysis result dictionary to a JSON output file."""
|
||||||
|
with output_path.open(mode="w", encoding="utf-8") as f:
|
||||||
|
json.dump(result_dict, f, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""CLI entry point for temperature data analysis."""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Analyse von Temperaturdaten aus CSV-Dateien."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--input",
|
||||||
|
required=True,
|
||||||
|
help="Pfad zur CSV-Datei mit Temperaturmesswerten."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--output",
|
||||||
|
required=True,
|
||||||
|
help="Pfad zur JSON-Ausgabedatei mit den Analyseergebnissen."
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
input_path = Path(args.input)
|
||||||
|
output_path = Path(args.output)
|
||||||
|
|
||||||
|
assert input_path.exists(), f"Input file not found: {input_path}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = _read_temperature_data(input_path)
|
||||||
|
result_dict = analyze_temperature_data(data)
|
||||||
|
_write_json_output(output_path, result_dict)
|
||||||
|
print(f"Analysis completed. Results written to {output_path}.")
|
||||||
|
except (ValueError, StatisticsError) as e:
|
||||||
|
print(f"Error during analysis: {e}")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue