Add spike_finder/src/spike_finder/cli.py
This commit is contained in:
parent
65564e4e5b
commit
4ee336d3cd
1 changed files with 48 additions and 0 deletions
48
spike_finder/src/spike_finder/cli.py
Normal file
48
spike_finder/src/spike_finder/cli.py
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from spike_finder import core
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""CLI-Interface zur Steuerung des Spike-Finders."""
|
||||||
|
parser = argparse.ArgumentParser(description="P99 Spike Finder CLI")
|
||||||
|
parser.add_argument("--input", required=True, help="Pfad zur JSON-Logdatei.")
|
||||||
|
parser.add_argument("--threshold", required=True, type=float, help="Schwellenwert für Spike-Erkennung.")
|
||||||
|
parser.add_argument("--output", required=True, help="Ausgabepfad für die erkannten Spike-Events.")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
input_path = Path(args.input)
|
||||||
|
output_path = Path(args.output)
|
||||||
|
|
||||||
|
if not input_path.exists():
|
||||||
|
print(f"Fehler: Eingabedatei {input_path} existiert nicht.", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with input_path.open("r", encoding="utf-8") as f:
|
||||||
|
log_data = json.load(f)
|
||||||
|
except (json.JSONDecodeError, OSError) as e:
|
||||||
|
print(f"Fehler beim Lesen der Logdatei: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
spikes = core.find_spikes(log_data, args.threshold)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Fehler bei der Spike-Erkennung: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with output_path.open("w", encoding="utf-8") as f:
|
||||||
|
json.dump([s for s in spikes], f, ensure_ascii=False, indent=2, default=str)
|
||||||
|
except OSError as e:
|
||||||
|
print(f"Fehler beim Schreiben der Ausgabedatei: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue