kshana 0.33.1

Open, reproducible PNT-resilience simulator with quantum-sensor performance models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
// SPDX-License-Identifier: AGPL-3.0-only
//! Telecom timing: time error, maximum time interval error (MTIE) and time deviation
//! (TDEV) checked against the International Telecommunication Union Telecommunication
//! Standardization Sector (ITU-T) masks, with a PASS or FAIL and a margin per mask.
//!
//! The `telecom-timing` scenario kind takes one of two inputs:
//!
//! * **a synthetic holdover** — an oscillator preset, the time the Global Navigation
//!   Satellite System (GNSS) reference is lost, a record length and a sample interval.
//!   Before the loss the clock is disciplined and its time error is a small white
//!   phase noise; from the loss on it free-runs with white frequency noise, flicker
//!   frequency noise, random-walk frequency noise where the datasheet fit needs it,
//!   linear frequency aging and a sinusoidal temperature term;
//! * **an ingested series** of `(time_s, time_error_ns)` pairs, given inline in the
//!   scenario (the WebAssembly build has no filesystem) or, on native builds only,
//!   read from a comma-separated values (CSV) file.
//!
//! From the series it reports the maximum absolute time error, the MTIE and TDEV
//! curves, a PASS or FAIL with a margin against every selected mask, and the time to
//! exceed each time-error budget.
//!
//! ## Sources
//!
//! Every limit in [`MASKS`], in [`eprtc_a_holdover_limit_ns`] and in the default
//! budgets is transcribed from a freely downloadable ITU-T Recommendation, named with
//! its edition and table or clause beside the number. `docs/TELECOM-TIMING.md` lists
//! them all, and lists what was *not* transcribed: every entry the Recommendations
//! mark "for further study" is absent here, never filled in.
//!
//! The oscillator presets in [`PRESETS`] carry the stability, aging and temperature
//! figures of four named public datasheets. How those figures become a noise model is
//! this module's choice and is labelled MODELLED in every report.
//!
//! ## What the estimators are
//!
//! MTIE is the largest peak-to-peak time error in any window of `m + 1` samples,
//! exactly [`crate::allan::mtie`]; [`mtie_sliding`] computes the same number with a
//! monotonic-queue sliding window so a day of one-second samples stays fast, and a
//! unit test holds the two equal. TDEV is [`crate::allan::time_deviation`] unchanged.
//! Both are checked against the third-party `allantools` package on a committed
//! holdover series in `tests/telecom_timing_reference.rs`.

use crate::field_schema::FieldUnit;
use crate::models::{ClockModel, ErrorModel};
use crate::palette::chart::{AMBER, BLUE, GRID, INK_3, MUTED};
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;
use rand_distr::{Distribution, Normal};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

// ---------------------------------------------------------------------------------------
// Masks
// ---------------------------------------------------------------------------------------

/// One piece of a piecewise mask: on the observation interval between `lo_s` and `hi_s`
/// (each end open or closed exactly as the source table states it) the limit is
/// `slope_ns_per_s · τ + intercept_ns`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Segment {
    /// Lower end of the observation interval (s).
    pub lo_s: f64,
    /// Whether `lo_s` itself belongs to the interval.
    pub lo_inclusive: bool,
    /// Upper end of the observation interval (s); infinite when the piece is open-ended.
    pub hi_s: f64,
    /// Whether `hi_s` itself belongs to the interval.
    pub hi_inclusive: bool,
    /// Slope of the limit in the observation interval Ï„ (ns per s).
    pub slope_ns_per_s: f64,
    /// Intercept of the limit, its value extrapolated to Ï„ = 0 (ns).
    pub intercept_ns: f64,
}

impl Segment {
    /// Whether `tau_s` lies in this piece's interval.
    pub fn contains(&self, tau_s: f64) -> bool {
        let above = if self.lo_inclusive {
            tau_s >= self.lo_s
        } else {
            tau_s > self.lo_s
        };
        let below = if self.hi_inclusive {
            tau_s <= self.hi_s
        } else {
            tau_s < self.hi_s
        };
        above && below
    }

    /// The limit at `tau_s`, in nanoseconds (meaningful only inside the interval).
    pub fn limit_ns(&self, tau_s: f64) -> f64 {
        self.slope_ns_per_s * tau_s + self.intercept_ns
    }
}

const fn seg(
    lo_s: f64,
    lo_inclusive: bool,
    hi_s: f64,
    hi_inclusive: bool,
    slope_ns_per_s: f64,
    intercept_ns: f64,
) -> Segment {
    Segment {
        lo_s,
        lo_inclusive,
        hi_s,
        hi_inclusive,
        slope_ns_per_s,
        intercept_ns,
    }
}

/// The mask limit at `tau_s`, or `None` where the source defines no limit (outside the
/// tabulated range, or in a gap the table leaves between two rows).
pub fn limit_at(segments: &[Segment], tau_s: f64) -> Option<f64> {
    segments
        .iter()
        .find(|s| s.contains(tau_s))
        .map(|s| s.limit_ns(tau_s))
}

/// A piecewise MTIE or TDEV mask with the table it is transcribed from.
#[derive(Clone, Copy, Debug)]
pub struct CurveMask {
    /// The pieces of the mask, each with its own observation interval.
    pub segments: &'static [Segment],
    /// The table the mask is transcribed from.
    pub source: &'static str,
}

/// A single time-error limit with the table or clause it is transcribed from.
#[derive(Clone, Copy, Debug)]
pub struct Limit {
    /// The limit (ns).
    pub value_ns: f64,
    /// The table or clause the limit is transcribed from.
    pub source: &'static str,
}

/// The measurement filter a mask is defined through.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MeasurementFilter {
    /// No filter: the time error is compared as sampled (the one pulse per second case
    /// of ITU-T G.8272 and G.8272.1).
    None,
    /// A first-order low-pass filter with a 0.1 Hz bandwidth, as ITU-T G.8273.2 and
    /// G.8271.1 state for the low-pass-filtered time error.
    LowPass0p1Hz,
}

impl MeasurementFilter {
    /// The filter as it is written in the report.
    pub fn as_str(self) -> &'static str {
        match self {
            MeasurementFilter::None => "none",
            MeasurementFilter::LowPass0p1Hz => "first-order low-pass, 0.1 Hz",
        }
    }
}

/// The operating condition a mask is written for.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MaskCondition {
    /// The mask applies while the clock is locked to its reference.
    Locked,
    /// The mask applies while the clock is in holdover.
    Holdover,
}

impl MaskCondition {
    /// The condition as it is written in the report (`locked` or `holdover`).
    pub fn as_str(self) -> &'static str {
        match self {
            MaskCondition::Locked => "locked",
            MaskCondition::Holdover => "holdover",
        }
    }
}

/// One selectable mask: everything a Recommendation states for one clock type or one
/// reference point, and nothing it leaves for further study.
#[derive(Clone, Copy, Debug)]
pub struct TimingMask {
    /// Identifier a scenario selects the mask by (for example `prtc-a`).
    pub id: &'static str,
    /// Name of the clock type or reference point the mask is for.
    pub title: &'static str,
    /// The ITU-T Recommendation, with its edition, the mask is transcribed from.
    pub recommendation: &'static str,
    /// The operating condition the mask is written for.
    pub condition: MaskCondition,
    /// The measurement filter the mask is defined through.
    pub filter: MeasurementFilter,
    /// Maximum absolute time error (after the filter, when there is one).
    pub max_abs_te: Option<Limit>,
    /// Permissible range of constant time error, ± this value.
    pub cte: Option<Limit>,
    /// Maximum time interval error (MTIE) mask, when the Recommendation states one.
    pub mtie: Option<CurveMask>,
    /// Time deviation (TDEV) mask, when the Recommendation states one.
    pub tdev: Option<CurveMask>,
    /// Whether the ITU-T G.8272.1 ePRTC-A holdover time-error envelope applies.
    pub holdover_envelope: bool,
    /// What the Recommendation leaves for further study for this clock or point, so a
    /// reader sees what is absent rather than assuming it was checked.
    pub not_specified: &'static str,
}

const G8272: &str = "ITU-T G.8272/Y.1367 (07/2025)";
const G8272_1: &str = "ITU-T G.8272.1/Y.1367.1 (2024) Amd. 1 (07/2025)";
const G8273_2: &str = "ITU-T G.8273.2/Y.1368.2 (2023) Amd. 2 (11/2025)";
const G8271_1: &str = "ITU-T G.8271.1/Y.1366.1 (2022) Amd. 3 (05/2025)";

const INF: f64 = f64::INFINITY;

// ITU-T G.8272 (07/2025) Table 1: 0.275e-3·τ + 0.025 µs for 0.1 < τ ≤ 273 s, 0.10 µs above.
const PRTC_A_MTIE: &[Segment] = &[
    seg(0.1, false, 273.0, true, 0.275, 25.0),
    seg(273.0, false, INF, false, 0.0, 100.0),
];
// ITU-T G.8272 (07/2025) Table 2: 0.275e-3·τ + 0.025 µs for 0.1 < τ ≤ 54.5 s, 0.04 µs above.
const PRTC_B_MTIE: &[Segment] = &[
    seg(0.1, false, 54.5, true, 0.275, 25.0),
    seg(54.5, false, INF, false, 0.0, 40.0),
];
// ITU-T G.8272 (07/2025) Table 3.
const PRTC_A_TDEV: &[Segment] = &[
    seg(0.1, false, 100.0, true, 0.0, 3.0),
    seg(100.0, false, 1000.0, true, 0.03, 0.0),
    seg(1000.0, false, 10_000.0, false, 0.0, 30.0),
];
// ITU-T G.8272 (07/2025) Table 4.
const PRTC_B_TDEV: &[Segment] = &[
    seg(0.1, false, 100.0, true, 0.0, 1.0),
    seg(100.0, false, 500.0, true, 0.01, 0.0),
    seg(500.0, false, 100_000.0, false, 0.0, 5.0),
];
// ITU-T G.8272.1 (2024) Amd. 1 (07/2025) Table 1 (locked mode).
const EPRTC_MTIE: &[Segment] = &[
    seg(0.1, false, 1.0, true, 0.0, 4.0),
    seg(1.0, false, 100.0, true, 0.11114, 3.89),
    seg(100.0, false, 400_000.0, true, 0.0375e-3, 15.0),
    seg(400_000.0, false, INF, false, 0.0, 30.0),
];
// ITU-T G.8272.1 (2024) Amd. 1 (07/2025) Table 2 (locked mode).
const EPRTC_TDEV: &[Segment] = &[
    seg(0.1, false, 30_000.0, true, 0.0, 1.0),
    seg(30_000.0, false, 300_000.0, true, 3.33333e-5, 0.0),
    seg(300_000.0, false, 1_000_000.0, false, 0.0, 10.0),
];
// ITU-T G.8272.1 (2024) Amd. 1 (07/2025) Table 4 (ePRTC-A holdover). Longer observation
// intervals are for further study (Note 1 under the table).
const EPRTC_A_HOLDOVER_MTIE: &[Segment] = &[
    seg(0.1, false, 1.0, true, 0.0, 4.0),
    seg(1.0, false, 100.0, true, 0.11114, 3.89),
    seg(100.0, false, 10_000.0, true, 0.0375e-3, 15.0),
];
// ITU-T G.8272.1 (2024) Amd. 1 (07/2025) Table 5 (ePRTC-A holdover).
const EPRTC_A_HOLDOVER_TDEV: &[Segment] = &[seg(0.1, false, 10_000.0, true, 0.0, 1.0)];
// ITU-T G.8273.2 Amd. 2 Table 7-4 (dTE_L MTIE, constant temperature): m ≤ τ ≤ 1 000 s,
// with m = 1 s for a one pulse per second output (the table's note).
const TBC_AB_MTIE: &[Segment] = &[seg(1.0, true, 1000.0, true, 0.0, 40.0)];
const TBC_C_MTIE: &[Segment] = &[seg(1.0, true, 1000.0, true, 0.0, 10.0)];
// ITU-T G.8273.2 Amd. 2 Table 7-5 (dTE_L TDEV, constant temperature): classes A and B
// are written m < τ ≤ 1 000 s, class C m ≤ τ ≤ 1 000 s.
const TBC_AB_TDEV: &[Segment] = &[seg(1.0, false, 1000.0, true, 0.0, 4.0)];
const TBC_C_TDEV: &[Segment] = &[seg(1.0, true, 1000.0, true, 0.0, 2.0)];
// ITU-T G.8271.1 Amd. 3 Table 7-1 (reference point C, clause 7.3.1).
const POINT_C_MTIE: &[Segment] = &[
    seg(1.3, false, 2.4, true, 75.0, 100.0),
    seg(2.4, false, 275.0, true, 1.1, 277.0),
    seg(275.0, false, 10_000.0, true, 0.0, 580.0),
];
// ITU-T G.8271.1 Amd. 3 Table 7-2 (enhanced network limits, clause 7.3.2).
const POINT_C_ENHANCED_MTIE: &[Segment] = &[
    seg(1.3, false, 2.4, true, 9.7, 37.39),
    seg(2.4, false, 20.2, true, 2.25, 55.27),
    seg(20.2, false, 211.11, true, 0.52, 90.22),
    seg(211.11, false, 10_000.0, true, 0.0, 200.0),
];
// ITU-T G.8271.1 Amd. 3 Table 7-3 (PRTC in the access network, clause 7.3.3). The table
// writes "1 < τ < 400" and "400 < τ ≤ 10 000", so τ = 400 s exactly carries no limit.
const POINT_C_ACCESS_MTIE: &[Segment] = &[
    seg(1.0, false, 400.0, false, 0.0475, 25.0),
    seg(400.0, false, 10_000.0, true, 0.0, 44.0),
];

/// Every selectable mask.
pub const MASKS: &[TimingMask] = &[
    TimingMask {
        id: "prtc-a",
        title: "Primary reference time clock, class A (PRTC-A)",
        recommendation: G8272,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::None,
        max_abs_te: Some(Limit {
            value_ns: 100.0,
            source: "G.8272 clause 6.1",
        }),
        cte: None,
        mtie: Some(CurveMask {
            segments: PRTC_A_MTIE,
            source: "G.8272 Table 1",
        }),
        tdev: Some(CurveMask {
            segments: PRTC_A_TDEV,
            source: "G.8272 Table 3",
        }),
        holdover_envelope: false,
        not_specified: "G.8272 states no holdover requirement for a PRTC",
    },
    TimingMask {
        id: "prtc-b",
        title: "Primary reference time clock, class B (PRTC-B)",
        recommendation: G8272,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::None,
        max_abs_te: Some(Limit {
            value_ns: 40.0,
            source: "G.8272 clause 6.1",
        }),
        cte: None,
        mtie: Some(CurveMask {
            segments: PRTC_B_MTIE,
            source: "G.8272 Table 2",
        }),
        tdev: Some(CurveMask {
            segments: PRTC_B_TDEV,
            source: "G.8272 Table 4",
        }),
        holdover_envelope: false,
        not_specified: "G.8272 states no holdover requirement for a PRTC",
    },
    TimingMask {
        id: "eprtc",
        title: "Enhanced primary reference time clock (ePRTC), locked mode",
        recommendation: G8272_1,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::None,
        max_abs_te: Some(Limit {
            value_ns: 30.0,
            source: "G.8272.1 clause 6.1",
        }),
        cte: None,
        mtie: Some(CurveMask {
            segments: EPRTC_MTIE,
            source: "G.8272.1 Table 1",
        }),
        tdev: Some(CurveMask {
            segments: EPRTC_TDEV,
            source: "G.8272.1 Table 2",
        }),
        holdover_envelope: false,
        not_specified: "phase discontinuity (clause 7) is for further study",
    },
    TimingMask {
        id: "eprtc-a-holdover",
        title: "Enhanced primary reference time clock, class A (ePRTC-A), holdover",
        recommendation: G8272_1,
        condition: MaskCondition::Holdover,
        filter: MeasurementFilter::None,
        max_abs_te: None,
        cte: None,
        mtie: Some(CurveMask {
            segments: EPRTC_A_HOLDOVER_MTIE,
            source: "G.8272.1 Table 4",
        }),
        tdev: Some(CurveMask {
            segments: EPRTC_A_HOLDOVER_TDEV,
            source: "G.8272.1 Table 5",
        }),
        holdover_envelope: true,
        not_specified: "MTIE and TDEV beyond 10 000 s in holdover, and every ePRTC-B \
                        holdover requirement, are for further study",
    },
    TimingMask {
        id: "t-bc-class-a",
        title: "Telecom boundary clock / telecom time slave clock (T-BC/T-TSC), class A",
        recommendation: G8273_2,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::LowPass0p1Hz,
        max_abs_te: Some(Limit {
            value_ns: 100.0,
            source: "G.8273.2 Table 7-1 (unfiltered)",
        }),
        cte: Some(Limit {
            value_ns: 50.0,
            source: "G.8273.2 Table 7-3",
        }),
        mtie: Some(CurveMask {
            segments: TBC_AB_MTIE,
            source: "G.8273.2 Table 7-4",
        }),
        tdev: Some(CurveMask {
            segments: TBC_AB_TDEV,
            source: "G.8273.2 Table 7-5",
        }),
        holdover_envelope: false,
        not_specified: "phase/time holdover with both inputs lost is for further study",
    },
    TimingMask {
        id: "t-bc-class-b",
        title: "Telecom boundary clock / telecom time slave clock (T-BC/T-TSC), class B",
        recommendation: G8273_2,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::LowPass0p1Hz,
        max_abs_te: Some(Limit {
            value_ns: 70.0,
            source: "G.8273.2 Table 7-1 (unfiltered)",
        }),
        cte: Some(Limit {
            value_ns: 20.0,
            source: "G.8273.2 Table 7-3",
        }),
        mtie: Some(CurveMask {
            segments: TBC_AB_MTIE,
            source: "G.8273.2 Table 7-4",
        }),
        tdev: Some(CurveMask {
            segments: TBC_AB_TDEV,
            source: "G.8273.2 Table 7-5",
        }),
        holdover_envelope: false,
        not_specified: "phase/time holdover with both inputs lost is for further study",
    },
    TimingMask {
        id: "t-bc-class-c",
        title: "Telecom boundary clock / telecom time slave clock (T-BC/T-TSC), class C",
        recommendation: G8273_2,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::LowPass0p1Hz,
        max_abs_te: Some(Limit {
            value_ns: 30.0,
            source: "G.8273.2 Table 7-1 (unfiltered)",
        }),
        cte: Some(Limit {
            value_ns: 10.0,
            source: "G.8273.2 Table 7-3",
        }),
        mtie: Some(CurveMask {
            segments: TBC_C_MTIE,
            source: "G.8273.2 Table 7-4",
        }),
        tdev: Some(CurveMask {
            segments: TBC_C_TDEV,
            source: "G.8273.2 Table 7-5",
        }),
        holdover_envelope: false,
        not_specified: "phase/time holdover with both inputs lost is for further study",
    },
    TimingMask {
        id: "t-bc-class-d",
        title: "Telecom boundary clock / telecom time slave clock (T-BC/T-TSC), class D",
        recommendation: G8273_2,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::LowPass0p1Hz,
        max_abs_te: Some(Limit {
            value_ns: 5.0,
            source: "G.8273.2 Table 7-2 (max|TE_L|, 0.1 Hz low-pass)",
        }),
        cte: None,
        mtie: None,
        tdev: None,
        holdover_envelope: false,
        not_specified: "class D unfiltered max|TE|, constant time error, dTE_L MTIE and \
                        TDEV, dTE_H and holdover are all for further study",
    },
    TimingMask {
        id: "g8271-1-point-c",
        title: "Network limit at reference point C, deployment case 1 (class 4 applications)",
        recommendation: G8271_1,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::LowPass0p1Hz,
        max_abs_te: Some(Limit {
            value_ns: 1100.0,
            source: "G.8271.1 clause 7.3.1 (max|TE_L|)",
        }),
        cte: None,
        mtie: Some(CurveMask {
            segments: POINT_C_MTIE,
            source: "G.8271.1 Table 7-1",
        }),
        tdev: None,
        holdover_envelope: false,
        not_specified: "the TDEV network limit is for further study; limits exclude \
                        rearrangements and long holdover",
    },
    TimingMask {
        id: "g8271-1-point-c-enhanced",
        title: "Enhanced network limit at reference point C, deployment case 1",
        recommendation: G8271_1,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::LowPass0p1Hz,
        max_abs_te: Some(Limit {
            value_ns: 600.0,
            source: "G.8271.1 clause 7.3.2 (max|TE_L|)",
        }),
        cte: None,
        mtie: Some(CurveMask {
            segments: POINT_C_ENHANCED_MTIE,
            source: "G.8271.1 Table 7-2",
        }),
        tdev: None,
        holdover_envelope: false,
        not_specified: "the TDEV network limit is for further study",
    },
    TimingMask {
        id: "g8271-1-point-c-access",
        title: "Network limit at reference point C with the PRTC in the access network",
        recommendation: G8271_1,
        condition: MaskCondition::Locked,
        filter: MeasurementFilter::LowPass0p1Hz,
        max_abs_te: Some(Limit {
            value_ns: 100.0,
            source: "G.8271.1 clause 7.3.3 (max|TE_L|)",
        }),
        cte: None,
        mtie: Some(CurveMask {
            segments: POINT_C_ACCESS_MTIE,
            source: "G.8271.1 Table 7-3",
        }),
        tdev: None,
        holdover_envelope: false,
        not_specified: "the TDEV network limit is for further study",
    },
];

/// The mask with this id.
pub fn mask_by_id(id: &str) -> Option<&'static TimingMask> {
    MASKS.iter().find(|m| m.id == id)
}

/// The ePRTC-A holdover period `H` (seconds) for a locked-mode duration of
/// `locked_days` before the loss: ITU-T G.8272.1 (2024) Amd. 1 (07/2025) clause 8.2.1
/// and Table 3.
pub fn eprtc_a_holdover_period_s(locked_days: f64) -> f64 {
    if locked_days < 6.0 {
        70_000.0
    } else if locked_days <= 40.0 {
        locked_days * 86_400.0
    } else {
        3_456_000.0
    }
}

/// The ePRTC-A holdover time-error limit `|Δx(t)|` (ns) at `t_s` seconds after the
/// start of holdover, for a locked-mode duration of `locked_days` — ITU-T G.8272.1
/// (2024) Amd. 1 (07/2025) Table 3. `None` outside `0 < t ≤ H`, where the table states
/// nothing.
pub fn eprtc_a_holdover_limit_ns(locked_days: f64, t_s: f64) -> Option<f64> {
    let h = eprtc_a_holdover_period_s(locked_days);
    if !(t_s > 0.0 && t_s <= h) {
        return None;
    }
    Some(if locked_days < 6.0 {
        30.0 + 1.000e-3 * t_s
    } else if locked_days <= 40.0 {
        30.0 + 70.0 * t_s / (locked_days * 86_400.0)
    } else {
        30.0 + 2.025463e-5 * t_s
    })
}

/// A maximum-absolute-time-error budget the report measures the time to exceed.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct Budget {
    /// Name of the budget, as reported.
    pub name: String,
    /// The budget: a maximum absolute time error (ns).
    pub max_abs_te_ns: f64,
    #[serde(default)]
    /// Where the budget comes from; empty when not given.
    pub source: String,
}

/// The default budgets: each is a number an ITU-T Recommendation states, named with
/// its clause or table.
pub fn default_budgets() -> Vec<Budget> {
    vec![
        Budget {
            name: "ePRTC default max|TE_HO|".into(),
            max_abs_te_ns: 100.0,
            source: "ITU-T G.8272.1 (2024) Amd. 1 (07/2025) clause 8.3.1: default maximum \
                     holdover time error"
                .into(),
        },
        Budget {
            name: "network holdover allocation, short GNSS interruption".into(),
            max_abs_te_ns: 400.0,
            source: "ITU-T G.8271.1 (2022) Amd. 3 (05/2025) Table V.1, failure scenario \
                     (b): rearrangements and holdover in the network (an example \
                     allocation, not a requirement)"
                .into(),
        },
        Budget {
            name: "network limit at reference point C".into(),
            max_abs_te_ns: 1100.0,
            source: "ITU-T G.8271.1 (2022) Amd. 3 (05/2025) clause 7.3.1: max|TE_L| at \
                     reference point C (a filtered quantity; compared here unfiltered)"
                .into(),
        },
        Budget {
            name: "end application, accuracy class 4".into(),
            max_abs_te_ns: 1500.0,
            source: "ITU-T G.8271 (03/2020) Amd. 1 (08/2024) Table 1, class 4: 1.5 \
                     microseconds"
                .into(),
        },
    ]
}

// ---------------------------------------------------------------------------------------
// Oscillator presets
// ---------------------------------------------------------------------------------------

/// A named oscillator with the figures of one public datasheet.
#[derive(Clone, Copy, Debug)]
pub struct OscillatorPreset {
    /// Identifier a scenario selects the preset by (`ocxo`, `rubidium`, `caesium` or
    /// `csac`).
    pub id: &'static str,
    /// The oscillator class, spelled out.
    pub class: &'static str,
    /// Maker's model name of the oscillator.
    pub model: &'static str,
    /// The datasheet the figures are read from, with its revision.
    pub document: &'static str,
    /// Where the datasheet can be downloaded.
    pub url: &'static str,
    /// Datasheet Allan deviation `(τ in s, σ_y)` points (maximum values), τ ≥ 1 s.
    pub adev_points: &'static [(f64, f64)],
    /// Linear fractional-frequency aging per day, as stated or converted as noted.
    pub aging_per_day: f64,
    /// The datasheet's aging statement and any conversion applied to it.
    pub aging_note: &'static str,
    /// Stated fractional-frequency change over the operating temperature range (±).
    pub temperature_bound: f64,
    /// Lower end of the datasheet operating temperature range (°C).
    pub temperature_min_c: f64,
    /// Upper end of the datasheet operating temperature range (°C).
    pub temperature_max_c: f64,
    /// The datasheet's temperature statement, with its reference temperature.
    pub temperature_note: &'static str,
}

impl OscillatorPreset {
    /// Fractional frequency per kelvin: the stated bound over half the operating span.
    /// A linear reading of a bound the datasheet states over the whole range, so a
    /// MODELLED worst case rather than a measured coefficient.
    pub fn temperature_coeff_per_k(&self) -> f64 {
        let half_span = 0.5 * (self.temperature_max_c - self.temperature_min_c);
        self.temperature_bound / half_span
    }
}

/// The four presets. Every figure below is read from the document named beside it.
pub const PRESETS: &[OscillatorPreset] = &[
    OscillatorPreset {
        id: "ocxo",
        class: "oven-controlled crystal oscillator (OCXO)",
        model: "Microchip OX-208, 10 MHz",
        document: "Microchip (Vectron) OX-208 Oven Controlled Crystal Oscillator \
                   datasheet, Rev 12-1-2021",
        url: "https://ww1.microchip.com/downloads/aemDocuments/documents/VOP/\
              ProductDocuments/DataSheets/OX-208.pdf",
        adev_points: &[(1.0, 5e-12), (10.0, 8e-12), (100.0, 1e-11), (1000.0, 5e-11)],
        aging_per_day: 0.15e-9,
        aging_note: "±0.15 ppb per day after 72 hours of operation (f ≤ 10 MHz)",
        temperature_bound: 0.4e-9,
        temperature_min_c: 0.0,
        temperature_max_c: 70.0,
        temperature_note: "±0.4 ppb over 0 °C to +70 °C, referenced to +25 °C",
    },
    OscillatorPreset {
        id: "rubidium",
        class: "rubidium frequency standard",
        model: "Microchip 8040C, standard performance",
        document: "Microchip 8040C Rubidium Frequency Standard datasheet, DS00003047A \
                   (2/20)",
        url: "https://ww1.microchip.com/downloads/en/DeviceDoc/00003047A.pdf",
        // The datasheet's 10 s row prints "<1.0 x 10^11", a misprinted exponent, so it
        // is left out rather than corrected by guesswork.
        adev_points: &[(1.0, 3.0e-11), (100.0, 3.0e-12)],
        aging_per_day: 5e-11 / 30.0,
        aging_note: "<5e-11 per month after 30 days of operation, converted at 30 days \
                     per month",
        temperature_bound: 3e-10,
        temperature_min_c: 0.0,
        temperature_max_c: 50.0,
        temperature_note: "temperature coefficient <3E-10 over the 0 °C to 50 °C \
                           operating range, read as a ± bound over that range",
    },
    OscillatorPreset {
        id: "caesium",
        class: "caesium beam primary frequency standard",
        model: "Microchip 5071A, high-performance tube",
        document: "Microchip 5071A Primary Frequency Standard datasheet, DS00002980D \
                   (5/25)",
        url: "https://ww1.microchip.com/downloads/aemDocuments/documents/FTD/\
              ProductDocuments/Brochures/5071A-Sell-Sheet-00002980.pdf",
        adev_points: &[
            (1.0, 5.0e-12),
            (10.0, 3.5e-12),
            (100.0, 8.5e-13),
            (1000.0, 2.7e-13),
            (10_000.0, 8.5e-14),
            (100_000.0, 2.7e-14),
            (432_000.0, 1.0e-14),
            (2_592_000.0, 1.0e-14),
        ],
        aging_per_day: 0.0,
        aging_note: "no aging rate is published; the datasheet bounds the lifetime \
                     change at 5.0e-14, which is not converted into a rate",
        temperature_bound: 8.0e-14,
        temperature_min_c: 0.0,
        temperature_max_c: 50.0,
        temperature_note: "±8.0e-14 frequency change versus environment (0 °C to 50 °C, \
                           humidity, magnetic field, shock), attributed wholly to \
                           temperature",
    },
    OscillatorPreset {
        id: "csac",
        class: "chip-scale atomic clock (CSAC)",
        model: "Microchip SA.45s CSAC, options 001 and 003",
        document: "Microchip SA.45s CSAC Options 001 and 003 datasheet, DS00002985D \
                   (5/23)",
        url: "https://ww1.microchip.com/downloads/aemDocuments/documents/FTD/\
              ProductDocuments/Brochures/SA.45s-CSAC-Options-001-and-003-00002985.pdf",
        adev_points: &[(1.0, 3e-10), (10.0, 1e-10), (100.0, 3e-11), (1000.0, 1e-11)],
        aging_per_day: 9e-10 / 30.0,
        aging_note: "<9e-10 per month after 30 days of continuous operation, converted \
                     at 30 days per month",
        temperature_bound: 5e-10,
        temperature_min_c: -10.0,
        temperature_max_c: 70.0,
        temperature_note: "±5e-10 maximum frequency change over −10 °C to 70 °C",
    },
];

/// The preset with this id.
pub fn preset_by_id(id: &str) -> Option<&'static OscillatorPreset> {
    PRESETS.iter().find(|p| p.id == id)
}

/// Power-law noise levels as Allan variances: `σ_y²(τ) = white/τ + flicker + rw·τ`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct NoiseFit {
    /// White frequency noise: the Allan variance at τ = 1 s from this term (s·1).
    pub white: f64,
    /// Flicker frequency noise: the flat Allan-variance floor.
    pub flicker: f64,
    /// Random-walk frequency noise: the Allan variance slope per second.
    pub random_walk: f64,
    /// The factor every term's Allan deviation was scaled up by so the model is at or
    /// above every datasheet point (1 when the fit already was).
    pub envelope_scale: f64,
}

impl NoiseFit {
    /// The model Allan deviation at `tau_s`.
    pub fn adev(&self, tau_s: f64) -> f64 {
        (self.white / tau_s + self.flicker + self.random_walk * tau_s).sqrt()
    }
}

/// Fit the three power-law levels to datasheet Allan-deviation points.
///
/// Least squares on the relative Allan-variance residual `model/spec² − 1`, which is
/// linear in the three non-negative levels; the non-negativity is enforced by trying
/// every subset of active terms and keeping the best feasible one. The fit is then
/// scaled up, if needed, until the model is at or above every point: the datasheet
/// figures are maxima, so the model is an envelope of them, not a median through them.
pub fn fit_noise(points: &[(f64, f64)]) -> NoiseFit {
    let rows: Vec<[f64; 3]> = points
        .iter()
        .map(|&(t, s)| {
            let v = s * s;
            [1.0 / (t * v), 1.0 / v, t / v]
        })
        .collect();
    let mut best: Option<([f64; 3], f64)> = None;
    for mask in 1u8..8 {
        let active: Vec<usize> = (0..3).filter(|i| mask & (1 << i) != 0).collect();
        let k = active.len();
        let mut ata = vec![vec![0.0; k]; k];
        let mut atb = vec![0.0; k];
        for r in &rows {
            for (a, &i) in active.iter().enumerate() {
                atb[a] += r[i];
                for (b, &j) in active.iter().enumerate() {
                    ata[a][b] += r[i] * r[j];
                }
            }
        }
        let Some(sol) = solve_small(ata, atb) else {
            continue;
        };
        if sol.iter().any(|&x| x.is_nan() || x < 0.0 || !x.is_finite()) {
            continue;
        }
        let mut coef = [0.0; 3];
        for (a, &i) in active.iter().enumerate() {
            coef[i] = sol[a];
        }
        let res: f64 = rows
            .iter()
            .map(|r| {
                let m = r[0] * coef[0] + r[1] * coef[1] + r[2] * coef[2];
                (m - 1.0) * (m - 1.0)
            })
            .sum();
        if best.as_ref().is_none_or(|(_, b)| res < *b) {
            best = Some((coef, res));
        }
    }
    let coef = best.map(|(c, _)| c).unwrap_or([0.0; 3]);
    let raw = NoiseFit {
        white: coef[0],
        flicker: coef[1],
        random_walk: coef[2],
        envelope_scale: 1.0,
    };
    let scale = points
        .iter()
        .map(|&(t, s)| s / raw.adev(t))
        .fold(1.0_f64, f64::max);
    let s2 = scale * scale;
    NoiseFit {
        white: raw.white * s2,
        flicker: raw.flicker * s2,
        random_walk: raw.random_walk * s2,
        envelope_scale: scale,
    }
}

/// Solve a small dense linear system by Gaussian elimination with partial pivoting.
fn solve_small(mut a: Vec<Vec<f64>>, mut b: Vec<f64>) -> Option<Vec<f64>> {
    let n = b.len();
    for col in 0..n {
        let piv = (col..n).max_by(|&i, &j| a[i][col].abs().total_cmp(&a[j][col].abs()))?;
        if a[piv][col].abs() < 1e-300 {
            return None;
        }
        a.swap(col, piv);
        b.swap(col, piv);
        for row in col + 1..n {
            let f = a[row][col] / a[col][col];
            let pivot_row = a[col].clone();
            for (dst, src) in a[row].iter_mut().zip(pivot_row.iter()).skip(col) {
                *dst -= f * src;
            }
            b[row] -= f * b[col];
        }
    }
    let mut x = vec![0.0; n];
    for i in (0..n).rev() {
        let s: f64 = (i + 1..n).map(|j| a[i][j] * x[j]).sum();
        x[i] = (b[i] - s) / a[i][i];
    }
    Some(x)
}

// ---------------------------------------------------------------------------------------
// Estimators
// ---------------------------------------------------------------------------------------

/// MTIE over windows of `m + 1` samples: the same number as [`crate::allan::mtie`],
/// computed with monotonic-queue sliding minimum and maximum in O(n).
pub fn mtie_sliding(x: &[f64], m: usize) -> f64 {
    assert!(m >= 1, "m must be >= 1");
    let win = m + 1;
    assert!(x.len() >= win, "need at least m+1 samples for MTIE");
    let mut maxq: std::collections::VecDeque<usize> = std::collections::VecDeque::new();
    let mut minq: std::collections::VecDeque<usize> = std::collections::VecDeque::new();
    let mut worst = 0.0_f64;
    for (i, &v) in x.iter().enumerate() {
        while maxq.back().is_some_and(|&j| x[j] <= v) {
            maxq.pop_back();
        }
        maxq.push_back(i);
        while minq.back().is_some_and(|&j| x[j] >= v) {
            minq.pop_back();
        }
        minq.push_back(i);
        if i + 1 >= win {
            let start = i + 1 - win;
            while maxq.front().is_some_and(|&j| j < start) {
                maxq.pop_front();
            }
            while minq.front().is_some_and(|&j| j < start) {
                minq.pop_front();
            }
            let (hi, lo) = (x[maxq[0]], x[minq[0]]);
            worst = worst.max(hi - lo);
        }
    }
    worst
}

/// Averaging factors on a roughly logarithmic grid (1, 2, 3, 5, 7 per decade) up to
/// `max_m`, with `max_m` itself appended.
pub fn tau_grid(max_m: usize) -> Vec<usize> {
    let mut out = Vec::new();
    let mut decade = 1usize;
    'outer: loop {
        for k in [1usize, 2, 3, 5, 7] {
            let m = k.saturating_mul(decade);
            if m > max_m {
                break 'outer;
            }
            out.push(m);
        }
        decade = match decade.checked_mul(10) {
            Some(d) => d,
            None => break,
        };
    }
    if max_m >= 1 && out.last() != Some(&max_m) {
        out.push(max_m);
    }
    out
}

/// One point of a curve: observation interval and value, both as reported.
#[derive(Clone, Copy, Debug, Serialize, PartialEq)]
pub struct CurvePoint {
    /// Observation interval (s).
    pub tau_s: f64,
    /// Value of the curve at that observation interval (ns).
    pub value_ns: f64,
}

/// The MTIE curve of `te_ns` (sample interval `dt_s`) on [`tau_grid`] up to the whole
/// record.
pub fn mtie_curve_ns(te_ns: &[f64], dt_s: f64) -> Vec<CurvePoint> {
    if te_ns.len() < 2 {
        return Vec::new();
    }
    tau_grid(te_ns.len() - 1)
        .into_iter()
        .map(|m| CurvePoint {
            tau_s: m as f64 * dt_s,
            value_ns: mtie_sliding(te_ns, m),
        })
        .collect()
}

/// The TDEV curve of `te_ns`, only at averaging times the record supports: the record
/// must be at least twelve times Ï„ long (the minimum measurement period ITU-T G.8272
/// clause 6.2 and G.8272.1 clause 6.2 state for TDEV), and the estimator needs more
/// than `3m` samples.
pub fn tdev_curve_ns(te_ns: &[f64], dt_s: f64) -> Vec<CurvePoint> {
    let n = te_ns.len();
    if n < 13 {
        return Vec::new();
    }
    let max_m = (n - 1) / 12;
    tau_grid(max_m)
        .into_iter()
        .filter(|&m| n > 3 * m)
        .map(|m| CurvePoint {
            tau_s: m as f64 * dt_s,
            value_ns: crate::allan::time_deviation(te_ns, dt_s, m),
        })
        .collect()
}

/// A first-order low-pass filter with a 0.1 Hz bandwidth, as the discrete exponential
/// smoother `y_k = y_{k-1} + α (x_k − y_{k-1})` with `α = 1 − exp(−2π·0.1·dt)`, started
/// at the first sample. An approximation of the test equipment's filter, not a copy of
/// any instrument's implementation.
pub fn low_pass_0p1hz(x: &[f64], dt_s: f64) -> Vec<f64> {
    let alpha = 1.0 - (-2.0 * std::f64::consts::PI * 0.1 * dt_s).exp();
    let mut out = Vec::with_capacity(x.len());
    let mut y = match x.first() {
        Some(&v) => v,
        None => return out,
    };
    for &v in x {
        y += alpha * (v - y);
        out.push(y);
    }
    out
}

/// The largest sample interval the 0.1 Hz filter is applied at: one second, the one
/// pulse per second sampling the masks are written for. Coarser records are reported
/// NOT-EVALUATED against a filtered mask rather than filtered badly.
pub const MAX_FILTER_DT_S: f64 = 1.0;

// ---------------------------------------------------------------------------------------
// Scenario
// ---------------------------------------------------------------------------------------

fn d_seed() -> u64 {
    20_260_925
}
fn d_masks() -> Vec<String> {
    vec!["eprtc-a-holdover".into(), "prtc-a".into()]
}
fn d_oscillator() -> String {
    "rubidium".into()
}
fn d_loss() -> f64 {
    3600.0
}
fn d_duration() -> f64 {
    90_000.0
}
fn d_dt() -> f64 {
    1.0
}
fn d_locked_sigma() -> f64 {
    1.0
}
fn d_temp_amp() -> f64 {
    2.0
}
fn d_temp_period() -> f64 {
    86_400.0
}
fn d_locked_days() -> f64 {
    5.0
}

