Skip to content

Instantly share code, notes, and snippets.

@zusitools
Created April 8, 2021 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zusitools/9b493196ef2cc76cba43386a339d227a to your computer and use it in GitHub Desktop.
Save zusitools/9b493196ef2cc76cba43386a339d227a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import math
import xml.etree.ElementTree as ET
tree = ET.parse(sys.argv[1])
for se in tree.findall("./Strecke/StrElement"):
ueberh = float(se.attrib.get("Ueberh", 0))
if not ueberh:
continue
for richtung, ueberh_soll in (("Norm", ueberh), ("Gegen", -ueberh)):
signal = se.find("./Info" + richtung + "Richtung/Signal")
if signal is None:
continue
buf = f'Signal {signal.attrib.get("NameBetriebsstelle", "?")} {signal.attrib.get("Signalname", "?")} an Streckenelement {se.attrib["Nr"]}\n'
found = False
signal_phi = signal.find("./phi")
ueberh_signal = (
float(signal_phi.attrib.get("X", 0)) if signal_phi is not None else 0
)
for frame in signal.findall("./SignalFrame"):
datei = frame.find("./Datei")
if (
datei is None
or "indusi" not in datei.attrib.get("Dateiname", "").lower()
):
continue
frame_phi = frame.find("./phi")
ueberh_magnet = (
float(frame_phi.attrib.get("X", 0)) if frame_phi is not None else 0
)
if abs(ueberh_signal + ueberh_magnet - ueberh_soll) > math.radians(
1
): # 1 Grad
found = True
buf += f' - Signalframe {datei.attrib.get("Dateiname", "")}: phiX soll={math.degrees(ueberh_soll)}° ist={math.degrees(ueberh_magnet)}°\n'
if not frame_phi:
frame_phi = ET.SubElement(frame, "phi")
frame_phi.attrib["X"] = str(ueberh_soll - ueberh_signal)
if found:
print(buf)
if len(sys.argv) >= 3 and sys.argv[2] == "fix":
with open(sys.argv[1] + ".new.st3", "wb") as fp:
fp.write(b"\xef\xbb\xbf")
fp.write('<?xml version="1.0" encoding="UTF-8"?>\r\n'.encode("utf-8"))
fp.write(ET.tostring(tree.getroot(), encoding="unicode").encode("utf-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment