# INCA MDA: post-process .mf4 recording
import mdf4reader as mdf
import numpy as np, matplotlib.pyplot as plt
rec = mdf.load("bench_run_wltp_001.mf4")
# Load channels
rpm = rec.get("engine_rpm")
lambda_= rec.get("lambda_actual")
hc_ppm = rec.get("HC_concentration_ppm")
time = rpm.timestamps
# Find operating points: warm engine (coolant > 70C), RPM 1500-3000
coolant = rec.get("coolant_temp_degC").samples
warm_mask = (coolant > 70) & (rpm.samples > 1500) & (rpm.samples < 3000)
print(f"Warm operating window: {np.sum(warm_mask)/len(warm_mask)*100:.1f}% of recording")
print(f"Mean lambda in window: {np.mean(lambda_.samples[warm_mask]):.3f}")
print(f"Max HC in window: {np.max(hc_ppm.samples[warm_mask]):.0f} ppm")
# Plot lambda over time
plt.figure(figsize=(12, 4))
plt.plot(time, lambda_.samples, 'b-', linewidth=0.5, label='lambda_actual')
plt.axhline(1.0, color='r', linestyle='--', label='stoichiometric')
plt.xlabel('Time (s)'); plt.ylabel('Lambda')
plt.title('Lambda trace — WLTP cycle run 001')
plt.legend(); plt.tight_layout()
plt.savefig("lambda_wltp_001.png", dpi=150)