/// The synthetic-holdover input.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HoldoverInput {
    /// One of `ocxo`, `rubidium`, `caesium`, `csac`.
    #[serde(default = "d_oscillator")]
    pub oscillator: String,
    /// Time of the GNSS loss from the start of the record (s).
    #[serde(default = "d_loss")]
    pub gnss_loss_s: f64,
    /// Record length (s).
    #[serde(default = "d_duration")]
    pub duration_s: f64,
    /// Sample interval (s).
    #[serde(default = "d_dt")]
    pub sample_interval_s: f64,
    /// White phase noise of the disciplined clock before the loss, 1 sigma (ns).
    #[serde(default = "d_locked_sigma")]
    pub locked_te_sigma_ns: f64,
    /// Residual fractional frequency offset at the moment of loss.
    #[serde(default)]
    pub initial_frequency_offset: f64,
    /// Peak temperature swing about the value at the loss (K).
    #[serde(default = "d_temp_amp")]
    pub temperature_amplitude_k: f64,
    /// Period of the temperature cycle (s).
    #[serde(default = "d_temp_period")]
    pub temperature_period_s: f64,
    /// Locked-mode duration before the loss (days), for the ePRTC-A holdover envelope.
    #[serde(default = "d_locked_days")]
    pub locked_duration_days: f64,
    /// Overrides of the preset's figures.
    #[serde(default)]
    pub aging_per_day: Option<f64>,
    #[serde(default)]
    /// Override of the preset's fractional frequency per kelvin (1/K).
    pub temperature_coeff_per_k: Option<f64>,
}

impl Default for HoldoverInput {
    fn default() -> Self {
        toml::from_str("").expect("an empty holdover table takes every default")
    }
}

/// The ingested-series input.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct SeriesInput {
    /// `[time_s, time_error_ns]` pairs, uniformly sampled.
    #[serde(default)]
    pub samples: Vec<[f64; 2]>,
    /// A CSV file of `time_s,time_error_ns` rows (native builds only; a header line and
    /// `#` comments are skipped).
    #[serde(default)]
    pub csv_path: Option<String>,
    /// Start of a holdover within the series (s), from which budgets and the ePRTC-A
    /// holdover envelope are timed. Absent: timed from the first sample, and the
    /// envelope is not evaluated.
    #[serde(default)]
    pub holdover_start_s: Option<f64>,
    /// Locked-mode duration before `holdover_start_s` (days), for the envelope.
    #[serde(default = "d_locked_days")]
    pub locked_duration_days: f64,
}

/// The `telecom-timing` scenario.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TelecomTimingScenario {
    #[serde(default)]
    /// Scenario kind (`telecom-timing`).
    pub kind: String,
    #[serde(default = "d_seed")]
    /// Seed of the ChaCha8 random-number generator the synthetic holdover draws its
    /// noise from.
    pub seed: u64,
    /// Mask ids from [`MASKS`].
    #[serde(default = "d_masks")]
    pub masks: Vec<String>,
    /// Time-error budgets; the [`default_budgets`] when absent.
    #[serde(default)]
    pub budgets: Option<Vec<Budget>>,
    #[serde(default)]
    /// The synthetic-holdover input; give this or `series`, not both.
    pub holdover: Option<HoldoverInput>,
    #[serde(default)]
    /// The ingested-series input; give this or `holdover`, not both.
    pub series: Option<SeriesInput>,
}

/// A time-error record: uniform samples starting at `t0_s`.
#[derive(Clone, Debug)]
pub struct TeRecord {
    /// Time stamp of the first sample (s).
    pub t0_s: f64,
    /// Interval between consecutive samples (s).
    pub dt_s: f64,
    /// Time error of each sample (ns).
    pub te_ns: Vec<f64>,
}

/// The oscillator model a synthetic holdover ran with.
#[derive(Clone, Debug)]
pub struct HoldoverModel {
    /// The oscillator preset the model starts from.
    pub preset: &'static OscillatorPreset,
    /// The noise model fitted to the preset's datasheet Allan deviation.
    pub fit: NoiseFit,
    /// Linear fractional-frequency aging per day used (1/d): the preset's figure unless
    /// overridden.
    pub aging_per_day: f64,
    /// Fractional frequency per kelvin used (1/K): the preset's figure unless
    /// overridden.
    pub temperature_coeff_per_k: f64,
}

/// Build the oscillator model for `h` (preset, fitted noise, and any override).
pub fn holdover_model(h: &HoldoverInput) -> Result<HoldoverModel, String> {
    let preset = preset_by_id(&h.oscillator).ok_or_else(|| {
        format!(
            "unknown oscillator {:?}; expected one of {}",
            h.oscillator,
            PRESETS.iter().map(|p| p.id).collect::<Vec<_>>().join(", ")
        )
    })?;
    let aging = h.aging_per_day.unwrap_or(preset.aging_per_day);
    let tc = h
        .temperature_coeff_per_k
        .unwrap_or_else(|| preset.temperature_coeff_per_k());
    if !aging.is_finite() || !tc.is_finite() {
        return Err("aging_per_day and temperature_coeff_per_k must be finite".into());
    }
    Ok(HoldoverModel {
        preset,
        fit: with_flicker_floor(fit_noise(preset.adev_points), preset.adev_points),
        aging_per_day: aging,
        temperature_coeff_per_k: tc,
    })
}

/// The stated rule for the flicker floor: never below the datasheet's Allan deviation
/// at its longest listed averaging time. A datasheet stops listing where its maker
/// stops promising improvement, so the model does not let the clock keep improving
/// past that point on the strength of a white-noise extrapolation. A MODELLED
/// assumption that can only make the holdover worse, never better.
pub const FLICKER_FLOOR_RULE: &str = "flicker floor = max(least-squares fit, the datasheet \
                                      Allan deviation at its longest listed averaging time)";

/// Apply [`FLICKER_FLOOR_RULE`] to a fit.
pub fn with_flicker_floor(fit: NoiseFit, points: &[(f64, f64)]) -> NoiseFit {
    let last = points
        .iter()
        .max_by(|a, b| a.0.total_cmp(&b.0))
        .map(|&(_, s)| s * s)
        .unwrap_or(0.0);
    NoiseFit {
        flicker: fit.flicker.max(last),
        ..fit
    }
}

/// The temperature term's phase contribution (s) at `tau_s` after the loss:
/// `∫ k·A·sin(2πt/P) dt = k·A·P/(2π)·(1 − cos(2πτ/P))`.
pub fn temperature_phase_s(coeff_per_k: f64, amp_k: f64, period_s: f64, tau_s: f64) -> f64 {
    let w = 2.0 * std::f64::consts::PI / period_s;
    coeff_per_k * amp_k / w * (1.0 - (w * tau_s).cos())
}

/// Synthesise the time-error record of a GNSS loss followed by free-running holdover.
pub fn synthesize_holdover(
    h: &HoldoverInput,
    model: &HoldoverModel,
    seed: u64,
) -> Result<TeRecord, String> {
    let dt = h.sample_interval_s;
    if !(dt.is_finite() && dt > 0.0) {
        return Err("sample_interval_s must be finite and positive".into());
    }
    if !(h.duration_s.is_finite() && h.duration_s >= 12.0 * dt) {
        return Err("duration_s must be finite and at least twelve samples long".into());
    }
    if !(h.gnss_loss_s.is_finite() && h.gnss_loss_s >= 0.0 && h.gnss_loss_s < h.duration_s) {
        return Err("gnss_loss_s must lie in [0, duration_s)".into());
    }
    if !(h.locked_te_sigma_ns.is_finite() && h.locked_te_sigma_ns >= 0.0) {
        return Err("locked_te_sigma_ns must be finite and >= 0".into());
    }
    if !(h.temperature_period_s.is_finite()
        && h.temperature_period_s > 0.0
        && h.temperature_amplitude_k.is_finite()
        && h.initial_frequency_offset.is_finite())
    {
        return Err("temperature and frequency-offset inputs must be finite (period > 0)".into());
    }
    let n = (h.duration_s / dt).floor() as usize + 1;
    if n > 5_000_000 {
        return Err("the record would exceed 5 000 000 samples; raise sample_interval_s".into());
    }
    let hold_len = h.duration_s - h.gnss_loss_s;
    let mut clock = ClockModel::new(
        "telecom-holdover",
        model.preset.document,
        h.initial_frequency_offset,
        model.fit.white,
        3.0 * model.fit.random_walk,
    )
    .with_drift(model.aging_per_day / 86_400.0);
    if model.fit.flicker > 0.0 {
        clock = clock.with_flicker_band(model.fit.flicker.sqrt(), dt, hold_len.max(10.0 * dt), 4);
    }
    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    let locked = if h.locked_te_sigma_ns > 0.0 {
        Some(Normal::new(0.0, h.locked_te_sigma_ns).map_err(|e| e.to_string())?)
    } else {
        None
    };
    let mut te = Vec::with_capacity(n);
    for k in 0..n {
        let t = k as f64 * dt;
        if t < h.gnss_loss_s {
            te.push(locked.map(|d| d.sample(&mut rng)).unwrap_or(0.0));
        } else {
            let tau = t - h.gnss_loss_s;
            let x = clock.phase()
                + temperature_phase_s(
                    model.temperature_coeff_per_k,
                    h.temperature_amplitude_k,
                    h.temperature_period_s,
                    tau,
                );
            te.push(x * 1e9);
            clock.step(dt, &mut rng);
        }
    }
    Ok(TeRecord {
        t0_s: 0.0,
        dt_s: dt,
        te_ns: te,
    })
}

/// Build a record from `(time_s, time_error_ns)` pairs, requiring uniform sampling.
pub fn record_from_pairs(pairs: &[[f64; 2]]) -> Result<TeRecord, String> {
    if pairs.len() < 13 {
        return Err(format!(
            "a time-error series needs at least 13 samples, got {}",
            pairs.len()
        ));
    }
    if pairs.iter().any(|p| !p[0].is_finite() || !p[1].is_finite()) {
        return Err("every time_s and time_error_ns must be finite".into());
    }
    let dt = pairs[1][0] - pairs[0][0];
    if dt.is_nan() || dt <= 0.0 {
        return Err("time_s must increase".into());
    }
    for (i, w) in pairs.windows(2).enumerate() {
        let d = w[1][0] - w[0][0];
        if (d - dt).abs() > 1e-6 * dt.max(1.0) {
            return Err(format!(
                "the series must be uniformly sampled: step {} is {d} s, the first is {dt} s",
                i + 1
            ));
        }
    }
    Ok(TeRecord {
        t0_s: pairs[0][0],
        dt_s: dt,
        te_ns: pairs.iter().map(|p| p[1]).collect(),
    })
}

/// Parse CSV text of `time_s,time_error_ns` rows. The first non-comment line may be a
/// header that does not parse as numbers; blank lines and `#` comments are skipped.
pub fn parse_csv_pairs(text: &str) -> Result<Vec<[f64; 2]>, String> {
    let mut out = Vec::new();
    let mut header_seen = false;
    for (i, line) in text.lines().enumerate() {
        let l = line.trim();
        if l.is_empty() || l.starts_with('#') {
            continue;
        }
        let mut it = l.split(',').map(|s| s.trim().parse::<f64>());
        match (it.next(), it.next()) {
            (Some(Ok(t)), Some(Ok(v))) => out.push([t, v]),
            _ if out.is_empty() && !header_seen => header_seen = true,
            _ => return Err(format!("CSV line {} is not `time_s,time_error_ns`", i + 1)),
        }
    }
    Ok(out)
}

#[cfg(not(target_arch = "wasm32"))]
fn read_csv(path: &str) -> Result<Vec<[f64; 2]>, String> {
    let text =
        std::fs::read_to_string(path).map_err(|e| format!("cannot read csv_path {path:?}: {e}"))?;
    parse_csv_pairs(&text)
}

#[cfg(target_arch = "wasm32")]
fn read_csv(_path: &str) -> Result<Vec<[f64; 2]>, String> {
    Err(
        "csv_path is not available in the WebAssembly build; give the series inline as \
         `samples`"
            .into(),
    )
}

/// The outcome of one check.
#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct CheckResult {
    /// The quantity checked: `max_abs_te`, `cte`, `mtie`, `tdev` or
    /// `holdover_te_envelope`.
    pub metric: &'static str,
    /// The ITU-T table or clause the limit is transcribed from.
    pub source: &'static str,
    /// `PASS`, `FAIL`, `INCOMPLETE` or `NOT-EVALUATED`.
    pub verdict: &'static str,
    /// The limit at the binding point (ns); null when nothing was evaluated.
    pub limit_ns: Option<f64>,
    /// The measured value at the binding point (ns).
    pub value_ns: Option<f64>,
    /// `limit − value` at the binding point (ns); negative on a FAIL.
    pub margin_ns: Option<f64>,
    /// For a curve: the observation interval of the binding point (s).
    pub worst_tau_s: Option<f64>,
    /// For a curve: how many points of the curve fell inside the mask's range.
    pub points_evaluated: usize,
    /// Explanation of the result; empty when there is nothing to add.
    pub note: String,
}

fn verdict(ok: bool) -> &'static str {
    if ok {
        "PASS"
    } else {
        "FAIL"
    }
}

fn not_evaluated(metric: &'static str, source: &'static str, note: String) -> CheckResult {
    CheckResult {
        metric,
        source,
        verdict: "NOT-EVALUATED",
        limit_ns: None,
        value_ns: None,
        margin_ns: None,
        worst_tau_s: None,
        points_evaluated: 0,
        note,
    }
}

/// Check a curve against a piecewise mask. The binding point is the one with the
/// smallest margin `limit − value`.
pub fn check_curve(metric: &'static str, curve: &[CurvePoint], mask: &CurveMask) -> CheckResult {
    let mut best: Option<(f64, f64, f64)> = None; // (margin, tau, limit)
    let mut n = 0usize;
    let mut value_at = 0.0;
    for p in curve {
        if let Some(lim) = limit_at(mask.segments, p.tau_s) {
            n += 1;
            let margin = lim - p.value_ns;
            if best.is_none_or(|(m, _, _)| margin < m) {
                best = Some((margin, p.tau_s, lim));
                value_at = p.value_ns;
            }
        }
    }
    match best {
        None => not_evaluated(
            metric,
            mask.source,
            "no point of the curve falls inside the mask's observation-interval range".into(),
        ),
        Some((margin, tau, lim)) => CheckResult {
            metric,
            source: mask.source,
            verdict: verdict(margin >= 0.0),
            limit_ns: Some(lim),
            value_ns: Some(value_at),
            margin_ns: Some(margin),
            worst_tau_s: Some(tau),
            points_evaluated: n,
            note: String::new(),
        },
    }
}

