# Show what each file in a RoadData.AI scenario holds: the OpenDRIVE roads and the OpenSCENARIO road users.
# usage: python inspect_scenario_files.py roaddata-scenario-starter/sc_3ebeafb59c
import sys, xml.etree.ElementTree as ET
from collections import Counter
folder = sys.argv[1]
odr = ET.parse(f"{folder}/map.xodr").getroot()
h = odr.find("header")
print(f"map.xodr      OpenDRIVE {h.get('revMajor')}.{h.get('revMinor')}: {len(odr.findall('road'))} roads, {len(odr.findall('junction'))} junctions")
for road in odr.findall("road")[:3]:
    lanes = [l for l in road.findall(".//lane") if l.get("id") != "0"]   # lane 0 is the reference line, not a lane
    print(f"              road {road.get('id')}: {float(road.get('length')):.1f} m, lanes: {len(lanes)}")
osc = ET.parse(f"{folder}/scenario.xosc").getroot()
h = osc.find("FileHeader")
print(f"scenario.xosc OpenSCENARIO {h.get('revMajor')}.{h.get('revMinor')}, map: {osc.find('.//RoadNetwork/LogicFile').get('filepath')}")
kinds = Counter()
for obj in osc.findall(".//Entities/ScenarioObject"):
    e = next(iter(obj))                                                     # Vehicle or Pedestrian
    kinds[e.get("vehicleCategory") or e.get("pedestrianCategory") or e.tag] += 1
print("              road users:", ", ".join(f"{k} {n}" for k, n in kinds.items()))
