#!/usr/bin/env python3 """First controller tick of every B01 MPC episode (input of d3_range.py). For each episode b01__mpc of the B01 logs, writes the first row of ticks.csv.gz with status 'ok' (the first tick at which the controller received a state): the controller's lookahead error e0 and the tracking error et0, et1, et2. Values are copied as logged (strings), so the downstream floats are exact. Usage: python first_tick.py """ import csv import glob import gzip import os import sys RUN, OUT = sys.argv[1:3] rows = [] for d in sorted(glob.glob(os.path.join(RUN, 'episodes', 'b01_*_mpc'))): sid = os.path.basename(d)[len('b01_'):-len('_mpc')] with gzip.open(os.path.join(d, 'ticks.csv.gz'), 'rt') as fh: t = next(x for x in csv.DictReader(fh) if x['status'] == 'ok') rows.append({'scenario_id': sid, 'tick': t['tick'], 'e0': t['e0'], 'et0': t['et0'], 'et1': t['et1'], 'et2': t['et2']}) with open(OUT, 'w', newline='') as f: w = csv.DictWriter(f, fieldnames=list(rows[0].keys())) w.writeheader() w.writerows(rows) print(f'{len(rows)} episodes -> {OUT}')