fn check_scalar(metric: &'static str, limit: &Limit, value_ns: f64) -> CheckResult {
    let margin = limit.value_ns - value_ns;
    CheckResult {
        metric,
        source: limit.source,
        verdict: verdict(margin >= 0.0),
        limit_ns: Some(limit.value_ns),
        value_ns: Some(value_ns),
        margin_ns: Some(margin),
        worst_tau_s: None,
        points_evaluated: 1,
        note: String::new(),
    }
}

/// The worst absolute mean of consecutive non-overlapping 1 000 s blocks: the
/// constant-time-error estimate ITU-T G.8273.2 Table 7-3 Note 1 describes ("averaging
/// the time error sequence over 1 000 s"), taken over every block of the record.
pub fn worst_cte_ns(te_ns: &[f64], dt_s: f64) -> Option<f64> {
    let per = (1000.0 / dt_s).round() as usize;
    if per == 0 || te_ns.len() < per {
        return None;
    }
    te_ns
        .chunks_exact(per)
        .map(|c| (c.iter().sum::<f64>() / c.len() as f64).abs())
        .reduce(f64::max)
}

/// The result of checking one mask.
#[derive(Clone, Debug, Serialize)]
pub struct MaskResult {
    /// Identifier of the mask checked.
    pub id: &'static str,
    /// Name of the clock type or reference point the mask is for.
    pub title: &'static str,
    /// The ITU-T Recommendation, with its edition, the mask is transcribed from.
    pub recommendation: &'static str,
    /// The operating condition the mask is written for (`locked` or `holdover`).
    pub condition: &'static str,
    /// The measurement filter the mask is defined through.
    pub measurement_filter: &'static str,
    /// Overall verdict: `FAIL` if any check fails, else `INCOMPLETE`, `PASS` or
    /// `NOT-EVALUATED`.
    pub verdict: &'static str,
    /// One result per limit the mask states.
    pub checks: Vec<CheckResult>,
    /// What the Recommendation leaves for further study for this clock or point.
    pub not_specified: &'static str,
}

fn overall(checks: &[CheckResult]) -> &'static str {
    if checks.iter().any(|c| c.verdict == "FAIL") {
        "FAIL"
    } else if checks.iter().any(|c| c.verdict == "INCOMPLETE") {
        "INCOMPLETE"
    } else if checks.iter().any(|c| c.verdict == "PASS") {
        "PASS"
    } else {
        "NOT-EVALUATED"
    }
}

/// The ePRTC-A holdover envelope check.
#[derive(Clone, Debug, Serialize)]
pub struct EnvelopeResult {
    /// The ITU-T clause and table the envelope is transcribed from.
    pub source: &'static str,
    /// Locked-mode duration before the loss (days), L in G.8272.1 Table 3.
    pub locked_duration_days: f64,
    /// Holdover period H that G.8272.1 Table 3 assigns to that locked duration (s).
    pub holdover_period_s: f64,
    /// Whether the record reaches the end of the holdover period (within half a sample
    /// interval).
    pub covers_holdover_period: bool,
    /// First time after the start of holdover at which `|TE|` is above the envelope (s).
    pub time_to_exceed_s: Option<f64>,
    /// Smallest `limit − |TE|` inside the holdover period (ns).
    pub min_margin_ns: Option<f64>,
    /// `PASS`, `FAIL`, `INCOMPLETE` (the record ends before the holdover period does)
    /// or `NOT-EVALUATED`.
    pub verdict: &'static str,
}

/// Check `|TE|` against the ePRTC-A holdover envelope from `start_s` on.
pub fn check_envelope(rec: &TeRecord, start_s: f64, locked_days: f64) -> EnvelopeResult {
    let h = eprtc_a_holdover_period_s(locked_days);
    let mut exceed = None;
    let mut min_margin: Option<f64> = None;
    let mut last_tau = 0.0_f64;
    for (k, &x) in rec.te_ns.iter().enumerate() {
        let tau = rec.t0_s + k as f64 * rec.dt_s - start_s;
        if let Some(lim) = eprtc_a_holdover_limit_ns(locked_days, tau) {
            last_tau = last_tau.max(tau);
            let m = lim - x.abs();
            if min_margin.is_none_or(|v| m < v) {
                min_margin = Some(m);
            }
            if m < 0.0 && exceed.is_none() {
                exceed = Some(tau);
            }
        }
    }
    let covers = last_tau + 0.5 * rec.dt_s >= h;
    let verdict = match (min_margin, exceed) {
        (None, _) => "NOT-EVALUATED",
        (_, Some(_)) => "FAIL",
        (Some(_), None) if covers => "PASS",
        _ => "INCOMPLETE",
    };
    EnvelopeResult {
        source: "G.8272.1 clause 8.2.1, Table 3",
        locked_duration_days: locked_days,
        holdover_period_s: h,
        covers_holdover_period: covers,
        time_to_exceed_s: exceed,
        min_margin_ns: min_margin,
        verdict,
    }
}

/// Time to exceed one budget.
#[derive(Clone, Debug, Serialize)]
pub struct BudgetResult {
    /// Name of the budget.
    pub name: String,
    /// Where the budget comes from.
    pub source: String,
    /// The budget: a maximum absolute time error (ns).
    pub max_abs_te_ns: f64,
    /// First time after the start of holdover (or of the record) at which `|TE|` is
    /// above the budget (s); null when it never is.
    pub time_to_exceed_s: Option<f64>,
    /// Whether `|TE|` goes above the budget at any sample from the start on.
    pub exceeded: bool,
}

/// Time to exceed each budget, from `start_s` on, at the record's sample resolution.
pub fn budget_results(rec: &TeRecord, start_s: f64, budgets: &[Budget]) -> Vec<BudgetResult> {
    budgets
        .iter()
        .map(|b| {
            let t = rec.te_ns.iter().enumerate().find_map(|(k, &x)| {
                let tau = rec.t0_s + k as f64 * rec.dt_s - start_s;
                (tau >= 0.0 && x.abs() > b.max_abs_te_ns).then_some(tau)
            });
            BudgetResult {
                name: b.name.clone(),
                source: b.source.clone(),
                max_abs_te_ns: b.max_abs_te_ns,
                time_to_exceed_s: t,
                exceeded: t.is_some(),
            }
        })
        .collect()
}

/// Check the record against one mask.
pub fn check_mask(
    mask: &TimingMask,
    rec: &TeRecord,
    raw_mtie: &[CurvePoint],
    raw_tdev: &[CurvePoint],
    envelope: Option<&EnvelopeResult>,
) -> MaskResult {
    let mut checks = Vec::new();
    let filtered;
    let (series, mtie, tdev): (&[f64], Vec<CurvePoint>, Vec<CurvePoint>) = match mask.filter {
        MeasurementFilter::None => (&rec.te_ns, raw_mtie.to_vec(), raw_tdev.to_vec()),
        MeasurementFilter::LowPass0p1Hz => {
            if rec.dt_s > MAX_FILTER_DT_S {
                let note = format!(
                    "the mask is defined through a 0.1 Hz low-pass filter, which this \
                     engine applies only at sample intervals of {MAX_FILTER_DT_S} s or \
                     less; this record is sampled every {} s",
                    rec.dt_s
                );
                for (metric, present, source) in [
                    (
                        "max_abs_te",
                        mask.max_abs_te.is_some(),
                        mask.max_abs_te.map(|l| l.source),
                    ),
                    ("cte", mask.cte.is_some(), mask.cte.map(|l| l.source)),
                    ("mtie", mask.mtie.is_some(), mask.mtie.map(|c| c.source)),
                    ("tdev", mask.tdev.is_some(), mask.tdev.map(|c| c.source)),
                ] {
                    if present {
                        checks.push(not_evaluated(metric, source.unwrap_or(""), note.clone()));
                    }
                }
                return MaskResult {
                    id: mask.id,
                    title: mask.title,
                    recommendation: mask.recommendation,
                    condition: mask.condition.as_str(),
                    measurement_filter: mask.filter.as_str(),
                    verdict: overall(&checks),
                    checks,
                    not_specified: mask.not_specified,
                };
            }
            filtered = low_pass_0p1hz(&rec.te_ns, rec.dt_s);
            (
                &filtered,
                mtie_curve_ns(&filtered, rec.dt_s),
                tdev_curve_ns(&filtered, rec.dt_s),
            )
        }
    };
    if let Some(l) = &mask.max_abs_te {
        // G.8273.2 Table 7-1 is unfiltered even though the rest of the class is not.
        let unfiltered = l.source.contains("unfiltered");
        let src: &[f64] = if unfiltered { &rec.te_ns } else { series };
        let v = src.iter().fold(0.0_f64, |a, &x| a.max(x.abs()));
        checks.push(check_scalar("max_abs_te", l, v));
    }
    if let Some(l) = &mask.cte {
        match worst_cte_ns(series, rec.dt_s) {
            Some(v) => checks.push(check_scalar("cte", l, v)),
            None => checks.push(not_evaluated(
                "cte",
                l.source,
                "the record is shorter than the 1 000 s averaging the estimate needs".into(),
            )),
        }
    }
    if let Some(c) = &mask.mtie {
        checks.push(check_curve("mtie", &mtie, c));
    }
    if let Some(c) = &mask.tdev {
        checks.push(check_curve("tdev", &tdev, c));
    }
    if mask.holdover_envelope {
        match envelope {
            Some(e) => {
                let lim = e.min_margin_ns.map(|_| {
                    eprtc_a_holdover_limit_ns(e.locked_duration_days, e.holdover_period_s)
                });
                checks.push(CheckResult {
                    metric: "holdover_te_envelope",
                    source: e.source,
                    verdict: e.verdict,
                    limit_ns: lim.flatten(),
                    value_ns: None,
                    margin_ns: e.min_margin_ns,
                    worst_tau_s: e.time_to_exceed_s,
                    points_evaluated: usize::from(e.min_margin_ns.is_some()),
                    note: "limit_ns is the envelope at the end of the holdover period; \
                           margin_ns is the smallest margin inside it; worst_tau_s is the \
                           time to exceed, when the envelope is exceeded"
                        .into(),
                });
            }
            None => checks.push(not_evaluated(
                "holdover_te_envelope",
                "G.8272.1 clause 8.2.1, Table 3",
                "no holdover start is known for this record (set series.holdover_start_s)".into(),
            )),
        }
    }
    MaskResult {
        id: mask.id,
        title: mask.title,
        recommendation: mask.recommendation,
        condition: mask.condition.as_str(),
        measurement_filter: mask.filter.as_str(),
        verdict: overall(&checks),
        checks,
        not_specified: mask.not_specified,
    }
}

/// Everything a run computes, before it is rendered.
#[derive(Debug)]
pub struct TelecomRun {
    /// The time-error record checked.
    pub record: TeRecord,
    /// The MTIE curve of the record.
    pub mtie: Vec<CurvePoint>,
    /// The TDEV curve of the record.
    pub tdev: Vec<CurvePoint>,
    /// One result per selected mask.
    pub masks: Vec<MaskResult>,
    /// The ePRTC-A holdover envelope check; `None` when no holdover start is known.
    pub envelope: Option<EnvelopeResult>,
    /// Time to exceed each budget.
    pub budgets: Vec<BudgetResult>,
    /// Start of the holdover (s): the GNSS loss, or the series' `holdover_start_s`;
    /// `None` when unknown.
    pub holdover_start_s: Option<f64>,
    /// The oscillator model of a synthetic holdover; `None` for an ingested series.
    pub model: Option<HoldoverModel>,
}

/// SHA-256 of the time-error series, as little-endian IEEE-754 bytes.
pub fn series_sha256(te_ns: &[f64]) -> String {
    let mut h = Sha256::new();
    for x in te_ns {
        h.update(x.to_le_bytes());
    }
    hex::encode(h.finalize())
}

impl TelecomTimingScenario {
    fn mode(&self) -> Result<&'static str, String> {
        match (&self.holdover, &self.series) {
            (Some(_), Some(_)) => Err("give either [holdover] or [series], not both".into()),
            (_, Some(_)) => Ok("ingested-series"),
            _ => Ok("synthetic-holdover"),
        }
    }

    /// The canonical scenario hash: SHA-256 of the scenario as parsed (defaults
    /// filled), so the seed and every input are inside it.
    pub fn scenario_hash(&self) -> String {
        let c = serde_json::to_string(self).unwrap_or_default();
        let mut h = Sha256::new();
        h.update(c.as_bytes());
        hex::encode(h.finalize())
    }

    /// Compute the run.
    pub fn run(&self) -> Result<TelecomRun, String> {
        let mode = self.mode()?;
        let mut masks = Vec::new();
        for id in &self.masks {
            masks.push(mask_by_id(id).ok_or_else(|| {
                format!(
                    "unknown mask {id:?}; expected one of {}",
                    MASKS.iter().map(|m| m.id).collect::<Vec<_>>().join(", ")
                )
            })?);
        }
        let budgets = self.budgets.clone().unwrap_or_else(default_budgets);
        if budgets
            .iter()
            .any(|b| !(b.max_abs_te_ns.is_finite() && b.max_abs_te_ns > 0.0))
        {
            return Err("every budget max_abs_te_ns must be finite and positive".into());
        }
        let (record, start, locked_days, model) = if mode == "synthetic-holdover" {
            let h = self.holdover.clone().unwrap_or_default();
            let model = holdover_model(&h)?;
            let rec = synthesize_holdover(&h, &model, self.seed)?;
            (
                rec,
                Some(h.gnss_loss_s),
                h.locked_duration_days,
                Some(model),
            )
        } else {
            let s = self.series.clone().unwrap_or_default();
            let pairs = match (&s.csv_path, s.samples.is_empty()) {
                (Some(_), false) => {
                    return Err("give either series.samples or series.csv_path, not both".into())
                }
                (Some(p), true) => read_csv(p)?,
                (None, _) => s.samples.clone(),
            };
            let rec = record_from_pairs(&pairs)?;
            (rec, s.holdover_start_s, s.locked_duration_days, None)
        };
        if !(locked_days.is_finite() && locked_days >= 0.0) {
            return Err("locked_duration_days must be finite and >= 0".into());
        }
        let mtie = mtie_curve_ns(&record.te_ns, record.dt_s);
        let tdev = tdev_curve_ns(&record.te_ns, record.dt_s);
        let envelope = start.map(|s| check_envelope(&record, s, locked_days));
        let checked = masks
            .iter()
            .map(|m| check_mask(m, &record, &mtie, &tdev, envelope.as_ref()))
            .collect();
        let budgets = budget_results(&record, start.unwrap_or(record.t0_s), &budgets);
        Ok(TelecomRun {
            record,
            mtie,
            tdev,
            masks: checked,
            envelope,
            budgets,
            holdover_start_s: start,
            model,
        })
    }

    /// Run and render `(json, summary, svg)`.
    pub fn run_output(&self) -> Result<(String, String, String), String> {
        let r = self.run()?;
        let json = self.to_json(&r)?;
        let summary = summary_line(&r);
        let svg = to_svg(&r);
        Ok((json, summary, svg))
    }

    /// Run and render the CSV table: one row per MTIE observation interval, with the
    /// TDEV where it was computed and every selected mask's limit there.
    pub fn to_csv(&self) -> Result<String, String> {
        let r = self.run()?;
        Ok(csv_table(&r))
    }

    /// Run once and render `(json, summary, svg, csv)`.
    pub fn run_all(&self) -> Result<(String, String, String, String), String> {
        let r = self.run()?;
        Ok((
            self.to_json(&r)?,
            summary_line(&r),
            to_svg(&r),
            csv_table(&r),
        ))
    }

    fn to_json(&self, r: &TelecomRun) -> Result<String, String> {
        let rec = &r.record;
        let n = rec.te_ns.len();
        let max_abs = rec.te_ns.iter().fold(0.0_f64, |a, &x| a.max(x.abs()));
        let mean = rec.te_ns.iter().sum::<f64>() / n as f64;
        let oscillator = r.model.as_ref().map(|m| {
            let p = m.preset;
            serde_json::json!({
                "preset": p.id,
                "class": p.class,
                "model": p.model,
                "datasheet": p.document,
                "datasheet_url": p.url,
                "evidence": "MODELLED",
                "provenance": "stability, aging and temperature figures from the named \
                               datasheet; the power-law fit, the 30-day month, the linear \
                               temperature reading and the sinusoidal temperature profile \
                               are this engine's modelling choices",
                "flicker_floor_rule": FLICKER_FLOOR_RULE,
                "aging_note": p.aging_note,
                "temperature_note": p.temperature_note,
                "datasheet_adev": p.adev_points.iter().map(|&(t, s)| serde_json::json!({
                    "tau_s": t,
                    "adev": s,
                    "model_adev": m.fit.adev(t),
                    "model_over_datasheet": m.fit.adev(t) / s,
                })).collect::<Vec<_>>(),
                "aging_per_day": m.aging_per_day,
                "temperature_bound": p.temperature_bound,
                "temperature_min_c": p.temperature_min_c,
                "temperature_max_c": p.temperature_max_c,
                "temperature_coeff_per_k": m.temperature_coeff_per_k,
                "fitted": {
                    "white_fm_adev_1s": m.fit.white.sqrt(),
                    "flicker_floor_adev": m.fit.flicker.sqrt(),
                    "random_walk_fm_adev_1s": m.fit.random_walk.sqrt(),
                    "envelope_scale": m.fit.envelope_scale,
                },
            })
        });
        let holdover = match (&self.holdover, &r.model) {
            (_, Some(m)) => {
                let h = self.holdover.clone().unwrap_or_default();
                let t_end = h.duration_s - h.gnss_loss_s;
                let aging = 0.5 * (m.aging_per_day / 86_400.0) * t_end * t_end * 1e9;
                let temp_peak = 2.0
                    * m.temperature_coeff_per_k
                    * h.temperature_amplitude_k.abs()
                    * h.temperature_period_s
                    / (2.0 * std::f64::consts::PI)
                    * 1e9;
                let det_end = (h.initial_frequency_offset * t_end
                    + 0.5 * (m.aging_per_day / 86_400.0) * t_end * t_end
                    + temperature_phase_s(
                        m.temperature_coeff_per_k,
                        h.temperature_amplitude_k,
                        h.temperature_period_s,
                        t_end,
                    ))
                    * 1e9;
                serde_json::json!({
                    "gnss_loss_s": h.gnss_loss_s,
                    "holdover_duration_s": t_end,
                    "initial_frequency_offset": h.initial_frequency_offset,
                    "locked_te_sigma_ns": h.locked_te_sigma_ns,
                    "temperature_amplitude_k": h.temperature_amplitude_k,
                    "temperature_period_s": h.temperature_period_s,
                    "aging_te_at_end_ns": aging,
                    "temperature_te_peak_ns": temp_peak,
                    "deterministic_te_at_end_ns": det_end,
                })
            }
            _ => serde_json::Value::Null,
        };
        let doc = serde_json::json!({
            "kind": "telecom-timing",
            "label": "MODELLED — time error, MTIE and TDEV checked against transcribed \
                      ITU-T masks. The estimators are validated against allantools; the \
                      mask tables are transcriptions of the named Recommendations; a \
                      synthetic holdover is a model built from public datasheet figures, \
                      not a measurement of any unit. Not a conformance test.",
            "engine_version": env!("CARGO_PKG_VERSION"),
            "scenario_hash": self.scenario_hash(),
            "seed": self.seed,
            "mode": self.mode()?,
            "record": {
                "sample_interval_s": rec.dt_s,
                "n_samples": n,
                "record_length_s": (n - 1) as f64 * rec.dt_s,
                "start_s": rec.t0_s,
                "holdover_start_s": r.holdover_start_s,
                "series_sha256": series_sha256(&rec.te_ns),
            },
            "oscillator": oscillator,
            "holdover": holdover,
            "time_error": {
                "max_abs_te_ns": max_abs,
                "mean_te_ns": mean,
                "te_at_end_ns": rec.te_ns[n - 1],
            },
            "mtie": r.mtie.iter().map(|p| serde_json::json!({"tau_s": p.tau_s, "mtie_ns": p.value_ns})).collect::<Vec<_>>(),
            "tdev": r.tdev.iter().map(|p| serde_json::json!({"tau_s": p.tau_s, "tdev_ns": p.value_ns})).collect::<Vec<_>>(),
            "masks": r.masks,
            "holdover_envelope": r.envelope,
            "budgets": r.budgets,
            "not_implemented": NOT_IMPLEMENTED,
            "units": crate::field_schema::units_block(UNITS),
        });
        serde_json::to_string_pretty(&doc).map_err(|e| e.to_string())
    }
}

/// What a reader might expect here and will not find, stated in every report.
pub const NOT_IMPLEMENTED: &[&str] = &[
    "every requirement the Recommendations mark 'for further study' (class D beyond \
     max|TE_L|, ePRTC-B holdover, T-BC/T-TSC holdover with both inputs lost, TDEV \
     network limits, ePRTC-A holdover MTIE and TDEV beyond 10 000 s)",
    "high-pass-filtered dynamic time error (dTE_H, G.8273.2 Table 7-7 and G.8271.1 \
     clauses 7.3.2-7.3.3)",
    "the variable-temperature MTIE of G.8273.2 Table 7-6 and the physical-layer-assisted \
     holdover MTIE of G.8273.2 Table 7-10",
    "relative time error (cTE_R, dTE_R,L) between two outputs",
    "the moving-average packet filter G.8272 and G.8272.1 state for PTP interfaces: the \
     series is treated as one pulse per second time interval error",
];

fn summary_line(r: &TelecomRun) -> String {
    let max_abs = r.record.te_ns.iter().fold(0.0_f64, |a, &x| a.max(x.abs()));
    let masks: Vec<String> = r
        .masks
        .iter()
        .map(|m| format!("{} {}", m.id, m.verdict))
        .collect();
    let first_budget = r.budgets.first().map(|b| match b.time_to_exceed_s {
        Some(t) => format!(
            "; {:.0} ns budget exceeded after {:.0} s",
            b.max_abs_te_ns, t
        ),
        None => format!("; {:.0} ns budget never exceeded", b.max_abs_te_ns),
    });
    format!(
        "telecom-timing: max|TE| {:.1} ns over {:.0} s; {}{} (MODELLED)",
        max_abs,
        (r.record.te_ns.len() - 1) as f64 * r.record.dt_s,
        masks.join(", "),
        first_budget.unwrap_or_default()
    )
}

fn csv_table(r: &TelecomRun) -> String {
    let mut header = String::from("tau_s,mtie_ns,tdev_ns");
    let mut cols: Vec<(&'static [Segment], bool)> = Vec::new();
    for m in &r.masks {
        if let Some(mask) = mask_by_id(m.id) {
            if let Some(c) = mask.mtie {
                header.push_str(&format!(",{}_mtie_limit_ns", m.id));
                cols.push((c.segments, true));
            }
            if let Some(c) = mask.tdev {
                header.push_str(&format!(",{}_tdev_limit_ns", m.id));
                cols.push((c.segments, false));
            }
        }
    }
    let mut out = header;
    out.push('\n');
    for p in &r.mtie {
        let tdev = r
            .tdev
            .iter()
            .find(|q| q.tau_s == p.tau_s)
            .map(|q| format!("{:.6e}", q.value_ns))
            .unwrap_or_default();
        out.push_str(&format!("{},{:.6e},{}", p.tau_s, p.value_ns, tdev));
        for (segs, _) in &cols {
            match limit_at(segs, p.tau_s) {
                Some(v) => out.push_str(&format!(",{v:.6}")),
                None => out.push(','),
            }
        }
        out.push('\n');
    }
    out
}

// ---------------------------------------------------------------------------------------
// Chart
// ---------------------------------------------------------------------------------------

/// Two panels: |TE| against time with the budgets, and MTIE and TDEV on log-log axes
/// with the first selected mask's MTIE limit.
pub fn to_svg(r: &TelecomRun) -> String {
    let (w, h) = (1180.0, 460.0);
    let mut s = crate::chart::frame_open(
        w,
        h,
        "Telecom timing: time error, MTIE and TDEV",
        &summary_line(r),
    );
    // Left panel: |TE| (ns) vs time (h).
    let (lx, top, pw, ph) = (80.0, 80.0, 470.0, 320.0);
    let rec = &r.record;
    let n = rec.te_ns.len();
    let mut y_max = rec.te_ns.iter().fold(0.0_f64, |a, &x| a.max(x.abs()));
    for b in &r.budgets {
        if b.max_abs_te_ns < 4.0 * y_max.max(1e-9) {
            y_max = y_max.max(b.max_abs_te_ns);
        }
    }
    let y_max = if y_max > 0.0 { y_max * 1.05 } else { 1.0 };
    s.push_str(&crate::chart::y_axis(
        lx,
        top,
        pw,
        ph,
        y_max,
        "|time error| (ns)",
    ));
    s.push_str(&crate::chart::panel_axes(
        lx,
        top,
        pw,
        top + ph,
        "absolute time error against the budgets",
    ));
    let t_span = ((n - 1) as f64 * rec.dt_s).max(1e-9);
    let stride = (n / 600).max(1);
    let pts: Vec<String> = (0..n)
        .step_by(stride)
        .map(|k| {
            let x = lx + pw * (k as f64 * rec.dt_s) / t_span;
            let y = top + ph - ph * (rec.te_ns[k].abs() / y_max).min(1.0);
            format!("{x:.1},{y:.1}")
        })
        .collect();
    s.push_str(&format!(
        "<polyline fill=\"none\" stroke=\"{AMBER}\" stroke-width=\"1.2\" points=\"{}\"/>",
        pts.join(" ")
    ));
    for b in &r.budgets {
        if b.max_abs_te_ns <= y_max {
            let y = top + ph - ph * b.max_abs_te_ns / y_max;
            s.push_str(&format!(
                "<line x1=\"{lx:.0}\" y1=\"{y:.1}\" x2=\"{:.0}\" y2=\"{y:.1}\" stroke=\"{INK_3}\" stroke-dasharray=\"4 3\"/>\
                 <text x=\"{:.0}\" y=\"{:.1}\" font-size=\"10\" fill=\"{MUTED}\" text-anchor=\"end\">{:.0} ns</text>",
                lx + pw,
                lx + pw - 4.0,
                y - 3.0,
                b.max_abs_te_ns
            ));
        }
    }
    if let Some(t0) = r.holdover_start_s {
        let x = lx + pw * (t0 - rec.t0_s) / t_span;
        s.push_str(&format!(
            "<line x1=\"{x:.1}\" y1=\"{top:.0}\" x2=\"{x:.1}\" y2=\"{:.0}\" stroke=\"{BLUE}\" stroke-dasharray=\"2 3\"/>\
             <text x=\"{:.1}\" y=\"{:.0}\" font-size=\"10\" fill=\"{BLUE}\">holdover</text>",
            top + ph,
            x + 3.0,
            top + 12.0
        ));
    }
    s.push_str(&format!(
        "<text x=\"{:.0}\" y=\"{:.0}\" text-anchor=\"middle\" fill=\"{MUTED}\">time ({:.1} h record)</text>",
        lx + pw / 2.0,
        top + ph + 28.0,
        t_span / 3600.0
    ));
    // Right panel: log-log MTIE / TDEV.
    let (rx, rw) = (660.0, 480.0);
    s.push_str(&crate::chart::panel_axes(
        rx,
        top,
        rw,
        top + ph,
        "MTIE (solid), TDEV (dashed), first MTIE mask (grey), ns",
    ));
    let mut vals: Vec<f64> = r
        .mtie
        .iter()
        .chain(r.tdev.iter())
        .map(|p| p.value_ns)
        .filter(|v| *v > 0.0)
        .collect();
    let mask_curve: Option<Vec<(f64, f64)>> = r
        .masks
        .iter()
        .filter_map(|m| mask_by_id(m.id).and_then(|mm| mm.mtie))
        .next()
        .map(|c| {
            r.mtie
                .iter()
                .filter_map(|p| limit_at(c.segments, p.tau_s).map(|l| (p.tau_s, l)))
                .collect()
        });
    if let Some(mc) = &mask_curve {
        vals.extend(mc.iter().map(|&(_, l)| l));
    }
    let taus: Vec<f64> = r.mtie.iter().map(|p| p.tau_s).collect();
    if !vals.is_empty() && !taus.is_empty() {
        let (vlo, vhi) = vals
            .iter()
            .fold((f64::INFINITY, 0.0_f64), |(a, b), &v| (a.min(v), b.max(v)));
        let (ylo, yhi) = (
            vlo.log10().floor(),
            vhi.log10().ceil().max(vlo.log10().floor() + 1.0),
        );
        let (tlo, thi) = (
            taus[0].log10().floor(),
            taus[taus.len() - 1]
                .log10()
                .ceil()
                .max(taus[0].log10().floor() + 1.0),
        );
        let px = |t: f64| rx + rw * (t.log10() - tlo) / (thi - tlo);
        let py = |v: f64| top + ph - ph * (v.max(10f64.powf(ylo)).log10() - ylo) / (yhi - ylo);
        let mut d = ylo as i32;
        while d <= yhi as i32 {
            let y = py(10f64.powi(d));
            s.push_str(&format!(
                "<line x1=\"{rx:.0}\" y1=\"{y:.1}\" x2=\"{:.0}\" y2=\"{y:.1}\" stroke=\"{GRID}\"/>\
                 <text x=\"{:.0}\" y=\"{:.1}\" text-anchor=\"end\" fill=\"{MUTED}\" font-size=\"11\">1e{d}</text>",
                rx + rw,
                rx - 6.0,
                y + 4.0
            ));
            d += 1;
        }
        let mut e = tlo as i32;
        while e <= thi as i32 {
            let x = px(10f64.powi(e));
            s.push_str(&format!(
                "<text x=\"{x:.1}\" y=\"{:.0}\" text-anchor=\"middle\" fill=\"{MUTED}\" font-size=\"11\">1e{e} s</text>",
                top + ph + 16.0
            ));
            e += 1;
        }
        let line = |pts: &[(f64, f64)], colour: &str, dash: &str| {
            let p: Vec<String> = pts
                .iter()
                .filter(|&&(_, v)| v > 0.0)
                .map(|&(t, v)| format!("{:.1},{:.1}", px(t), py(v)))
                .collect();
            format!(
                "<polyline fill=\"none\" stroke=\"{colour}\" stroke-width=\"1.5\"{dash} points=\"{}\"/>",
                p.join(" ")
            )
        };
        if let Some(mc) = &mask_curve {
            s.push_str(&line(mc, INK_3, ""));
        }
        let m: Vec<(f64, f64)> = r.mtie.iter().map(|p| (p.tau_s, p.value_ns)).collect();
        let t: Vec<(f64, f64)> = r.tdev.iter().map(|p| (p.tau_s, p.value_ns)).collect();
        s.push_str(&line(&m, AMBER, ""));
        s.push_str(&line(&t, BLUE, " stroke-dasharray=\"5 3\""));
    }
    s.push_str("</svg>");
    s
}

// ---------------------------------------------------------------------------------------
// Units
// ---------------------------------------------------------------------------------------

/// Unit and provenance class for every numeric field the report emits.
pub const UNITS: &[FieldUnit] = {
    use crate::field_schema::ProvenanceClass::*;
    &[
        FieldUnit {
            path: "seed",
            unit: "1",
            provenance: Input,
            definition: "seed of the ChaCha8 random-number generator the synthetic holdover \
                         draws its noise from",
        },
        FieldUnit {
            path: "record.sample_interval_s",
            unit: "s",
            provenance: Input,
            definition: "interval between consecutive time-error samples",
        },
        FieldUnit {
            path: "record.n_samples",
            unit: "count",
            provenance: Computed,
            definition: "number of time-error samples in the record",
        },
        FieldUnit {
            path: "record.record_length_s",
            unit: "s",
            provenance: Computed,
            definition: "time from the first to the last sample",
        },
        FieldUnit {
            path: "record.start_s",
            unit: "s",
            provenance: Input,
            definition: "time stamp of the first sample",
        },
        FieldUnit {
            path: "record.holdover_start_s",
            unit: "s",
            provenance: Input,
            definition: "time the holdover starts (the GNSS loss), from which budgets and \
                         the ePRTC-A holdover envelope are timed",
        },
        FieldUnit {
            path: "oscillator.datasheet_adev[].tau_s",
            unit: "s",
            provenance: Spec,
            definition: "averaging time of a datasheet Allan-deviation figure",
        },
        FieldUnit {
            path: "oscillator.datasheet_adev[].adev",
            unit: "1",
            provenance: Spec,
            definition: "the datasheet's maximum Allan deviation at that averaging time",
        },
        FieldUnit {
            path: "oscillator.datasheet_adev[].model_adev",
            unit: "1",
            provenance: Modelled,
            definition: "Allan deviation of the fitted white + flicker + random-walk \
                         frequency-noise model at that averaging time",
        },
        FieldUnit {
            path: "oscillator.datasheet_adev[].model_over_datasheet",
            unit: "1",
            provenance: Computed,
            definition: "model Allan deviation divided by the datasheet figure; at least 1 \
                         because the model is scaled to envelope the datasheet maxima",
        },
        FieldUnit {
            path: "oscillator.aging_per_day",
            unit: "1/d",
            provenance: Spec,
            definition: "linear fractional-frequency aging per day used for the holdover \
                         (the datasheet figure, converted as the aging note states, unless \
                         the scenario overrides it)",
        },
        FieldUnit {
            path: "oscillator.temperature_bound",
            unit: "1",
            provenance: Spec,
            definition: "the datasheet's fractional-frequency change bound (±) over its \
                         operating temperature range",
        },
        FieldUnit {
            path: "oscillator.temperature_min_c",
            unit: "degC",
            provenance: Spec,
            definition: "lower end of the datasheet operating temperature range",
        },
        FieldUnit {
            path: "oscillator.temperature_max_c",
            unit: "degC",
            provenance: Spec,
            definition: "upper end of the datasheet operating temperature range",
        },
        FieldUnit {
            path: "oscillator.temperature_coeff_per_k",
            unit: "1/K",
            provenance: Modelled,
            definition: "fractional frequency per kelvin: the stated bound over half the \
                         operating span, a linear worst-case reading",
        },
        FieldUnit {
            path: "oscillator.fitted.white_fm_adev_1s",
            unit: "1",
            provenance: Modelled,
            definition: "Allan deviation at 1 s of the fitted white frequency noise",
        },
        FieldUnit {
            path: "oscillator.fitted.flicker_floor_adev",
            unit: "1",
            provenance: Modelled,
            definition: "flat Allan-deviation floor of the fitted flicker frequency noise",
        },
        FieldUnit {
            path: "oscillator.fitted.random_walk_fm_adev_1s",
            unit: "1",
            provenance: Modelled,
            definition: "Allan deviation at 1 s of the fitted random-walk frequency noise \
                         (it grows as the square root of the averaging time)",
        },
        FieldUnit {
            path: "oscillator.fitted.envelope_scale",
            unit: "1",
            provenance: Computed,
            definition: "factor the least-squares fit was scaled up by so it lies at or \
                         above every datasheet point",
        },
        FieldUnit {
            path: "holdover.gnss_loss_s",
            unit: "s",
            provenance: Input,
            definition: "time of the GNSS loss from the start of the record",
        },
        FieldUnit {
            path: "holdover.holdover_duration_s",
            unit: "s",
            provenance: Computed,
            definition: "record length after the GNSS loss",
        },
        FieldUnit {
            path: "holdover.initial_frequency_offset",
            unit: "1",
            provenance: Input,
            definition: "residual fractional frequency offset at the moment of loss",
        },
        FieldUnit {
            path: "holdover.locked_te_sigma_ns",
            unit: "ns",
            provenance: Input,
            definition: "1 sigma white phase noise of the disciplined clock before the loss",
        },
        FieldUnit {
            path: "holdover.temperature_amplitude_k",
            unit: "K",
            provenance: Input,
            definition: "peak temperature swing of the sinusoidal profile about its value at \
                         the loss",
        },
        FieldUnit {
            path: "holdover.temperature_period_s",
            unit: "s",
            provenance: Input,
            definition: "period of the sinusoidal temperature profile",
        },
        FieldUnit {
            path: "holdover.aging_te_at_end_ns",
            unit: "ns",
            provenance: ClosedForm,
            definition: "time error the aging alone accumulates by the end of the record, \
                         (1/2)·D·t^2",
        },
        FieldUnit {
            path: "holdover.temperature_te_peak_ns",
            unit: "ns",
            provenance: ClosedForm,
            definition: "largest time error the temperature term alone reaches, \
                         k·A·P/pi",
        },
        FieldUnit {
            path: "holdover.deterministic_te_at_end_ns",
            unit: "ns",
            provenance: ClosedForm,
            definition: "time error of the deterministic terms (offset, aging, temperature) \
                         at the end of the record, without noise",
        },
        FieldUnit {
            path: "time_error.max_abs_te_ns",
            unit: "ns",
            provenance: Computed,
            definition: "largest absolute time error in the record, unfiltered",
        },
        FieldUnit {
            path: "time_error.mean_te_ns",
            unit: "ns",
            provenance: Computed,
            definition: "mean time error over the record",
        },
        FieldUnit {
            path: "time_error.te_at_end_ns",
            unit: "ns",
            provenance: Computed,
            definition: "time error of the last sample",
        },
        FieldUnit {
            path: "mtie[].tau_s",
            unit: "s",
            provenance: Computed,
            definition: "observation interval of an MTIE point, m times the sample interval",
        },
        FieldUnit {
            path: "mtie[].mtie_ns",
            unit: "ns",
            provenance: Computed,
            definition: "maximum time interval error: the largest peak-to-peak time error in \
                         any window of that length",
        },
        FieldUnit {
            path: "tdev[].tau_s",
            unit: "s",
            provenance: Computed,
            definition: "observation interval of a TDEV point; only intervals at most one \
                         twelfth of the record are reported",
        },
        FieldUnit {
            path: "tdev[].tdev_ns",
            unit: "ns",
            provenance: Computed,
            definition: "time deviation, tau/sqrt(3) times the modified Allan deviation",
        },
        FieldUnit {
            path: "masks[].checks[].limit_ns",
            unit: "ns",
            provenance: Published,
            definition: "the limit the transcribed ITU-T table or clause states at the \
                         binding point",
        },
        FieldUnit {
            path: "masks[].checks[].value_ns",
            unit: "ns",
            provenance: Computed,
            definition: "the measured quantity at the binding point (filtered where the mask \
                         says so)",
        },
        FieldUnit {
            path: "masks[].checks[].margin_ns",
            unit: "ns",
            provenance: Computed,
            definition: "limit minus value at the binding point, the smallest over the \
                         curve; negative on a FAIL",
        },
        FieldUnit {
            path: "masks[].checks[].worst_tau_s",
            unit: "s",
            provenance: Computed,
            definition: "observation interval of the binding point (for the holdover \
                         envelope, the time to exceed it)",
        },
        FieldUnit {
            path: "masks[].checks[].points_evaluated",
            unit: "count",
            provenance: Computed,
            definition: "curve points that fell inside the mask's observation-interval range",
        },
        FieldUnit {
            path: "holdover_envelope.locked_duration_days",
            unit: "d",
            provenance: Input,
            definition: "locked-mode duration before the loss, L in G.8272.1 Table 3",
        },
        FieldUnit {
            path: "holdover_envelope.holdover_period_s",
            unit: "s",
            provenance: Published,
            definition: "holdover period H G.8272.1 Table 3 assigns to that locked duration",
        },
        FieldUnit {
            path: "holdover_envelope.time_to_exceed_s",
            unit: "s",
            provenance: Computed,
            definition: "first time after the start of holdover at which |TE| is above the \
                         ePRTC-A envelope, at the sample resolution",
        },
        FieldUnit {
            path: "holdover_envelope.min_margin_ns",
            unit: "ns",
            provenance: Computed,
            definition: "smallest envelope minus |TE| inside the holdover period",
        },
        FieldUnit {
            path: "budgets[].max_abs_te_ns",
            unit: "ns",
            provenance: Published,
            definition: "the budget: a maximum absolute time error from the named source",
        },
        FieldUnit {
            path: "budgets[].time_to_exceed_s",
            unit: "s",
            provenance: Computed,
            definition: "first time after the start of holdover (or of the record) at which \
                         |TE| is above the budget, at the sample resolution",
        },
    ]
};

#[cfg(test)]
mod tests {
    use super::*;

    fn scn(src: &str) -> TelecomTimingScenario {
        toml::from_str(src).expect("scenario parses")
    }

    #[test]
    fn mtie_sliding_equals_the_allan_reference_estimator() {
        let h = HoldoverInput {
            duration_s: 2000.0,
            gnss_loss_s: 100.0,
            ..HoldoverInput::default()
        };
        let model = holdover_model(&h).unwrap();
        let rec = synthesize_holdover(&h, &model, 7).unwrap();
        for m in [1usize, 2, 3, 7, 50, 333, 1999, 2000] {
            assert_eq!(
                mtie_sliding(&rec.te_ns, m),
                crate::allan::mtie(&rec.te_ns, m),
                "m = {m}"
            );
        }
    }

    #[test]
    fn mtie_hand_derived() {
        // x = [0, 3, 1, 4, 1, 5]: two-sample windows swing 3,2,3,3,4 -> 4; three-sample
        // windows {0,3,1}=3, {3,1,4}=3, {1,4,1}=3, {4,1,5}=4 -> 4; the whole record 5 - 0.
        let x = [0.0, 3.0, 1.0, 4.0, 1.0, 5.0];
        assert_eq!(mtie_sliding(&x, 1), 4.0);
        assert_eq!(mtie_sliding(&x, 2), 4.0);
        assert_eq!(mtie_sliding(&x, 5), 5.0);
    }

    #[test]
    fn tdev_hand_derived() {
        // x = [0, 1, 0, 1, 0, 1, 0], tau0 = 1, m = 1: every second difference is ±2, so
        // MVAR = 4 / (2·1·1) = 2, MDEV = sqrt(2), TDEV = 1/sqrt(3)·sqrt(2) = sqrt(2/3).
        let x = [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0];
        let t = crate::allan::time_deviation(&x, 1.0, 1);
        assert!((t - (2.0f64 / 3.0).sqrt()).abs() < 1e-15, "{t}");
    }

    #[test]
    fn mtie_curve_is_monotone_non_decreasing() {
        let h = HoldoverInput {
            duration_s: 20_000.0,
            ..HoldoverInput::default()
        };
        let model = holdover_model(&h).unwrap();
        let rec = synthesize_holdover(&h, &model, 3).unwrap();
        let c = mtie_curve_ns(&rec.te_ns, rec.dt_s);
        assert!(c.len() > 10);
        for w in c.windows(2) {
            assert!(w[1].value_ns >= w[0].value_ns, "{w:?}");
        }
    }

    #[test]
    fn mask_boundaries_follow_the_tables_exactly() {
        // G.8272 Table 1, PRTC-A: 0.275e-3·τ + 0.025 µs up to and including 273 s.
        let a = PRTC_A_MTIE;
        assert_eq!(limit_at(a, 0.1), None, "0.1 s is excluded (0.1 < Ï„)");
        assert!((limit_at(a, 273.0).unwrap() - (0.275 * 273.0 + 25.0)).abs() < 1e-9);
        assert_eq!(limit_at(a, 273.0 + 1e-9), Some(100.0));
        // G.8272 Table 2, PRTC-B: 40 ns above 54.5 s.
        assert!((limit_at(PRTC_B_MTIE, 54.5).unwrap() - 39.9875).abs() < 1e-9);
        assert_eq!(limit_at(PRTC_B_MTIE, 54.6), Some(40.0));
        // G.8272 Table 3: 30 ns up to but excluding 10 000 s.
        assert_eq!(limit_at(PRTC_A_TDEV, 9999.0), Some(30.0));
        assert_eq!(limit_at(PRTC_A_TDEV, 10_000.0), None);
        // G.8272.1 Table 1 continuity at its breakpoints.
        assert!((limit_at(EPRTC_MTIE, 1.0).unwrap() - 4.0).abs() < 1e-12);
        assert!((limit_at(EPRTC_MTIE, 100.0).unwrap() - 15.004).abs() < 1e-9);
        assert!((limit_at(EPRTC_MTIE, 400_000.0).unwrap() - 30.0).abs() < 1e-9);
        // G.8271.1 Table 7-3 leaves Ï„ = 400 s itself undefined.
        assert_eq!(limit_at(POINT_C_ACCESS_MTIE, 400.0), None);
        assert!(
            (limit_at(POINT_C_ACCESS_MTIE, 399.0).unwrap() - (0.0475 * 399.0 + 25.0)).abs() < 1e-9
        );
        // G.8271.1 Table 7-1 is continuous to rounding at 2.4 s and 275 s.
        assert!((limit_at(POINT_C_MTIE, 2.4).unwrap() - 280.0).abs() < 1e-9);
        assert!((limit_at(POINT_C_MTIE, 275.0).unwrap() - 579.5).abs() < 1e-9);
        // G.8273.2 Table 7-5: class A/B excludes Ï„ = m, class C includes it.
        assert_eq!(limit_at(TBC_AB_TDEV, 1.0), None);
        assert_eq!(limit_at(TBC_C_TDEV, 1.0), Some(2.0));
    }

    #[test]
    fn a_curve_exactly_on_the_limit_passes_and_just_above_fails() {
        let mask = mask_by_id("prtc-a").unwrap().mtie.unwrap();
        let on = [CurvePoint {
            tau_s: 1000.0,
            value_ns: 100.0,
        }];
        let r = check_curve("mtie", &on, &mask);
        assert_eq!(r.verdict, "PASS");
        assert_eq!(r.margin_ns, Some(0.0));
        let above = [CurvePoint {
            tau_s: 1000.0,
            value_ns: 100.0 + 1e-9,
        }];
        assert_eq!(check_curve("mtie", &above, &mask).verdict, "FAIL");
        let outside = [CurvePoint {
            tau_s: 0.05,
            value_ns: 1e9,
        }];
        assert_eq!(
            check_curve("mtie", &outside, &mask).verdict,
            "NOT-EVALUATED"
        );
    }

    #[test]
    fn eprtc_a_holdover_envelope_follows_table_3() {
        // L < 6 days: H = 70 000 s, 30 ns + 1e-3 ns/s, so 100 ns at H.
        assert_eq!(eprtc_a_holdover_period_s(5.0), 70_000.0);
        assert!((eprtc_a_holdover_limit_ns(5.0, 70_000.0).unwrap() - 100.0).abs() < 1e-9);
        assert_eq!(eprtc_a_holdover_limit_ns(5.0, 70_001.0), None);
        assert_eq!(eprtc_a_holdover_limit_ns(5.0, 0.0), None);
        // 6 ≤ L ≤ 40: H = L days, 100 ns at H.
        assert!((eprtc_a_holdover_limit_ns(10.0, 864_000.0).unwrap() - 100.0).abs() < 1e-9);
        // L > 40: H = 40 days, 30 + 2.025463e-5·t reaches 100 ns (to the table's rounding).
        let end = eprtc_a_holdover_limit_ns(41.0, 3_456_000.0).unwrap();
        assert!((end - 100.0).abs() < 1e-3, "{end}");
    }

    #[test]
    fn every_preset_carries_a_named_datasheet_and_figures() {
        assert_eq!(PRESETS.len(), 4);
        for p in PRESETS {
            assert!(p.model.contains("Microchip"), "{}", p.id);
            assert!(p.document.contains("datasheet"), "{}", p.id);
            assert!(p.url.starts_with("https://"), "{}", p.id);
            assert!(!p.adev_points.is_empty());
            assert!(p.aging_per_day >= 0.0 && !p.aging_note.is_empty());
            assert!(p.temperature_bound > 0.0 && !p.temperature_note.is_empty());
            assert!(p.temperature_max_c > p.temperature_min_c);
        }
        for id in ["ocxo", "rubidium", "caesium", "csac"] {
            assert!(preset_by_id(id).is_some(), "{id}");
        }
    }

    #[test]
    fn the_noise_fit_envelopes_every_datasheet_point() {
        for p in PRESETS {
            let f = with_flicker_floor(fit_noise(p.adev_points), p.adev_points);
            assert!(f.envelope_scale >= 1.0);
            let last = p.adev_points.last().unwrap().1;
            assert!(
                f.flicker.sqrt() >= last * (1.0 - 1e-12),
                "{}: floor below rule",
                p.id
            );
            let raw = fit_noise(p.adev_points);
            for &(t, s) in p.adev_points {
                // The floored model envelopes every point ...
                let r = f.adev(t) / s;
                assert!(r >= 1.0 - 1e-12, "{}: model/datasheet {r} at {t} s", p.id);
                // ... and the least-squares fit alone stays close to the datasheet: the
                // floor rule is the only thing allowed to lift the model further.
                let r_raw = raw.adev(t) / s;
                assert!(
                    (1.0 - 1e-12..3.0).contains(&r_raw),
                    "{}: fit is {r_raw}x the datasheet at {t} s",
                    p.id
                );
            }
        }
        // A pure white-FM specification is fitted back exactly.
        let f = fit_noise(&[(1.0, 1e-11), (100.0, 1e-12), (10_000.0, 1e-13)]);
        assert!((f.white.sqrt() - 1e-11).abs() < 1e-20);
        assert!(f.flicker < 1e-40 && f.random_walk < 1e-40);
    }

    #[test]
    fn pure_aging_matches_its_closed_form() {
        // No noise and no temperature: the record is the discrete aging ramp
        // D·dt²·k(k−1)/2, within 0.1 % of (1/2)·D·t² at the end of a day.
        let h = HoldoverInput {
            gnss_loss_s: 0.0,
            duration_s: 86_400.0,
            sample_interval_s: 10.0,
            locked_te_sigma_ns: 0.0,
            temperature_amplitude_k: 0.0,
            ..HoldoverInput::default()
        };
        let mut model = holdover_model(&h).unwrap();
        model.fit = NoiseFit {
            white: 0.0,
            flicker: 0.0,
            random_walk: 0.0,
            envelope_scale: 1.0,
        };
        let rec = synthesize_holdover(&h, &model, 1).unwrap();
        let d = model.aging_per_day / 86_400.0;
        let want = 0.5 * d * 86_400.0f64.powi(2) * 1e9;
        let got = *rec.te_ns.last().unwrap();
        assert!((got / want - 1.0).abs() < 1e-3, "{got} vs {want}");
    }

    #[test]
    fn same_seed_same_series_and_hash_different_seed_different_series() {
        let a =
            scn("kind = \"telecom-timing\"\n[holdover]\nduration_s = 3000\ngnss_loss_s = 600\n");
        let ra = a.run().unwrap();
        let rb = a.run().unwrap();
        assert_eq!(
            series_sha256(&ra.record.te_ns),
            series_sha256(&rb.record.te_ns)
        );
        assert_eq!(a.scenario_hash(), a.scenario_hash());
        let c = scn("kind = \"telecom-timing\"\nseed = 2\n[holdover]\nduration_s = 3000\ngnss_loss_s = 600\n");
        assert_ne!(a.scenario_hash(), c.scenario_hash());
        assert_ne!(
            series_sha256(&ra.record.te_ns),
            series_sha256(&c.run().unwrap().record.te_ns)
        );
    }

    #[test]
    fn ingested_series_is_checked_and_non_uniform_sampling_is_refused() {
        let pairs: Vec<String> = (0..200)
            .map(|k| format!("[{k}.0, {:.3}]", 0.2 * ((k as f64) * 0.1).sin()))
            .collect();
        let src = format!(
            "kind = \"telecom-timing\"\nmasks = [\"prtc-b\", \"t-bc-class-c\"]\n[series]\nsamples = [{}]\n",
            pairs.join(", ")
        );
        let r = scn(&src).run().unwrap();
        assert_eq!(r.record.te_ns.len(), 200);
        assert_eq!(r.masks[0].verdict, "PASS");
        assert!(r.envelope.is_none());
        let bad = "kind = \"telecom-timing\"\n[series]\nsamples = [[0,0],[1,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0],[13,0]]\n";
        assert!(scn(bad).run().unwrap_err().contains("uniformly sampled"));
    }

    #[test]
    fn csv_text_parses_with_a_header_and_comments() {
        let p = parse_csv_pairs("time_s,time_error_ns\n# comment\n0,1.5\n1,2.5\n").unwrap();
        assert_eq!(p, vec![[0.0, 1.5], [1.0, 2.5]]);
        assert!(parse_csv_pairs("0,1\nx,y\n").is_err());
        assert!(
            parse_csv_pairs("a,b\nc,d\n0,1\n").is_err(),
            "one header only"
        );
    }

    #[test]
    fn an_unknown_mask_or_oscillator_is_refused() {
        let e = scn("kind = \"telecom-timing\"\nmasks = [\"prtc-c\"]\n").run();
        assert!(e.err().unwrap().contains("unknown mask"));
        let e = scn("kind = \"telecom-timing\"\n[holdover]\noscillator = \"maser\"\n").run();
        assert!(e.err().unwrap().contains("unknown oscillator"));
    }

    #[test]
    fn a_coarse_record_is_not_evaluated_against_a_filtered_mask() {
        let src = "kind = \"telecom-timing\"\nmasks = [\"t-bc-class-c\"]\n[holdover]\nduration_s = 20000\nsample_interval_s = 10\n";
        let r = scn(src).run().unwrap();
        assert_eq!(r.masks[0].verdict, "NOT-EVALUATED");
    }

    #[test]
    fn the_units_block_describes_every_emitted_field() {
        for src in [
            "kind = \"telecom-timing\"\n[holdover]\nduration_s = 4000\n",
            "kind = \"telecom-timing\"\nmasks = [\"prtc-a\", \"t-bc-class-a\", \"g8271-1-point-c\", \"eprtc-a-holdover\"]\n[series]\nholdover_start_s = 100\nsamples = [[0,1],[1,2],[2,1],[3,2],[4,1],[5,2],[6,1],[7,2],[8,1],[9,2],[10,1],[11,2],[12,1],[13,2]]\n",
        ] {
            let (json, _, _) = scn(src).run_output().unwrap();
            let doc: serde_json::Value = serde_json::from_str(&json).unwrap();
            let audit = crate::field_schema::audit_document(&doc);
            assert!(audit.missing.is_empty(), "{:?}", audit.missing);
            assert!(audit.malformed.is_empty(), "{:?}", audit.malformed);
        }
    }
}