File size: 261,873 Bytes
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
a6c006f
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
 
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
a6c006f
 
 
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
 
 
a6c006f
 
626e1aa
 
a6c006f
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
 
 
 
 
 
 
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
a6c006f
626e1aa
 
 
a6c006f
626e1aa
a6c006f
 
 
 
 
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c006f
 
626e1aa
 
a6c006f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626e1aa
 
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
 
 
 
a6c006f
 
626e1aa
 
 
 
 
 
 
 
 
 
a6c006f
626e1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
"""CHEM-156 Bruker X4 POSEIDON benchtop micro-CT / 3D X-ray microscope, reflection-source configuration with the 7 MP flat-panel detector
(essential configuration: standard rotation stage, single detector mount, fixed scan geometry, automated 7-position filter changer):
runtime (SI units).

Physics owned by the host clock (``step`` is the only entry that advances time):
  * power and safety: mains switch, boot with homing of the stage and the filter changer, motorized lead-glass door with the X-ray interlock
    and the safety light curtain, emergency stop (latched until released and acknowledged), multi-function status light;
  * X-ray source (tungsten reflection target, sealed): kV / current set points limited to 100 kV and 20 W, ramp and a warm-up program
    after each power-up, focal spot growing with the target power (< 5 um at 4 W), Kramers thick-target bremsstrahlung with the tungsten
    self-absorption of a reflection target, W K and L lines, beryllium exit window, 7-position filter changer and air path (NIST SRD 126
    attenuation coefficients from the template library);
  * geometry: cone beam, fixed source-detector distance, the sample on a rotation stage carried along the beam axis (source-object distance
    = geometric magnification) and lifted vertically; exact constant-speed motion with event cuts; collision guard against the source window,
    the filter changer and the detector cover; hidden rotation-axis offset (geometry calibration and post-alignment);
  * flat-panel detector (CsI:Tl scintillator on a CMOS sensor, 2800 x 2400 pixels): absorbed energy per photon from mu and mu_en, conversion
    gain, compound-Poisson quantum noise, read noise, dark offset and dark current, 16-bit saturation, binning, exposure, frame averaging,
    fixed-pattern gain non-uniformity corrected by the dark / bright field calibration (stale after changing kV, current, filter, binning or
    exposure), optional random detector movement against ring artifacts, injected defective columns;
  * samples: analytic phantoms (cylinder with horizontal layers of library materials or volume mixtures, spherical pores and inclusions,
    planar cracks, optional container tube and mounting pin, calibration phantom with steel balls, tungsten edge for radiographic resolution);
    projections by exact ray tracing (chord lengths per material) and polychromatic Beer-Lambert transmission in energy groups (beam
    hardening follows), focal-spot penumbra and scintillator blur; exact truth (porosity, pores, cracks) for the evaluator;
  * reconstruction: Feldkamp (FDK) cone-beam filtered back-projection (Ram-Lak / Shepp-Logan / Hann), Parker weights for 180 deg + fan scans,
    beam-hardening linearisation for a named material, ring correction, post-alignment (manual or automatic), Gaussian smoothing, volume of
    interest; analysis: porosity (total, open, closed, pore sizes), crack detection (planar components), attenuation statistics, slices;
  * fault injection (evaluator): tube arc, defective detector columns, rotation stage stall, door interlock sensor, light curtain blocked;
  * ledgers: electrical energy into the tube, X-ray energy emitted and where it goes (target self-absorption, window, filter, air, sample
    absorbed and scattered, detector absorbed and passed, shielding), dose to the sample, projections, frames, stage travel.
"""
from __future__ import annotations

import copy
import functools
import hashlib
import json
import math
import os
import random
import tempfile
from pathlib import Path

import numpy as np


class ActionRejected(ValueError):
    pass


# ---------------------------------------------------------------- schema helpers
def number(unit='1', lo=None, hi=None):
    s = {'type': 'number', 'x-unit': unit}
    if lo is not None:
        s['minimum'] = lo
    if hi is not None:
        s['maximum'] = hi
    return s


def obj(properties):
    return dict(type='object', properties=properties, required=list(properties), additionalProperties=False)


def nullable(s):
    return {'anyOf': [s, {'type': 'null'}]}


def array(s, minimum=0, maximum=4096):
    return dict(type='array', items=s, minItems=minimum, maxItems=maximum)


def enum(items):
    return {'type': 'string', 'enum': list(items)}


BOOL = {'type': 'boolean'}
TEXT = {'type': 'string'}
INT = {'type': 'integer', 'x-unit': '1'}


def integer(lo, hi):
    return {'type': 'integer', 'x-unit': '1', 'minimum': lo, 'maximum': hi}


def validate(s, v):
    if 'anyOf' in s:
        for o in s['anyOf']:
            try:
                validate(o, v)
                return
            except ActionRejected:
                pass
        raise ActionRejected('INVALID_ARGUMENT')
    k = s.get('type')
    if k in ('number', 'integer'):
        if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v) or (k == 'integer' and not float(v).is_integer()):
            raise ActionRejected('INVALID_ARGUMENT')
        if not s.get('minimum', -math.inf) <= v <= s.get('maximum', math.inf):
            raise ActionRejected('INVALID_ARGUMENT')
    elif k == 'object':
        if not isinstance(v, dict):
            raise ActionRejected('INVALID_ARGUMENT')
        if 'properties' in s:                                            # required keys (obj(): all of them) and no others
            if not set(s.get('required', s['properties'])) <= set(v) <= set(s['properties']):
                raise ActionRejected('INVALID_ARGUMENT')
            for key, value in v.items():
                validate(s['properties'][key], value)
    elif k == 'array':
        if not isinstance(v, list) or not s.get('minItems', 0) <= len(v) <= s.get('maxItems', math.inf):
            raise ActionRejected('INVALID_ARGUMENT')
        for x in v:
            validate(s['items'], x)
    elif k == 'boolean' and type(v) is not bool:
        raise ActionRejected('INVALID_ARGUMENT')
    elif k == 'string':
        if not isinstance(v, str) or len(v) > s.get('maxLength', 10 ** 6) or len(v) < s.get('minLength', 0):
            raise ActionRejected('INVALID_ARGUMENT')
        if 'pattern' in s:
            import re
            if not re.fullmatch(s['pattern'], v):
                raise ActionRejected('INVALID_ARGUMENT')
    elif k == 'null' and v is not None:
        raise ActionRejected('INVALID_ARGUMENT')
    if 'enum' in s and v not in s['enum']:
        raise ActionRejected('INVALID_ARGUMENT')


# ---------------------------------------------------------------- identity, paths
PROTOCOL_VERSION = '1.0.0'
SLUG = 'bruker_x4poseidon'
ASSET_TYPE_ID = 'instrument.' + SLUG
CATALOG_ID = 'CHEM-156'
ROOT = Path(__file__).resolve().parents[2]
ASSET = ROOT / 'assets/expansion_batch04/t3_analytical' / SLUG
LIBRARY_PATH = ASSET / 'xray_attenuation_library.json'
LIBRARY_REL = str(LIBRARY_PATH.relative_to(ROOT))

# ---------------------------------------------------------------- documented (Bruker X4 POSEIDON product page, brochure, configurator)
XRAY_KV_MAX_V = 100.0e3                          # reflection source: up to 100 kV
XRAY_POWER_MAX_W = 20.0                          # reflection source: 20 W
SPOT_AT_4W_M = 5.0e-6                            # reflection source: < 5 um spot at 4 W
DETECTOR_PIXELS = (2800, 2400)                   # 7 MP flat panel, 2800 x 2400 pixels
SAMPLE_MAX_DIAMETER_M = 0.110                    # object size up to 110 mm in diameter
SAMPLE_MAX_HEIGHT_M = 0.300                      # object size up to 300 mm in height
FILTER_POSITIONS = 7                             # automated 7-position filter changer (standard with both sources; configurator)
DETAIL_DETECTABILITY_M = 5.0e-6                  # reflection source: detail detectability down to 5 um (configurator)
BODY_DIMENSIONS_M = (1.250, 0.604, 0.690)        # W 1250 x D 604 x H 690 mm (specification table)
MASS_KG = 350.0                                  # weight 350 kg
MAINS_V = (100.0, 240.0)                         # 100 - 240 V AC, 50 - 60 Hz, 3 A max
MAINS_A_MAX = 3.0

# ---------------------------------------------------------------- engineering values (not published; used for the process model)
XRAY_KV_MIN_V = 20.0e3
XRAY_CURRENT_MAX_A = 0.5e-3                      # tube current limit besides the 20 W power limit
KV_RAMP_V_S = 10.0e3                             # high-voltage ramp
CURRENT_RAMP_A_S = 100.0e-6                      # emission-current ramp
WARMUP_S = 180.0                                 # conditioning program after every power-up of the tube (first enable after a boot)
SPOT_GROWTH_EXP = 1.0                            # focal spot = 5 um x max(1, P / 4 W)^exp (constant power density above 4 W)
TAKEOFF_RAD = math.radians(20.0)                 # take-off angle of the reflection target
BE_WINDOW_M = 0.25e-3                            # beryllium exit window
KRAMERS_K_PER_V = 1.1e-9                         # bremsstrahlung efficiency eta = K Z V (thick target)
K_LINE_FRACTION_100KV = 0.08                     # W K lines: fraction of the emitted X-ray energy at 100 kV, scaling (U - 1)^1.63
L_LINE_FRACTION_50KV = 0.04                      # W L lines: fraction at 50 kV, scaling (U_L - 1)^1.63
W_K_LINES_KEV = ((59.32, 0.46), (57.98, 0.27), (67.24, 0.19), (69.10, 0.08))     # energy (keV), relative intensity
W_L_LINES_KEV = ((8.40, 0.55), (9.67, 0.35), (11.29, 0.10))
W_K_EDGE_KEV, W_L3_EDGE_KEV = 69.525, 10.207
CONE_HALF_ANGLE_RAD = math.radians(20.0)         # emission cone of the source used for the shielding share of the ledger
SDD_M = 0.450                                    # source-detector distance (fixed scan geometry)
SOD_RANGE_M = (0.015, 0.395)                     # source-object distance (sample carriage on the beam rail)
CHUCK_Z_RANGE_M = (0.160, 0.399)                 # chuck top height above the bench (sample lift; limited by the rotation stage below it)
ENCLOSURE_X_M = 0.310                            # enclosure centre in the asset frame (the workstation stands on its left)
SOURCE_XYZ_M = (ENCLOSURE_X_M - 0.180, 0.020, 0.400)   # focal spot in the asset frame (beam along +X at this height)
WINDOW_OFFSET_M = 0.008                          # exit window face from the focal spot
FILTER_OFFSET_M = 0.010                          # filter plane from the focal spot
FILTER_CLEAR_M = 0.014                           # front face of the filter changer from the focal spot (collision guard)
DETECTOR_COVER_M = 0.012                         # detector entrance cover in front of the active area (collision guard)
COLLISION_MARGIN_M = 0.002
SOURCE_KEEPOUT_DZ = (-0.050, 0.050)              # source head block (the filter slide moves laterally at beam height): nothing closer than FILTER_CLEAR
DETECTOR_KEEPOUT_DZ = (-0.090, 0.090)            # detector housing below / above the beam
CHUCK_R_M, CHUCK_H_M = 0.012, 0.015              # brass chuck below the chuck top
STAGE_R_M, STAGE_H_M = 0.0566, 0.050             # 80 x 80 mm rotation-stage housing and platter below the chuck (circumscribed radius, conservative)
CHAMBER_CEILING_M = 0.640                        # inner ceiling height of the enclosure above the bench
PIXEL_PITCH_M = 49.5e-6                          # flat-panel pixel pitch (2800 x 49.5 um = 138.6 mm)
CSI_THICKNESS_M = 0.20e-3                        # CsI:Tl scintillator
ADU_PER_KEV = 0.8                                # conversion gain (energy deposited in the scintillator -> ADU)
READ_NOISE_ADU = 4.0
DARK_OFFSET_ADU = 100.0
DARK_CURRENT_ADU_S = 10.0
FULL_SCALE_ADU = 65535.0
DETECTOR_PSF_PX = 0.8                            # scintillator light spread, sigma in detector pixels
EXPOSURE_RANGE_S = (0.02, 10.0)
AVERAGING_RANGE = (1, 16)
BINNINGS = (1, 2, 4)
FRAME_OVERHEAD_S = 0.03                          # read-out per frame
STEP_OVERHEAD_S = 0.20                           # rotation step and settle per projection (step and shoot)
ROTATION_SPEED_RAD_S = math.radians(60.0)
CARRIAGE_SPEED_M_S = 0.020
LIFT_SPEED_M_S = 0.010
DOOR_TRAVEL_M = 0.400                            # the door slides to the left behind the front panel (direction: photo estimate)
DOOR_SPEED_M_S = 0.100
FILTER_PITCH_M = 0.012                           # lateral filter slide pitch between positions (slide moves along -Y at beam height)
FILTER_S_PER_STEP = 0.5
BOOT_S = 60.0
FLAT_FIELD_FRAMES = 20                           # bright frames (and as many dark frames) of the calibration
AXIS_OFFSET_SD_M = 40.0e-6                       # hidden lateral offset of the rotation axis (1 sigma, at the axis)
RECON_RATE_VOXEL_PROJ_S = 5.0e10                 # reconstruction throughput of the workstation (voxel x projection per s)
RECON_OVERHEAD_S = 5.0
SIM_DETECTOR = (175, 150)                        # simulated detector grid: 16 x 16 native pixels per simulated pixel
SIM_MAX_PROJECTIONS = 200                        # simulated projections (evenly spaced subset of the nominal ones, dose-equivalent)
ENERGY_GROUPS = 24
RECON_GRIDS = (48, 64, 80, 96, 128)
MAX_RECORDS = 60
MAX_EVENTS = 400
MAX_SAMPLES = 12
MAX_PORES = 400
MAX_CRACKS = 6
MAX_INCLUSIONS = 12
FILTERS = [  # position: id, label, layers (library material id, thickness m)
    dict(position=0, id='none', label='无滤片', layers=[]),
    dict(position=1, id='al_0p25', label='Al 0.25 mm', layers=[('al', 0.25e-3)]),
    dict(position=2, id='al_0p5', label='Al 0.5 mm', layers=[('al', 0.5e-3)]),
    dict(position=3, id='al_1p0', label='Al 1.0 mm', layers=[('al', 1.0e-3)]),
    dict(position=4, id='cu_0p1', label='Cu 0.1 mm', layers=[('cu', 0.1e-3)]),
    dict(position=5, id='cu_0p25', label='Cu 0.25 mm', layers=[('cu', 0.25e-3)]),
    dict(position=6, id='al_1p0_cu_0p1', label='Al 1.0 mm + Cu 0.1 mm', layers=[('al', 1.0e-3), ('cu', 0.1e-3)]),
]
LIBRARY_MATERIALS_USED = ['w', 'be', 'al', 'cu', 'nist_air', 'nist_cesium', 'fe', 'nist_pmma']
KEV = 1.602176634e-16                            # J per keV


def filter_of(position):
    return FILTERS[position]


# ---------------------------------------------------------------- template library (NIST SRD 126 mass attenuation coefficients)
_LIB = [None]


def library():
    if _LIB[0] is None:
        raw = LIBRARY_PATH.read_bytes()
        d = json.loads(raw)
        d['subs'] = {s['substance_id']: s for s in d['substances']}
        d['sha256'] = hashlib.sha256(raw).hexdigest()
        _LIB[0] = d
    return _LIB[0]


def substance(sid):
    s = library()['subs'].get(sid)
    if s is None:
        raise ActionRejected('UNKNOWN_SUBSTANCE')
    return s


@functools.lru_cache(maxsize=None)
def _segments(sid):
    """Edge-free segments of a tabulated material: (log E, log mu/rho, log mu_en/rho) per segment (energies in keV, coefficients in m2/kg)."""
    s = substance(sid)
    e = np.array(s['energy_MeV']) * 1e3
    mu = np.array(s['mu_rho_cm2_g']) * 0.1
    men = np.array(s['mu_en_rho_cm2_g']) * 0.1
    cut = np.where(np.diff(e) <= 0)[0] + 1
    return [(np.log(e[i]), np.log(mu[i]), np.log(men[i]), e[i[0]], e[i[-1]]) for i in np.split(np.arange(len(e)), cut)]


def mass_coefficients(sid, e_kev):
    """mu/rho and mu_en/rho (m2/kg) at the photon energies e_kev: log-log interpolation inside the edge-free segment containing each energy
    (an energy exactly at an edge takes the value above the edge)."""
    e_kev = np.atleast_1d(np.asarray(e_kev, float))
    mu = np.empty_like(e_kev)
    men = np.empty_like(e_kev)
    segs = _segments(sid)
    for k, (le, lm, ln, lo, hi) in enumerate(segs):
        last = k == len(segs) - 1
        m = (e_kev >= lo) & ((e_kev < segs[k + 1][3]) if not last else np.ones_like(e_kev, bool))
        if k == 0:
            m |= e_kev < lo
        if m.any():
            x = np.log(np.clip(e_kev[m], 1e-3, None))
            mu[m] = np.exp(np.interp(x, le, lm))
            men[m] = np.exp(np.interp(x, le, ln))
    return mu, men


def density(sid):
    return float(substance(sid)['density_kg_m3'])


def linear_mu(sid, e_kev, rho=None):
    """Linear attenuation and energy-absorption coefficients (1/m) of a library material at its tabulated density (or rho)."""
    mu, men = mass_coefficients(sid, e_kev)
    r = density(sid) if rho is None else rho
    return mu * r, men * r


def mixture_mu(parts, e_kev):
    """Linear coefficients of a volume mixture [(material id, volume fraction)], each constituent at its tabulated density."""
    mu = np.zeros(np.atleast_1d(e_kev).shape)
    men = np.zeros_like(mu)
    for sid, f in parts:
        a, b = linear_mu(sid, e_kev)
        mu += f * a
        men += f * b
    return mu, men


def mixture_density(parts):
    return sum(f * density(sid) for sid, f in parts)


# ---------------------------------------------------------------- source spectrum, filters, detector response
E_GRID_KEV = np.arange(1.0, 100.5, 0.5)          # fine grid of the spectrum model


def focal_spot_m(power_w):
    return SPOT_AT_4W_M * max(1.0, power_w / 4.0) ** SPOT_GROWTH_EXP


def kanaya_range_m(e0_kev):
    """Kanaya-Okayama electron range in tungsten (A = 183.84, Z = 74, rho = 19.3 g/cm3)."""
    return 0.0276 * 183.84 * e0_kev ** 1.67 / (74 ** 0.889 * 19.3) * 1e-6


@functools.lru_cache(maxsize=256)
def emitted_spectrum(kv_v):
    """Photon energy spectrum emitted per electron per steradian at the anode, after the tungsten self-absorption of the reflection target,
    before the window: (E keV grid, photons / (sr keV electron)).  Kramers intensity I(E) ~ Z (E0 - E) normalised so that the energy emitted
    into 4 pi equals eta V with eta = K Z V (V in volts); characteristic W K and L lines added as fractions of the emitted energy."""
    e0 = kv_v / 1e3
    e = E_GRID_KEV[E_GRID_KEV < e0]
    de = 0.5
    intensity = np.clip(e0 - e, 0.0, None)                      # energy per keV (arbitrary units)
    eta = KRAMERS_K_PER_V * 74 * kv_v
    energy_per_electron_j = eta * kv_v * 1.602176634e-19         # emitted X-ray energy per electron (J)
    brems_frac = 1.0
    lines = []
    if e0 > W_K_EDGE_KEV:
        fk = K_LINE_FRACTION_100KV * ((e0 / W_K_EDGE_KEV - 1) / (100.0 / W_K_EDGE_KEV - 1)) ** 1.63
        lines += [(E, fk * w) for E, w in W_K_LINES_KEV]
        brems_frac -= fk
    if e0 > W_L3_EDGE_KEV:
        fl = L_LINE_FRACTION_50KV * ((e0 / W_L3_EDGE_KEV - 1) / (50.0 / W_L3_EDGE_KEV - 1)) ** 1.63
        fl = min(fl, 0.08)
        inc = [(E, fl * w) for E, w in W_L_LINES_KEV if E < e0]         # a line above the tube voltage is not excited
        lines += inc
        brems_frac -= sum(f for _, f in inc)
    photons = np.zeros_like(e)
    ib = intensity * de
    photons += ib / ib.sum() * brems_frac * energy_per_electron_j / (e * KEV)            # bremsstrahlung photons per bin
    for E, f in lines:
        j = int(np.argmin(np.abs(e - E)))
        photons[j] += f * energy_per_electron_j / (e[j] * KEV)                            # line in its 0.5 keV bin: energy kept exact
    photons /= (4 * math.pi) * de                                                        # per steradian and keV
    depth = kanaya_range_m(e0) / 3.0 / math.sin(TAKEOFF_RAD)                            # mean emission depth along the exit path
    mu_w, _ = linear_mu('w', e)
    return e, photons * np.exp(-mu_w * depth)


def unabsorbed_spectrum(kv_v):
    """Same spectrum without the target self-absorption (for the energy ledger)."""
    e0 = kv_v / 1e3
    e, ph = emitted_spectrum(kv_v)
    depth = kanaya_range_m(e0) / 3.0 / math.sin(TAKEOFF_RAD)
    mu_w, _ = linear_mu('w', e)
    return e, ph * np.exp(mu_w * depth)


@functools.lru_cache(maxsize=512)
def beam_spectrum(kv_v, filter_position):
    """Photons / (sr keV electron) behind the beryllium window and the selected filter, before the air path."""
    e, ph = emitted_spectrum(kv_v)
    mu_be, _ = linear_mu('be', e)
    t = np.exp(-mu_be * BE_WINDOW_M)
    for sid, th in filter_of(filter_position)['layers']:
        mu, _ = linear_mu(sid, e)
        t = t * np.exp(-mu * th)
    return e, ph * t


def air_transmission(e_kev, length_m):
    mu, _ = linear_mu('nist_air', e_kev)
    return np.exp(-mu * length_m)


@functools.lru_cache(maxsize=None)
def detector_response(e_key):
    """Per incident photon of energy E: probability of interaction in the CsI layer and the mean energy deposited (keV) = E (1 - e^-mu t)
    mu_en / mu (the local-deposition share of the interaction; fluorescence and Compton escape leave)."""
    e = np.array(e_key)
    mu, men = linear_mu('nist_cesium', e)
    p = 1.0 - np.exp(-mu * CSI_THICKNESS_M)
    return p, e * p * men / mu


def energy_groups(e, w, edges_kev=(), n=ENERGY_GROUPS):
    """Group the fine spectrum into n groups of equal weight (weight w = detected energy per bin), forcing boundaries at the given edges;
    returns (group index per bin, group count)."""
    c = np.cumsum(w)
    c = c / c[-1] if c[-1] > 0 else np.linspace(0, 1, len(w))
    b = set(np.searchsorted(c, np.linspace(0, 1, n + 1)[1:-1]).tolist())
    for ed in edges_kev:
        j = int(np.searchsorted(e, ed))
        if 0 < j < len(e):
            b.add(j)
    b = sorted(x for x in b if 0 < x < len(e))
    idx = np.zeros(len(e), int)
    for k, j in enumerate(b):
        idx[j:] = k + 1
    return idx, len(b) + 1


# ---------------------------------------------------------------- sample phantoms (sample frame: origin at the bottom centre of the sample
# body on its own axis, +Z up; a mounting pin stands below it on the chuck)
SAMPLE_KINDS = ['cylinder', 'calibration_phantom', 'edge_phantom']
CAL_BALLS = 8                                    # steel balls of the geometry calibration phantom (helix in a PMMA cylinder)
CAL_BALL_RADIUS_M = 0.5e-3
EDGE_THICKNESS_M = 0.2e-3                        # tungsten plate of the edge phantom (beam along its thickness at angle 0)


def _layer_bounds(s):
    z = [0.0]
    for L in s['layers']:
        z.append(z[-1] + L['thickness_m'])
    return z


def _materials(s):
    """All library materials of a sample (matrix constituents, inclusions, container, pin, phantom materials)."""
    ids = []
    if s['kind'] == 'cylinder':
        for L in s['layers']:
            ids += [m['substance_id'] for m in L['materials']]
        ids += [i['substance_id'] for i in s['inclusions']]
    elif s['kind'] == 'calibration_phantom':
        ids += ['nist_pmma', 'fe']
    else:
        ids += ['w']
    if s['container']:
        ids.append(s['container']['substance_id'])
    if s['pin']:
        ids.append(s['pin']['substance_id'])
    return list(dict.fromkeys(ids))


def generate_pores(s):
    """Deterministic non-overlapping spherical pores (log-normal radii) fully inside the given layer of a cylinder sample; raises
    INVALID_SAMPLE when they cannot be placed."""
    p = s['pores']
    if s['kind'] != 'cylinder' or p['count'] == 0:
        return []
    zb = _layer_bounds(s)
    lay = p['layer']
    if lay >= len(s['layers']):
        raise ActionRejected('INVALID_SAMPLE')
    z0, z1 = zb[lay], zb[lay + 1]
    R = s['diameter_m'] / 2
    rng = np.random.default_rng(int(hashlib.sha256(f"{s['sample_id']}|{p['seed']}".encode()).hexdigest()[:12], 16))
    radii = np.sort(p['median_radius_m'] * np.exp(p['sigma_ln'] * rng.standard_normal(p['count'])))[::-1]
    out = []
    for r in radii:
        if 2 * r >= min(R, (z1 - z0) / 2):
            raise ActionRejected('INVALID_SAMPLE')
        for _ in range(4000):
            rho = (R - r - 1e-6) * math.sqrt(rng.random())
            ph = 2 * math.pi * rng.random()
            c = np.array([rho * math.cos(ph), rho * math.sin(ph), z0 + r + 1e-6 + (z1 - z0 - 2 * r - 2e-6) * rng.random()])
            if all(np.linalg.norm(c - q[0]) > r + q[1] + 1e-6 for q in out) and not _hits_inclusion(s, c, r):
                out.append((c, float(r)))
                break
        else:
            raise ActionRejected('INVALID_SAMPLE')              # too dense for non-overlapping pores
    return [dict(center_m=[float(v) for v in c], radius_m=r) for c, r in out]


def _hits_inclusion(s, c, r):
    return any(np.linalg.norm(c - np.array(i['center_m'])) < r + i['radius_m'] + 1e-6 for i in s['inclusions'])


def calibration_balls(s):
    """Steel balls of the geometry phantom on a helix (radius 0.35 D, one turn over 0.8 H)."""
    R, H = s['diameter_m'] / 2, s['height_m']
    return [dict(center_m=[0.35 * 2 * R * math.cos(2 * math.pi * k / CAL_BALLS), 0.35 * 2 * R * math.sin(2 * math.pi * k / CAL_BALLS),
                           0.1 * H + 0.8 * H * k / (CAL_BALLS - 1)], radius_m=CAL_BALL_RADIUS_M) for k in range(CAL_BALLS)]


def sample_truth(s):
    """Exact geometric truth of a phantom: volumes per material, porosity per layer and of the whole body, pores and cracks."""
    R = s['diameter_m'] / 2
    zb = _layer_bounds(s) if s['kind'] == 'cylinder' else [0.0, s['height_m']]
    body = math.pi * R * R * s['height_m'] if s['kind'] != 'edge_phantom' else s['diameter_m'] * EDGE_THICKNESS_M * s['height_m']
    pores = s.get('_pores', [])
    pv = sum(4 / 3 * math.pi * p['radius_m'] ** 3 for p in pores)
    cv = sum(math.pi * c['radius_m'] ** 2 * c['aperture_m'] for c in s['cracks']) if s['kind'] == 'cylinder' else 0.0
    iv = sum(4 / 3 * math.pi * i['radius_m'] ** 3 for i in s['inclusions']) if s['kind'] == 'cylinder' else 0.0
    layers = []
    if s['kind'] == 'cylinder':
        for k in range(len(s['layers'])):
            v = math.pi * R * R * (zb[k + 1] - zb[k])
            pk = sum(4 / 3 * math.pi * p['radius_m'] ** 3 for p in pores if zb[k] <= p['center_m'][2] < zb[k + 1])
            ck = sum(math.pi * c['radius_m'] ** 2 * c['aperture_m'] for c in s['cracks'] if zb[k] <= c['center_m'][2] < zb[k + 1])
            layers.append(dict(layer=k, volume_m3=v, pore_volume_m3=pk, crack_volume_m3=ck, porosity=(pk + ck) / v))
    return dict(body_volume_m3=body, pore_volume_m3=pv, crack_volume_m3=cv, inclusion_volume_m3=iv, porosity=(pv + cv) / body if body else 0.0,
                pore_count=len(pores), pore_equivalent_diameters_m=sorted(2 * p['radius_m'] for p in pores),
                cracks=[dict(area_m2=math.pi * c['radius_m'] ** 2, aperture_m=c['aperture_m'], radius_m=c['radius_m'], normal=list(c['normal']),
                             center_m=list(c['center_m'])) for c in s['cracks']], layers=layers)


def sample_height_total(s):
    return s['height_m'] + (s['pin']['length_m'] if s['pin'] else 0.0)


def sample_radius_total(s):
    """Radius of the rotation envelope about the sample axis (container included)."""
    if s['kind'] == 'edge_phantom':
        return math.hypot(s['diameter_m'], EDGE_THICKNESS_M / 2)
    return s['diameter_m'] / 2 + (s['container']['wall_m'] if s['container'] else 0.0)


def sample_mass_kg(s):
    """Mass of the sample body, consistent with the ray-traced geometry: layer matrices minus pores, cracks and inclusions, plus the
    inclusions at their own densities (container and pin are not part of the sample)."""
    R = s['diameter_m'] / 2
    m = 0.0
    if s['kind'] == 'cylinder':
        zb = _layer_bounds(s)
        rho = [mixture_density([(x['substance_id'], x['volume_fraction']) for x in L['materials']]) for L in s['layers']]

        def layer_of(z):
            return next((k for k in range(len(s['layers'])) if zb[k] <= z < zb[k + 1]), len(s['layers']) - 1)
        for k in range(len(s['layers'])):
            m += math.pi * R * R * (zb[k + 1] - zb[k]) * rho[k]
        for p in s.get('_pores', []):
            m -= 4 / 3 * math.pi * p['radius_m'] ** 3 * rho[s['pores']['layer']]
        for c in s['cracks']:
            m -= math.pi * c['radius_m'] ** 2 * c['aperture_m'] * rho[layer_of(c['center_m'][2])]
        for i in s['inclusions']:
            m += 4 / 3 * math.pi * i['radius_m'] ** 3 * (density(i['substance_id']) - rho[layer_of(i['center_m'][2])])
    elif s['kind'] == 'calibration_phantom':
        m = math.pi * R * R * s['height_m'] * density('nist_pmma') + CAL_BALLS * 4 / 3 * math.pi * CAL_BALL_RADIUS_M ** 3 * (density('fe') - density('nist_pmma'))
    else:
        m = s['diameter_m'] * EDGE_THICKNESS_M * s['height_m'] * density('w')
    return m


# ---------------------------------------------------------------- ray tracing (rays from the focal spot; lab frame: +X along the beam, +Y
# across (asset +Y), +Z up, origin at the focal spot)
def _slab(S, D, lo, hi, axis):
    """Parameter interval of the rays inside lo <= x_axis <= hi."""
    d = D[:, axis]
    s = S[axis] if S.ndim == 1 else S[:, axis]
    with np.errstate(divide='ignore', invalid='ignore'):
        t1 = (lo - s) / d
        t2 = (hi - s) / d
    tl = np.where(np.abs(d) < 1e-15, np.where((s >= lo) & (s <= hi), -np.inf, np.inf), np.minimum(t1, t2))
    th = np.where(np.abs(d) < 1e-15, np.where((s >= lo) & (s <= hi), np.inf, -np.inf), np.maximum(t1, t2))
    return tl, th


def _cyl(S, D, R, cx=0.0, cy=0.0):
    """Parameter interval of the rays inside the infinite vertical cylinder of radius R about (cx, cy)."""
    sx, sy = S[0] - cx, S[1] - cy
    a = D[:, 0] ** 2 + D[:, 1] ** 2
    b = 2 * (sx * D[:, 0] + sy * D[:, 1])
    c = sx * sx + sy * sy - R * R
    disc = b * b - 4 * a * c
    ok = (disc > 0) & (a > 1e-30)
    sq = np.sqrt(np.where(ok, disc, 0.0))
    with np.errstate(divide='ignore', invalid='ignore'):
        t1 = np.where(ok, (-b - sq) / (2 * a), np.inf)
        t2 = np.where(ok, (-b + sq) / (2 * a), -np.inf)
    return t1, t2


def _len(tl, th):
    return np.clip(th - tl, 0.0, None)


def _add(out, sid, L):
    out[sid] = out[sid] + L if sid in out else L.copy()


def body_paths(s, S, D):
    """Path lengths (m) of unit-direction rays S + t D (sample frame; S one point, D (n, 3)) through the materials of the sample body
    (layer matrices, pores, cracks, inclusions; phantom materials): {material id: array(n)}."""
    n = D.shape[0]
    out = {}
    R = s['diameter_m'] / 2
    if s['kind'] == 'edge_phantom':
        tl = np.full(n, -np.inf); th = np.full(n, np.inf)
        for ax, lo, hi in ((0, -EDGE_THICKNESS_M / 2, EDGE_THICKNESS_M / 2), (1, 0.0, s['diameter_m']), (2, 0.0, s['height_m'])):
            a, b = _slab(S, D, lo, hi, ax)
            tl, th = np.maximum(tl, a), np.minimum(th, b)
        _add(out, 'w', _len(tl, th))
        return out
    c1, c2 = _cyl(S, D, R)
    if s['kind'] == 'calibration_phantom':
        zl, zh = _slab(S, D, 0.0, s['height_m'], 2)
        body = _len(np.maximum(c1, zl), np.minimum(c2, zh))
        balls = sum_spheres(S, D, calibration_balls(s))
        _add(out, 'nist_pmma', np.clip(body - balls, 0.0, None))
        _add(out, 'fe', balls)
        return out
    zb = _layer_bounds(s)
    lay_pores = {}
    for p in s.get('_pores', []):
        lay_pores.setdefault(s['pores']['layer'], []).append(p)
    for k, L in enumerate(s['layers']):
        zl, zh = _slab(S, D, zb[k], zb[k + 1], 2)
        chord = _len(np.maximum(c1, zl), np.minimum(c2, zh))
        if not chord.any():
            continue
        void = sum_spheres(S, D, lay_pores.get(k, []))
        inc_paths = {}
        for i in [i for i in s['inclusions'] if zb[k] <= i['center_m'][2] < zb[k + 1]]:
            inc_paths.setdefault(i['substance_id'], np.zeros(n))
            inc_paths[i['substance_id']] += sum_spheres(S, D, [i])
        crack = np.zeros(n)
        for c in s['cracks']:
            if zb[k] <= c['center_m'][2] < zb[k + 1]:
                crack += disc_path(S, D, c)
        matrix = np.clip(chord - void - crack - sum(inc_paths.values(), np.zeros(n)), 0.0, None)
        for m in L['materials']:
            _add(out, m['substance_id'], matrix * m['volume_fraction'])
        for sid, v in inc_paths.items():
            _add(out, sid, v)
    return out


def container_walls(s, S, D):
    """Path lengths through the container wall (open coaxial tube over the body height) before and after the inside of the tube along each
    ray: ({material: near}, {material: far}); a ray that misses the inside has all of its wall path in 'near'.  A ray leaving the inside
    through the open top or bottom has no far part."""
    if not s['container'] or s['kind'] == 'edge_phantom':
        return {}, {}
    R = s['diameter_m'] / 2
    zl, zh = _slab(S, D, 0.0, s['height_m'], 2)
    o1, o2 = _cyl(S, D, R + s['container']['wall_m'])
    c1, c2 = _cyl(S, D, R)
    ol, oh = np.maximum(o1, zl), np.minimum(o2, zh)
    il, ih = np.maximum(c1, zl), np.minimum(c2, zh)
    inside = ih > il
    with np.errstate(invalid='ignore'):                              # rays missing a cylinder carry +-inf parameters
        near = np.where(inside, np.clip(il - ol, 0.0, None), _len(ol, oh))
        far = np.where(inside, np.clip(oh - ih, 0.0, None), 0.0)
    near, far = np.where(np.isfinite(near), near, 0.0), np.where(np.isfinite(far), far, 0.0)
    sid = s['container']['substance_id']
    return {sid: near}, {sid: far}


def container_paths(s, S, D):
    """Path lengths through the container wall around the body (coaxial tube over the body height)."""
    near, far = container_walls(s, S, D)
    return {sid: near[sid] + far[sid] for sid in near}


def pin_paths(s, S, D):
    """Path lengths through the mounting pin below the body."""
    out = {}
    if s['pin']:
        zl, zh = _slab(S, D, -s['pin']['length_m'], 0.0, 2)
        p1, p2 = _cyl(S, D, s['pin']['diameter_m'] / 2)
        _add(out, s['pin']['substance_id'], _len(np.maximum(p1, zl), np.minimum(p2, zh)))
    return out


def holder_paths(s, S, D):
    """Path lengths through the sample holder: the container around the body and the mounting pin below it."""
    out = container_paths(s, S, D)
    for sid, L in pin_paths(s, S, D).items():
        _add(out, sid, L)
    return out


HARDWARE_MATERIALS = ('cu', 'al')                  # brass chuck (modelled as copper) and the aluminium rotation-stage housing with its platter
STAGE_HOUSING_R_M = 0.0451                         # 80 x 80 mm housing as the area-equivalent cylinder (it does not turn with the platter)


def hardware_paths(S, D, ax, ay, z_top):
    """Path lengths through the stage hardware below the sample (sample frame): the chuck (r 12 mm, 15 mm high, modelled as copper) whose top
    is at z_top on the rotation axis (ax, ay), and below it the rotation-stage housing with the platter (aluminium, 50 mm high)."""
    out = {}
    for sid, r, z0, z1 in (('cu', CHUCK_R_M, z_top - CHUCK_H_M, z_top), ('al', STAGE_HOUSING_R_M, z_top - CHUCK_H_M - STAGE_H_M, z_top - CHUCK_H_M)):
        zl, zh = _slab(S, D, z0, z1, 2)
        c1, c2 = _cyl(S, D, r, ax, ay)
        L = _len(np.maximum(c1, zl), np.minimum(c2, zh))
        if L.any():
            _add(out, sid, L)
    return out


def sample_paths(s, S, D):
    """Path lengths through every material of the sample body and its holder: {material id: array(n)}."""
    out = body_paths(s, S, D)
    for sid, L in holder_paths(s, S, D).items():
        _add(out, sid, L)
    return out


def sum_spheres(S, D, spheres):
    """Sum of the chord lengths of the rays through the given spheres (exact; only rays within the sphere's angular footprint evaluated)."""
    n = D.shape[0]
    tot = np.zeros(n)
    if not spheres:
        return tot
    if n * len(spheres) <= 2_000_000:                  # small ray sets (energy ledger): all spheres at once, same formula
        C = np.array([sp['center_m'] for sp in spheres], float)
        r = np.array([sp['radius_m'] for sp in spheres], float)
        v = C - S
        dist = np.linalg.norm(v, axis=1)
        ok = dist > r
        if not ok.any():
            return tot
        v, dist, r = v[ok], dist[ok], r[ok]
        cosmax = np.sqrt(np.clip(1 - (r / dist) ** 2, 0.0, None))
        tc = D @ v.T                                               # (rays, spheres)
        inside = tc / dist > cosmax - 1e-12
        chord = 2 * np.sqrt(np.clip(r * r - (dist * dist - tc * tc), 0.0, None))
        return (chord * inside).sum(1)
    for sp in spheres:
        C = np.asarray(sp['center_m'], float)
        r = sp['radius_m']
        v = C - S
        dist = np.linalg.norm(v)
        if dist <= r:
            continue
        cosmax = math.sqrt(max(0.0, 1 - (r / dist) ** 2))
        cand = np.where(D @ (v / dist) > cosmax - 1e-12)[0]
        if not len(cand):
            continue
        tc = D[cand] @ v
        d2 = dist * dist - tc * tc
        tot[cand] += 2 * np.sqrt(np.clip(r * r - d2, 0.0, None))
    return tot


def disc_path(S, D, c):
    """Path of the rays through a planar crack (disc of radius R and aperture a): a / |n.d| where the ray crosses the disc, at most 2 R."""
    nrm = np.asarray(c['normal'], float)
    nrm = nrm / np.linalg.norm(nrm)
    C = np.asarray(c['center_m'], float)
    nd = D @ nrm
    with np.errstate(divide='ignore', invalid='ignore'):
        t = ((C - S) @ nrm) / nd
    P = S + t[:, None] * D
    inside = (np.abs(nd) > 1e-9) & (np.einsum('ij,ij->i', P - C, P - C) <= c['radius_m'] ** 2)
    return np.where(inside, np.minimum(c['aperture_m'] / np.maximum(np.abs(nd), 1e-12), 2 * c['radius_m']), 0.0)


# ---------------------------------------------------------------- capabilities and state slots
CAPS = {}


def cap(k, s, o, e, c, meaning, visibility='agent', kind='device_command'):
    CAPS[k] = dict(input_schema=s, output_schema=o, example_arguments=e, component_ids=c, function_zh=meaning, visibility=visibility, kind=kind)


SAMPLE_ID = dict(type='string', minLength=1, maxLength=40, pattern='^[A-Za-z0-9_.-]+$')
SUB_ID = dict(type='string', minLength=1, maxLength=40)
XYZ = array(number('m', -0.2, 0.2), 3, 3)
FAULTS = ['xray_arc', 'detector_defects', 'rotation_stall', 'door_sensor', 'light_curtain_blocked']
FAULT_ERROR = {'xray_arc': 'XRAY_ARC', 'rotation_stall': 'STAGE_FAULT', 'door_sensor': 'INTERLOCK_FAULT'}
PLANT_EXAMPLE = dict(room_k=295.15, mains_voltage_v=230.0, relative_humidity=0.45, noise_seed=1)
PLANT = obj(dict(room_k=number('K', 283.15, 308.15), mains_voltage_v=number('V', 0.0, 264.0), relative_humidity=number('1', 0.0, 1.0), noise_seed=INT))
MIX = array(obj(dict(substance_id=SUB_ID, volume_fraction=number('1', 0.0, 1.0))), 1, 4)
LAYER = obj(dict(thickness_m=number('m', 1.0e-5, SAMPLE_MAX_HEIGHT_M), materials=MIX))
PORES = obj(dict(count=integer(0, MAX_PORES), median_radius_m=number('m', 1.0e-6, 0.01), sigma_ln=number('1', 0.0, 1.5), layer=integer(0, 5), seed=INT))
CRACK = obj(dict(center_m=XYZ, normal=array(number('1', -1.0, 1.0), 3, 3), radius_m=number('m', 1.0e-4, 0.05), aperture_m=number('m', 1.0e-6, 1.0e-3)))
INCLUSION = obj(dict(substance_id=SUB_ID, center_m=XYZ, radius_m=number('m', 1.0e-6, 0.02)))
CONTAINER = nullable(obj(dict(substance_id=SUB_ID, wall_m=number('m', 1.0e-5, 5.0e-3))))
PIN = nullable(obj(dict(substance_id=SUB_ID, diameter_m=number('m', 5.0e-4, 0.02), length_m=number('m', 1.0e-3, 0.1))))
SAMPLE = obj(dict(sample_id=SAMPLE_ID, kind=enum(SAMPLE_KINDS), diameter_m=number('m', 5.0e-4, SAMPLE_MAX_DIAMETER_M),
                  height_m=number('m', 5.0e-4, SAMPLE_MAX_HEIGHT_M), layers=array(LAYER, 0, 6), pores=PORES, cracks=array(CRACK, 0, MAX_CRACKS),
                  inclusions=array(INCLUSION, 0, MAX_INCLUSIONS), container=CONTAINER, pin=PIN))
SAMPLE_EXAMPLE = dict(sample_id='alumina_pellet', kind='cylinder', diameter_m=0.006, height_m=0.006,
                      layers=[dict(thickness_m=0.006, materials=[dict(substance_id='alumina', volume_fraction=1.0)])],
                      pores=dict(count=120, median_radius_m=0.12e-3, sigma_ln=0.35, layer=0, seed=7),
                      cracks=[dict(center_m=[0.0, 0.0, 0.003], normal=[1.0, 0.0, 0.0], radius_m=0.0018, aperture_m=30e-6)], inclusions=[], container=None,
                      pin=dict(substance_id='al', diameter_m=0.003, length_m=0.035))
OPERATION = obj(dict(operation_id=TEXT))
STATUS = enum(['running', 'completed', 'failed', 'cancelled'])
RANGES = ['180', '360']
SCAN = obj(dict(range_deg=enum(RANGES), step_rad=number('rad', math.radians(0.05), math.radians(2.0)), exposure_s=number('s', EXPOSURE_RANGE_S[0], EXPOSURE_RANGE_S[1]),
                averaging=integer(AVERAGING_RANGE[0], AVERAGING_RANGE[1]), binning=enum(['1x1', '2x2', '4x4']), random_movement=BOOL))
SCAN_EXAMPLE = dict(range_deg='180', step_rad=math.radians(0.4), exposure_s=0.3, averaging=2, binning='2x2', random_movement=True)
FILTER_NAMES = ['none', 'hann', 'shepp_logan', 'ram_lak']
VOI = nullable(obj(dict(center_m=XYZ, size_m=array(number('m', 1.0e-4, 0.2), 3, 3))))
RECON = obj(dict(record_id=TEXT, grid=enum([str(g) for g in RECON_GRIDS]), filter=enum(FILTER_NAMES[1:]), beam_hardening=number('1', 0.0, 1.0),
                 bh_material=nullable(SUB_ID), ring_correction=number('1', 0.0, 1.0), smoothing_vox=number('1', 0.0, 3.0), post_alignment=nullable(number('m', -0.005, 0.005)),
                 auto_alignment=BOOL, voi=VOI))
RECON_EXAMPLE = dict(record_id='PJ0001', grid='80', filter='shepp_logan', beam_hardening=0.5, bh_material='alumina', ring_correction=0.5, smoothing_vox=0.0,
                     post_alignment=None, auto_alignment=True, voi=None)
PREVIEW = array(array(nullable(number('1')), 1, 128), 1, 128)
XSET = obj(dict(voltage_v=number('V'), current_a=number('A'), power_w=number('W'), filter=TEXT, focal_spot_m=number('m')))
RECORD = obj(dict(record_id=TEXT, kind=enum(['flat_field', 'geometry_calibration', 'radiograph', 'projections', 'volume']), status=STATUS, started_s=number('s'),
                  finished_s=nullable(number('s')), reason=nullable(TEXT), sample_id=nullable(TEXT), xray=nullable(XSET), scan=nullable(SCAN),
                  sod_m=nullable(number('m')), sdd_m=number('m'), magnification=nullable(number('1')), voxel_size_nominal_m=nullable(number('m')),
                  projections_nominal=INT, projections_simulated=INT, sim_detector=array(INT, 2, 2), sim_pixel_m=nullable(number('m')), transmission=nullable(obj(dict(min=number('1'), mean=number('1'),
                                                                                                                                   max=number('1')))),
                  saturated_fraction=nullable(number('1', 0.0, 1.0)), dose_gy=nullable(number('Gy')), duration_s=number('s'), preview=nullable(PREVIEW),
                  preview_unit=nullable(TEXT), source_record_id=nullable(TEXT), recon=nullable(obj({k: v for k, v in RECON['properties'].items() if k != 'record_id'})),
                  volume=nullable(obj(dict(shape=array(INT, 3, 3), voxel_m=number('m'), origin_m=XYZ, mu_min_1_m=number('1/m'), mu_max_1_m=number('1/m'),
                                           histogram=array(INT, 0, 128), histogram_edges_1_m=array(number('1/m'), 0, 129), axis_offset_used_m=number('m')))),
                  flat_field=nullable(obj(dict(frames=INT, mean_bright_adu=number('1'), dark_adu=number('1'), key=TEXT))),
                  geometry=nullable(obj(dict(axis_offset_m=number('m'), residual_m=number('m'), balls=INT))), warnings=array(TEXT, 0, 16),
                  template_source=obj(dict(library=TEXT, substances=array(TEXT, 0, 32))), note_zh=TEXT))
EVENT = obj(dict(time_s=number('s'), kind=enum(['power', 'door', 'xray', 'stage', 'filter', 'measurement', 'calibration', 'sample', 'error', 'fault', 'safety']),
                 code=TEXT, message_zh=TEXT))
LEDGER_KEYS = ['xray_on_s', 'electrical_energy_j', 'xray_emitted_j', 'target_absorbed_j', 'window_absorbed_j', 'filter_absorbed_j', 'air_absorbed_j',
               'sample_absorbed_j', 'holder_absorbed_j', 'sample_scattered_j', 'detector_absorbed_j', 'detector_passed_j', 'shielding_j', 'energy_residual_j',
               'sample_dose_gy',
               'projections', 'frames', 'rotation_rad', 'carriage_travel_m', 'lift_travel_m', 'door_cycles']
LEDGER = obj({k: number('J' if k.endswith('_j') else 's' if k.endswith('_s') else 'm' if k.endswith('_m') else 'rad' if k.endswith('_rad') else 'Gy' if k.endswith('_gy')
                        else '1') for k in LEDGER_KEYS})
REGION = nullable(obj(dict(center_m=XYZ, radius_m=number('m', 1.0e-4, 0.06), z_min_m=number('m', -0.2, 0.2), z_max_m=number('m', -0.2, 0.2))))
POROSITY_OUT = obj(dict(record_id=TEXT, threshold_1_m=number('1/m'), method=TEXT, envelope_voxels=INT, pore_voxels=INT, porosity=number('1', 0.0, 1.0),
                        open_porosity=number('1', 0.0, 1.0), closed_porosity=number('1', 0.0, 1.0), pore_count=INT,
                        equivalent_diameter_m=obj(dict(mean=nullable(number('m')), d10=nullable(number('m')), d50=nullable(number('m')), d90=nullable(number('m')),
                                                       max=nullable(number('m')))), voxel_m=number('m'), note_zh=TEXT))
CRACK_OUT = obj(dict(record_id=TEXT, threshold_1_m=number('1/m'), cracks=array(obj(dict(area_m2=number('m2'), aperture_m=number('m'), length_m=number('m'),
                                                                                         normal=array(number('1'), 3, 3), center_m=XYZ, voxels=INT)), 0, 32),
                     crack_count=INT, total_area_m2=number('m2'), note_zh=TEXT))
cap('plant.configure', PLANT, obj(dict(plant=PLANT)), PLANT_EXAMPLE, ['enclosure_base'], '宿主声明实验室条件(室温、电源电压、相对湿度、噪声种子)', 'evaluator', 'configuration')
cap('sample.provide', SAMPLE, obj(dict(sample_id=TEXT, location=TEXT, mass_kg=number('kg'))), SAMPLE_EXAMPLE, ['sample_chuck'],
    '宿主在实验台上提供一个样品(圆柱体分层材料/体积混合物、球形孔、平面裂纹、夹杂、样品管、样品针;几何标定体模;钨刃边体模;材料来自X射线衰减模板库)', 'evaluator',
    'configuration')
cap('sample.place', obj(dict(sample_id=SAMPLE_ID, offset_x_m=number('m', -0.005, 0.005), offset_y_m=number('m', -0.005, 0.005))),
    obj(dict(sample_id=TEXT, offset_x_m=number('m'), offset_y_m=number('m'))), dict(sample_id='alumina_pellet', offset_x_m=0.0, offset_y_m=0.0),
    ['sample_chuck', 'sample_turntable', 'door_glass'], '门打开时把实验台上的样品装到旋转台样品夹上(宿主/机器人抽象;样品轴相对旋转轴的偏移)', 'agent', 'robot_skill')
cap('sample.remove', obj(dict(sample_id=SAMPLE_ID)), obj(dict(sample_id=TEXT)), dict(sample_id='alumina_pellet'), ['sample_chuck', 'door_glass'],
    '门打开时把样品从样品夹取回实验台(宿主/机器人抽象)', 'agent', 'robot_skill')
cap('power.set', obj(dict(enabled=BOOL)), obj(dict(enabled=BOOL, state=TEXT)), dict(enabled=True), ['button_mains', 'enclosure_base'],
    '系统电源(上电后60 s启动;各轴为绝对位置,保持断电时的位置,不回原点)')
cap('power.press_button', obj({}), obj(dict(enabled=BOOL, state=TEXT)), {}, ['button_mains', 'enclosure_base'],
    '按右侧电源开关(按一次切换通断;与power.set同一上电/断电过程)')
cap('emergency.stop', obj({}), obj(dict(stopped=BOOL)), {}, ['button_emergency_stop'], '按下正面红色安全按钮:立即关闭X射线、停止所有运动与门,锁存直到释放并确认')
cap('emergency.release', obj({}), obj(dict(stopped=BOOL)), {}, ['button_emergency_stop'], '旋转释放安全按钮(之后用system.acknowledge确认错误)')
cap('system.acknowledge', obj({}), obj(dict(cleared=nullable(TEXT))), {}, ['touchscreen'], '确认已排除原因的锁存错误')
cap('fault.inject', obj(dict(fault=enum(FAULTS), active=BOOL)), obj(dict(faults=array(TEXT, 0, 8))), dict(fault='xray_arc', active=True), ['enclosure_base'],
    '注入或清除故障(评测者):X射线管打火、探测器坏列、旋转台卡滞、门联锁传感器故障、安全光幕被挡', 'evaluator', 'configuration')
cap('door.open', obj({}), obj(dict(target_m=number('m'), duration_s=number('s'))), {}, ['door_glass'], '打开电动铅玻璃门(X射线开启时门锁住)')
cap('door.close', obj({}), obj(dict(target_m=number('m'), duration_s=number('s'))), {}, ['door_glass'], '关闭电动铅玻璃门(安全光幕被挡时停止并重新打开)')
cap('xray.set', obj(dict(voltage_v=number('V', XRAY_KV_MIN_V, XRAY_KV_MAX_V), current_a=number('A', 1.0e-6, XRAY_CURRENT_MAX_A))),
    obj(dict(voltage_v=number('V'), current_a=number('A'), power_w=number('W'), focal_spot_m=number('m'))), dict(voltage_v=60.0e3, current_a=150.0e-6),
    ['source_housing', 'source_head'], 'X射线管电压与电流设定(功率不超过20 W;束流开启时按斜率变化)', 'agent', 'configuration')
cap('xray.enable', obj(dict(enabled=BOOL)), obj(dict(enabled=BOOL, warmup_s=number('s'), ramp_s=number('s'))), dict(enabled=True), ['source_housing', 'source_head'],
    '开关X射线(门关闭、联锁正常时;每次上电后首次开启先运行预热程序)')
cap('filter.select', obj(dict(position=integer(0, FILTER_POSITIONS - 1))), obj(dict(filter=TEXT, duration_s=number('s'))), dict(position=3),
    ['filter_changer', 'filter_slide'], '电动7位滤片转换器选择滤片(无、Al 0.25/0.5/1.0 mm、Cu 0.1/0.25 mm、Al 1 mm+Cu 0.1 mm)')
cap('stage.move', obj(dict(sod_m=number('m', SOD_RANGE_M[0], SOD_RANGE_M[1]), chuck_z_m=number('m', CHUCK_Z_RANGE_M[0], CHUCK_Z_RANGE_M[1]))),
    obj(dict(target=array(number('m'), 2, 2), duration_s=number('s'), magnification=number('1'))), dict(sod_m=0.045, chuck_z_m=0.362),
    ['sample_carriage', 'sample_lift'], '样品滑台沿光束轴移动(源-物距=几何放大)并升降样品夹(碰撞保护:源窗口、滤片转换器、探测器盖板、腔顶)')
cap('stage.rotate', obj(dict(angle_rad=number('rad', 0.0, 2 * math.pi))), obj(dict(target_rad=number('rad'), duration_s=number('s'))), dict(angle_rad=math.pi / 2),
    ['sample_turntable', 'sample_lift'], '旋转台转到给定角度(沿最短方向;电机在升降支架上的旋转台壳体内)')
cap('stage.go_to', obj(dict(position=enum(['load', 'center']))), obj(dict(target=array(number('m'), 2, 2), duration_s=number('s'))), dict(position='load'),
    ['sample_carriage', 'sample_lift'], '样品滑台移到命名位置(门口装样位、中间位)')
cap('scan.configure', SCAN, obj(dict(settings=SCAN, projections=INT, duration_s=number('s'))), SCAN_EXAMPLE, ['touchscreen'],
    '扫描设置:角度范围180°(+锥角)/360°、旋转步长、曝光、帧平均、像素合并、随机移动(抑制环状伪影)', 'agent', 'configuration')
cap('calibration.flat_field', obj({}), OPERATION, {}, ['detector_housing', 'source_head'],
    '探测器暗场与亮场校正(当前电压、电流、滤片、合并与曝光;光束中不能有样品)')
cap('calibration.geometry', obj({}), OPERATION, {}, ['sample_turntable', 'detector_housing'], '几何标定:用钢球标定体模做36投影360°扫描,测出旋转轴横向偏移')
cap('xray.radiograph', obj(dict(roi=nullable(obj(dict(center_u_m=number('m', -0.07, 0.07), center_v_m=number('m', -0.06, 0.06), size_px=integer(16, 256)))))),
    OPERATION, dict(roi=None), ['detector_housing', 'source_head', 'sample_turntable'], '单张投影(透视);可选探测器局部区域以合并后像素分辨率成像')
cap('scan.start', obj({}), OPERATION, {}, ['source_head', 'sample_turntable', 'detector_housing', 'filter_slide'], '按扫描设置采集旋转投影序列')
cap('recon.start', RECON, OPERATION, RECON_EXAMPLE, ['workstation_monitor'],
    'FDK锥束重建(滤波器、束硬化校正、环状伪影校正、平滑、后对准(手动或自动)、感兴趣体积)', 'agent', 'analysis')
cap('measurement.result', obj(dict(record_id=TEXT)), RECORD, dict(record_id='PJ0001'), ['workstation_monitor'], '读取记录(校正、透视、投影序列或体数据摘要)', 'agent', 'query')
cap('data.porosity', obj(dict(record_id=TEXT, region=REGION, threshold_1_m=nullable(number('1/m', 0.0, 1.0e6)), min_pore_voxels=integer(1, 1000))), POROSITY_OUT,
    dict(record_id='VO0001', region=None, threshold_1_m=None, min_pore_voxels=2), ['workstation_monitor'],
    '孔隙率:样品包络内低衰减体素(阈值Otsu或给定)、连通孔(开孔/闭孔)、孔数与等效直径分布', 'agent', 'analysis')
cap('data.cracks', obj(dict(record_id=TEXT, region=REGION, threshold_1_m=nullable(number('1/m', 0.0, 1.0e6)), min_voxels=integer(3, 10000))), CRACK_OUT,
    dict(record_id='VO0001', region=None, threshold_1_m=None, min_voxels=20), ['workstation_monitor'],
    '裂纹:低衰减连通体中的平面状部分(主成分最小/最大尺度比),面积、开口与方向', 'agent', 'analysis')
cap('data.attenuation', obj(dict(record_id=TEXT, region=REGION)),
    obj(dict(record_id=TEXT, mean_1_m=number('1/m'), std_1_m=number('1/m'), median_1_m=number('1/m'), p05_1_m=number('1/m'), p95_1_m=number('1/m'), voxels=INT)),
    dict(record_id='VO0001', region=None), ['workstation_monitor'], '区域内线衰减系数统计(均值、标准差、分位数)', 'agent', 'analysis')
cap('data.slice', obj(dict(record_id=TEXT, axis=enum(['x', 'y', 'z']), position_m=number('m', -0.2, 0.2), size_px=integer(16, 128))),
    obj(dict(record_id=TEXT, axis=TEXT, position_m=number('m'), pixel_m=number('m'), mu_1_m=PREVIEW)), dict(record_id='VO0001', axis='z', position_m=0.003, size_px=64),
    ['workstation_monitor'], '取体数据的一张切片(线衰减系数,重采样到给定像素数)', 'agent', 'analysis')
cap('data.edge_resolution', obj(dict(record_id=TEXT)), obj(dict(record_id=TEXT, width_10_90_m=nullable(number('m')), width_detector_px=nullable(number('1')),
                                                                edge_angle_rad=nullable(number('rad')), note_zh=TEXT)), dict(record_id='RG0001'), ['workstation_monitor'],
    '透视刃边法分辨率:刃边扩展函数10–90 %宽度(换算到样品平面)', 'agent', 'analysis')
cap('records.list', obj({}), obj(dict(records=array(TEXT, 0, 200))), {}, ['workstation_monitor'], '列出记录', 'agent', 'query')
cap('records.delete', obj(dict(record_id=TEXT)), obj(dict(record_id=TEXT, remaining=INT)), dict(record_id='RG0001'), ['workstation_monitor'],
    '删除一条记录及其缓存数据(记录数上限60;进行中的操作或正被重建的投影记录不能删除)', 'agent', 'configuration')
cap('events.read', obj(dict(since_s=number('s', 0.0))), obj(dict(events=array(EVENT, 0, 500), dropped=INT)), dict(since_s=0.0), ['touchscreen'], '读取事件与报警',
    'agent', 'query')
cap('ledger.read', obj({}), LEDGER, {}, ['enclosure_base'], '能量与使用账本(X射线能量去向、样品剂量、投影与帧数、行程)', 'evaluator', 'query')
cap('provenance.read', obj({}), obj(dict(library=TEXT, library_sha256=TEXT, substances=INT, license=TEXT)), {}, ['workstation_monitor'], '模板库来源', 'agent', 'query')
cap('sim.step', obj(dict(dt_s=number('s', 0.0, 86400.0))), obj(dict(time_s=number('s'))), dict(dt_s=1.0), ['enclosure_base'], '宿主推进仿真时间', 'evaluator', 'simulation')
UNSUPPORTED = {
    'hardware.connect': '没有真实X4 POSEIDON硬件或3DxSUITE软件接口',
    'robot.handle_sample': '机器人接触装样(抓取、插入样品夹)未建模;sample.place为宿主抽象',
    'sample_changer.load': '15位样品转换器是选件,不在本配置',
    'stage.micropositioning': '带微定位的高级旋转台是选件(本配置为标准旋转台)',
    'detector.select_scmos': '16 MP sCMOS探测器与Multi-Vision双探测器是选件,不在本配置',
    'source.transmission': '透射式X射线源(110 kV、16 W、<2 µm@2 W)是另一种源配置',
    'geometry.best_scan': 'Best-Scan-Geometry(探测器连续移动)是选件(本配置为固定扫描几何)',
    'scan.helical': '高级扫描模式(螺旋、HART+)是选件',
    'stage.in_situ': '原位材料试验台(压缩4400 N、拉伸440 N)与加热/冷却台是选件',
    'export.dataset': '3DxSUITE数据集导出(切片图像、网格、CAD比对)与相位恢复算法未建模',
}
for _k, _why in UNSUPPORTED.items():
    cap(_k, obj({}), obj({}), {}, ['enclosure_base'], _why, 'agent', 'device_command')


SLOTS = {}


def slot(k, schema, unit, origin, component, meaning, visibility='agent', reference=None):
    SLOTS[k] = dict(schema=schema, unit=unit, origin=origin, component=component, meaning=meaning, visibility=visibility, reference=reference)


XY = array(number('m'), 2, 2)
slot('clock.time_s', number('s'), 's', 'derived', 'enclosure_base', '仿真时钟')
slot('plant.configured', BOOL, '1', 'setpoint', 'enclosure_base', '已声明实验室条件')
slot('power.enabled', BOOL, '1', 'setpoint', 'button_mains', '系统电源')
slot('system.state', enum(['off', 'booting', 'ready']), '1', 'derived', 'touchscreen', '系统状态(断电/启动中/就绪)')
slot('system.error_code', nullable(TEXT), '1', 'derived', 'touchscreen', '锁存的错误代码')
slot('emergency.stopped', BOOL, '1', 'sensor', 'button_emergency_stop', '安全按钮被按下(锁存)')
slot('door.position_m', number('m'), 'm', 'derived', 'door_glass', '铅玻璃门打开量(0为关闭,向左滑开;场景门关节镜像)')
slot('door.target_m', number('m'), 'm', 'derived', 'door_glass', '门的目标位置')
slot('door.moving', BOOL, '1', 'derived', 'door_glass', '门在移动')
slot('door.closed', BOOL, '1', 'sensor', 'door_glass', '门关闭(联锁开关)')
slot('light_curtain.blocked', BOOL, '1', 'sensor', 'door_frame', '门口安全光幕被挡')
slot('interlock.ok', BOOL, '1', 'derived', 'door_frame', 'X射线联锁满足(门关闭、联锁传感器正常、安全按钮未按下)')
slot('xray.voltage_set_v', number('V'), 'V', 'setpoint', 'source_head', '管电压设定')
slot('xray.current_set_a', number('A'), 'A', 'setpoint', 'source_head', '管电流设定')
slot('xray.enabled', BOOL, '1', 'setpoint', 'source_head', 'X射线开启命令')
slot('xray.voltage_v', number('V'), 'V', 'sensor', 'source_head', '实际管电压(斜坡)')
slot('xray.current_a', number('A'), 'A', 'sensor', 'source_head', '实际管电流(斜坡)')
slot('xray.power_w', number('W'), 'W', 'derived', 'source_head', '靶功率=电压×电流')
slot('xray.on', BOOL, '1', 'derived', 'source_head', 'X射线正在发射')
slot('xray.ready', BOOL, '1', 'derived', 'source_head', 'X射线稳定在设定值且已预热')
slot('xray.warmup_remaining_s', number('s', 0.0), 's', 'derived', 'source_head', '预热剩余时间')
slot('xray.focal_spot_m', number('m'), 'm', 'derived', 'source_head', '焦点尺寸(随靶功率增大)')
slot('filter.position', integer(0, FILTER_POSITIONS - 1), '1', 'setpoint', 'filter_slide', '滤片位置')
slot('filter.label', TEXT, '1', 'derived', 'filter_slide', '滤片名称')
slot('filter.slide_m', number('m'), 'm', 'derived', 'filter_slide', '滤片滑块位置(场景滑块关节镜像)')
slot('filter.moving', BOOL, '1', 'derived', 'filter_slide', '滤片转换器在移动')
slot('stage.sod_m', number('m'), 'm', 'derived', 'sample_carriage', '源-物距(焦点到旋转轴;场景滑台关节镜像)')
slot('stage.sod_target_m', number('m'), 'm', 'derived', 'sample_carriage', '源-物距目标')
slot('stage.chuck_z_m', number('m'), 'm', 'derived', 'sample_lift', '样品夹顶面高度(台面坐标;场景升降关节镜像)')
slot('stage.chuck_z_target_m', number('m'), 'm', 'derived', 'sample_lift', '样品夹高度目标')
slot('stage.rotation_rad', number('rad'), 'rad', 'derived', 'sample_turntable', '旋转台角度(场景旋转关节镜像)')
slot('stage.rotation_target_rad', number('rad'), 'rad', 'derived', 'sample_turntable', '旋转台目标角度')
slot('stage.moving', BOOL, '1', 'derived', 'sample_carriage', '滑台、升降或旋转台在移动')
slot('stage.position_label', nullable(TEXT), '1', 'derived', 'sample_carriage', '命名位置(到位后)')
slot('geometry.magnification', number('1'), '1', 'derived', 'sample_carriage', '几何放大倍数=源-探距/源-物距')
slot('geometry.voxel_size_m', number('m'), 'm', 'derived', 'detector_housing', '名义体素尺寸=像素间距×合并/放大')
slot('geometry.fov_m', XY, 'm', 'derived', 'detector_housing', '旋转轴处的视场宽×高')
slot('scan.settings', SCAN, '1', 'setpoint', 'touchscreen', '扫描设置')
slot('scan.projections', INT, '1', 'derived', 'touchscreen', '扫描投影数(名义)')
slot('scan.duration_s', number('s'), 's', 'derived', 'touchscreen', '扫描时间估计')
slot('calibration.flat_field', obj(dict(valid=BOOL, key=nullable(TEXT), calibrated_s=nullable(number('s')))), '1', 'derived', 'detector_housing',
     '暗场/亮场校正是否与当前电压、电流、滤片、合并与曝光一致')
slot('calibration.geometry', obj(dict(axis_offset_m=nullable(number('m')), calibrated_s=nullable(number('s')))), '1', 'derived', 'sample_turntable',
     '几何标定测得的旋转轴横向偏移')
slot('measurement.id', nullable(TEXT), '1', 'derived', 'touchscreen', '进行中的记录ID')
slot('measurement.kind', nullable(TEXT), '1', 'derived', 'touchscreen', '进行中的操作种类')
slot('measurement.progress', nullable(number('1', 0.0, 1.0)), '1', 'derived', 'touchscreen', '进行中操作的进度')
slot('measurement.remaining_s', nullable(number('s', 0.0)), 's', 'derived', 'touchscreen', '进行中操作的剩余时间')
slot('measurement.started_by_other_agent', BOOL, '1', 'derived', 'touchscreen', '进行中的操作属于另一个agent')
slot('recon.id', nullable(TEXT), '1', 'derived', 'workstation_monitor', '工作站上进行中的重建记录ID(与仪器采集并行)')
slot('recon.remaining_s', nullable(number('s', 0.0)), 's', 'derived', 'workstation_monitor', '重建剩余时间')
slot('result.last_record_id', nullable(TEXT), '1', 'derived', 'workstation_monitor', '最近完成的记录ID(agent只看到自己或宿主创建的记录)')
slot('result.last_porosity', nullable(number('1', 0.0, 1.0)), '1', 'derived', 'workstation_monitor', '最近一次孔隙率分析的结果')
slot('sample.bench_ids', array(TEXT, 0, MAX_SAMPLES), '1', 'derived', 'sample_chuck', '实验台上的样品')
slot('sample.on_stage', nullable(obj(dict(sample_id=TEXT, offset_x_m=number('m'), offset_y_m=number('m')))), '1', 'derived', 'sample_chuck', '样品夹上的样品及偏移')
slot('panel.status_green', BOOL, '1', 'derived', 'lamp_status_green', '状态灯绿(通电、无错误、X射线关)')
slot('panel.status_orange', BOOL, '1', 'derived', 'lamp_status_orange', '状态灯橙(X射线开启)')
slot('panel.status_red', BOOL, '1', 'derived', 'lamp_status_red', '状态灯红(错误或安全按钮按下)')
slot('truth.axis_offset_m', number('m'), 'm', 'process_truth', 'sample_turntable', '旋转轴真实横向偏移', 'evaluator')
slot('truth.samples', {'type': 'object'}, '1', 'process_truth', 'sample_chuck', '样品真实定义(含生成的孔)与真实孔隙率、裂纹', 'evaluator')
slot('truth.faults', array(TEXT, 0, 8), '1', 'process_truth', 'enclosure_base', '注入的故障', 'evaluator')
slot('truth.detector_defect_columns', array(INT, 0, 16), '1', 'process_truth', 'detector_housing', '注入的坏列(模拟探测器列号)', 'evaluator')


class _Sum:
    """Neumaier-compensated running sum."""

    def __init__(self):
        self.s = 0.0
        self.c = 0.0

    def add(self, x):
        t = self.s + x
        if abs(self.s) >= abs(x):
            self.c += (self.s - t) + x
        else:
            self.c += (x - t) + self.s
        self.s = t

    def value(self):
        return self.s + self.c


def _axis_move(pos, target, speed, dt):
    """Linear move towards the target; arrives exactly (a remainder of rounding size snaps to the target)."""
    d = target - pos
    step = speed * dt
    return target if abs(d) <= step + 1e-12 else pos + math.copysign(step, d)


def _rot_delta(a, b):
    """Shortest signed rotation from a to b (rad)."""
    return (b - a + math.pi) % (2 * math.pi) - math.pi


def coerce(s, v):
    """Integral floats of 'integer' fields as int, recursively (the value was validated against s)."""
    if 'anyOf' in s:
        for o in s['anyOf']:
            try:
                validate(o, v)
            except ActionRejected:
                continue
            return coerce(o, v)
        return v
    k = s.get('type')
    if k == 'integer' and isinstance(v, float):
        return int(v)
    if k == 'object' and isinstance(v, dict) and 'properties' in s:
        return {key: coerce(s['properties'][key], val) if key in s['properties'] else val for key, val in v.items()}
    if k == 'array' and isinstance(v, list):
        return [coerce(s['items'], x) for x in v]
    return v


def _segment_hits_box(p, q, box):
    """Does the segment p->q pass through the inside of the open axis-aligned rectangle ((x0, x1), (y0, y1)) (bounds may be infinite)?"""
    t0, t1 = 0.0, 1.0
    for a, d, (lo, hi) in ((p[0], q[0] - p[0], box[0]), (p[1], q[1] - p[1], box[1])):
        if abs(d) < 1e-15:
            if not (lo < a < hi):
                return False
            continue
        ta, tb = (lo - a) / d, (hi - a) / d
        if ta > tb:
            ta, tb = tb, ta
        t0, t1 = max(t0, ta), min(t1, tb)
        if t0 >= t1:
            return False
    return t0 < t1


FAULT_PRIORITY = ('door_sensor', 'xray_arc', 'rotation_stall')     # order of the latched errors after power-up / acknowledgement
FLAT_MAX_SIGMA = 0.10                            # a bright field whose binned-pixel relative error exceeds 10 % is not a usable calibration
GEO_FIT_MAX_RESIDUAL_PX = 2.0                    # ball-track fit accepted up to 2 window pixels (at the axis) rms, and at least ...
GEO_FIT_MIN_LIMIT_M = 5.0e-6                     # ... 5 um
RATE_ROT_CELLS = 32                              # energy-rate cells: the beam's energy flow is evaluated at the cell centre of the pose
RATE_ROT_CELL_RAD = 2 * math.pi / RATE_ROT_CELLS
RATE_POS_CELL_M = 0.25e-3
RATE_KV_CELL_V = 1.0e3
LEDGER_FINE_RAYS = 1024                          # about this many rays over the projection of the sample body (energy ledger)
LEDGER_RING_CELL_M = 0.008                       # coarse ledger cells (detector plane) between the active area and the emission cone
ENERGY_SINKS = ('target_absorbed_j', 'window_absorbed_j', 'filter_absorbed_j', 'air_absorbed_j', 'sample_absorbed_j', 'holder_absorbed_j', 'sample_scattered_j',
                'detector_absorbed_j', 'detector_passed_j', 'shielding_j')


def _to_boundary(x, w, cell):
    """Time until x moving at rate w (!= 0) crosses the next boundary of cells of width `cell` centred on integer multiples (boundaries at
    (k + 1/2) cell)."""
    if w > 0:
        b = (math.floor(x / cell - 0.5) + 1.5) * cell
    else:
        b = (math.ceil(x / cell - 0.5) - 0.5) * cell
    if (b - x) / w < 1e-9:                       # x sits on a boundary within rounding: the next one
        b += math.copysign(cell, w)
    return (b - x) / w



@functools.lru_cache(maxsize=None)
def _mu_grid(sid, n):
    """Linear coefficients (1/m) of a library material on the first n energies of the fine spectrum grid (every tube voltage uses a prefix)."""
    return linear_mu(sid, E_GRID_KEV[:n])


def _cells(u0, u1, v0, v1, nu, nv):
    """Centres and sizes of an (nu x nv) grid of cells over the rectangle [u0, u1] x [v0, v1] of the detector plane."""
    pu, pv = (u1 - u0) / nu, (v1 - v0) / nv
    U, V = np.meshgrid(u0 + (np.arange(nu) + 0.5) * pu, v1 - (np.arange(nv) + 0.5) * pv)
    return U.ravel(), V.ravel(), np.full(U.size, pu), np.full(U.size, pv)


def _cell_rays(U, V, PU, PV):
    """Rays from the focal spot to cell centres of the detector plane: (unit directions, solid angles sr, focal-spot-to-plane distances,
    cell on the active area)."""
    P = np.stack([np.full(U.size, SDD_M), U, V], -1)
    r = np.linalg.norm(P, axis=1)
    hu, hv = DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2, DETECTOR_PIXELS[1] * PIXEL_PITCH_M / 2
    return P / r[:, None], PU * PV * SDD_M / r ** 3, r, (np.abs(U) < hu) & (np.abs(V) < hv)


@functools.lru_cache(maxsize=1)
def _ledger_grid():
    """Coarse cells of the energy ledger over the emission cone: 35 x 30 on the active area and about 8 mm (detector plane) between it and
    the cone, so that no cell straddles the edge of the active area; a cell belongs to the cone when its centre does.  Returns the cell
    centres and sizes (U, V, PU, PV) and their rays (directions, solid angles, distances, on the active area)."""
    c = SDD_M * math.tan(CONE_HALF_ANGLE_RAD)
    hu, hv = DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2, DETECTOR_PIXELS[1] * PIXEL_PITCH_M / 2
    nu, nv = max(1, math.ceil((c - hu) / LEDGER_RING_CELL_M)), max(1, math.ceil((c - hv) / LEDGER_RING_CELL_M))
    blocks = [_cells(u0, u1, v0, v1, a, b) for u0, u1, a in ((-c, -hu, nu), (-hu, hu, 35), (hu, c, nu))
              for v0, v1, b in ((-c, -hv, nv), (-hv, hv, 30), (hv, c, nv))]
    U, V, PU, PV = (np.concatenate([bk[k] for bk in blocks]) for k in range(4))
    keep = U * U + V * V <= c * c
    U, V, PU, PV = U[keep], V[keep], PU[keep], PV[keep]
    return (U, V, PU, PV) + _cell_rays(U, V, PU, PV)


@functools.lru_cache(maxsize=256)
def _free_rays(kv_v, filter_position):
    """Energy per electron (J) of every coarse ledger ray that meets no object: absorbed in the air, deposited in and passing through the
    detector (rays on the active area), reaching the shielding (rays beside it)."""
    _, omega, r, det = _ledger_grid()[4:]
    e, ph = beam_spectrum(kv_v, filter_position)
    mu_air, _ = _mu_grid('nist_air', len(e))
    inc = ph * np.exp(-np.multiply.outer(r, mu_air))
    _, edep = detector_response(tuple(e))
    f = 0.5 * KEV
    return (f * omega * (float(ph @ e) - inc @ e), np.where(det, f * omega * (inc @ edep), 0.0),
            np.where(det, f * omega * (inc @ (e - edep)), 0.0), np.where(det, 0.0, f * omega * (inc @ e)))

DOOR_X_RANGE_M = (ENCLOSURE_X_M - 0.085, ENCLOSURE_X_M + 0.270)   # door opening in the asset frame (the chuck is reachable when the axis lies inside)
LOAD_POSITION = dict(sod=0.250, cz=0.250)
CENTER_POSITION = dict(sod=0.200, cz=0.300)


ASSET_VERSION = '1.1.2'


# ---------------------------------------------------------------- front panel (touch-screen status display, mains button, emergency stop, status light)
def _panel_engine():
    try:
        from . import _panel_ui as engine                                   # imported as asset_runtime.bruker_x4poseidon (tests, Matterix bridge)
        return engine
    except ImportError:                                                      # loaded from its file path
        import importlib.util
        import sys
        if '_chemsim_panel_ui' not in sys.modules:
            spec = importlib.util.spec_from_file_location('_chemsim_panel_ui', Path(__file__).with_name('_panel_ui.py'))
            sys.modules['_chemsim_panel_ui'] = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(sys.modules['_chemsim_panel_ui'])
        return sys.modules['_chemsim_panel_ui']


_panel_ui = _panel_engine()
if os.environ.get('X4_PANEL_BUILD'):                                         # the spec builder imports this module before the spec exists
    PANEL_SPEC = dict(spec_version=_panel_ui.SPEC_VERSION, inputs={}, displays={'screen': dict(kind='touch_tft', size=[800, 480])},
                      screens={'off': dict(style='static')})
else:
    PANEL_SPEC = _panel_ui.load_spec(ASSET / 'panel_ui.json', capabilities=set(CAPS))
PANEL_CAPS = _panel_ui.capability_definitions(PANEL_SPEC)                    # panel.touch / press / switch / capture / frame (kept out of CAPS:
ALL_CAPS = dict(CAPS, **PANEL_CAPS)                                          # CAPS stays the instrument's own command set)
PANEL_GESTURES = [k for k in PANEL_CAPS if k not in ('panel.capture', 'panel.frame')]


class BrukerX4PoseidonRuntime:
    """X4 POSEIDON (reflection source, flat panel, fixed geometry, standard rotation stage): power and safety, door, X-ray source, filter
    changer, sample carriage / lift / rotation, detector calibration, radiography, CT scans, FDK reconstruction and analysis (SI units)."""

    def __init__(self, instance_id='x4_poseidon_01'):
        self.instance_id = instance_id
        self.t = 0.0
        self.revision = 0
        self.plant = None
        self.power = False
        self.state = 'off'
        self.state_t0 = 0.0
        self.error = None
        self.estop = False
        self.faults = set()
        self.door, self.door_target = 0.0, 0.0
        self.xv_set, self.xi_set = 60.0e3, 150.0e-6
        self.x_enabled = False
        self.xv, self.xi = 0.0, 0.0
        self.warm_needed = True                                  # first enable after every boot runs the warm-up program
        self.warm_v, self.warm_i = 0.0, 0.0                      # end point of the running warm-up program (set point at its start)
        self.warm_end = None
        self.warm_from = None
        self.filter_pos, self.slide, self.slide_target = 0, 0.0, 0.0
        self.sod, self.sod_target = LOAD_POSITION['sod'], LOAD_POSITION['sod']
        self.cz, self.cz_target = LOAD_POSITION['cz'], LOAD_POSITION['cz']
        self.rot, self.rot_target = 0.0, 0.0
        self.position_label = 'load'
        self.scan_settings = dict(SCAN_EXAMPLE)
        self.flat = None                                         # dict(key, t, bright, dark, frames) of the last dark / bright calibration
        self.geo_cal = dict(axis_offset_m=None, calibrated_s=None)
        self.samples = {}
        self.on_stage = None
        self.records = {}
        self._data = {}                                          # record id -> simulated arrays (projections, volumes, images)
        self.seq = dict(ff=0, gc=0, rg=0, pj=0, vo=0, op=0)
        self.op = None                                           # acquisition operation (flat field, geometry calibration, radiograph, scan)
        self.recon = None                                        # reconstruction on the workstation (independent of the instrument)
        self.operations = {}
        self.events, self.events_dropped = [], 0
        self.truth = dict(axis_offset=0.0, defects=[])
        self.led = {k: _Sum() for k in LEDGER_KEYS}
        self.last_porosity = None
        self.last_porosity_rid = None
        self.used_substances = set()
        self._rate_cache = {}
        self._principals, self._requests, self._owners = {}, {}, {}
        self._caller = None                                                  # session principal while a session call runs (panel gestures act as that caller)
        self._porosity_owner = None                              # principal of the last porosity analysis (the session sets it)
        self.power_epoch = 0                                     # power-ups seen by the panel
        self.panel_capture_dir = None                            # panel.capture files (default: <tmp>/chemistry_sim_panel/<instance>)
        self.panel = _panel_ui.PanelUI(PANEL_SPEC, self)

    # ---------------------------------------------------------------- basics
    @property
    def time_s(self):
        return self.t

    def register_principal(self, name, visibility):
        if visibility not in ('agent', 'evaluator') or name in self._principals:
            raise ActionRejected('INVALID_ARGUMENT')
        self._principals[name] = visibility

    def session(self, name):
        if name not in self._principals:
            raise ActionRejected('ACCESS_DENIED')
        return BrukerX4PoseidonSession(self, name)

    def _event(self, kind, code, message, rid=None, t=None):
        if len(self.events) >= MAX_EVENTS:
            self.events.pop(0)
            self.events_dropped += 1
        self.events.append(dict(time_s=self.t if t is None else t, kind=kind, code=code, message_zh=message, _rid=rid))

    def _seed(self):
        return self.plant['noise_seed'] if self.plant else 0

    def _binning(self):
        return int(self.scan_settings['binning'][0])

    def _magnification(self, sod=None):
        return SDD_M / (self.sod if sod is None else sod)

    def _voxel_nominal(self, sod=None):
        return PIXEL_PITCH_M * self._binning() / self._magnification(sod)

    def _fov(self, sod=None):
        m = self._magnification(sod)
        return [DETECTOR_PIXELS[0] * PIXEL_PITCH_M / m, DETECTOR_PIXELS[1] * PIXEL_PITCH_M / m]

    def _door_closed(self):
        return self.door <= 1e-9 and self.door_target <= 1e-9

    def _door_moving(self):
        return abs(self.door - self.door_target) > 1e-12

    def _interlock_ok(self):
        return self._door_closed() and 'door_sensor' not in self.faults and not self.estop

    def _xray_on(self):
        return self.power and self.xv > 0.0 and self.xi > 0.0

    def _xray_ready(self):
        return (self._xray_on() and self.x_enabled and self.warm_end is None and abs(self.xv - self.xv_set) < 1e-6 and abs(self.xi - self.xi_set) < 1e-12)

    def _stage_moving(self):
        return abs(self.sod - self.sod_target) > 1e-12 or abs(self.cz - self.cz_target) > 1e-12 or abs(_rot_delta(self.rot, self.rot_target)) > 1e-12

    def _filter_moving(self):
        return abs(self.slide - self.slide_target) > 1e-12

    def _flat_key(self):
        s = self.scan_settings
        return f"{self.xv_set:.1f}|{self.xi_set * 1e9:.1f}|{self.filter_pos}|{s['binning']}|{s['exposure_s']:.6g}"

    def _flat_valid(self):
        return self.flat is not None and self.flat['key'] == self._flat_key() and self.flat.get('usable', True)

    def _nominal_projections(self, s=None):
        s = s or self.scan_settings
        span = self._scan_span(s)
        return int(math.floor(span / s['step_rad'] + 1e-9)) + (0 if s['range_deg'] == '360' else 1)

    def _scan_span(self, s=None):
        s = s or self.scan_settings
        if s['range_deg'] == '360':
            return 2 * math.pi
        half_fan = math.atan(DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2 / SDD_M)
        return math.pi + 2 * half_fan                            # 180 deg + fan angle (short scan)

    def _projection_time(self, s=None):
        s = s or self.scan_settings
        return s['averaging'] * (s['exposure_s'] + FRAME_OVERHEAD_S) + STEP_OVERHEAD_S

    def _scan_duration(self, s=None):
        return self._nominal_projections(s) * self._projection_time(s)

    # ---------------------------------------------------------------- read
    def read(self, visibility='agent'):
        o = self.op
        prog = rem = None
        if o is not None:
            prog = min(1.0, max(0.0, (self.t - o['t0']) / o['duration_s'])) if o['duration_s'] > 0 else 0.0
            rem = max(0.0, o['t0'] + o['duration_s'] - self.t)
        err = self.error is not None or self.estop
        values = {
            'clock.time_s': self.t, 'plant.configured': self.plant is not None, 'power.enabled': self.power, 'system.state': self.state,
            'system.error_code': self.error, 'emergency.stopped': self.estop, 'door.position_m': self.door, 'door.target_m': self.door_target,
            'door.moving': self._door_moving(), 'door.closed': self._door_closed(), 'light_curtain.blocked': 'light_curtain_blocked' in self.faults,
            'interlock.ok': self._interlock_ok(), 'xray.voltage_set_v': self.xv_set, 'xray.current_set_a': self.xi_set, 'xray.enabled': self.x_enabled,
            'xray.voltage_v': self.xv, 'xray.current_a': self.xi, 'xray.power_w': self.xv * self.xi, 'xray.on': self._xray_on(), 'xray.ready': self._xray_ready(),
            'xray.warmup_remaining_s': max(0.0, self.warm_end - self.t) if self.warm_end is not None else 0.0,
            'xray.focal_spot_m': focal_spot_m(self.xv * self.xi if self._xray_on() else self.xv_set * self.xi_set),
            'filter.position': self.filter_pos, 'filter.label': filter_of(self.filter_pos)['label'], 'filter.slide_m': self.slide, 'filter.moving': self._filter_moving(),
            'stage.sod_m': self.sod, 'stage.sod_target_m': self.sod_target, 'stage.chuck_z_m': self.cz, 'stage.chuck_z_target_m': self.cz_target,
            'stage.rotation_rad': self.rot, 'stage.rotation_target_rad': self.rot_target, 'stage.moving': self._stage_moving(), 'stage.position_label': self.position_label,
            'geometry.magnification': self._magnification(), 'geometry.voxel_size_m': self._voxel_nominal(), 'geometry.fov_m': self._fov(),
            'scan.settings': dict(self.scan_settings), 'scan.projections': self._nominal_projections(), 'scan.duration_s': self._scan_duration(),
            'calibration.flat_field': dict(valid=self._flat_valid(), key=self.flat['key'] if self.flat else None, calibrated_s=self.flat['t'] if self.flat else None),
            'calibration.geometry': dict(self.geo_cal), 'measurement.id': o['record_id'] if o else None, 'measurement.kind': o['kind'] if o else None,
            'measurement.progress': prog, 'measurement.remaining_s': rem, 'measurement.started_by_other_agent': False,
            'recon.id': self.recon['record_id'] if self.recon else None,
            'recon.remaining_s': max(0.0, self.recon['t0'] + self.recon['duration_s'] - self.t) if self.recon else None,
            'result.last_record_id': self._last_record(), 'result.last_porosity': self.last_porosity,
            'sample.bench_ids': sorted(k for k, s in self.samples.items() if s['location'] == 'bench'),
            'sample.on_stage': (dict(sample_id=self.on_stage, offset_x_m=self.samples[self.on_stage]['offset_x_m'], offset_y_m=self.samples[self.on_stage]['offset_y_m'])
                                if self.on_stage else None),
            'panel.status_green': self.power and not err and not self._xray_on(), 'panel.status_orange': self.power and self._xray_on(),
            'panel.status_red': self.power and err,
            'truth.axis_offset_m': self.truth['axis_offset'], 'truth.samples': self._truth_samples(), 'truth.faults': sorted(self.faults),
            'truth.detector_defect_columns': list(self.truth['defects']),
        }
        return {k: v for k, v in values.items() if SLOTS[k]['visibility'] == 'agent' or visibility == 'evaluator'}

    def _last_record(self, keep=None):
        done = [k for k, r in self.records.items() if r['status'] == 'completed' and (keep is None or keep(k))]
        return max(done, key=lambda k: (self.records[k]['finished_s'] or 0.0, k), default=None)

    def _truth_samples(self):
        out = {}
        for k, s in self.samples.items():
            d = {kk: copy.deepcopy(vv) for kk, vv in s.items() if kk != '_pores'}
            d['pores_generated'] = copy.deepcopy(s.get('_pores', []))
            d['truth'] = sample_truth(s)
            out[k] = d
        return out

    # ---------------------------------------------------------------- commands
    def invoke(self, capability_id, **a):
        if capability_id not in ALL_CAPS:
            raise ActionRejected('UNKNOWN_CAPABILITY')
        if capability_id in UNSUPPORTED:
            raise ActionRejected('UNSUPPORTED')
        validate(ALL_CAPS[capability_id]['input_schema'], a)
        a = coerce(ALL_CAPS[capability_id]['input_schema'], a)         # integral floats of (nested) integer fields -> int
        fn = getattr(self, '_cmd_' + capability_id.replace('.', '_'))
        out = fn(**a)
        if ALL_CAPS[capability_id]['kind'] not in ('query', 'analysis', 'simulation'):
            self.revision += 1
        return out

    def _need_plant(self):
        if self.plant is None:
            raise ActionRejected('PLANT_REQUIRED')

    def _need_power(self):
        self._need_plant()
        if not self.power:
            raise ActionRejected('POWER_OFF')

    def _need_ready(self, allow_error=False):
        self._need_power()
        if self.state != 'ready':
            raise ActionRejected('NOT_READY')
        if self.estop:
            raise ActionRejected('EMERGENCY_STOP')
        if self.error is not None and not allow_error:
            raise ActionRejected('ERROR_PENDING')

    def _need_idle(self):
        if self.op is not None:
            raise ActionRejected('MEASUREMENT_RUNNING')

    def _need_still(self):
        if self._stage_moving() or self._filter_moving() or self._door_moving():
            raise ActionRejected('MOTION_IN_PROGRESS')

    # ---------------------------------------------------------------- front panel: host protocol of the engine (_panel_ui.py) and the panel.* commands
    def panel_read(self):
        """What the touch screen and the status light show: the agent slots plus whether the system has power and the power-up count."""
        out = self.read('agent')
        out.update({'host.powered': bool(self.power), 'host.power_epoch': self.power_epoch, 'host.booting': self.state == 'booting'})
        return out

    def panel_invoke(self, capability_id, arguments):
        """A panel action calls the same command a direct call would.  A gesture made through a session acts as that session's caller: the
        session's guards apply (the mains button is refused while another agent's acquisition runs; the safety button always works) and an
        operation started from the panel belongs to the caller."""
        try:
            caller = self._caller
            if caller is not None:
                BrukerX4PoseidonSession(self, caller)._guard(capability_id, arguments)
            out = self.invoke(capability_id, **arguments)
            op = out.get('operation_id') if capability_id in ASYNC else None
            if caller is not None and op:
                self._owners[op] = caller
                rid = self.operations[op].get('record_id')
                if rid:
                    self._owners[rid] = caller
            return out
        except ActionRejected as exc:
            raise _panel_ui.PanelRejected(str(exc)) from None

    def panel_time(self):
        return self.t

    def _panel(self, gesture, *args, **kw):
        self._need_plant()
        try:
            return dict(accepted=bool(gesture(*args, **kw)))
        except _panel_ui.PanelError:
            raise ActionRejected('INVALID_ARGUMENT') from None

    def _cmd_panel_touch(self, display_id, x, y, duration_s=0.1, to_x=None, to_y=None):
        to = None if to_x is None or to_y is None else (int(to_x), int(to_y))
        return self._panel(self.panel.touch, display_id, int(x), int(y), float(duration_s), to=to)

    def _cmd_panel_press(self, input_id, duration_s=0.1, held=()):
        return self._panel(self.panel.press, input_id, float(duration_s), held=tuple(held))

    def _cmd_panel_switch(self, input_id, position, held=()):
        return self._panel(self.panel.switch, input_id, position, held=tuple(held))

    def _cmd_panel_capture(self, display_id):
        """Close-up camera of a display: a PNG file named by its content (large data is returned as a file reference)."""
        self._need_plant()
        data, w, h = self.panel.png_bytes(display_id)
        sha = hashlib.sha256(data).hexdigest()
        d = Path(self.panel_capture_dir) if self.panel_capture_dir else Path(tempfile.gettempdir()) / 'chemistry_sim_panel' / self.instance_id
        d.mkdir(parents=True, exist_ok=True)
        path = d / f'{display_id}_{sha[:16]}.png'
        if not path.exists():
            path.write_bytes(data)
        return dict(display_id=display_id, time_s=self.t, image=dict(path=str(path), format='png', width=w, height=h, sha256=sha))

    def _cmd_panel_frame(self):
        return dict(frame=self.panel.frame())

    # ---------------------------------------------------------------- setup, samples, power, safety, faults
    def _cmd_plant_configure(self, room_k, mains_voltage_v, relative_humidity, noise_seed):
        first = self.plant is None
        if not first and noise_seed != self.plant['noise_seed']:
            raise ActionRejected('SEED_FIXED')
        self.plant = dict(room_k=room_k, mains_voltage_v=mains_voltage_v, relative_humidity=relative_humidity, noise_seed=noise_seed)
        if first:
            rng = random.Random(noise_seed * 7919 + 17)
            self.truth['axis_offset'] = rng.gauss(0.0, AXIS_OFFSET_SD_M)
        if self.power and not (MAINS_V[0] * 0.9 <= mains_voltage_v <= MAINS_V[1] * 1.1):
            self._power_off('MAINS_FAULT')
        return dict(plant=dict(self.plant))

    def _cmd_sample_provide(self, sample_id, kind, diameter_m, height_m, layers, pores, cracks, inclusions, container, pin):
        self._need_plant()
        if sample_id in self.samples:
            raise ActionRejected('SAMPLE_EXISTS')
        if len(self.samples) >= MAX_SAMPLES:
            raise ActionRejected('TOO_MANY_SAMPLES')
        s = dict(sample_id=sample_id, kind=kind, diameter_m=diameter_m, height_m=height_m, layers=copy.deepcopy(layers), pores=copy.deepcopy(pores),
                 cracks=copy.deepcopy(cracks), inclusions=copy.deepcopy(inclusions), container=copy.deepcopy(container), pin=copy.deepcopy(pin),
                 location='bench', offset_x_m=0.0, offset_y_m=0.0)
        R = diameter_m / 2
        if kind == 'cylinder':
            if not layers or abs(sum(L['thickness_m'] for L in layers) - height_m) > 1e-9:
                raise ActionRejected('INVALID_SAMPLE')
            for L in layers:
                if abs(sum(m['volume_fraction'] for m in L['materials']) - 1.0) > 1e-6:
                    raise ActionRejected('INVALID_SAMPLE')
            zb = _layer_bounds(s)
            for c in cracks:
                nrm = np.asarray(c['normal'], float)
                if np.linalg.norm(nrm) < 0.5 or math.hypot(c['center_m'][0], c['center_m'][1]) + c['radius_m'] > R or not any(
                        zb[k] + c['radius_m'] <= c['center_m'][2] <= zb[k + 1] - c['radius_m'] for k in range(len(layers))):
                    raise ActionRejected('INVALID_SAMPLE')
            for i in inclusions:
                if math.hypot(i['center_m'][0], i['center_m'][1]) + i['radius_m'] > R or not any(
                        zb[k] + i['radius_m'] <= i['center_m'][2] <= zb[k + 1] - i['radius_m'] for k in range(len(layers))):
                    raise ActionRejected('INVALID_SAMPLE')
        elif layers or pores['count'] or cracks or inclusions:
            raise ActionRejected('INVALID_SAMPLE')                  # phantoms have fixed materials and no pores / cracks / inclusions
        for sid in _materials(s):
            substance(sid)
        s['_pores'] = generate_pores(s)
        self.samples[sample_id] = s
        self.used_substances.update(_materials(s))
        self._event('sample', 'SAMPLE_PROVIDED', f'样品{sample_id}放在实验台上')
        return dict(sample_id=sample_id, location='bench', mass_kg=sample_mass_kg(s))

    def _envelope(self, s, ox, oy):
        return sample_radius_total(s) + math.hypot(ox, oy)

    def _geometry_ok(self, sod, cz, sample_id=None, ox=None, oy=None):
        sid = self.on_stage if sample_id is None else sample_id
        if sid is None:
            return self._clear(sod, cz, None)
        s = self.samples[sid]
        ox = s['offset_x_m'] if ox is None else ox
        oy = s['offset_y_m'] if oy is None else oy
        return self._clear(sod, cz, s, ox, oy)

    def _clear(self, sod, cz, s, ox=0.0, oy=0.0):
        """Keep-out check of the moving column (sample body, pin, chuck, rotation-stage housing; each a vertical cylinder about the rotation axis)
        against the source head / filter slide and the detector housing where their heights overlap, and against the chamber ceiling."""
        bz = SOURCE_XYZ_M[2]
        segs = [(CHUCK_R_M, cz - CHUCK_H_M, cz), (STAGE_R_M, cz - CHUCK_H_M - STAGE_H_M, cz - CHUCK_H_M)]
        top = cz
        if s is not None:
            e = math.hypot(ox, oy)
            z0 = cz
            if s['pin']:
                segs.append((s['pin']['diameter_m'] / 2 + e, cz, cz + s['pin']['length_m']))
                z0 = cz + s['pin']['length_m']
            segs.append((sample_radius_total(s) + e, z0, z0 + s['height_m']))
            top = z0 + s['height_m']
        for r, za, zb_ in segs:
            if za < bz + SOURCE_KEEPOUT_DZ[1] and zb_ > bz + SOURCE_KEEPOUT_DZ[0] and sod - r < FILTER_CLEAR_M + COLLISION_MARGIN_M:
                return False
            if za < bz + DETECTOR_KEEPOUT_DZ[1] and zb_ > bz + DETECTOR_KEEPOUT_DZ[0] and SDD_M - DETECTOR_COVER_M - sod - r < COLLISION_MARGIN_M:
                return False
        return top <= CHAMBER_CEILING_M - COLLISION_MARGIN_M

    def _accessible(self):
        x_axis = SOURCE_XYZ_M[0] + self.sod
        return DOOR_X_RANGE_M[0] + 0.02 <= x_axis <= DOOR_X_RANGE_M[1] - 0.02

    def _cmd_sample_place(self, sample_id, offset_x_m, offset_y_m):
        self._need_plant()
        s = self.samples.get(sample_id)
        if s is None:
            raise ActionRejected('UNKNOWN_SAMPLE')
        if s['location'] != 'bench':
            raise ActionRejected('SAMPLE_NOT_ON_BENCH')
        if self.on_stage is not None:
            raise ActionRejected('STAGE_OCCUPIED')
        self._need_idle()
        if self.door < DOOR_TRAVEL_M - 1e-9 or self._door_moving():
            raise ActionRejected('DOOR_CLOSED')
        if self._stage_moving():
            raise ActionRejected('MOTION_IN_PROGRESS')
        if not self._accessible():
            raise ActionRejected('NOT_ACCESSIBLE')
        if not self._geometry_ok(self.sod, self.cz, sample_id, offset_x_m, offset_y_m):
            raise ActionRejected('COLLISION_RISK')
        s['location'], s['offset_x_m'], s['offset_y_m'] = 'stage', offset_x_m, offset_y_m
        self.on_stage = sample_id
        self._rate_cache.clear()
        self._event('sample', 'SAMPLE_PLACED', f'样品{sample_id}装到样品夹(偏移{offset_x_m * 1e3:.2f}, {offset_y_m * 1e3:.2f} mm)')
        return dict(sample_id=sample_id, offset_x_m=offset_x_m, offset_y_m=offset_y_m)

    def _cmd_sample_remove(self, sample_id):
        self._need_plant()
        if self.on_stage != sample_id:
            raise ActionRejected('SAMPLE_NOT_ON_STAGE')
        self._need_idle()
        if self.door < DOOR_TRAVEL_M - 1e-9 or self._door_moving():
            raise ActionRejected('DOOR_CLOSED')
        if self._stage_moving():
            raise ActionRejected('MOTION_IN_PROGRESS')
        if not self._accessible():
            raise ActionRejected('NOT_ACCESSIBLE')
        s = self.samples[sample_id]
        s['location'], s['offset_x_m'], s['offset_y_m'] = 'bench', 0.0, 0.0
        self.on_stage = None
        self._rate_cache.clear()
        self._event('sample', 'SAMPLE_REMOVED', f'样品{sample_id}取回实验台')
        return dict(sample_id=sample_id)

    def _cmd_fault_inject(self, fault, active):
        self._need_plant()
        if active:
            self.faults.add(fault)
            code = FAULT_ERROR.get(fault)
            if code and self.power:
                self._raise_error(code)
            if fault == 'detector_defects' and not self.truth['defects']:
                g = random.Random(self._seed() * 31 + 3)
                self.truth['defects'] = sorted(g.sample(range(20, SIM_DETECTOR[0] - 20), 3))
            if fault == 'xray_arc':
                self._beam_off('XRAY_ARC')
            if fault == 'rotation_stall':
                self.rot_target = self.rot
                if self.op is not None and self.op['kind'] in ('projections', 'geometry_calibration'):
                    self._fail_op('STAGE_FAULT')
            if fault == 'door_sensor':
                self._beam_off('INTERLOCK_FAULT')
            if fault == 'light_curtain_blocked' and self.door_target < self.door - 1e-12:
                self.door_target = DOOR_TRAVEL_M                    # closing door stops and reopens
                self._event('safety', 'LIGHT_CURTAIN', '安全光幕被挡:门停止关闭并重新打开')
        else:
            self.faults.discard(fault)
            if fault == 'detector_defects':
                self.truth['defects'] = []
        self._event('fault', 'FAULT_' + ('ON' if active else 'OFF'), fault)
        return dict(faults=sorted(self.faults))

    def _raise_error(self, code):
        if self.error is None:
            self.error = code
            self._event('error', code, ERROR_ZH.get(code, code))

    def _beam_off(self, code=None):
        """Switch the X-ray off at once (interlock, arc, emergency stop); a running exposure fails."""
        was_on = self._xray_on() or self.x_enabled
        self.x_enabled = False
        self.xv = self.xi = 0.0
        self.warm_end = None
        if self.op is not None and self.op['kind'] in ('flat_field', 'geometry_calibration', 'radiograph', 'projections'):
            self._fail_op(code or 'XRAY_OFF')
        if was_on:
            self._event('xray', 'XRAY_OFF', 'X射线关闭' + (f'({code})' if code else ''))

    def _cmd_power_press_button(self):
        return self._cmd_power_set(not self.power)

    def _cmd_power_set(self, enabled):
        self._need_plant()
        if enabled and not self.power:
            if not (MAINS_V[0] * 0.9 <= self.plant['mains_voltage_v'] <= MAINS_V[1] * 1.1):
                raise ActionRejected('MAINS_OUT_OF_RANGE')
            self.power, self.state, self.state_t0 = True, 'booting', self.t
            self.power_epoch += 1
            self.warm_needed = True
            if self.error == 'MAINS_FAULT':
                self.error = None
            self._latch_persisting_causes()
            self._event('power', 'POWER_ON', '系统通电,启动(各轴保持断电时的位置)')
        elif not enabled and self.power:
            self._power_off(None)
        return dict(enabled=self.power, state=self.state)

    def _power_off(self, code):
        self._beam_off('POWER_OFF')
        if self.op is not None:
            self._fail_op('POWER_OFF')
        self.power, self.state = False, 'off'
        self.sod_target, self.cz_target, self.rot_target = self.sod, self.cz, self.rot
        self.door_target = self.door
        self.slide_target = self.slide
        if code:
            if self.error is None:
                self.error = code
            self._event('error', code, ERROR_ZH.get(code, code))
        self._event('power', 'POWER_OFF', '系统断电:X射线关闭,各轴停止')

    def _cmd_emergency_stop(self):
        self._need_plant()
        if not self.estop:
            self.estop = True
            self._beam_off('EMERGENCY_STOP')
            if self.op is not None:
                self._fail_op('EMERGENCY_STOP')
            self.sod_target, self.cz_target, self.rot_target = self.sod, self.cz, self.rot
            self.door_target = self.door
            self.slide_target = self.slide
            if self.power:
                self._raise_error('EMERGENCY_STOP')
            self._event('safety', 'EMERGENCY_STOP', '安全按钮按下:X射线关闭,所有运动停止')
        return dict(stopped=True)

    def _cmd_emergency_release(self):
        self._need_plant()
        if self.estop:
            self.estop = False
            self._event('safety', 'EMERGENCY_RELEASED', '安全按钮已释放(需确认错误)')
        return dict(stopped=False)

    def _cmd_system_acknowledge(self):
        self._need_power()
        if self.error is None:
            return dict(cleared=None)
        cause = {v: k for k, v in FAULT_ERROR.items()}.get(self.error)
        if cause in self.faults or (self.error == 'EMERGENCY_STOP' and self.estop) or \
                (self.error == 'MAINS_FAULT' and not (MAINS_V[0] * 0.9 <= self.plant['mains_voltage_v'] <= MAINS_V[1] * 1.1)):
            raise ActionRejected('CAUSE_PERSISTS')
        cleared, self.error = self.error, None
        self._event('error', 'ACKNOWLEDGED', f'确认错误{cleared}')
        if self.power:
            self._latch_persisting_causes()                             # another cause still present latches its own error
        return dict(cleared=cleared)

    # ---------------------------------------------------------------- door, X-ray, filter, stage
    def _cmd_door_open(self):
        self._need_ready()
        self._need_idle()
        if self._xray_on() or self.x_enabled:
            raise ActionRejected('XRAY_ON')
        self.door_target = DOOR_TRAVEL_M
        return dict(target_m=DOOR_TRAVEL_M, duration_s=abs(DOOR_TRAVEL_M - self.door) / DOOR_SPEED_M_S)

    def _cmd_door_close(self):
        self._need_ready()
        if 'light_curtain_blocked' in self.faults:
            raise ActionRejected('LIGHT_CURTAIN_BLOCKED')
        self.door_target = 0.0
        return dict(target_m=0.0, duration_s=self.door / DOOR_SPEED_M_S)

    def _cmd_xray_set(self, voltage_v, current_a):
        self._need_plant()
        if voltage_v * current_a > XRAY_POWER_MAX_W + 1e-9:
            raise ActionRejected('POWER_LIMIT')
        if self.op is not None and self.op['kind'] in ('flat_field', 'geometry_calibration', 'radiograph', 'projections'):
            raise ActionRejected('MEASUREMENT_RUNNING')
        self.xv_set, self.xi_set = voltage_v, current_a
        return dict(voltage_v=voltage_v, current_a=current_a, power_w=voltage_v * current_a, focal_spot_m=focal_spot_m(voltage_v * current_a))

    def _cmd_xray_enable(self, enabled):
        if not enabled:
            self._need_plant()
            if self.op is not None and self.op['kind'] in ('flat_field', 'geometry_calibration', 'radiograph', 'projections'):
                raise ActionRejected('MEASUREMENT_RUNNING')
            self._beam_off(None)
            return dict(enabled=False, warmup_s=0.0, ramp_s=0.0)
        self._need_ready()
        if not self._door_closed():
            raise ActionRejected('DOOR_OPEN')
        if not self._interlock_ok():
            raise ActionRejected('INTERLOCK_OPEN')
        if 'xray_arc' in self.faults:
            raise ActionRejected('XRAY_ARC')
        if self.x_enabled:
            return dict(enabled=True, warmup_s=max(0.0, self.warm_end - self.t) if self.warm_end else 0.0, ramp_s=self._ramp_time())
        self.x_enabled = True
        warm = 0.0
        if self.warm_needed:
            self.warm_from, self.warm_end = self.t, self.t + WARMUP_S
            self.warm_v, self.warm_i = self.xv_set, self.xi_set
            warm = WARMUP_S
            self._event('xray', 'WARMUP', f'X射线预热程序{WARMUP_S:.0f} s')
        self._event('xray', 'XRAY_ON', 'X射线开启')
        self._rate_cache.clear()
        return dict(enabled=True, warmup_s=warm, ramp_s=0.0 if warm else self._ramp_time())

    def _ramp_time(self):
        return max(abs(self.xv_set - self.xv) / KV_RAMP_V_S, abs(self.xi_set - self.xi) / CURRENT_RAMP_A_S)

    def _cmd_filter_select(self, position):
        self._need_ready()
        self._need_idle()
        self.filter_pos = position
        self.slide_target = position * FILTER_PITCH_M
        self._rate_cache.clear()
        return dict(filter=filter_of(position)['label'], duration_s=abs(self.slide_target - self.slide) / (FILTER_PITCH_M / FILTER_S_PER_STEP))

    def _stage_checks(self):
        self._need_ready()
        if 'rotation_stall' in self.faults:
            raise ActionRejected('STAGE_FAULT')
        self._need_idle()

    def _latch_persisting_causes(self):
        """Latch the error of the highest-priority cause that is still present (fixed order: safety button, interlock sensor, tube arc,
        rotation stall)."""
        if self.estop:
            self._raise_error('EMERGENCY_STOP')
        for f in FAULT_PRIORITY:
            if f in self.faults and FAULT_ERROR.get(f):
                self._raise_error(FAULT_ERROR[f])

    def _forbidden_boxes(self, s, ox, oy):
        """Open rectangles in the (SOD, chuck height) plane that the moving column must not enter (source head / filter slide and detector
        housing for each of its cylinders: stage housing, chuck, pin, sample) and the highest allowed chuck height (ceiling)."""
        bz = SOURCE_XYZ_M[2]
        segs = [(CHUCK_R_M, -CHUCK_H_M, 0.0), (STAGE_R_M, -CHUCK_H_M - STAGE_H_M, -CHUCK_H_M)]
        top = 0.0
        if s is not None:
            ecc = math.hypot(ox, oy)
            z0 = 0.0
            if s['pin']:
                segs.append((s['pin']['diameter_m'] / 2 + ecc, 0.0, s['pin']['length_m']))
                z0 = s['pin']['length_m']
            segs.append((sample_radius_total(s) + ecc, z0, z0 + s['height_m']))
            top = z0 + s['height_m']
        boxes = []
        for r, a, b in segs:
            boxes.append(((-math.inf, FILTER_CLEAR_M + COLLISION_MARGIN_M + r), (bz + SOURCE_KEEPOUT_DZ[0] - b, bz + SOURCE_KEEPOUT_DZ[1] - a)))
            boxes.append(((SDD_M - DETECTOR_COVER_M - COLLISION_MARGIN_M - r, math.inf), (bz + DETECTOR_KEEPOUT_DZ[0] - b, bz + DETECTOR_KEEPOUT_DZ[1] - a)))
        return boxes, CHAMBER_CEILING_M - COLLISION_MARGIN_M - top

    def _path_ok(self, sod1, cz1):
        """Swept-path keep-out check: carriage and lift start together at their own speeds, so the column follows two straight segments in
        the (SOD, chuck height) plane; neither may cross a forbidden rectangle."""
        s = self.samples[self.on_stage] if self.on_stage else None
        boxes, cz_max = self._forbidden_boxes(s, s['offset_x_m'] if s else 0.0, s['offset_y_m'] if s else 0.0)
        s0, c0 = self.sod, self.cz
        ts, tz = abs(sod1 - s0) / CARRIAGE_SPEED_M_S, abs(cz1 - c0) / LIFT_SPEED_M_S
        if ts <= tz:                                    # the carriage arrives first: the corner lies exactly on its target
            mid = (sod1, c0 + math.copysign(LIFT_SPEED_M_S * ts, cz1 - c0) if tz > 0 else c0)
        else:
            mid = (s0 + math.copysign(CARRIAGE_SPEED_M_S * tz, sod1 - s0), cz1)
        pts = [(s0, c0), mid, (sod1, cz1)]
        if max(p[1] for p in pts) > cz_max + 1e-12:
            return False
        return not any(_segment_hits_box(pts[k], pts[k + 1], box) for k in range(2) for box in boxes)

    def _recon_region(self, data, axis):
        """Reconstructable region of an acquisition window (as in _fdk): radius about the axis and the height range at that radius, less the
        +-200 um the automatic alignment may shift the axis."""
        w, sod = data['window'], data['sod']
        us = (w['u0'] + (np.arange(w['nu']) + 0.5) * w['pitch'] - axis * SDD_M / sod) * sod / SDD_M
        vs = (w['v1'] - (np.arange(w['nv']) + 0.5) * w['pitch']) * sod / SDD_M
        fov_r = min(abs(us.min()), abs(us.max())) * sod / math.hypot(sod, abs(us).max()) - 200e-6
        return fov_r, float(vs.min()) * (sod - fov_r) / sod, float(vs.max()) * (sod - fov_r) / sod

    def _move_stage(self, sod, cz, label=None):
        if not self._geometry_ok(sod, cz) or not self._path_ok(sod, cz):
            raise ActionRejected('COLLISION_RISK')
        self.sod_target, self.cz_target = sod, cz
        self.position_label = label
        d = max(abs(sod - self.sod) / CARRIAGE_SPEED_M_S, abs(cz - self.cz) / LIFT_SPEED_M_S)
        return dict(target=[sod, cz], duration_s=d, magnification=SDD_M / sod)

    def _cmd_stage_move(self, sod_m, chuck_z_m):
        self._stage_checks()
        out = self._move_stage(sod_m, chuck_z_m)
        return out

    def _cmd_stage_go_to(self, position):
        self._stage_checks()
        p = LOAD_POSITION if position == 'load' else CENTER_POSITION
        out = self._move_stage(p['sod'], p['cz'], position)
        return dict(target=out['target'], duration_s=out['duration_s'])

    def _cmd_stage_rotate(self, angle_rad):
        self._stage_checks()
        self.rot_target = angle_rad % (2 * math.pi)
        return dict(target_rad=self.rot_target, duration_s=abs(_rot_delta(self.rot, self.rot_target)) / ROTATION_SPEED_RAD_S)

    def _cmd_scan_configure(self, range_deg, step_rad, exposure_s, averaging, binning, random_movement):
        self._need_plant()
        self._need_idle()
        s = dict(range_deg=range_deg, step_rad=step_rad, exposure_s=exposure_s, averaging=averaging, binning=binning, random_movement=random_movement)
        n = self._nominal_projections(s)
        self.scan_settings = s
        return dict(settings=dict(s), projections=n, duration_s=self._scan_duration(s))

    # ---------------------------------------------------------------- operations
    def _new_op(self, kind, rid=None):
        self.seq['op'] += 1
        oid = f'op{self.seq["op"]:05d}'
        self.operations[oid] = dict(operation_id=oid, kind=kind, phase='running', record_id=rid, started_s=self.t, reason=None)
        return oid

    def _new_record_id(self, prefix, key):
        if len(self.records) >= MAX_RECORDS:
            raise ActionRejected('RECORDS_FULL')
        self.seq[key] += 1
        return f'{prefix}{self.seq[key]:04d}'

    def _acq_checks(self, need_sample=True, need_flat=True):
        self._need_ready()
        self._need_idle()
        self._need_still()
        if not self._door_closed():
            raise ActionRejected('DOOR_OPEN')
        if not self._xray_ready():
            raise ActionRejected('XRAY_NOT_READY')
        if 'rotation_stall' in self.faults:
            raise ActionRejected('STAGE_FAULT')
        if need_sample and self.on_stage is None:
            raise ActionRejected('NO_SAMPLE')
        if need_flat and not self._flat_valid():
            raise ActionRejected('FLAT_FIELD_REQUIRED')

    def _xsnap(self):
        return dict(voltage_v=self.xv_set, current_a=self.xi_set, power_w=self.xv_set * self.xi_set, filter=filter_of(self.filter_pos)['label'],
                    focal_spot_m=focal_spot_m(self.xv_set * self.xi_set))

    def _record_stub(self, rid, kind, ctx):
        r = dict(record_id=rid, kind=kind, status='running', started_s=self.t, finished_s=None, reason=None, sample_id=ctx.get('sample_id'),
                 xray=self._xsnap() if kind != 'volume' else None, scan=dict(ctx['scan']) if ctx.get('scan') else None, sod_m=ctx.get('sod'), sdd_m=SDD_M,
                 magnification=SDD_M / ctx['sod'] if ctx.get('sod') else None,
                 voxel_size_nominal_m=PIXEL_PITCH_M * int(ctx['scan']['binning'][0]) * ctx['sod'] / SDD_M if ctx.get('scan') and ctx.get('sod') else None,
                 projections_nominal=ctx.get('n_nom', 0), projections_simulated=ctx.get('n_sim', 0), sim_detector=list(SIM_DETECTOR), sim_pixel_m=None, transmission=None,
                 saturated_fraction=None, dose_gy=None, duration_s=0.0, preview=None, preview_unit=None, source_record_id=ctx.get('source'), recon=None, volume=None,
                 flat_field=None, geometry=None, warnings=[], template_source=dict(library=LIBRARY_REL, substances=sorted(ctx.get('materials', []))), note_zh='')
        self.records[rid] = r
        return r

    def _start_op(self, kind, record_id, items, ctx):
        oid = self._new_op(kind, record_id)
        t, ends = self.t, []
        for it in items:
            t += it[1]
            ends.append(t)
        self.op = dict(kind=kind, op=oid, record_id=record_id, t0=self.t, duration_s=ends[-1] - self.t if ends else 0.0, items=items, ends=ends, i=-1,
                       phase=None, ctx=ctx)
        self._next_item()
        return dict(operation_id=oid)

    def _next_item(self):
        o = self.op
        while o is not None:
            o['i'] += 1
            if o['i'] >= len(o['items']):
                return
            it = o['items'][o['i']]
            o['phase'] = it[0]
            if it[0] == 'rotate':                                # scan rotation driven by the phase clock (a full turn returns to the start)
                o['rot0'], o['rot_amount'], o['rot_t0'], o['rot_dur'] = self.rot, it[2], self.t, it[1]
            elif it[0] == 'call':
                getattr(self, it[2])(*it[3:])
                if self.op is not o:
                    return
            if self.t < o['ends'][o['i']] - 1e-12:
                return

    def _ctx(self, kind, materials=None):
        s = self.samples.get(self.on_stage) if self.on_stage else None
        return dict(kind=kind, sample_id=self.on_stage, sod=self.sod, cz=self.cz, rot0=self.rot, scan=dict(self.scan_settings), kv=self.xv_set, ia=self.xi_set,
                    filter=self.filter_pos, seed=self._op_seed(kind), axis=self.truth['axis_offset'], defects=list(self.truth['defects']),
                    materials=_materials(s) if s else [], flat=self.flat, offset=(s['offset_x_m'], s['offset_y_m']) if s else (0.0, 0.0),
                    axis_known=self.geo_cal['axis_offset_m'])

    def _op_seed(self, tag):
        self.seq[tag] = self.seq.get(tag, 0) + 1
        return int(hashlib.sha256(f'{self._seed()}|{tag}|{self.seq[tag]}'.encode()).hexdigest()[:12], 16)

    def _cmd_calibration_flat_field(self):
        self._acq_checks(need_sample=False, need_flat=False)
        if self._anything_in_field():
            raise ActionRejected('SAMPLE_IN_BEAM')
        rid = self._new_record_id('FF', 'ff')
        ctx = self._ctx('flat_field')
        self._record_stub(rid, 'flat_field', ctx)
        tf = FLAT_FIELD_FRAMES * (self.scan_settings['exposure_s'] + FRAME_OVERHEAD_S)
        items = [('dark', tf), ('bright', tf), ('call', 0.0, '_finish_flat_field')]
        return self._start_op('flat_field', rid, items, ctx)

    def _cyl_on_detector(self, r, zb, zt, full=False):
        """Does a vertical cylinder of radius r about the rotation axis between heights zb..zt above the beam project onto the detector
        (full=True: completely)?"""
        half_u, half_v = DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2, DETECTOR_PIXELS[1] * PIXEL_PITCH_M / 2
        sod = self.sod
        near, far = sod - r, sod + r
        uc = SDD_M * self.truth['axis_offset'] / sod
        uh = SDD_M * r / math.sqrt(max(sod * sod - r * r, 1e-12))
        vs = [SDD_M * z / d for z in (zb, zt) for d in (near, far)]
        if full:
            return abs(uc) + uh <= half_u and -half_v <= min(vs) and max(vs) <= half_v
        return abs(uc) - uh < half_u and max(vs) > -half_v and min(vs) < half_v

    def _sample_in_field(self):
        """Does the sample body (envelope cylinder, container included; not the mounting pin) project onto the detector?"""
        s = self.samples[self.on_stage]
        zb = self.cz - SOURCE_XYZ_M[2] + (s['pin']['length_m'] if s['pin'] else 0.0)
        return self._cyl_on_detector(self._envelope(s, s['offset_x_m'], s['offset_y_m']), zb, zb + s['height_m'])

    def _anything_in_field(self):
        """Sample, mounting pin, chuck or rotation-stage housing in the beam (a flat field must see the empty beam)."""
        z = self.cz - SOURCE_XYZ_M[2]
        if self._cyl_on_detector(STAGE_HOUSING_R_M, z - CHUCK_H_M - STAGE_H_M, z):
            return True
        if self.on_stage is None:
            return False
        s = self.samples[self.on_stage]
        return self._cyl_on_detector(self._envelope(s, s['offset_x_m'], s['offset_y_m']), z, z + sample_height_total(s))

    def _cmd_calibration_geometry(self):
        self._acq_checks(need_sample=True)
        s = self.samples[self.on_stage]
        if s['kind'] != 'calibration_phantom':
            raise ActionRejected('PHANTOM_REQUIRED')
        zb = self.cz - SOURCE_XYZ_M[2] + (s['pin']['length_m'] if s['pin'] else 0.0)
        if not self._cyl_on_detector(self._envelope(s, s['offset_x_m'], s['offset_y_m']), zb, zb + s['height_m'], full=True):
            raise ActionRejected('PHANTOM_NOT_IN_FIELD')                  # every ball must stay on the detector at every angle
        rid = self._new_record_id('GC', 'gc')
        ctx = self._ctx('geometry_calibration')
        ctx['n_nom'] = ctx['n_sim'] = 36
        self._record_stub(rid, 'geometry_calibration', ctx)
        tp = self._projection_time()
        items = [('rotate', 36 * tp, 2 * math.pi), ('call', 0.0, '_finish_geometry_calibration')]
        return self._start_op('geometry_calibration', rid, items, ctx)

    def _cmd_xray_radiograph(self, roi):
        self._acq_checks(need_sample=False)
        if roi is not None:
            half = roi['size_px'] / 2 * PIXEL_PITCH_M * self._binning()
            if abs(roi['center_u_m']) + half > DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2 + 1e-12 or \
                    abs(roi['center_v_m']) + half > DETECTOR_PIXELS[1] * PIXEL_PITCH_M / 2 + 1e-12:
                raise ActionRejected('ROI_OUTSIDE_DETECTOR')
        rid = self._new_record_id('RG', 'rg')
        ctx = self._ctx('radiograph')
        ctx['roi'] = copy.deepcopy(roi)
        ctx['n_nom'] = ctx['n_sim'] = 1
        self._record_stub(rid, 'radiograph', ctx)
        s = self.scan_settings
        items = [('expose', s['averaging'] * (s['exposure_s'] + FRAME_OVERHEAD_S)), ('call', 0.0, '_finish_radiograph')]
        return self._start_op('radiograph', rid, items, ctx)

    def _cmd_scan_start(self):
        self._acq_checks(need_sample=True)
        if not self._sample_in_field():
            raise ActionRejected('SAMPLE_NOT_IN_FIELD')
        rid = self._new_record_id('PJ', 'pj')
        ctx = self._ctx('projections')
        n = self._nominal_projections()
        ctx['n_nom'] = n
        ctx['n_sim'] = min(n, SIM_MAX_PROJECTIONS)
        ctx['span'] = self._scan_span()
        ctx['dose0'] = self.samples[self.on_stage].get('dose_gy', 0.0)
        self._record_stub(rid, 'projections', ctx)
        items = [('rotate', n * self._projection_time(), ctx['span']), ('call', 0.0, '_finish_scan')]
        return self._start_op('projections', rid, items, ctx)

    def _cmd_records_delete(self, record_id):
        self._need_plant()
        r = self.records.get(record_id)
        if r is None:
            raise ActionRejected('UNKNOWN_RECORD')
        if r['status'] == 'running' or (self.recon is not None and self.recon['ctx']['source'] == record_id):
            raise ActionRejected('RECORD_IN_USE')
        del self.records[record_id]
        self._data.pop(record_id, None)
        if self.last_porosity_rid == record_id:
            self.last_porosity = self.last_porosity_rid = None
        self._event('measurement', 'RECORD_DELETED', f'记录{record_id}已删除', rid=record_id)
        return dict(record_id=record_id, remaining=len(self.records))

    def _cmd_recon_start(self, record_id, grid, filter, beam_hardening, bh_material, ring_correction, smoothing_vox, post_alignment, auto_alignment, voi):
        self._need_plant()
        src = self.records.get(record_id)
        if src is None:
            raise ActionRejected('UNKNOWN_RECORD')
        if src['kind'] != 'projections' or src['status'] != 'completed':
            raise ActionRejected('RECORD_NOT_SUITABLE')
        if bh_material is not None:
            substance(bh_material)
        if self.recon is not None:
            raise ActionRejected('RECON_BUSY')
        data = self._data.get(record_id)
        if data is None:
            raise ActionRejected('DATA_NOT_CACHED')
        voi_partial = False
        if voi is not None:
            fov_r, z_lo, z_hi = self._recon_region(data, self.geo_cal['axis_offset_m'] or 0.0)
            cx, cy, cz_ = voi['center_m']
            sx, sy, sz = voi['size_m']
            if math.hypot(cx, cy) > fov_r or not z_lo <= cz_ <= z_hi:
                raise ActionRejected('INVALID_VOI')                      # the centre must lie in the reconstructable region
            voi_partial = math.hypot(abs(cx) + sx / 2, abs(cy) + sy / 2) > fov_r + 1e-12 or cz_ - sz / 2 < z_lo or cz_ + sz / 2 > z_hi
        rid = self._new_record_id('VO', 'vo')
        n = int(grid)
        ctx = dict(kind='volume', source=record_id, sample_id=src['sample_id'], sod=src['sod_m'], scan=src['scan'],
                   recon=dict(grid=grid, filter=filter, beam_hardening=beam_hardening, bh_material=bh_material, ring_correction=ring_correction,
                              smoothing_vox=smoothing_vox, post_alignment=post_alignment, auto_alignment=auto_alignment, voi=copy.deepcopy(voi)),
                   n_nom=src['projections_nominal'], n_sim=src['projections_simulated'], materials=src['template_source']['substances'],
                   axis_cal=self.geo_cal['axis_offset_m'], voi_partial=voi_partial)
        r = self._record_stub(rid, 'volume', ctx)
        r['recon'] = dict(ctx['recon'])
        r['xray'] = copy.deepcopy(src['xray'])
        b = int(src['scan']['binning'][0])
        frac = 1.0 if voi is None else min(1.0, float(np.prod(voi['size_m'])) / (self._fov(src['sod_m'])[0] ** 2 * self._fov(src['sod_m'])[1]))
        t = (DETECTOR_PIXELS[0] / b) ** 2 * (DETECTOR_PIXELS[1] / b) * frac * src['projections_nominal'] / RECON_RATE_VOXEL_PROJ_S + RECON_OVERHEAD_S
        oid = self._new_op('recon', rid)
        self.recon = dict(kind='recon', op=oid, record_id=rid, t0=self.t, duration_s=t, ctx=ctx)
        return dict(operation_id=oid)

    def _complete(self, o, r, dur=None):
        r.update(status='completed', finished_s=self.t, duration_s=self.t - r['started_s'] if dur is None else dur)
        self.operations[o['op']].update(phase='completed', finished_s=self.t)
        self._event('measurement', 'OP_DONE', f'操作{o["op"]}完成,记录{r["record_id"]}', rid=r['record_id'])
        if self.recon is o:
            self.recon = None
        else:
            self.op = None

    def _fail_op(self, code):
        o = self.op
        if o is None:
            return
        self.operations[o['op']].update(phase='failed', finished_s=self.t, reason=code)
        if o['record_id'] in self.records:
            r = self.records[o['record_id']]
            r.update(status='failed', finished_s=self.t, reason=code, duration_s=self.t - r['started_s'])
        self._event('measurement', 'OP_FAILED', f'操作{o["op"]}失败:{code}', rid=o['record_id'])
        self.op = None
        self.rot_target = self.rot

    def _cancel_op(self, op):
        if self.recon is not None and self.recon['op'] == op['operation_id']:
            o = self.recon
            self.operations[o['op']].update(phase='cancelled', finished_s=self.t, reason='USER_CANCEL')
            r = self.records[o['record_id']]
            r.update(status='cancelled', finished_s=self.t, reason='USER_CANCEL', duration_s=self.t - r['started_s'])
            self._event('measurement', 'OP_CANCELLED', f'重建{o["op"]}取消', rid=o['record_id'])
            self.recon = None
            return
        o = self.op
        if o is None or o['op'] != op['operation_id']:
            raise ActionRejected('NOT_RUNNING')
        self.operations[o['op']].update(phase='cancelled', finished_s=self.t, reason='USER_CANCEL')
        if o['record_id'] in self.records:
            r = self.records[o['record_id']]
            r.update(status='cancelled', finished_s=self.t, reason='USER_CANCEL', duration_s=self.t - r['started_s'])
        self._event('measurement', 'OP_CANCELLED', f'操作{o["op"]}取消', rid=o['record_id'])
        self.op = None
        self.rot_target = self.rot

    # ---------------------------------------------------------------- time
    def _cmd_sim_step(self, dt_s):
        self.step(dt_s)
        return dict(time_s=self.t)

    def step(self, dt_s):
        if not (isinstance(dt_s, (int, float)) and math.isfinite(dt_s) and dt_s >= 0):
            raise ActionRejected('INVALID_ARGUMENT')
        t_end = self.t + dt_s
        guard = 0
        while self.t < t_end and guard < 1_000_000:
            guard += 1
            h = min(t_end - self.t, self._next_event_dt())
            self._advance(h)
            self.t = t_end if h >= t_end - self.t else self.t + h
            self._handle_events()
        self._handle_events()

    def _xray_targets(self):
        """Voltage and current the tube is driven towards now (warm-up program: linear from 0 to the set point over WARMUP_S)."""
        if not (self.power and self.x_enabled and self._interlock_ok()):
            return 0.0, 0.0
        if self.warm_end is not None:
            f = min(1.0, max(0.0, (self.t - self.warm_from) / WARMUP_S))
            return self.warm_v * f, self.warm_i * f
        return self.xv_set, self.xi_set

    def _next_event_dt(self):
        c = [math.inf]
        if self.state == 'booting':
            c.append(self.state_t0 + BOOT_S - self.t)
        if self.power:
            if self._door_moving():
                c.append(abs(self.door_target - self.door) / DOOR_SPEED_M_S)
            if self._filter_moving():
                c.append(abs(self.slide_target - self.slide) / (FILTER_PITCH_M / FILTER_S_PER_STEP))
            if abs(self.sod - self.sod_target) > 1e-12:
                c.append(abs(self.sod_target - self.sod) / CARRIAGE_SPEED_M_S)
            if abs(self.cz - self.cz_target) > 1e-12:
                c.append(abs(self.cz_target - self.cz) / LIFT_SPEED_M_S)
            if not (self.op and self.op.get('phase') == 'rotate') and abs(_rot_delta(self.rot, self.rot_target)) > 1e-12:
                c.append(abs(_rot_delta(self.rot, self.rot_target)) / ROTATION_SPEED_RAD_S)
            if self.warm_end is not None:
                c.append(self.warm_end - self.t)
            elif self.x_enabled:
                tv, ti = self._xray_targets()
                if abs(self.xv - tv) > 1e-6:
                    c.append(abs(tv - self.xv) / KV_RAMP_V_S)
                if abs(self.xi - ti) > 1e-12:
                    c.append(abs(ti - self.xi) / CURRENT_RAMP_A_S)
        o = self.op
        if o is not None and 0 <= o['i'] < len(o['ends']):
            c.append(o['ends'][o['i']] - self.t)
        if self.recon is not None:
            c.append(self.recon['t0'] + self.recon['duration_s'] - self.t)
        if self._beam_live():
            c += self._cell_boundary_dt()
        return max(0.0, min(c))

    def _beam_live(self):
        """The tube emits now or starts to within the next sub-step (ramping up from zero)."""
        return self.power and (self._xray_on() or (self.x_enabled and self._interlock_ok()))

    def _cell_boundary_dt(self):
        """Times until a moving quantity crosses the next boundary of the energy-rate cells (rotation 2 pi / 32, carriage and lift 0.25 mm,
        filter slide one position pitch, voltage 1 kV): every sub-step with the beam on then lies inside one cell, so the booked energy does not
        depend on the host steps."""
        c = []
        o = self.op
        if o is not None and o.get('phase') == 'rotate' and o['rot_dur'] > 0:
            w = o['rot_amount'] / o['rot_dur']
        else:
            d = _rot_delta(self.rot, self.rot_target)
            w = math.copysign(ROTATION_SPEED_RAD_S, d) if abs(d) > 1e-12 else 0.0
        if w:
            c.append(_to_boundary(self.rot, w, RATE_ROT_CELL_RAD))
        for x, tgt, v in ((self.sod, self.sod_target, CARRIAGE_SPEED_M_S), (self.cz, self.cz_target, LIFT_SPEED_M_S)):
            if abs(tgt - x) > 1e-12:
                c.append(_to_boundary(x, math.copysign(v, tgt - x), RATE_POS_CELL_M))
        if abs(self.slide_target - self.slide) > 1e-12:
            c.append(_to_boundary(self.slide, math.copysign(FILTER_PITCH_M / FILTER_S_PER_STEP, self.slide_target - self.slide), FILTER_PITCH_M))
        if self.warm_end is not None:
            rate = self.warm_v / WARMUP_S
        else:
            tv, _ = self._xray_targets()
            rate = math.copysign(KV_RAMP_V_S, tv - self.xv) if abs(tv - self.xv) > 1e-6 else 0.0
        if rate:
            c.append(_to_boundary(self.xv, rate, RATE_KV_CELL_V))
        return c

    def _advance(self, h):
        if h <= 0:
            return
        if self.power:
            v0, i0, rot0, sod0, cz0, sl0 = self.xv, self.xi, self.rot, self.sod, self.cz, self.slide
            was = self.door != self.door_target
            self.door = _axis_move(self.door, self.door_target, DOOR_SPEED_M_S, h)
            if was and self.door == self.door_target:
                self.led['door_cycles'].add(0.5)
                self._event('door', 'DOOR_' + ('OPEN' if self.door > 0 else 'CLOSED'), '门已打开' if self.door > 0 else '门已关闭', t=self.t + h)
            self.slide = _axis_move(self.slide, self.slide_target, FILTER_PITCH_M / FILTER_S_PER_STEP, h)
            new = _axis_move(self.sod, self.sod_target, CARRIAGE_SPEED_M_S, h)
            self.led['carriage_travel_m'].add(abs(new - self.sod)); self.sod = new
            new = _axis_move(self.cz, self.cz_target, LIFT_SPEED_M_S, h)
            self.led['lift_travel_m'].add(abs(new - self.cz)); self.cz = new
            o = self.op
            if o is not None and o.get('phase') == 'rotate':
                f = min(1.0, (self.t + h - o['rot_t0']) / o['rot_dur']) if o['rot_dur'] > 0 else 1.0
                new = (o['rot0'] + o['rot_amount'] * f) % (2 * math.pi)
                self.led['rotation_rad'].add(abs(o['rot_amount']) * h / o['rot_dur'] if o['rot_dur'] > 0 else 0.0)
                self.rot = self.rot_target = new
            else:
                d = _rot_delta(self.rot, self.rot_target)
                step = min(abs(d), ROTATION_SPEED_RAD_S * h)
                self.led['rotation_rad'].add(step)
                self.rot = (self.rot + math.copysign(step, d)) % (2 * math.pi) if abs(d) > step else self.rot_target
            if self.warm_end is not None or self.x_enabled:
                tv, ti = self._xray_targets()
                if self.warm_end is not None:
                    f = min(1.0, max(0.0, (self.t + h - self.warm_from) / WARMUP_S))
                    self.xv, self.xi = self.warm_v * f, self.warm_i * f
                else:
                    self.xv = _axis_move(self.xv, tv, KV_RAMP_V_S, h)
                    self.xi = _axis_move(self.xi, ti, CURRENT_RAMP_A_S, h)
            if (v0 > 0.0 and i0 > 0.0) or self._xray_on():
                self.led['xray_on_s'].add(h)
                pose = dict(xv=0.5 * (v0 + self.xv), rot=(rot0 + 0.5 * _rot_delta(rot0, self.rot)) % (2 * math.pi), sod=0.5 * (sod0 + self.sod),
                            cz=0.5 * (cz0 + self.cz), slide=0.5 * (sl0 + self.slide))
                self._book_energy(h, v0, i0, self.xv, self.xi, pose)

    def _handle_events(self):
        for _ in range(10000):
            changed = False
            if self.state == 'booting' and self.t >= self.state_t0 + BOOT_S - 1e-9:
                self.state = 'ready'
                self._event('power', 'READY', '启动完成(各轴为绝对位置,保持断电时的位置)')
                changed = True
            if self.power:
                if abs(self.slide - self.slide_target) <= 1e-12:
                    self.slide = self.slide_target
                for a, b in (('sod', 'sod_target'), ('cz', 'cz_target')):
                    if abs(getattr(self, a) - getattr(self, b)) <= 1e-12:
                        setattr(self, a, getattr(self, b))
                if self.warm_end is not None and self.t >= self.warm_end - 1e-9:
                    self.warm_end = self.warm_from = None
                    self.warm_needed = False
                    self.xv, self.xi = self.warm_v, self.warm_i            # the program's end point; a newer set point follows with the ramp
                    self._event('xray', 'WARMUP_DONE', '预热完成')
                    changed = True
            o = self.op
            if o is not None and 0 <= o['i'] < len(o['ends']) and self.t >= o['ends'][o['i']] - 1e-9:
                it = o['items'][o['i']]
                if it[0] == 'rotate':
                    self.rot = self.rot_target = (o['rot0'] + o['rot_amount']) % (2 * math.pi)
                self._next_item()
                changed = True
            rc = self.recon
            if rc is not None and self.t >= rc['t0'] + rc['duration_s'] - 1e-9:
                self._finish_recon(rc)
                changed = True
            if not changed:
                return

    # ---------------------------------------------------------------- energy ledger
    def _to_sample(self, s, D, sod, cz, rot, axis, ox, oy):
        c, sn = math.cos(-rot), math.sin(-rot)
        Rm = np.array([[c, -sn, 0.0], [sn, c, 0.0], [0.0, 0.0, 1.0]])
        zb = cz - SOURCE_XYZ_M[2] + (s['pin']['length_m'] if s['pin'] else 0.0)
        S = Rm @ np.array([-sod, -axis, 0.0]) - np.array([ox, oy, zb])
        return S, D @ Rm.T

    def _frame(self, s, D, sod, cz, rot, axis):
        """Sample frame of a pose (origin at the bottom centre of the sample body on its own axis; without a sample at the chuck top on the
        rotation axis): (S, directions, rotation axis x, y and chuck-top z in that frame)."""
        if s is not None:
            S, Ds = self._to_sample(s, D, sod, cz, rot, axis, s['offset_x_m'], s['offset_y_m'])
            return S, Ds, -s['offset_x_m'], -s['offset_y_m'], -(s['pin']['length_m'] if s['pin'] else 0.0)
        c, sn = math.cos(-rot), math.sin(-rot)
        Rm = np.array([[c, -sn, 0.0], [sn, c, 0.0], [0.0, 0.0, 1.0]])
        S = Rm @ np.array([-sod, -axis, 0.0]) - np.array([0.0, 0.0, cz - SOURCE_XYZ_M[2]])
        return S, D @ Rm.T, 0.0, 0.0, 0.0

    def _book_energy(self, h, v0, i0, v1, i1, pose):
        """Energy of a sub-step inside one rate cell: the sinks scale with V^2 I (Kramers efficiency eta = K Z V times the power V I), the
        electrical energy with V I; V and I are linear in time within a sub-step (ramps and the warm-up program), so both integrals are exact."""
        dv, di = v1 - v0, i1 - i0
        vi = h * (v0 * i0 + 0.5 * (v0 * di + i0 * dv) + dv * di / 3.0)
        v2i = h * (v0 * v0 * i0 + 0.5 * (v0 * v0 * di + 2.0 * v0 * i0 * dv) + (2.0 * v0 * dv * di + i0 * dv * dv) / 3.0 + 0.25 * dv * dv * di)
        base, kv = self._base_rates(pose)
        f = v2i / (1.602176634e-19 * kv * kv)
        amt = {k: v * f for k, v in base.items()}
        amt['electrical_energy_j'] = vi
        amt['xray_emitted_j'] = KRAMERS_K_PER_V * 74 * v2i
        amt['energy_residual_j'] = amt['xray_emitted_j'] - sum(amt[k] for k in ENERGY_SINKS)
        for k, v in amt.items():
            self.led[k].add(v)
        if self.on_stage is not None:
            s = self.samples[self.on_stage]
            dose = amt['sample_absorbed_j'] / max(sample_mass_kg(s), 1e-12)          # energy absorbed in the sample body only
            s['dose_gy'] = s.get('dose_gy', 0.0) + dose
            self.led['sample_dose_gy'].add(dose)

    def _beam_filter(self, slide=None):
        """Filter position in the beam: the one nearest the slide (the spectrum changes while the slide passes, not at the command)."""
        return min(FILTER_POSITIONS - 1, max(0, int(round((self.slide if slide is None else slide) / FILTER_PITCH_M))))

    def _rate_key(self, pose):
        """Cell of the pose: voltage (1 kV, at least 2 kV since the spectrum grid starts at 1 keV), filter in the beam, sample and its offsets,
        carriage and lift (0.25 mm), rotation (2 pi / 32)."""
        s = self.samples.get(self.on_stage) if self.on_stage else None
        return (max(2, int(round(pose['xv'] / RATE_KV_CELL_V))), self._beam_filter(pose.get('slide')), self.on_stage, (s['offset_x_m'], s['offset_y_m']) if s else None,
                int(round(pose['sod'] / RATE_POS_CELL_M)), int(round(pose['cz'] / RATE_POS_CELL_M)),
                int(round(pose['rot'] / RATE_ROT_CELL_RAD)) % RATE_ROT_CELLS)

    def _object_box(self, s, sod, cz, rot):
        """Rectangle of the detector plane holding the projection of the sample body with its container at the rotation angle of the rate
        cell (the cell's energy flow is evaluated at that pose): the tangents of the enclosing vertical cylinder about the sample's own axis
        and its height seen from the near and far side, clipped to the emission cone (the beam irradiates the sample beside the active area
        too); None when it misses the cone."""
        c = SDD_M * math.tan(CONE_HALF_ANGLE_RAD)
        rt_ = sample_radius_total(s)
        ox, oy = s['offset_x_m'], s['offset_y_m']
        xc = sod + ox * math.cos(rot) - oy * math.sin(rot)                   # sample axis in the source frame (beam along x)
        yc = self.truth['axis_offset'] + ox * math.sin(rot) + oy * math.cos(rot)
        rho = math.hypot(xc, yc)
        if rho <= rt_ or xc - rt_ <= 0.0:
            return -c, c, -c, c
        th, al = math.atan2(yc, xc), math.asin(rt_ / rho)
        us = [SDD_M * math.tan(th - al), SDD_M * math.tan(th + al)]
        z0 = cz - SOURCE_XYZ_M[2] + (s['pin']['length_m'] if s['pin'] else 0.0)
        vv = [SDD_M * z / (xc + sg * rt_) for z in (z0, z0 + s['height_m']) for sg in (-1, 1)]
        pu = 0.02 * (us[1] - us[0]) + 2 * PIXEL_PITCH_M
        pv = 0.02 * (max(vv) - min(vv)) + 2 * PIXEL_PITCH_M
        u0, u1 = max(us[0] - pu, -c), min(us[1] + pu, c)
        v0, v1 = max(min(vv) - pv, -c), min(max(vv) + pv, c)
        if u0 >= u1 or v0 >= v1:
            return None
        return u0, u1, v0, v1

    def _ray_energy(self, e, ph_f, omega, r, det, up, mount, near, body, far):
        """Energy flow per electron (J) of a ray set through the objects in their order along each ray: the mount (stage housing, chuck and
        pin, stacked below the sample: first on rays going up, last on rays going down), the container wall before the inside of the tube,
        the sample body, the wall after it.  Each object absorbs the mu_en / mu share of the energy it removes from the beam reaching it;
        returns air, holder (mount and container), body, scattered, detector deposited and passed (rays on the active area) and shielding
        (rays beside it).  mount / near / body / far: {material: path lengths}."""
        f, n = 0.5 * KEV, len(e)
        mu_air, _ = _mu_grid('nist_air', n)
        inc = ph_f * np.exp(-np.multiply.outer(r, mu_air))                   # (rays, E) photons at the objects / detector plane
        W = inc * e
        out = dict(air=f * (float(omega.sum()) * float(ph_f @ e) - float(omega @ W.sum(1))), holder=0.0, body=0.0, scattered=0.0)

        def seg(paths):
            if not paths:
                return None
            mats = sorted(paths)
            L = np.stack([paths[m] for m in mats], 1)
            mt = L @ np.stack([_mu_grid(m, n)[0] for m in mats])
            return mt, L @ np.stack([_mu_grid(m, n)[1] for m in mats]), np.exp(-mt)
        segs = [('holder', seg(mount)), ('holder', seg(near)), ('body', seg(body)), ('holder', seg(far))]
        T = [x[2] if x is not None else None for _, x in segs]

        def prod(ks):
            p = None
            for k in ks:
                if T[k] is not None:
                    p = T[k] if p is None else p * T[k]
            return 1.0 if p is None else p
        before = [(prod([]), prod([1, 2, 3])), (prod([0]), prod([])), (prod([0, 1]), prod([1])), (prod([0, 1, 2]), prod([1, 2]))]   # (up, down)
        u = up[:, None]
        for k, (g, x) in enumerate(segs):
            if x is None:
                continue
            mt, me, Tk = x
            bu, bd = before[k]
            R = W * (1.0 - Tk) * (np.where(u, bu, bd) if (np.ndim(bu) or np.ndim(bd)) else 1.0)
            A = np.divide(R * me, mt, out=np.zeros_like(R), where=mt > 0)
            ra, aa = f * float(omega @ R.sum(1)), f * float(omega @ A.sum(1))
            out[g] += aa
            out['scattered'] += ra - aa
        at = inc * prod([0, 1, 2, 3])
        _, edep = detector_response(tuple(e))
        wd, ws = omega * det, omega * ~det
        out['dep'] = f * float(wd @ (at @ edep))
        out['passed'] = f * float(wd @ (at @ (e - edep)))
        out['shield'] = f * float(ws @ (at @ e))
        return out

    def _base_rates(self, pose):
        """Energy flow per electron (J) at the centre of the rate cell of the pose: the target absorbs the back half of the emission and the
        self-absorbed part of the front half (take-off path of the reflection target), the window and the filter their shares of the emission
        cone; the rays of the cone split into air, the sample body (absorbed: its dose), the holder and stage hardware (pin, container, chuck,
        housing), scattered, detector deposited and passed (rays on the active area) and shielding (rays beside it); the rest of the front
        half goes to the shielding.  The coarse cells of the cone keep one ray each (rays meeting no object from a per-spectrum cache); the
        cells overlapping the projection box of the sample body (at the cell's angle) are cut at the box edges, the parts inside it split into
        sub-cells of a fine pitch (about LEDGER_FINE_RAYS rays over the box, at least 16 across either side), the parts beside it keep one
        ray; the pieces tile the cone, so every share is a sum of positive ray terms, and along each ray the objects absorb in their order
        (_ray_energy); returns (rates, cell voltage)."""
        key = self._rate_key(pose)
        kv = key[0] * RATE_KV_CELL_V
        base = self._rate_cache.get(key)
        if base is not None:
            return base, kv
        sod, cz, rot = key[4] * RATE_POS_CELL_M, key[5] * RATE_POS_CELL_M, key[6] * RATE_ROT_CELL_RAD
        fpos = key[1]
        s = self.samples.get(self.on_stage) if self.on_stage else None
        axis = self.truth['axis_offset']
        e, ph_un = unabsorbed_spectrum(kv)
        _, ph_self = emitted_spectrum(kv)
        ph_win = ph_self * np.exp(-_mu_grid('be', len(e))[0] * BE_WINDOW_M)
        _, ph_f = beam_spectrum(kv, fpos)
        E = lambda ph: float((ph * e).sum() * 0.5 * KEV)                      # J per sr per electron
        omega_cone = 2 * math.pi * (1 - math.cos(CONE_HALF_ANGLE_RAD))

        def objects(D, with_body):
            """(mount, near wall, body, far wall) paths of rays D; the sample frame keeps the vertical, so D[:, 2] > 0 is a ray going up."""
            S, Ds, ax, ay, zt = self._frame(s, D, sod, cz, rot, axis)
            mount, near, bod, far = dict(hardware_paths(S, Ds, ax, ay, zt)), {}, {}, {}
            if s is not None:
                for sid, L in pin_paths(s, S, Ds).items():
                    _add(mount, sid, L)
                near, far = container_walls(s, S, Ds)
                if with_body:
                    bod = body_paths(s, S, Ds)
            return mount, near, bod, far
        U, V, PU, PV, D, omega, r, det = _ledger_grid()
        box = self._object_box(s, sod, cz, rot) if s is not None else None
        sel = np.zeros(len(U), bool)
        if box is not None:
            u0, u1, v0, v1 = box
            sel = (U + PU / 2 > u0) & (U - PU / 2 < u1) & (V + PV / 2 > v0) & (V - PV / 2 < v1)
        tot = dict(air=0.0, dep=0.0, passed=0.0, shield=0.0, scattered=0.0, holder=0.0, body=0.0)
        c = np.flatnonzero(~sel)
        mount, near, _, far = objects(D[c], False)
        hit = np.zeros(len(c), bool)
        for L in list(mount.values()) + list(near.values()) + list(far.values()):
            hit |= L > 0
        for k, a in zip(('air', 'dep', 'passed', 'shield'), _free_rays(kv, fpos)):
            tot[k] += float(a[c[~hit]].sum())
        if hit.any():
            ch = c[hit]
            o = self._ray_energy(e, ph_f, omega[ch], r[ch], det[ch], D[ch, 2] > 0, *({k: v[hit] for k, v in g.items()} for g in (mount, near, {}, far)))
            for k, v in o.items():
                tot[k] += v
        w_used = float(omega[c].sum())
        if sel.any():
            u0, u1, v0, v1 = box
            nb = max(16, int(round(math.sqrt(LEDGER_FINE_RAYS * (u1 - u0) / (v1 - v0)))))        # at least 16 rays across either side
            pu_f, pv_f = (u1 - u0) / nb, (v1 - v0) / max(16, int(round(LEDGER_FINE_RAYS / nb)))
            a0, a1, b0, b1 = U[sel] - PU[sel] / 2, U[sel] + PU[sel] / 2, V[sel] - PV[sel] / 2, V[sel] + PV[sel] / 2
            ue = (a0, np.maximum(a0, u0), np.minimum(a1, u1), a1)             # each cell cut at the box edges into 3 x 3 pieces
            ve = (b0, np.maximum(b0, v0), np.minimum(b1, v1), b1)
            pieces = []
            for iu in range(3):
                for iv in range(3):
                    lu, hu_, lv, hv_ = ue[iu], ue[iu + 1], ve[iv], ve[iv + 1]
                    ok = (hu_ - lu > 1e-12) & (hv_ - lv > 1e-12)
                    if iu == 1 and iv == 1:                                     # inside the box: sub-cells of about the fine pitch
                        nu_ = np.maximum(1, np.ceil((hu_ - lu) / pu_f - 1e-9)).astype(int)
                        nv_ = np.maximum(1, np.ceil((hv_ - lv) / pv_f - 1e-9)).astype(int)
                    else:                                                       # beside the box (no body at any angle): one ray
                        nu_ = nv_ = np.ones(len(lu), int)
                    pieces.append([x[ok] for x in (lu, hu_, lv, hv_, nu_, nv_)])
            lu, hu_, lv, hv_, nu_, nv_ = (np.concatenate([p[k] for p in pieces]) for k in range(6))
            cnt = nu_ * nv_
            idx = np.repeat(np.arange(len(cnt)), cnt)
            j = np.arange(int(cnt.sum())) - np.repeat(np.cumsum(cnt) - cnt, cnt)
            wu, wv = (hu_ - lu) / nu_, (hv_ - lv) / nv_
            Df, omf, rf, detf = _cell_rays(lu[idx] + (j % nu_[idx] + 0.5) * wu[idx], lv[idx] + (j // nu_[idx] + 0.5) * wv[idx], wu[idx], wv[idx])
            o = self._ray_energy(e, ph_f, omf, rf, detf, Df[:, 2] > 0, *objects(Df, True))
            for kk, v in o.items():
                tot[kk] += v
            w_used += float(omf.sum())
        out = dict(xray_emitted_j=4 * math.pi * E(ph_un), target_absorbed_j=2 * math.pi * E(ph_un) + 2 * math.pi * (E(ph_un) - E(ph_self)),
                   window_absorbed_j=omega_cone * (E(ph_self) - E(ph_win)), filter_absorbed_j=omega_cone * (E(ph_win) - E(ph_f)),
                   shielding_j=(2 * math.pi - omega_cone) * E(ph_self) + (omega_cone - w_used) * E(ph_f) + tot['shield'],
                   air_absorbed_j=tot['air'], sample_absorbed_j=tot['body'], holder_absorbed_j=tot['holder'],
                   sample_scattered_j=tot['scattered'], detector_absorbed_j=tot['dep'], detector_passed_j=tot['passed'])
        out = {k: float(v) for k, v in out.items()}
        if len(self._rate_cache) > 512:
            self._rate_cache.clear()
        self._rate_cache[key] = out
        return out, kv

    def _energy_rates(self):
        """Instantaneous energy flow (J/s) at the actual voltage, current and pose (for inspection; the ledger integrates _book_energy)."""
        pose = dict(xv=self.xv, rot=self.rot, sod=self.sod, cz=self.cz)
        base, kv = self._base_rates(pose)
        f = self.xv * self.xv * self.xi / (1.602176634e-19 * kv * kv)
        rates = {k: v * f for k, v in base.items()}
        rates['electrical_energy_j'] = self.xv * self.xi
        rates['xray_emitted_j'] = KRAMERS_K_PER_V * 74 * self.xv * self.xv * self.xi
        rates['energy_residual_j'] = rates['xray_emitted_j'] - sum(rates[k] for k in ENERGY_SINKS)
        return rates

    # ---------------------------------------------------------------- detector simulation
    def _groups(self, kv, fpos, materials):
        """Energy groups of the detected spectrum at the detector (central-ray air path): photons per sr per electron per group, mean deposited
        energy per incident photon (keV), second moment of the deposited energy (keV2 per incident photon) and group-mean linear attenuation
        coefficients of the materials (weighted by the detected energy)."""
        e, ph = beam_spectrum(kv, fpos)
        de = 0.5
        ph = ph * air_transmission(e, SDD_M) * de
        p, edep = detector_response(tuple(e))
        mu_c, men_c = linear_mu('nist_cesium', e)
        eps_int = e * men_c / mu_c
        w = ph * edep
        edges = [33.169, 35.985]
        for sid in materials:
            edges += [ed['energy_MeV'] * 1e3 for ed in substance(sid)['edges'] if ed['label'].endswith(' K') and 5.0 < ed['energy_MeV'] * 1e3 < kv / 1e3]
        idx, G = energy_groups(e, w, edges)
        n = np.array([ph[idx == g].sum() for g in range(G)])
        keep = n > 0
        eps = np.array([(ph * edep)[idx == g].sum() for g in range(G)])
        m2 = np.array([(ph * p * eps_int ** 2)[idx == g].sum() for g in range(G)])
        mus = {}
        for sid in materials:
            mu, _ = linear_mu(sid, e)
            mus[sid] = np.array([(w * mu)[idx == g].sum() / max(w[idx == g].sum(), 1e-300) for g in range(G)])[keep]
        return dict(n=n[keep], eps=eps[keep] / n[keep], m2=m2[keep] / n[keep], mu=mus, mean_e=float((w * e).sum() / w.sum()))

    def _window(self, ctx, s, roi=None):
        """Detector region simulated for an acquisition: the ROI of a radiograph (binned native pixels), the whole detector for a radiograph
        without sample, else the projected envelope of the sample over all angles (nominal geometry, calibrated axis offset, margins for the
        unknown offset), sampled with at most SIM_DETECTOR pixels but not finer than one binned native pixel."""
        b = int(ctx['scan']['binning'][0])
        nat = PIXEL_PITCH_M * b
        hu, hv = DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2, DETECTOR_PIXELS[1] * PIXEL_PITCH_M / 2
        if roi is not None:
            n = roi['size_px']
            return dict(u0=roi['center_u_m'] - n / 2 * nat, v1=roi['center_v_m'] + n / 2 * nat, pitch=nat, nu=n, nv=n, binning=b)
        if s is None or ctx.get('full_window'):
            u0, u1, v0, v1 = -hu, hu, -hv, hv
        else:
            r = self._envelope(s, *ctx['offset'])
            sod = ctx['sod']
            a = ctx.get('axis_known') or 0.0
            uh = SDD_M * r / math.sqrt(max(sod * sod - r * r, 1e-12))
            uc = SDD_M * a / sod
            mu_ = 0.06 * uh + 4 * AXIS_OFFSET_SD_M * SDD_M / sod + 2 * nat
            u0, u1 = uc - uh - mu_, uc + uh + mu_
            zb = ctx['cz'] - SOURCE_XYZ_M[2] + (s['pin']['length_m'] if s['pin'] else 0.0)         # sample body only (not the mounting pin)
            zt = zb + s['height_m']
            vv = [SDD_M * z / (sod + sg * r) for z in (zb, zt) for sg in (-1, 1)]
            mv = 0.04 * (max(vv) - min(vv)) + 2 * nat
            v0, v1 = min(vv) - mv, max(vv) + mv
            u0, u1, v0, v1 = max(u0, -hu), min(u1, hu), max(v0, -hv), min(v1, hv)
        p = max(math.sqrt((u1 - u0) * (v1 - v0) / (SIM_DETECTOR[0] * SIM_DETECTOR[1])), (u1 - u0) / 400, (v1 - v0) / 400, nat)   # pixel budget
        return dict(u0=u0, v1=v1, pitch=p, nu=max(2, int(math.ceil((u1 - u0) / p - 1e-9))), nv=max(2, int(math.ceil((v1 - v0) / p - 1e-9))), binning=b)

    def _window_rays(self, w):
        u = w['u0'] + (np.arange(w['nu']) + 0.5) * w['pitch']
        v = w['v1'] - (np.arange(w['nv']) + 0.5) * w['pitch']
        U, V = np.meshgrid(u, v)
        P = np.stack([np.full(U.size, SDD_M), U.ravel(), V.ravel()], -1)
        r = np.linalg.norm(P, axis=1)
        return P / r[:, None], w['pitch'] ** 2 * (SDD_M / r) ** 3 / SDD_M ** 2, u, v

    def _flat_residual(self, flat, w):
        """Residual fixed pattern after the dark / bright correction on the window grid: the bright-field noise of each window pixel (the
        per-pixel gain itself is divided out), a field fixed for that calibration; dead columns of the injected defect fault."""
        n_px = (w['pitch'] / (PIXEL_PITCH_M * w['binning'])) ** 2
        sig = flat['sigma_binned'] / math.sqrt(n_px)
        key = int(hashlib.sha256(f"{flat['seed']}|{w['u0']:.9f}|{w['v1']:.9f}|{w['pitch']:.12f}|{w['nu']}|{w['nv']}".encode()).hexdigest()[:12], 16)
        xi = np.random.default_rng(key).standard_normal((w['nv'], w['nu']))
        return 1.0 / (1.0 + sig * xi)

    def _dead_columns(self, ctx, w):
        """Window columns that fall into the injected dead detector columns (native column bands of 16 pixels)."""
        if not ctx['defects']:
            return np.zeros(w['nu'], bool)
        u = w['u0'] + (np.arange(w['nu']) + 0.5) * w['pitch']
        band = DETECTOR_PIXELS[0] / SIM_DETECTOR[0] * PIXEL_PITCH_M
        col = np.floor((u + DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2) / band).astype(int)
        return np.isin(col, ctx['defects'])

    def _simulate(self, ctx, s, angles, shifts, frames, t_exp, w, rng=None, with_sample=True):
        """Normalised transmission images of the given rotation angles on the window w: exact paths, energy groups, focal-spot penumbra and
        scintillator blur, compound-Poisson quantum noise, read noise, 16-bit saturation, residual fixed pattern after the flat-field correction
        (random detector movement shifts it by 'shifts' window columns), dead columns."""
        from scipy.ndimage import gaussian_filter
        mats = list(dict.fromkeys((_materials(s) if (s is not None and with_sample) else []) + list(HARDWARE_MATERIALS)))
        grp = self._groups(ctx['kv'], ctx['filter'], mats)
        electrons = ctx['ia'] / 1.602176634e-19
        D, omega, _, _ = self._window_rays(w)
        n_bin = (w['pitch'] / (PIXEL_PITCH_M * w['binning'])) ** 2        # binned native pixels per simulated pixel
        spot = focal_spot_m(ctx['kv'] * ctx['ia'])
        M = SDD_M / ctx['sod']
        sigma_px = math.hypot(spot / 2.355 * (M - 1) / w['pitch'], DETECTOR_PSF_PX * PIXEL_PITCH_M / w['pitch'])
        flat = ctx['flat']
        resid0 = self._flat_residual(flat, w)
        dead = self._dead_columns(ctx, w)
        out, sat = [], 0.0
        for k, beta in enumerate(angles):
            if s is not None and with_sample:
                S, Ds = self._to_sample(s, D, ctx['sod'], ctx['cz'], beta, ctx['axis'], ctx['offset'][0], ctx['offset'][1])
                paths = sample_paths(s, S, Ds)
                ax, ay, zt = -ctx['offset'][0], -ctx['offset'][1], -(s['pin']['length_m'] if s['pin'] else 0.0)
            else:
                S, Ds, ax, ay, zt = self._frame(None, D, ctx['sod'], ctx['cz'], beta, ctx['axis'])
                paths = {}
            for sid, L in hardware_paths(S, Ds, ax, ay, zt).items():          # brass chuck and rotation-stage housing below the sample
                _add(paths, sid, L)
            att = np.zeros((D.shape[0], len(grp['n'])))
            for sid, L in paths.items():
                att += np.outer(L, grp['mu'][sid])
            T = np.exp(-att)
            ph = electrons * t_exp * frames * omega[:, None] * grp['n'][None, :]      # incident photons per group and pixel
            mean_kev = (ph * T * grp['eps'][None, :]).sum(1)
            var_kev2 = (ph * T * grp['m2'][None, :]).sum(1)
            mean0 = (ph * grp['eps'][None, :]).sum(1)
            img = mean_kev.reshape(w['nv'], w['nu'])
            if sigma_px > 0.05:
                img = gaussian_filter(img, sigma_px, mode='nearest')
            sig = ADU_PER_KEV * img.ravel()
            var = ADU_PER_KEV ** 2 * var_kev2 + READ_NOISE_ADU ** 2 * n_bin * frames
            full = (FULL_SCALE_ADU - DARK_OFFSET_ADU - DARK_CURRENT_ADU_S * t_exp) * n_bin * frames
            meas = sig + np.sqrt(var) * rng.standard_normal(sig.shape)
            sat += float(np.mean(sig > full))
            meas = np.minimum(meas, full)
            bright = np.minimum(ADU_PER_KEV * mean0, full)                 # the bright field clips at full scale too
            t = (meas / np.maximum(bright, 1e-30)).reshape(w['nv'], w['nu'])
            sh = shifts[k] if shifts is not None else 0
            t = t * np.roll(resid0, sh, axis=1)
            if dead.any():
                dk = np.roll(dead, sh)
                if flat['defects']:                                      # dead columns in the bright field too: gain divided out, noise stays
                    t[:, dk] = t[:, dk] + 0.15 * rng.standard_normal((w['nv'], int(dk.sum())))
                else:
                    t[:, dk] = t[:, dk] * 0.02
            out.append(t)
        return out, sat / max(len(angles), 1), grp

    # ---------------------------------------------------------------- operation finishers
    def _finish_flat_field(self):
        o = self.op
        ctx = o['ctx']
        r = self.records[o['record_id']]
        b = int(ctx['scan']['binning'][0])
        grp = self._groups(ctx['kv'], ctx['filter'], [])
        electrons = ctx['ia'] / 1.602176634e-19
        omega = (PIXEL_PITCH_M * b) ** 2 / SDD_M ** 2                    # one binned pixel on the central ray
        texp = ctx['scan']['exposure_s']
        m_b = ADU_PER_KEV * electrons * texp * omega * float((grp['n'] * grp['eps']).sum())          # ADU per binned pixel per frame
        v_b = ADU_PER_KEV ** 2 * electrons * texp * omega * float((grp['n'] * grp['m2']).sum())
        nf = FLAT_FIELD_FRAMES
        sigma_binned = math.sqrt(v_b * nf + READ_NOISE_ADU ** 2 * nf * 2) / (m_b * nf)            # relative bright-field error of one binned pixel
        level = m_b + DARK_OFFSET_ADU + DARK_CURRENT_ADU_S * texp
        usable = sigma_binned <= FLAT_MAX_SIGMA
        if level > FULL_SCALE_ADU:
            r['warnings'].append('亮场饱和:降低电流或曝光(亮区的透过率在满量程截止)')
        if not usable:
            r['warnings'].append(f'亮场信号不足:单个合并像素相对误差{sigma_binned * 100:.1f} %(上限{FLAT_MAX_SIGMA * 100:.0f} %),校正无效;提高电流、曝光或帧平均,或减少滤片')
        self.led['frames'].add(2 * nf)
        self.flat = dict(key=self._flat_key_of(ctx), t=self.t, seed=ctx['seed'], sigma_binned=sigma_binned, defects=bool(ctx['defects']), frames=nf,
                         level_adu=level, usable=usable)
        r['flat_field'] = dict(frames=nf, mean_bright_adu=float(min(level, FULL_SCALE_ADU)), dark_adu=DARK_OFFSET_ADU + DARK_CURRENT_ADU_S * texp, key=self.flat['key'])
        _, omega_px, _, _ = self._window_rays(dict(u0=-DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2, v1=DETECTOR_PIXELS[1] * PIXEL_PITCH_M / 2,
                                                   pitch=DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 88, nu=88, nv=75, binning=b))
        prof = (omega_px / omega_px.max()).reshape(75, 88) * min(level, FULL_SCALE_ADU) / FULL_SCALE_ADU
        r['preview'] = [[float(x) for x in row] for row in prof]
        r['preview_unit'] = 'fraction of full scale (bright field, cos^3 fall-off)'
        r['note_zh'] = f'暗场与亮场各{nf}帧;亮场中心{level:.0f} ADU(满量程65535);单个合并像素亮场相对误差{sigma_binned * 100:.2f} %'
        self._complete(o, r)

    def _flat_key_of(self, ctx):
        s = ctx['scan']
        return f"{ctx['kv']:.1f}|{ctx['ia'] * 1e9:.1f}|{ctx['filter']}|{s['binning']}|{s['exposure_s']:.6g}"

    def _finish_radiograph(self):
        o = self.op
        ctx = o['ctx']
        r = self.records[o['record_id']]
        s = self.samples.get(ctx['sample_id']) if ctx['sample_id'] else None
        rng = np.random.default_rng(ctx['seed'])
        sc = ctx['scan']
        w = self._window(dict(ctx, full_window=True), s, ctx['roi'])
        self.led['projections'].add(1); self.led['frames'].add(sc['averaging'])
        imgs, sat, grp = self._simulate(ctx, s, [ctx['rot0']], None, sc['averaging'], sc['exposure_s'], w, rng=rng)
        t = imgs[0]
        self._data[r['record_id']] = dict(image=t.astype(np.float32), roi=copy.deepcopy(ctx['roi']), sod=ctx['sod'], binning=int(sc['binning'][0]), window=w)
        r['sim_detector'], r['sim_pixel_m'] = [w['nu'], w['nv']], w['pitch']
        r.update(transmission=dict(min=float(t.min()), mean=float(t.mean()), max=float(t.max())), saturated_fraction=sat)
        step = max(1, int(math.ceil(max(t.shape) / 88)))
        r['preview'] = [[float(x) for x in row] for row in t[::step, ::step][:128, :128]]
        r['preview_unit'] = 'transmission I/I0'
        r['note_zh'] = f'透视(平均探测能量{grp["mean_e"]:.1f} keV)' + (',局部区域' if ctx['roi'] else '')
        self._evict()
        self._complete(o, r)

    def _finish_geometry_calibration(self):
        o = self.op
        ctx = o['ctx']
        r = self.records[o['record_id']]
        s = self.samples[ctx['sample_id']]
        rng = np.random.default_rng(ctx['seed'])
        angles = [ctx['rot0'] + 2 * math.pi * k / 36 for k in range(36)]
        sc = ctx['scan']
        w = self._window(ctx, s)
        self.led['projections'].add(36); self.led['frames'].add(36 * sc['averaging'])
        imgs, sat, grp = self._simulate(ctx, s, angles, None, sc['averaging'], sc['exposure_s'], w, rng=rng)
        r['sim_detector'], r['sim_pixel_m'] = [w['nu'], w['nv']], w['pitch']
        est = self._fit_axis(imgs, angles, ctx['sod'], w)
        if est is None:
            r['warnings'].append('标定球识别失败')
            r['note_zh'] = '钢球识别失败:检查标定体模是否完整在视场内'
            self._complete(o, r)
            return
        a, resid, n_balls = est
        r['geometry'] = dict(axis_offset_m=a, residual_m=resid, balls=n_balls)
        limit = max(GEO_FIT_MAX_RESIDUAL_PX * w['pitch'] * ctx['sod'] / SDD_M, GEO_FIT_MIN_LIMIT_M)
        if resid > limit or n_balls < CAL_BALLS - 1:
            r['warnings'].append(f'钢球轨迹拟合残差{resid * 1e6:.1f} µm超过{limit * 1e6:.1f} µm或识别的钢球不足({n_balls}个):标定未采用')
            r['note_zh'] = f'拟合失败:旋转轴横向偏移{a * 1e6:.1f} µm不可信(残差{resid * 1e6:.1f} µm),保留原标定'
            self._complete(o, r)
            return
        self.geo_cal = dict(axis_offset_m=a, calibrated_s=self.t)
        r['note_zh'] = f'旋转轴横向偏移{a * 1e6:.1f} µm({n_balls}个钢球,拟合残差{resid * 1e6:.2f} µm)'
        self._complete(o, r)

    def _fit_axis(self, imgs, angles, sod, w):
        """Track the steel balls in the 36 projections (blobs of high line integral) and fit the exact cone-beam trajectories
        u = SDD (a + r sin(b + phi)) / (SOD + r cos(b + phi)) jointly for the axis offset a."""
        from scipy import ndimage, optimize
        pu = pv = w['pitch']
        ball_px = 2 * CAL_BALL_RADIUS_M * SDD_M / sod / pu                 # projected ball diameter in window pixels
        se = max(9, 2 * int(round(2 * ball_px)) + 1)                     # top-hat element: about four ball diameters
        blobs = []
        for k, t in enumerate(imgs):
            q = -np.log(np.clip(t, 1e-6, None))
            d = ndimage.white_tophat(q, size=se)                          # balls stand out of the PMMA cylinder background
            lab, n = ndimage.label(d > 0.5 * max(d.max(), 1e-9))
            found = []
            for j in range(1, n + 1):
                wt = np.where(lab == j, d, 0.0)
                if wt.sum() <= 0 or (lab == j).sum() < 2:
                    continue
                cy, cx = ndimage.center_of_mass(wt)
                found.append((w['u0'] + (cx + 0.5) * pu, w['v1'] - (cy + 0.5) * pv))
            blobs.append(found)
        seed = max(range(len(blobs)), key=lambda k: len(blobs[k]))
        if len(blobs[seed]) < 3:
            return None
        means = sorted(v for _, v in blobs[seed])
        tol = 0.45 * min(np.diff(means)) if len(means) > 1 else 10 * pv
        tracks = {i: [] for i in range(len(means))}
        for k, found in enumerate(blobs):                                  # nearest track in v (cone-beam magnification makes v wobble)
            for u, v in found:
                i = int(np.argmin([abs(v - m) for m in means]))
                if abs(v - means[i]) < tol:
                    tracks[i].append((angles[k], u))
        balls = [np.array(v) for v in tracks.values() if len(v) >= 24]
        if len(balls) < 3:
            return None
        M = SDD_M / sod

        def res(p):
            a = p[0]
            out = []
            for i, bl in enumerate(balls):
                rr, ph = p[1 + 2 * i], p[2 + 2 * i]
                out.append(SDD_M * (a + rr * np.sin(bl[:, 0] + ph)) / (sod + rr * np.cos(bl[:, 0] + ph)) - bl[:, 1])
            return np.concatenate(out)
        p0 = [float(np.mean([bl[:, 1].mean() for bl in balls]) / M)]
        for bl in balls:
            amp = (bl[:, 1].max() - bl[:, 1].min()) / 2 / M
            k = int(np.argmax(bl[:, 1]))
            p0 += [max(amp, 1e-4), math.pi / 2 - bl[k, 0]]
        sol = optimize.least_squares(res, p0, method='lm')
        rms = float(np.sqrt(np.mean(sol.fun ** 2)))
        return float(sol.x[0]), rms / M, len(balls)

    def _finish_scan(self):
        o = self.op
        ctx = o['ctx']
        r = self.records[o['record_id']]
        s = self.samples[ctx['sample_id']]
        rng = np.random.default_rng(ctx['seed'])
        sc = ctx['scan']
        n_sim, n_nom = ctx['n_sim'], ctx['n_nom']
        full = sc['range_deg'] == '360'
        angles = [ctx['rot0'] + ctx['span'] * k / (n_sim if full else max(n_sim - 1, 1)) for k in range(n_sim)]
        shifts = [int(x) for x in rng.integers(-4, 5, n_sim)] if sc['random_movement'] else None
        frames = sc['averaging'] * n_nom / n_sim                        # dose-equivalent aggregation of the skipped projections
        self.led['projections'].add(n_nom); self.led['frames'].add(n_nom * sc['averaging'])
        w = self._window(ctx, s)
        imgs, sat, grp = self._simulate(ctx, s, angles, shifts, frames, sc['exposure_s'], w, rng=rng)
        q = np.stack([-np.log(np.clip(t, 1e-6, None)) for t in imgs]).astype(np.float32)
        self._data[r['record_id']] = dict(q=q, angles=np.array(angles) - ctx['rot0'], full=full, sod=ctx['sod'], span=ctx['span'], grp=grp,
                                          materials=_materials(s), kv=ctx['kv'], filter=ctx['filter'], window=w)
        r['sim_detector'], r['sim_pixel_m'] = [w['nu'], w['nv']], w['pitch']
        tt = np.stack(imgs)
        r.update(transmission=dict(min=float(tt.min()), mean=float(tt.mean()), max=float(tt.max())), saturated_fraction=sat,
                 dose_gy=float(s.get('dose_gy', 0.0) - ctx.get('dose0', 0.0)))
        st = max(1, int(math.ceil(max(imgs[0].shape) / 96)))
        r['preview'] = [[float(x) for x in row] for row in imgs[0][::st, ::st][:128, :128]]
        r['preview_unit'] = 'transmission I/I0 (first projection, acquisition window)'
        if tt.min() < 0.02:
            r['warnings'].append('部分射线透过率低于2 %:提高电压或加滤片')
        if sat > 0.001:
            r['warnings'].append('探测器饱和:降低电流或曝光')
        r['note_zh'] = f'{n_nom}个名义投影(模拟{n_sim}个,剂量等效合并);平均探测能量{grp["mean_e"]:.1f} keV'
        self._evict()
        self._complete(o, r)

    def _evict(self):
        keep = sorted((k for k in self._data if self.records[k]['kind'] in ('projections', 'radiograph')), key=lambda k: self.records[k]['started_s'])
        for k in keep[:-8]:
            del self._data[k]

    # ---------------------------------------------------------------- reconstruction
    def _finish_recon(self, rc):
        ctx = rc['ctx']
        r = self.records[rc['record_id']]
        data = self._data.get(ctx['source'])
        if data is None:
            self.operations[rc['op']].update(phase='failed', finished_s=self.t, reason='DATA_EVICTED')
            r.update(status='failed', finished_s=self.t, reason='DATA_EVICTED', duration_s=self.t - r['started_s'])
            self._event('measurement', 'OP_FAILED', f'重建{rc["op"]}失败:投影数据已被清除', rid=r['record_id'])
            self.recon = None
            return
        rs = ctx['recon']
        q = data['q'].astype(np.float64)
        sod = data['sod']
        M = SDD_M / sod
        w = data['window']
        warnings = []
        if rs['beam_hardening'] > 0 and rs['bh_material']:
            q = self._bh_linearise(q, data['grp'], data['kv'], data['filter'], rs['bh_material'], rs['beam_hardening'])
        if rs['ring_correction'] > 0:
            from scipy.ndimage import median_filter
            m = q.mean(axis=0)
            q = q - rs['ring_correction'] * (m - median_filter(m, size=(1, 9)))[None, :, :]
        axis = ctx['axis_cal'] if ctx['axis_cal'] is not None else 0.0
        if rs['post_alignment'] is not None:
            axis += rs['post_alignment']
        elif rs['auto_alignment']:
            d_axis = self._auto_align(q, data['angles'], data['full'], M, w, axis, sod, data['span'])
            if d_axis is None:
                warnings.append('中心平面(光束高度)没有可用的样品数据:自动对准未执行,使用几何标定值或0')
            else:
                axis += d_axis
        if ctx['axis_cal'] is None and rs['post_alignment'] is None and not rs['auto_alignment']:
            warnings.append('未做几何标定也未后对准:旋转轴偏移会造成伪影')
        if ctx.get('voi_partial'):
            warnings.append('感兴趣体积部分超出采集窗口可重建的区域:该部分体素不完整或为0')
        vol, origin, voxel = self._fdk(q, data['angles'], data['full'], data['span'], sod, w, axis, int(rs['grid']), rs['filter'], rs['voi'])
        if rs['smoothing_vox'] > 0:
            from scipy.ndimage import gaussian_filter
            vol = gaussian_filter(vol, rs['smoothing_vox'])
        vol = vol.astype(np.float32)
        self._data[r['record_id']] = dict(volume=vol, origin=origin, voxel=voxel, sod=sod, sample_id=ctx['sample_id'])
        hist, edges = np.histogram(vol, bins=64)
        r['volume'] = dict(shape=list(vol.shape), voxel_m=voxel, origin_m=[float(x) for x in origin], mu_min_1_m=float(vol.min()), mu_max_1_m=float(vol.max()),
                           histogram=[int(x) for x in hist], histogram_edges_1_m=[float(x) for x in edges], axis_offset_used_m=float(axis))
        zc = vol.shape[0] // 2
        stepv = max(1, int(math.ceil(max(vol.shape[1:]) / 96)))
        r['preview'] = [[float(x) for x in row] for row in vol[zc, ::stepv, ::stepv][:128, :128]]
        r['preview_unit'] = 'linear attenuation coefficient 1/m (central axial slice)'
        r['warnings'] = warnings
        r['note_zh'] = f'FDK重建{vol.shape[2]}×{vol.shape[1]}×{vol.shape[0]}体素(模拟体素{voxel * 1e6:.1f} µm;名义体素{r["voxel_size_nominal_m"] * 1e6:.2f} µm)'
        o = rc
        self._complete(o, r)

    def _bh_linearise(self, q, grp, kv, fpos, material, strength):
        """Beam-hardening linearisation for one material: the polychromatic line integral q_poly(L) of the scan spectrum (detected-energy
        weights of the energy groups) is mapped to mu(E_eff) L with E_eff the detected mean energy of the unattenuated beam (a monochromatic
        equivalent); strength 0..1 blends the corrected and the measured line integrals."""
        g = grp if material in grp['mu'] else self._groups(kv, fpos, [material])
        mu = g['mu'][material]
        w = g['n'] * g['eps'] / (g['n'] * g['eps']).sum()
        mu_ref = float(linear_mu(material, [g['mean_e']])[0][0])
        L = np.linspace(0.0, 30.0 / max(float((w * mu).sum()), 1e-9), 400)
        qp = -np.log((w[None, :] * np.exp(-np.outer(L, mu))).sum(1))
        lin = np.interp(q, qp, L, right=L[-1]) * mu_ref
        return q + strength * (lin - q)

    def _auto_align(self, q, angles, full, M, w, axis0, sod=None, span=None):
        """Residual axis offset from the consistency of conjugate rays in the central plane (the detector row at the beam height is an exact
        fan-beam sinogram): a ray of view angle b1 and lab angle t1 = atan(u1 / SDD) lies at distance s = SOD sin t - a cos t from the axis
        and is measured again, reversed, at b2 = b1 - pi + 2 (alpha - t1), t2 = 2 alpha - t1 with alpha = atan(a / SOD).  The candidate a
        (+-200 um around the starting offset, 5 um grid, parabolic vertex) that minimises the robust mean squared difference of all conjugate
        pairs inside the scanned range wins; short scans use their redundant end region, full scans every ray."""
        sod = SDD_M / M if sod is None else sod
        span = (2 * math.pi if full else math.pi) if span is None else span
        nv = q.shape[1]
        vs = w['v1'] - (np.arange(nv) + 0.5) * w['pitch']
        j = int(np.argmin(np.abs(vs)))
        if abs(vs[j]) > w['pitch']:
            return None                                                  # the acquisition window does not reach the central plane
        row = q[:, j, :].astype(np.float64)
        if float(np.mean(row > 0.05 * max(float(row.max()), 1e-9))) < 0.05:
            return None                                                  # no sample in the central plane
        us = w['u0'] + (np.arange(w['nu']) + 0.5) * w['pitch']
        b = np.asarray(angles, float)
        n_p, nu = row.shape
        db = span / n_p if full else span / max(n_p - 1, 1)
        B1, T1 = np.meshgrid(b, np.arctan(us / SDD_M), indexing='ij')
        body = row > 0.02 * max(float(row.max()), 1e-9)

        def pairs(a):
            al = math.atan2(a, sod)
            B2 = B1 - math.pi + 2 * (al - T1)
            B2 = np.mod(B2, 2 * math.pi)
            fk = B2 / db
            fi = (SDD_M * np.tan(2 * al - T1) - us[0]) / w['pitch']
            k0, i0 = np.floor(fk).astype(int), np.floor(fi).astype(int)
            ok = (i0 >= 0) & (i0 < nu - 1) & ((k0 >= 0) & (k0 < n_p) if full else (k0 >= 0) & (k0 < n_p - 1))
            k0 = np.clip(k0, 0, n_p - 1)
            k1 = np.mod(k0 + 1, n_p) if full else np.clip(k0 + 1, 0, n_p - 1)
            i0 = np.clip(i0, 0, nu - 2)
            ak, ai = fk - np.floor(fk), np.clip(fi - i0, 0.0, 1.0)
            p2 = (1 - ak) * ((1 - ai) * row[k0, i0] + ai * row[k0, i0 + 1]) + ak * ((1 - ai) * row[k1, i0] + ai * row[k1, i0 + 1])
            sel = ok & (body | (p2 > 0.02 * max(float(row.max()), 1e-9)))
            return row[sel] - p2[sel]
        d0 = pairs(axis0)
        if d0.size < 20:
            return None                                                  # no conjugate rays in the scanned range (keep the starting offset)
        c2 = (10.0 * max(float(np.median(np.abs(d0))), 1e-6)) ** 2          # robust: clip outliers (dead columns, noise spikes)

        def err(a):
            d = pairs(a)
            return float(np.mean(np.minimum(d * d, c2))) if d.size else math.inf
        cand = axis0 + np.linspace(-200e-6, 200e-6, 81)
        e = [err(a) for a in cand]
        k = int(np.argmin(e))
        best = cand[k]
        if 0 < k < len(cand) - 1:
            dd = e[k - 1] - 2 * e[k] + e[k + 1]
            if dd > 0:
                best = cand[k] + 0.5 * (e[k - 1] - e[k + 1]) / dd * (cand[1] - cand[0])
        return float(best - axis0)

    def _fdk(self, q, angles, full, span, sod, w, axis, n, filt, voi):
        """Feldkamp-Davis-Kress reconstruction of the projections q (line integrals) on an n-grid (whole field of view or a volume of
        interest); returns (volume [z, y, x] in 1/m, origin of voxel (0, 0, 0) in the rotating frame (axis at the beam height), voxel size)."""
        n_p, nv, nu = q.shape
        pu = pv = w['pitch']
        us = (w['u0'] + (np.arange(nu) + 0.5) * pu - axis * SDD_M / sod) * sod / SDD_M          # virtual detector at the axis, axis-centred
        vs = (w['v1'] - (np.arange(nv) + 0.5) * pv) * sod / SDD_M
        dus = pu * sod / SDD_M
        U, V = np.meshgrid(us, vs)
        pre = sod / np.sqrt(sod ** 2 + U ** 2 + V ** 2)
        npad = int(2 ** math.ceil(math.log2(2 * nu)))
        k = np.arange(npad)
        k = np.where(k <= npad // 2, k, k - npad)
        h = np.zeros(npad)
        h[0] = 1.0 / (4 * dus * dus)
        odd = (k % 2) != 0
        h[odd] = -1.0 / (np.pi ** 2 * k[odd] ** 2 * dus * dus)
        H = np.real(np.fft.fft(h)) * dus
        f = np.abs(np.fft.fftfreq(npad, d=dus))
        fmax = 0.5 / dus
        if filt == 'shepp_logan':
            H = H * np.sinc(f / (2 * fmax))
        elif filt == 'hann':
            H = H * 0.5 * (1 + np.cos(np.pi * f / fmax))
        gamma_m = math.atan(DETECTOR_PIXELS[0] * PIXEL_PITCH_M / 2 / SDD_M)          # fan half-angle of the whole detector (Parker range)
        fov_r = min(abs(us.min()), abs(us.max())) * sod / math.hypot(sod, abs(us).max())
        if voi is None:
            size = [2 * fov_r, 2 * fov_r, (vs.max() - vs.min()) * (sod - fov_r) / sod]
            center = [0.0, 0.0, 0.5 * (vs.max() + vs.min()) * (sod - fov_r) / sod]
        else:
            size, center = list(voi['size_m']), [voi['center_m'][0], voi['center_m'][1], voi['center_m'][2]]
        voxel = max(size) / n
        dims = [max(2, int(round(sz / voxel))) for sz in size]              # x, y, z counts
        xs = center[0] + (np.arange(dims[0]) + 0.5 - dims[0] / 2) * voxel
        ys = center[1] + (np.arange(dims[1]) + 0.5 - dims[1] / 2) * voxel
        zs = center[2] + (np.arange(dims[2]) + 0.5 - dims[2] / 2) * voxel
        Z, Y, X = np.meshgrid(zs, ys, xs, indexing='ij')
        vol = np.zeros(X.shape)
        d_beta = span / (n_p if full else max(n_p - 1, 1))
        if not full:
            gp = -np.arctan(us / sod)                                    # Parker fan angle: the stage turns so that (b, g) and (b + pi - 2 g, -g) coincide

            def parker(bb):
                wp = np.ones_like(gp)
                wp = np.where(bb < 2 * (gamma_m - gp), np.sin(np.pi / 4 * bb / np.maximum(gamma_m - gp, 1e-12)) ** 2, wp)
                wp = np.where(bb > np.pi - 2 * gp, np.sin(np.pi / 4 * np.clip(np.pi + 2 * gamma_m - bb, 0.0, None) / np.maximum(gamma_m + gp, 1e-12)) ** 2, wp)
                return 2.0 * wp
        for kk in range(n_p):
            beta = angles[kk]
            proj = q[kk] * pre
            if not full:
                proj = proj * parker(beta)[None, :]
            pad = np.zeros((nv, npad))
            pad[:, :nu] = proj
            filt_p = np.real(np.fft.ifft(np.fft.fft(pad, axis=1) * H[None, :], axis=1))[:, :nu]
            cb, sb = math.cos(beta), math.sin(beta)
            xr = X * cb - Y * sb
            yr = X * sb + Y * cb
            Uu = sod + xr
            up = sod * yr / Uu
            vp = sod * Z / Uu
            fi = (up - us[0]) / dus
            fj = (vs[0] - vp) / (dus * pv / pu)
            i0 = np.floor(fi).astype(int)
            j0 = np.floor(fj).astype(int)
            ok = (i0 >= 0) & (i0 < nu - 1) & (j0 >= 0) & (j0 < nv - 1)
            i0c, j0c = np.clip(i0, 0, nu - 2), np.clip(j0, 0, nv - 2)
            ai, aj = fi - i0c, fj - j0c
            val = ((1 - aj) * ((1 - ai) * filt_p[j0c, i0c] + ai * filt_p[j0c, i0c + 1]) + aj * ((1 - ai) * filt_p[j0c + 1, i0c] + ai * filt_p[j0c + 1, i0c + 1]))
            w = (sod / Uu) ** 2
            vol += np.where(ok, w * val, 0.0)
        vol *= 0.5 * d_beta
        origin = [float(xs[0]), float(ys[0]), float(zs[0])]
        return vol, origin, voxel

    # ---------------------------------------------------------------- analysis
    def _vol(self, rid):
        r = self.records.get(rid)
        if r is None:
            raise ActionRejected('UNKNOWN_RECORD')
        if r['kind'] != 'volume' or r['status'] != 'completed':
            raise ActionRejected('RECORD_NOT_SUITABLE')
        return r, self._data[rid]

    @staticmethod
    def _grids(d):
        nz, ny, nx = d['volume'].shape
        x0, y0, z0 = d['origin']
        v = d['voxel']
        return x0 + np.arange(nx) * v, y0 + np.arange(ny) * v, z0 + np.arange(nz) * v

    def _region_mask(self, d, region):
        if region is None:
            return None
        if region['z_max_m'] <= region['z_min_m']:
            raise ActionRejected('INVALID_REGION')
        xs, ys, zs = self._grids(d)
        Z, Y, X = np.meshgrid(zs, ys, xs, indexing='ij')
        return ((X - region['center_m'][0]) ** 2 + (Y - region['center_m'][1]) ** 2 <= region['radius_m'] ** 2) & (Z >= region['z_min_m']) & (Z <= region['z_max_m'])

    @staticmethod
    def _otsu(x, bins=256):
        hist, edges = np.histogram(x, bins=bins)
        c = (edges[:-1] + edges[1:]) / 2
        w0 = np.cumsum(hist)
        w1 = w0[-1] - w0
        m0 = np.cumsum(hist * c) / np.maximum(w0, 1)
        m1 = ((hist * c).sum() - np.cumsum(hist * c)) / np.maximum(w1, 1)
        k = int(np.argmax(w0 * w1 * (m0 - m1) ** 2))
        return float(c[k])

    def _segment(self, d, region, threshold):
        """Sample envelope (Otsu between air and material, holes filled slice by slice and in 3D, one voxel eroded against the partial-volume
        shell, clipped to the region) and the pore threshold (ISO 50: midway between the air and the matrix levels, unless given)."""
        from scipy import ndimage
        vol = d['volume']
        t_env = self._otsu(vol.ravel())
        solid = vol > t_env
        env = np.stack([ndimage.binary_fill_holes(sl) for sl in solid])
        env = ndimage.binary_fill_holes(env)
        env = ndimage.binary_erosion(env, iterations=1)
        mask = self._region_mask(d, region)
        if mask is not None:
            env &= mask
        outside = ~ndimage.binary_dilation(solid, iterations=2)
        mu_air = float(np.median(vol[outside])) if outside.any() else 0.0
        mu_mat = float(np.median(vol[env])) if env.any() else t_env
        t = threshold if threshold is not None else 0.5 * (mu_air + mu_mat)
        return env, t, mu_air, mu_mat

    def _cmd_data_porosity(self, record_id, region, threshold_1_m, min_pore_voxels):
        from scipy import ndimage
        r, d = self._vol(record_id)
        env, t, mu_air, mu_mat = self._segment(d, region, threshold_1_m)
        n_env = int(env.sum())
        v3 = d['voxel'] ** 3
        empty = dict(mean=None, d10=None, d50=None, d90=None, max=None)
        if n_env < 27:
            return dict(record_id=record_id, threshold_1_m=t, method='iso50' if threshold_1_m is None else 'manual', envelope_voxels=n_env, pore_voxels=0, porosity=0.0,
                        open_porosity=0.0, closed_porosity=0.0, pore_count=0, equivalent_diameter_m=empty, voxel_m=d['voxel'], note_zh='区域内没有足够的样品体素')
        pores = env & (d['volume'] < t)
        lab, n = ndimage.label(pores, structure=np.ones((3, 3, 3)))
        sizes = ndimage.sum(pores, lab, np.arange(1, n + 1)) if n else np.array([])
        keep = np.where(sizes >= min_pore_voxels)[0] + 1
        boundary = env & ~ndimage.binary_erosion(env)
        open_ids = set(np.unique(lab[boundary]).tolist()) - {0}
        vox = sizes[keep - 1] if len(keep) else np.array([])
        n_open = float(sum(sizes[i - 1] for i in keep if i in open_ids))
        eqd = np.sort((6 * vox * v3 / math.pi) ** (1 / 3)) if len(vox) else np.array([])
        por = float(vox.sum() / n_env)
        stats = (dict(mean=float(eqd.mean()), d10=float(np.percentile(eqd, 10)), d50=float(np.percentile(eqd, 50)), d90=float(np.percentile(eqd, 90)), max=float(eqd.max()))
                 if len(eqd) else empty)
        self.last_porosity, self.last_porosity_rid = por, record_id
        self._porosity_owner = None
        self.revision += 1
        return dict(record_id=record_id, threshold_1_m=t, method='iso50' if threshold_1_m is None else 'manual', envelope_voxels=n_env, pore_voxels=int(vox.sum()),
                    porosity=por, open_porosity=n_open / n_env, closed_porosity=por - n_open / n_env, pore_count=int(len(keep)), equivalent_diameter_m=stats,
                    voxel_m=d['voxel'], note_zh=f'阈值{t:.1f} 1/m(空气{mu_air:.1f}、基体{mu_mat:.1f});体素{d["voxel"] * 1e6:.1f} µm,小于约2体素的孔无法分辨')

    def _cmd_data_cracks(self, record_id, region, threshold_1_m, min_voxels):
        from scipy import ndimage
        r, d = self._vol(record_id)
        env, t, mu_air, mu_mat = self._segment(d, region, threshold_1_m)
        xs, ys, zs = self._grids(d)
        low = env & (d['volume'] < t)
        lab, n = ndimage.label(low, structure=np.ones((3, 3, 3)))
        out = []
        for j in range(1, n + 1):
            idx = np.argwhere(lab == j)
            if len(idx) < min_voxels:
                continue
            P = np.column_stack([xs[idx[:, 2]], ys[idx[:, 1]], zs[idx[:, 0]]])
            c = P.mean(0)
            w, vec = np.linalg.eigh(np.cov((P - c).T) + np.eye(3) * (d['voxel'] ** 2 / 12))
            l3, l2, l1 = w
            if math.sqrt(l3 / l1) < 0.3 and math.sqrt(l2 / l1) > 0.35:
                R1, R2 = 2 * math.sqrt(l1), 2 * math.sqrt(l2)
                area = math.pi * R1 * R2
                out.append(dict(area_m2=area, aperture_m=len(idx) * d['voxel'] ** 3 / area, length_m=2 * R1, normal=[float(x) for x in vec[:, 0]],
                                center_m=[float(x) for x in c], voxels=int(len(idx))))
        out.sort(key=lambda x: -x['area_m2'])
        return dict(record_id=record_id, threshold_1_m=t, cracks=out[:32], crack_count=len(out), total_area_m2=float(sum(x['area_m2'] for x in out)),
                    note_zh=f'平面状低衰减连通体(厚/长<0.3,宽/长>0.35);开口受体素{d["voxel"] * 1e6:.1f} µm限制(部分体积)')

    def _cmd_data_attenuation(self, record_id, region):
        r, d = self._vol(record_id)
        if region is None:
            env, _, _, _ = self._segment(d, None, None)
            m = env
        else:
            m = self._region_mask(d, region)
            if not m.any():
                raise ActionRejected('INVALID_REGION')                   # the region holds no voxel of the volume
        x = d['volume'][m] if m.any() else d['volume'].ravel()
        return dict(record_id=record_id, mean_1_m=float(x.mean()), std_1_m=float(x.std()), median_1_m=float(np.median(x)), p05_1_m=float(np.percentile(x, 5)),
                    p95_1_m=float(np.percentile(x, 95)), voxels=int(x.size))

    def _cmd_data_slice(self, record_id, axis, position_m, size_px):
        from scipy.ndimage import map_coordinates
        r, d = self._vol(record_id)
        vol = d['volume']
        xs, ys, zs = self._grids(d)
        g = dict(x=xs, y=ys, z=zs)
        if not g[axis][0] - 0.5 * d['voxel'] <= position_m <= g[axis][-1] + 0.5 * d['voxel']:
            raise ActionRejected('OUT_OF_VOLUME')
        other = [a for a in 'xyz' if a != axis]
        a0, a1 = g[other[0]], g[other[1]]
        ext = max(a0[-1] - a0[0], a1[-1] - a1[0]) + d['voxel']
        pix = ext / size_px
        c0, c1 = (a0[0] + a0[-1]) / 2, (a1[0] + a1[-1]) / 2
        p0 = c0 + (np.arange(size_px) + 0.5 - size_px / 2) * pix
        p1 = c1 + (np.arange(size_px) + 0.5 - size_px / 2) * pix
        B, A = np.meshgrid(p1, p0, indexing='ij')
        coord = {axis: np.full(A.shape, position_m), other[0]: A, other[1]: B}
        idx = [(coord[k] - g[k][0]) / d['voxel'] for k in ('z', 'y', 'x')]
        img = map_coordinates(vol, idx, order=1, mode='constant', cval=0.0)
        return dict(record_id=record_id, axis=axis, position_m=position_m, pixel_m=pix, mu_1_m=[[float(v) for v in row] for row in img])

    def _cmd_data_edge_resolution(self, record_id):
        r = self.records.get(record_id)
        if r is None:
            raise ActionRejected('UNKNOWN_RECORD')
        if r['kind'] != 'radiograph' or r['status'] != 'completed' or record_id not in self._data:
            raise ActionRejected('RECORD_NOT_SUITABLE')
        d = self._data[record_id]
        t = d['image'].astype(float)
        q = -np.log(np.clip(t, 1e-6, None))
        rows, cols = q.shape
        pitch = PIXEL_PITCH_M * d['binning'] if d['roi'] else DETECTOR_PIXELS[0] * PIXEL_PITCH_M / SIM_DETECTOR[0]
        M = SDD_M / d['sod']
        lo, hi = np.percentile(q, 5), np.percentile(q, 95)
        if hi - lo < 0.05:
            return dict(record_id=record_id, width_10_90_m=None, width_detector_px=None, edge_angle_rad=None, note_zh='图像中没有足够对比度的刃边')
        mid = 0.5 * (lo + hi)
        xe, ye = [], []
        for i in range(rows):
            p = q[i]
            s = np.where(np.diff(np.sign(p - mid)) != 0)[0]
            if len(s) == 1:
                j = s[0]
                xe.append(j + (mid - p[j]) / (p[j + 1] - p[j])); ye.append(i)
        if len(xe) < rows // 3:
            return dict(record_id=record_id, width_10_90_m=None, width_detector_px=None, edge_angle_rad=None, note_zh='找不到单一直刃边(每行应只穿越一次)')
        k, b0 = np.polyfit(ye, xe, 1)
        Y, X = np.mgrid[0:rows, 0:cols]
        dist = (X - (k * Y + b0)) / math.sqrt(1 + k * k)
        order = np.argsort(dist.ravel())
        ds, qs = dist.ravel()[order], q.ravel()[order]
        bins = np.arange(-12, 12.25, 0.25)
        prof = np.array([qs[(ds >= a) & (ds < a + 0.25)].mean() if np.any((ds >= a) & (ds < a + 0.25)) else np.nan for a in bins[:-1]])
        xc = bins[:-1] + 0.125
        ok = np.isfinite(prof)
        xc, prof = xc[ok], prof[ok]
        a, bnd = prof[:8].mean(), prof[-8:].mean()
        f = (prof - a) / (bnd - a)
        if f[-1] < f[0]:
            f = 1 - f
        f = np.maximum.accumulate(np.clip(f, 0, 1))
        x10, x90 = float(np.interp(0.1, f, xc)), float(np.interp(0.9, f, xc))
        w_px = x90 - x10
        return dict(record_id=record_id, width_10_90_m=w_px * pitch / M, width_detector_px=w_px, edge_angle_rad=float(math.atan(k)),
                    note_zh=f'刃边扩展函数10–90 %宽度{w_px:.2f}像素(探测器像素{pitch * 1e6:.1f} µm,放大{M:.2f})')

    # ---------------------------------------------------------------- queries
    @staticmethod
    def _out(r):
        return copy.deepcopy(r)

    def _cmd_measurement_result(self, record_id):
        r = self.records.get(record_id)
        if r is None:
            raise ActionRejected('UNKNOWN_RECORD')
        return self._out(r)

    def _cmd_records_list(self):
        return dict(records=sorted(self.records))

    def _cmd_events_read(self, since_s):
        return dict(events=[{k: v for k, v in e.items() if k != '_rid'} for e in self.events if e['time_s'] >= since_s], dropped=self.events_dropped)

    def ledger(self):
        out = {k: v.value() for k, v in self.led.items()}
        return out

    def _cmd_ledger_read(self):
        return self.ledger()

    def _cmd_provenance_read(self):
        lib = library()
        return dict(library=LIBRARY_REL, library_sha256=lib['sha256'], substances=len(lib['substances']), license=lib['license_zh'])


ASYNC = {'calibration.flat_field', 'calibration.geometry', 'xray.radiograph', 'scan.start', 'recon.start'}
ACQUISITION = {'calibration.flat_field', 'calibration.geometry', 'xray.radiograph', 'scan.start'}
_DATA_GUARDED = {'measurement.result', 'records.delete'}
_RECORD_ARG = {'measurement.result': 'record_id', 'records.delete': 'record_id'}
_ANALYSIS_RECORD_ARGS = {'recon.start': ['record_id'], 'data.porosity': ['record_id'], 'data.cracks': ['record_id'], 'data.attenuation': ['record_id'],
                         'data.slice': ['record_id'], 'data.edge_resolution': ['record_id']}
_HARD_POWER = ('power.set', 'power.press_button')   # the mains button cuts the power: physical, never refused (the device reacts as to a power loss)
# during another agent's acquisition only the commands that stop or change it are refused (ACCESS_DENIED): the door, the filter changer, the stage
# and switching the X-ray off; starting another acquisition or calibration, acknowledging and the settings follow the device's own lock
# (MEASUREMENT_RUNNING)
_CONTROL_GUARDED = {'door.open', 'door.close', 'filter.select', 'stage.go_to', 'stage.move', 'stage.rotate', 'xray.enable'}
# placing / removing a sample at the open door (robot skills) is physical and never refused; the safety button (push and release) stays open to all
_SAFETY = ('emergency.stop', 'emergency.release')


class BrukerX4PoseidonSession:
    def __init__(self, host, principal_id):
        self._host = host
        self._principal = principal_id

    @property
    def _role(self):
        return self._host._principals[self._principal]

    def _target(self, instance_id):
        if not isinstance(instance_id, str) or instance_id != self._host.instance_id:
            raise ActionRejected('UNKNOWN_INSTANCE')

    def describe(self, *, instance_id=None, asset_type_id=None):
        if (instance_id is None) == (asset_type_id is None):
            raise ActionRejected('INVALID_ARGUMENT')
        if instance_id is not None:
            self._target(instance_id)
        if asset_type_id is not None and asset_type_id != ASSET_TYPE_ID:
            raise ActionRejected('UNKNOWN_INSTANCE')
        meta = copy.deepcopy(export_bruker_x4poseidon_contract_metadata())
        caps = {k: v for k, v in meta['capabilities'].items() if v['visibility'] == 'agent' or self._role != 'agent'}
        slots = {k: v for k, v in meta['state_slots'].items() if v['visibility'] == 'agent' or self._role != 'agent'}
        if self._role == 'agent':
            for c in caps.values():
                c['state_effects'] = [e for e in c['state_effects'] if e['state_id'] in slots]
                c['observes'] = [x for x in c['observes'] if x in slots]
                for pre in c['preconditions']:
                    pre['state_ids'] = [x for x in pre['state_ids'] if x in slots]
                c['execution']['completion_state_ids'] = [x for x in c['execution']['completion_state_ids'] if x in slots]
            for m in meta['mechanisms'].values():
                m['state_ids'] = [x for x in m['state_ids'] if x in slots]
        return dict(protocol_version=PROTOCOL_VERSION, asset_type_id=ASSET_TYPE_ID, asset_version=ASSET_VERSION, capabilities=caps, state_slots=slots)

    def _foreign(self, object_id):
        owner = self._host._owners.get(object_id)
        return owner is not None and owner != self._principal and self._host._principals.get(owner) == 'agent'

    def _foreign_op(self):
        h = self._host
        return h.op is not None and self._foreign(h.op['op'])

    def read(self, *, instance_id, state_ids=None):
        self._target(instance_id)
        h = self._host
        values = h.read(self._role)
        foreign = self._role == 'agent' and self._foreign_op()
        values['measurement.started_by_other_agent'] = foreign
        if foreign:
            values['measurement.id'] = None
        if self._role == 'agent':
            if h.recon is not None and self._foreign(h.recon['op']):
                values['recon.id'] = None
            values['result.last_record_id'] = h._last_record(lambda k: not self._foreign(k))
            if self._foreign(h.last_porosity_rid) or (h._porosity_owner not in (None, self._principal) and h._principals.get(h._porosity_owner) == 'agent'):
                values['result.last_porosity'] = None                    # another agent's analysis (or an analysis of its volume)
        ids = list(values) if state_ids is None else state_ids
        if not isinstance(ids, list) or any(not isinstance(k, str) for k in ids):
            raise ActionRejected('INVALID_ARGUMENT')
        if any(k not in values for k in ids):
            raise ActionRejected('ACCESS_DENIED')
        return dict(instance_id=instance_id, status='succeeded', state_revision=h.revision, clock={'domain': 'simulation', 'time_s': h.time_s},
                    observations={k: dict(value=copy.deepcopy(values[k]), unit=SLOTS[k]['unit'],
                                          quality='unavailable' if values[k] is None else 'estimated' if SLOTS[k]['origin'] in ('sensor', 'derived') else 'valid',
                                          sample_time_s=h.time_s, sample_window_s=None) for k in ids})

    def _guard(self, capability_id, arguments):
        if self._role != 'agent':
            return
        h = self._host
        if capability_id in _CONTROL_GUARDED and self._foreign_op():
            if capability_id in ('power.set', 'power.press_button') and not h.power:
                return
            if capability_id in _SAFETY:                                # the safety button (push and release) always works
                return
            raise ActionRejected('ACCESS_DENIED')
        if capability_id in _DATA_GUARDED:
            rid = arguments.get(_RECORD_ARG[capability_id])
            if self._foreign(rid):
                raise ActionRejected('ACCESS_DENIED')
            owner = h._owners.get(rid)
            if capability_id == 'records.delete' and owner is not None and owner != self._principal:
                raise ActionRejected('ACCESS_DENIED')                  # an agent deletes only its own records and those of the host
        for arg in _ANALYSIS_RECORD_ARGS.get(capability_id, []):
            if arguments.get(arg) is not None and self._foreign(arguments[arg]):
                raise ActionRejected('ACCESS_DENIED')

    def invoke(self, *, instance_id, capability_id, arguments, request_id):
        h = self._host
        key = digest = None
        try:
            self._target(instance_id)
            if not isinstance(request_id, str) or not request_id:
                raise ActionRejected('INVALID_ARGUMENT')
            key = (self._principal, request_id)
            try:
                digest = json.dumps([capability_id, arguments], sort_keys=True, allow_nan=False)
            except (TypeError, ValueError):
                digest = None
            if key in h._requests:
                old, response = h._requests[key]
                if digest is None or old != digest or not isinstance(response, dict):
                    raise ActionRejected('REQUEST_CONFLICT')
                return copy.deepcopy(response)
            if not isinstance(capability_id, str) or capability_id not in ALL_CAPS:
                raise ActionRejected('UNKNOWN_CAPABILITY')
            if self._role == 'agent' and ALL_CAPS[capability_id]['visibility'] != 'agent':
                raise ActionRejected('ACCESS_DENIED')
            if not isinstance(arguments, dict):
                raise ActionRejected('INVALID_ARGUMENT')
            validate(ALL_CAPS[capability_id]['input_schema'], arguments)
            self._guard(capability_id, arguments)
            h._caller = self._principal
            try:
                result = h.invoke(capability_id, **arguments)
            finally:
                h._caller = None
            if self._role == 'agent':
                if capability_id == 'records.list':
                    result = dict(records=[r for r in result['records'] if not self._foreign(r)])
                elif capability_id == 'events.read':
                    result = dict(events=[{k: v for k, v in e.items() if k != '_rid'} for e in h.events
                                          if e['time_s'] >= arguments['since_s'] and not self._foreign(e.get('_rid')) and e['kind'] != 'fault'], dropped=h.events_dropped)
                elif capability_id == 'measurement.result':
                    result = dict(result, template_source=dict(library=result['template_source']['library'], substances=[]), dose_gy=None)
            if capability_id == 'data.porosity':
                h._porosity_owner = self._principal
            operation = result.get('operation_id') if capability_id in ASYNC else None
            if operation:
                h._owners[operation] = self._principal
                rid = h.operations[operation].get('record_id')
                if rid:
                    h._owners[rid] = self._principal
            response = dict(protocol_version=PROTOCOL_VERSION, instance_id=instance_id, request_id=request_id, operation_id=operation,
                            status='accepted' if operation else 'succeeded', state_revision=h.revision, clock={'domain': 'simulation', 'time_s': h.time_s},
                            result=copy.deepcopy(result), observations={}, error=None)
            h._requests[key] = (digest, copy.deepcopy(response))
            return response
        except ActionRejected as exc:
            response = dict(protocol_version=PROTOCOL_VERSION, instance_id=instance_id, request_id=request_id, operation_id=None,
                            status='unsupported' if str(exc) == 'UNSUPPORTED' else 'rejected', state_revision=h.revision,
                            clock={'domain': 'simulation', 'time_s': h.time_s}, result={}, observations={},
                            error=dict(code=str(exc), message=ERROR_ZH.get(str(exc), str(exc)), retryable=False, side_effects='none'))
            if key is not None and digest is not None and key not in h._requests:
                h._requests[key] = (digest, copy.deepcopy(response))
            return response

    def write(self, *, instance_id, state_id, value, request_id):
        self._target(instance_id)
        raise ActionRejected('ACCESS_DENIED')                       # set points are written through capabilities

    def poll(self, operation_id):
        h = self._host
        if not isinstance(operation_id, str):
            raise ActionRejected('INVALID_ARGUMENT')
        op = h.operations.get(operation_id)
        if op is None:
            raise ActionRejected('UNKNOWN_OPERATION')
        if h._owners.get(operation_id) != self._principal and self._role != 'evaluator':
            raise ActionRejected('ACCESS_DENIED')                       # evaluators observe every operation
        if op.get('record_id') in h.records:
            res = h._out(h.records[op['record_id']])
            if self._role == 'agent':
                res['template_source'] = dict(library=res['template_source']['library'], substances=[])
                res['dose_gy'] = None                                   # absorbed dose follows the true sample mass: evaluator only
        else:
            res = {k: v for k, v in op.items() if k in ('phase', 'reason')}
        return dict(operation_id=operation_id, status=op['phase'], result=res)

    def cancel(self, operation_id, request_id):
        h = self._host
        if not isinstance(request_id, str) or not request_id:
            raise ActionRejected('INVALID_ARGUMENT')
        key = (self._principal, request_id)
        digest = json.dumps(['cancel', operation_id]) if isinstance(operation_id, str) else None
        if key in h._requests:
            old, response = h._requests[key]
            if digest is None or old != digest:
                raise ActionRejected('REQUEST_CONFLICT')
            if isinstance(response, str):
                raise ActionRejected(response)
            return copy.deepcopy(response)
        try:
            self.poll(operation_id)
            op = h.operations[operation_id]
            if op['phase'] != 'running':
                raise ActionRejected('NOT_RUNNING')
            h._cancel_op(op)
        except ActionRejected as exc:
            if digest is not None:
                h._requests[key] = (digest, str(exc))
            raise
        h.revision += 1
        response = dict(protocol_version=PROTOCOL_VERSION, instance_id=h.instance_id, request_id=request_id, operation_id=operation_id, status='succeeded',
                        state_revision=h.revision, clock={'domain': 'simulation', 'time_s': h.time_s}, result=dict(phase=op['phase']), observations={}, error=None)
        h._requests[key] = (digest, copy.deepcopy(response))
        return response


ERROR_ZH = {
    'INVALID_ARGUMENT': '参数不符合输入模式或取值范围', 'REQUEST_CONFLICT': '同一request_id对应不同的调用', 'UNKNOWN_INSTANCE': '实例不存在',
    'UNKNOWN_CAPABILITY': '能力不存在', 'UNSUPPORTED': '本配置不支持该能力', 'ACCESS_DENIED': '无权访问(评测槽、另一个agent的记录或操作)',
    'PLANT_REQUIRED': '尚未声明实验室条件', 'SEED_FIXED': '噪声种子只能在首次声明时设定', 'POWER_OFF': '系统未通电', 'NOT_READY': '系统启动中',
    'ERROR_PENDING': '有未确认的错误', 'EMERGENCY_STOP': '安全按钮被按下(锁存)', 'CAUSE_PERSISTS': '错误原因仍存在,不能确认',
    'MAINS_OUT_OF_RANGE': '电源电压超出100–240 VAC ±10 %', 'MAINS_FAULT': '供电电压超出范围,系统断电', 'UNKNOWN_SUBSTANCE': '模板库中没有该材料',
    'SAMPLE_EXISTS': '样品ID已存在', 'TOO_MANY_SAMPLES': '样品数量超过上限',
    'INVALID_SAMPLE': '样品定义不自洽(层厚之和须等于高度、体积分数之和为1、裂纹/夹杂须在所在层与圆柱内、孔过密无法不重叠放置、体模不能带层/孔/裂纹/夹杂)',
    'UNKNOWN_SAMPLE': '样品不存在', 'SAMPLE_NOT_ON_BENCH': '样品不在实验台上', 'STAGE_OCCUPIED': '样品夹上已有样品', 'DOOR_CLOSED': '门没有完全打开',
    'NOT_ACCESSIBLE': '样品夹不在门口范围(先stage.go_to load)', 'COLLISION_RISK': '目标位置或移动路径上样品、样品针、样品夹或旋转台会碰到滤片转换器/源头部、探测器外壳或腔顶(碰撞保护)',
    'SAMPLE_NOT_ON_STAGE': '该样品不在样品夹上', 'MEASUREMENT_RUNNING': '有采集操作进行中', 'MOTION_IN_PROGRESS': '门、滤片或样品台在移动',
    'XRAY_ON': 'X射线开启时门锁住', 'LIGHT_CURTAIN_BLOCKED': '门口安全光幕被挡,门不能关闭', 'POWER_LIMIT': '靶功率超过20 W',
    'DOOR_OPEN': '门没有关闭', 'INTERLOCK_OPEN': 'X射线联锁未满足(门、联锁传感器或安全按钮)', 'XRAY_ARC': 'X射线管打火',
    'INTERLOCK_FAULT': '门联锁传感器故障', 'STAGE_FAULT': '旋转台故障(卡滞)',
    'RECORDS_FULL': '记录数已达上限', 'XRAY_NOT_READY': 'X射线未开启、未预热完成或未稳定在设定值', 'NO_SAMPLE': '样品夹上没有样品',
    'FLAT_FIELD_REQUIRED': '当前电压、电流、滤片、合并与曝光没有有效的暗场/亮场校正', 'SAMPLE_IN_BEAM': '亮场校正时样品、样品针、样品夹或旋转台在光束中(先降低或移开)',
    'PHANTOM_REQUIRED': '几何标定需要钢球标定体模', 'SAMPLE_NOT_IN_FIELD': '样品不在探测器视场内', 'UNKNOWN_RECORD': '记录不存在',
    'RECORD_NOT_SUITABLE': '记录种类或状态不适用', 'RECON_BUSY': '工作站正在进行另一个重建', 'INVALID_VOI': '感兴趣体积的中心不在采集窗口可重建的区域内',
    'INVALID_REGION': '分析区域无效(z上限须大于下限,区域须含体数据的体素)', 'OUT_OF_VOLUME': '切片位置不在体数据内', 'NOT_RUNNING': '操作不在进行中',
    'ROI_OUTSIDE_DETECTOR': '局部区域超出探测器有效面', 'PHANTOM_NOT_IN_FIELD': '钢球标定体模须在所有角度完整位于视场内(降低放大或调整高度)',
    'DATA_NOT_CACHED': '投影数据已不在缓存中(只保留最近8组投影/透视)', 'RECORD_IN_USE': '记录正在使用(进行中的操作或正被重建的投影)',
    'UNKNOWN_OPERATION': '操作不存在', 'USER_CANCEL': '操作被取消', 'XRAY_OFF': '采集中X射线被关闭', 'DATA_EVICTED': '投影数据已从缓存中清除(只保留最近8组投影/透视)',
}
EFFECTS = {
    'plant.configure': ['door.closed', 'door.moving', 'door.target_m', 'filter.moving', 'interlock.ok', 'measurement.id', 'measurement.kind', 'measurement.progress',
        'measurement.remaining_s', 'panel.status_green', 'panel.status_orange', 'panel.status_red', 'plant.configured', 'power.enabled', 'stage.chuck_z_target_m',
        'stage.moving', 'stage.rotation_target_rad', 'stage.sod_target_m', 'system.error_code', 'system.state', 'truth.axis_offset_m', 'xray.current_a', 'xray.enabled',
        'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready', 'xray.voltage_v', 'xray.warmup_remaining_s'],
    'sample.provide': ['sample.bench_ids', 'truth.samples'],
    'sample.place': ['sample.bench_ids', 'sample.on_stage', 'truth.samples'],
    'sample.remove': ['sample.bench_ids', 'sample.on_stage', 'truth.samples'],
    'power.set': ['door.closed', 'door.moving', 'door.target_m', 'filter.moving', 'interlock.ok', 'measurement.id', 'measurement.kind', 'measurement.progress',
        'measurement.remaining_s', 'panel.status_green', 'panel.status_orange', 'panel.status_red', 'power.enabled', 'stage.chuck_z_target_m', 'stage.moving',
        'stage.rotation_target_rad', 'stage.sod_target_m', 'system.error_code', 'system.state', 'xray.current_a', 'xray.enabled', 'xray.focal_spot_m', 'xray.on',
        'xray.power_w', 'xray.ready', 'xray.voltage_v', 'xray.warmup_remaining_s'],
    'power.press_button': ['door.closed', 'door.moving', 'door.target_m', 'filter.moving', 'interlock.ok', 'measurement.id', 'measurement.kind', 'measurement.progress',
        'measurement.remaining_s', 'panel.status_green', 'panel.status_orange', 'panel.status_red', 'power.enabled', 'stage.chuck_z_target_m', 'stage.moving',
        'stage.rotation_target_rad', 'stage.sod_target_m', 'system.error_code', 'system.state', 'xray.current_a', 'xray.enabled', 'xray.focal_spot_m', 'xray.on',
        'xray.power_w', 'xray.ready', 'xray.voltage_v', 'xray.warmup_remaining_s'],
    'emergency.stop': ['door.closed', 'door.moving', 'door.target_m', 'emergency.stopped', 'filter.moving', 'interlock.ok', 'measurement.id', 'measurement.kind',
        'measurement.progress', 'measurement.remaining_s', 'panel.status_green', 'panel.status_orange', 'panel.status_red', 'stage.chuck_z_target_m', 'stage.moving',
        'stage.rotation_target_rad', 'stage.sod_target_m', 'system.error_code', 'xray.current_a', 'xray.enabled', 'xray.focal_spot_m', 'xray.on', 'xray.power_w',
        'xray.ready', 'xray.voltage_v', 'xray.warmup_remaining_s'],
    'emergency.release': ['emergency.stopped', 'interlock.ok'],
    'system.acknowledge': ['panel.status_green', 'panel.status_red', 'system.error_code'],
    'fault.inject': ['door.target_m', 'interlock.ok', 'light_curtain.blocked', 'measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s',
        'panel.status_green', 'panel.status_orange', 'panel.status_red', 'stage.moving', 'stage.rotation_target_rad', 'system.error_code',
        'truth.detector_defect_columns', 'truth.faults', 'xray.current_a', 'xray.enabled', 'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready',
        'xray.voltage_v', 'xray.warmup_remaining_s'],
    'door.open': ['door.closed', 'door.moving', 'door.target_m', 'interlock.ok'],
    'door.close': ['door.closed', 'door.moving', 'door.target_m', 'interlock.ok'],
    'xray.set': ['calibration.flat_field', 'xray.current_set_a', 'xray.focal_spot_m', 'xray.ready', 'xray.voltage_set_v'],
    'xray.enable': ['panel.status_green', 'panel.status_orange', 'xray.current_a', 'xray.enabled', 'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready',
        'xray.voltage_v', 'xray.warmup_remaining_s'],
    'filter.select': ['calibration.flat_field', 'filter.label', 'filter.moving', 'filter.position'],
    'stage.move': ['stage.chuck_z_target_m', 'stage.moving', 'stage.position_label', 'stage.sod_target_m'],
    'stage.rotate': ['stage.moving', 'stage.rotation_target_rad'],
    'stage.go_to': ['stage.chuck_z_target_m', 'stage.moving', 'stage.position_label', 'stage.sod_target_m'],
    'scan.configure': ['calibration.flat_field', 'geometry.voxel_size_m', 'scan.duration_s', 'scan.projections', 'scan.settings'],
    'calibration.flat_field': ['measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s', 'measurement.started_by_other_agent'],
    'calibration.geometry': ['measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s', 'measurement.started_by_other_agent'],
    'xray.radiograph': ['measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s', 'measurement.started_by_other_agent'],
    'scan.start': ['measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s', 'measurement.started_by_other_agent'],
    'recon.start': ['recon.id', 'recon.remaining_s'],
    'data.porosity': ['result.last_porosity'],
    'data.cracks': [],
    'data.attenuation': [],
    'data.slice': [],
    'data.edge_resolution': [],
    'records.delete': ['result.last_porosity', 'result.last_record_id'],
}
EFFECTS['sim.step'] = ['clock.time_s']
ON_STEP = {
    'plant.configure': ['calibration.flat_field', 'calibration.geometry', 'door.closed', 'door.moving', 'door.position_m', 'filter.moving', 'filter.slide_m',
        'geometry.fov_m', 'geometry.magnification', 'geometry.voxel_size_m', 'interlock.ok', 'measurement.id', 'measurement.kind', 'measurement.progress',
        'measurement.remaining_s', 'panel.status_green', 'panel.status_orange', 'result.last_record_id', 'stage.chuck_z_m', 'stage.moving', 'stage.rotation_rad',
        'stage.rotation_target_rad', 'stage.sod_m', 'system.state', 'truth.samples', 'xray.current_a', 'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready',
        'xray.voltage_v', 'xray.warmup_remaining_s'],
    'power.set': ['calibration.flat_field', 'calibration.geometry', 'door.closed', 'door.moving', 'door.position_m', 'filter.moving', 'filter.slide_m', 'geometry.fov_m',
        'geometry.magnification', 'geometry.voxel_size_m', 'interlock.ok', 'measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s',
        'panel.status_green', 'panel.status_orange', 'result.last_record_id', 'stage.chuck_z_m', 'stage.moving', 'stage.rotation_rad', 'stage.rotation_target_rad',
        'stage.sod_m', 'system.state', 'truth.samples', 'xray.current_a', 'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready', 'xray.voltage_v',
        'xray.warmup_remaining_s'],
    'power.press_button': ['calibration.flat_field', 'calibration.geometry', 'door.closed', 'door.moving', 'door.position_m', 'filter.moving', 'filter.slide_m',
        'geometry.fov_m', 'geometry.magnification', 'geometry.voxel_size_m', 'interlock.ok', 'measurement.id', 'measurement.kind', 'measurement.progress',
        'measurement.remaining_s', 'panel.status_green', 'panel.status_orange', 'result.last_record_id', 'stage.chuck_z_m', 'stage.moving', 'stage.rotation_rad',
        'stage.rotation_target_rad', 'stage.sod_m', 'system.state', 'truth.samples', 'xray.current_a', 'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready',
        'xray.voltage_v', 'xray.warmup_remaining_s'],
    'emergency.stop': ['calibration.flat_field', 'calibration.geometry', 'door.closed', 'door.moving', 'door.position_m', 'filter.moving', 'filter.slide_m',
        'geometry.fov_m', 'geometry.magnification', 'geometry.voxel_size_m', 'interlock.ok', 'measurement.id', 'measurement.kind', 'measurement.progress',
        'measurement.remaining_s', 'panel.status_green', 'panel.status_orange', 'result.last_record_id', 'stage.chuck_z_m', 'stage.moving', 'stage.rotation_rad',
        'stage.rotation_target_rad', 'stage.sod_m', 'truth.samples', 'xray.current_a', 'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready', 'xray.voltage_v',
        'xray.warmup_remaining_s'],
    'fault.inject': ['calibration.flat_field', 'calibration.geometry', 'door.closed', 'door.moving', 'door.position_m', 'interlock.ok', 'measurement.id',
        'measurement.kind', 'measurement.progress', 'measurement.remaining_s', 'panel.status_green', 'panel.status_orange', 'result.last_record_id', 'stage.moving',
        'stage.rotation_rad', 'stage.rotation_target_rad', 'truth.samples', 'xray.current_a', 'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready',
        'xray.voltage_v', 'xray.warmup_remaining_s'],
    'door.open': ['door.closed', 'door.moving', 'door.position_m', 'interlock.ok'],
    'door.close': ['door.closed', 'door.moving', 'door.position_m', 'interlock.ok'],
    'xray.set': ['truth.samples', 'xray.current_a', 'xray.focal_spot_m', 'xray.power_w', 'xray.ready', 'xray.voltage_v'],
    'xray.enable': ['panel.status_green', 'panel.status_orange', 'truth.samples', 'xray.current_a', 'xray.focal_spot_m', 'xray.on', 'xray.power_w', 'xray.ready',
        'xray.voltage_v', 'xray.warmup_remaining_s'],
    'filter.select': ['filter.moving', 'filter.slide_m', 'truth.samples'],
    'stage.move': ['geometry.fov_m', 'geometry.magnification', 'geometry.voxel_size_m', 'stage.chuck_z_m', 'stage.moving', 'stage.sod_m', 'truth.samples'],
    'stage.rotate': ['stage.moving', 'stage.rotation_rad', 'truth.samples'],
    'stage.go_to': ['geometry.fov_m', 'geometry.magnification', 'geometry.voxel_size_m', 'stage.chuck_z_m', 'stage.moving', 'stage.sod_m', 'truth.samples'],
    'scan.configure': ['geometry.voxel_size_m'],
    'calibration.flat_field': ['calibration.flat_field', 'measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s',
        'measurement.started_by_other_agent', 'result.last_record_id'],
    'calibration.geometry': ['calibration.geometry', 'measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s',
        'measurement.started_by_other_agent', 'result.last_record_id', 'stage.rotation_rad', 'stage.rotation_target_rad', 'truth.samples'],
    'xray.radiograph': ['measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s', 'measurement.started_by_other_agent',
        'result.last_record_id'],
    'scan.start': ['measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s', 'measurement.started_by_other_agent',
        'result.last_record_id', 'stage.rotation_rad', 'stage.rotation_target_rad', 'truth.samples'],
    'recon.start': ['recon.id', 'recon.remaining_s', 'result.last_record_id'],
    'records.delete': ['result.last_record_id'],
    'sim.step': ['calibration.flat_field', 'calibration.geometry', 'door.closed', 'door.moving', 'door.position_m', 'filter.moving', 'filter.slide_m', 'geometry.fov_m',
        'geometry.magnification', 'geometry.voxel_size_m', 'interlock.ok', 'measurement.id', 'measurement.kind', 'measurement.progress', 'measurement.remaining_s',
        'measurement.started_by_other_agent', 'panel.status_green', 'panel.status_orange', 'recon.id', 'recon.remaining_s', 'result.last_record_id', 'stage.chuck_z_m',
        'stage.moving', 'stage.rotation_rad', 'stage.rotation_target_rad', 'stage.sod_m', 'system.state', 'truth.samples', 'xray.current_a', 'xray.focal_spot_m',
        'xray.on', 'xray.power_w', 'xray.ready', 'xray.voltage_v', 'xray.warmup_remaining_s'],
}
ON_STEP_ZH = {
    'power.set': '启动60 s后进入就绪(各轴保持位置);断电时各轴停止', 'power.press_button': '同power.set', 'door.open': '门以0.1 m/s滑开', 'door.close': '门以0.1 m/s关闭',
    'xray.enable': '电压与电流按斜率(或上电后首次开启的180 s预热程序,升到开启时的设定值)升到设定值,X射线能量计入账本',
    'xray.set': '束流开启时电压与电流按斜率变化到新设定值(预热中程序照旧,结束后再按斜率变化)',
    'filter.select': '滤片滑块每档0.5 s(光束能谱随滑块经过的滤片变化)', 'stage.move': '滑台20 mm/s、升降10 mm/s', 'stage.go_to': '同stage.move', 'stage.rotate': '旋转60°/s',
    'calibration.flat_field': '暗场与亮场各20帧后写入校正', 'calibration.geometry': '36个投影、旋转一周后写入标定', 'xray.radiograph': '曝光×帧平均后写入透视记录',
    'scan.start': '按投影时间连续旋转,完成后写入投影记录', 'recon.start': '按工作站重建时间完成后写入体数据', 'sim.step': '推进全部运动、X射线斜坡、操作阶段与能量账',
    'emergency.stop': '已停止(无后续演化)', 'fault.inject': '故障对进行中的操作与运动的影响', 'records.delete': '删除本身没有后续演化;之后完成的采集或重建写入新的最新记录号(有无删除都一样;按有/无调用的对比列为步进效应)',
}
_R = ['power.enabled', 'system.state', 'system.error_code', 'emergency.stopped']
_ACQ = _R + ['measurement.id', 'door.closed', 'xray.ready', 'stage.moving', 'filter.moving', 'door.moving']
PRECONDITIONS = {
    'plant.configure': ('噪声种子只能在首次声明时设定', ['plant.configured']),
    'sample.provide': ('已声明实验室条件;样品ID未用过;材料在模板库中;几何自洽(层厚之和等于高度、体积分数之和为1、裂纹/夹杂在所在层内)', ['plant.configured', 'sample.bench_ids']),
    'sample.place': ('样品在实验台上、样品夹空;门完全打开且不在移动;无采集操作;样品台静止并在门口范围;样品包络与滤片转换器、探测器盖板、腔顶留有2 mm',
                     ['sample.bench_ids', 'sample.on_stage', 'door.position_m', 'door.moving', 'measurement.id', 'stage.moving', 'stage.sod_m', 'stage.chuck_z_m']),
    'sample.remove': ('样品在样品夹上;门完全打开;无采集操作;样品台静止并在门口范围', ['sample.on_stage', 'door.position_m', 'measurement.id', 'stage.moving', 'stage.sod_m']),
    'power.set': ('已声明实验室条件;上电需电源电压在100–240 VAC ±10 %内;上电时仍存在的原因(安全按钮锁存、故障)按优先级锁存错误', ['plant.configured']),
    'power.press_button': ('同power.set(按一次切换通断)', ['plant.configured', 'power.enabled']),
    'emergency.stop': ('已声明实验室条件;任何时候可按', ['plant.configured']), 'emergency.release': ('已声明实验室条件', ['plant.configured', 'emergency.stopped']),
    'system.acknowledge': ('已通电;错误原因已排除(故障已清除、安全按钮已释放、电源恢复)', ['power.enabled', 'system.error_code', 'emergency.stopped']),
    'fault.inject': ('已声明实验室条件', ['plant.configured']),
    'door.open': ('已就绪、无错误、安全按钮未按下;无采集操作;X射线关闭(门锁)', _R + ['measurement.id', 'xray.on', 'xray.enabled']),
    'door.close': ('已就绪、无错误、安全按钮未按下;安全光幕未被挡', _R + ['light_curtain.blocked']),
    'xray.set': ('已声明实验室条件;电压20–100 kV、电流≤0.5 mA且功率≤20 W;无采集操作', ['plant.configured', 'measurement.id']),
    'xray.enable': ('开启:已就绪、无错误、安全按钮未按下、门关闭、联锁正常、无打火;关闭:无采集操作', _R + ['door.closed', 'interlock.ok', 'measurement.id']),
    'filter.select': ('已就绪、无错误;无采集操作', _R + ['measurement.id']),
    'stage.move': ('已就绪、无错误;旋转台无故障;无采集操作;目标位置与滑台/升降同时运动的路径上样品、样品针、样品夹与旋转台都不进入禁入区', _R + ['measurement.id', 'sample.on_stage', 'stage.sod_m', 'stage.chuck_z_m']),
    'stage.go_to': ('已就绪、无错误;旋转台无故障;无采集操作;从当前位置到装样位或中心位、滑台(20 mm/s)与升降(10 mm/s)同时运动的路径上样品、样品针、样品夹与旋转台'
                    '都不进入禁入区(路径从当前位置开始,贴近禁入区时先用stage.move分步移开)', _R + ['measurement.id', 'sample.on_stage', 'stage.sod_m', 'stage.chuck_z_m']), 'stage.rotate': ('已就绪、无错误;旋转台无故障;无采集操作', _R + ['measurement.id']),
    'scan.configure': ('已声明实验室条件;无采集操作', ['plant.configured', 'measurement.id']),
    'calibration.flat_field': ('已就绪、无错误;无采集操作与运动;门关闭;X射线就绪;样品、样品针、样品夹与旋转台都不在光束中', _ACQ + ['sample.on_stage', 'stage.sod_m', 'stage.chuck_z_m']),
    'calibration.geometry': ('同扫描;样品夹上为钢球标定体模且在所有角度完整位于视场内;有效的暗场/亮场校正', _ACQ + ['sample.on_stage', 'calibration.flat_field', 'stage.sod_m', 'stage.chuck_z_m']),
    'xray.radiograph': ('已就绪、无错误;无采集操作与运动;门关闭;X射线就绪;有效的暗场/亮场校正;局部区域在探测器有效面内', _ACQ + ['calibration.flat_field']),
    'scan.start': ('同透视;样品夹上有样品且样品本体在视场内', _ACQ + ['sample.on_stage', 'calibration.flat_field', 'stage.sod_m', 'stage.chuck_z_m']),
    'recon.start': ('投影记录已完成且投影数据仍在缓存;工作站没有另一个重建;感兴趣体积的中心在采集窗口可重建的区域内(留出自动对准±200 µm;超出部分给出警告)', ['recon.id']),
    'records.delete': ('记录存在且不在使用中(进行中的操作、正被重建的投影记录);agent只能删除自己或宿主的记录', ['result.last_record_id', 'recon.id', 'measurement.id']),
}
_COMMON_ERRORS = ['INVALID_ARGUMENT', 'REQUEST_CONFLICT', 'UNKNOWN_INSTANCE']
_RE = ['PLANT_REQUIRED', 'POWER_OFF', 'NOT_READY', 'EMERGENCY_STOP', 'ERROR_PENDING']
_ACQ_ERR = _RE + ['MEASUREMENT_RUNNING', 'MOTION_IN_PROGRESS', 'DOOR_OPEN', 'XRAY_NOT_READY', 'STAGE_FAULT', 'RECORDS_FULL']
_ACQ_FAIL = ['POWER_OFF', 'EMERGENCY_STOP', 'XRAY_ARC', 'INTERLOCK_FAULT', 'USER_CANCEL']
CAP_ERRORS = {
    'plant.configure': ['SEED_FIXED'], 'sample.provide': ['PLANT_REQUIRED', 'SAMPLE_EXISTS', 'TOO_MANY_SAMPLES', 'INVALID_SAMPLE', 'UNKNOWN_SUBSTANCE'],
    'sample.place': ['PLANT_REQUIRED', 'UNKNOWN_SAMPLE', 'SAMPLE_NOT_ON_BENCH', 'STAGE_OCCUPIED', 'MEASUREMENT_RUNNING', 'DOOR_CLOSED', 'MOTION_IN_PROGRESS',
                     'NOT_ACCESSIBLE', 'COLLISION_RISK'],
    'sample.remove': ['PLANT_REQUIRED', 'SAMPLE_NOT_ON_STAGE', 'MEASUREMENT_RUNNING', 'DOOR_CLOSED', 'MOTION_IN_PROGRESS', 'NOT_ACCESSIBLE'],
    'power.set': ['PLANT_REQUIRED', 'MAINS_OUT_OF_RANGE'], 'power.press_button': ['PLANT_REQUIRED', 'MAINS_OUT_OF_RANGE'],
    'emergency.stop': ['PLANT_REQUIRED'], 'emergency.release': ['PLANT_REQUIRED'], 'system.acknowledge': ['PLANT_REQUIRED', 'POWER_OFF', 'CAUSE_PERSISTS'],
    'fault.inject': ['PLANT_REQUIRED'], 'door.open': _RE + ['MEASUREMENT_RUNNING', 'XRAY_ON'], 'door.close': _RE + ['LIGHT_CURTAIN_BLOCKED'],
    'xray.set': ['PLANT_REQUIRED', 'POWER_LIMIT', 'MEASUREMENT_RUNNING'],
    'xray.enable': _RE + ['DOOR_OPEN', 'INTERLOCK_OPEN', 'XRAY_ARC', 'MEASUREMENT_RUNNING'], 'filter.select': _RE + ['MEASUREMENT_RUNNING'],
    'stage.move': _RE + ['STAGE_FAULT', 'MEASUREMENT_RUNNING', 'COLLISION_RISK'], 'stage.go_to': _RE + ['STAGE_FAULT', 'MEASUREMENT_RUNNING', 'COLLISION_RISK'],
    'stage.rotate': _RE + ['STAGE_FAULT', 'MEASUREMENT_RUNNING'], 'scan.configure': ['PLANT_REQUIRED', 'MEASUREMENT_RUNNING'],
    'calibration.flat_field': _ACQ_ERR + ['SAMPLE_IN_BEAM'] + _ACQ_FAIL,
    'calibration.geometry': _ACQ_ERR + ['NO_SAMPLE', 'FLAT_FIELD_REQUIRED', 'PHANTOM_REQUIRED', 'PHANTOM_NOT_IN_FIELD'] + _ACQ_FAIL,
    'xray.radiograph': _ACQ_ERR + ['FLAT_FIELD_REQUIRED', 'ROI_OUTSIDE_DETECTOR'] + _ACQ_FAIL,
    'scan.start': _ACQ_ERR + ['NO_SAMPLE', 'FLAT_FIELD_REQUIRED', 'SAMPLE_NOT_IN_FIELD'] + _ACQ_FAIL,
    'recon.start': ['PLANT_REQUIRED', 'UNKNOWN_RECORD', 'RECORD_NOT_SUITABLE', 'UNKNOWN_SUBSTANCE', 'RECON_BUSY', 'DATA_NOT_CACHED', 'INVALID_VOI', 'RECORDS_FULL',
                    'DATA_EVICTED', 'USER_CANCEL'],
    'records.delete': ['PLANT_REQUIRED', 'UNKNOWN_RECORD', 'RECORD_IN_USE'],
    'measurement.result': ['UNKNOWN_RECORD'], 'data.porosity': ['UNKNOWN_RECORD', 'RECORD_NOT_SUITABLE', 'INVALID_REGION'],
    'data.cracks': ['UNKNOWN_RECORD', 'RECORD_NOT_SUITABLE', 'INVALID_REGION'], 'data.attenuation': ['UNKNOWN_RECORD', 'RECORD_NOT_SUITABLE', 'INVALID_REGION'],
    'data.slice': ['UNKNOWN_RECORD', 'RECORD_NOT_SUITABLE', 'OUT_OF_VOLUME'], 'data.edge_resolution': ['UNKNOWN_RECORD', 'RECORD_NOT_SUITABLE'],
}
_PARTIAL = {'USER_CANCEL', 'POWER_OFF', 'EMERGENCY_STOP', 'XRAY_ARC', 'INTERLOCK_FAULT', 'DATA_EVICTED'}
CAP_ERROR_ZH = {('scan.start', 'POWER_OFF'): '未通电;或扫描中断电(记录以failed结束,不保留部分投影)',
                ('scan.start', 'STAGE_FAULT'): '旋转台故障;或扫描中旋转台卡滞(记录以failed结束)',
                ('scan.start', 'XRAY_ARC'): '扫描中X射线管打火:X射线关闭,记录以failed结束',
                ('recon.start', 'DATA_EVICTED'): '重建完成前投影数据已从缓存清除:记录以failed结束'}
MODEL_NONE_ZH = {
    'plant.configure': '实验室条件没有可见模型', 'sample.provide': '样品在实验台上,场景中没有样品几何', 'sample.place': '样品在场景中没有几何(运行层状态)',
    'sample.remove': '样品在场景中没有几何', 'fault.inject': '注入故障本身没有可见变化', 'system.acknowledge': '只清除错误状态(状态灯随之变化)',
    'xray.set': '只写设定(束流开启时电压电流读数随之变化)', 'scan.configure': '只写设置', 'recon.start': '工作站计算,仪器没有可见变化',
    'emergency.release': '只释放锁存(错误确认后状态灯变化)', 'records.delete': '只删除工作站上的记录,仪器与显示没有变化',
}
SCENE_MIRRORED = {'door.position_m', 'filter.slide_m', 'stage.sod_m', 'stage.chuck_z_m', 'stage.rotation_rad', 'xray.voltage_v', 'xray.current_a',
                  'measurement.remaining_s', 'result.last_porosity', 'panel.status_green', 'panel.status_orange', 'panel.status_red'}


def _model_effects(k, un):
    if un:
        return dict(mode='none', description_zh='不支持:不实现,没有模型变化', binding_ids=[])
    seen = sorted((set(EFFECTS.get(k, [])) | set(ON_STEP.get(k, []))) & SCENE_MIRRORED)
    if seen:
        return dict(mode='host_required', binding_ids=[],
                    description_zh='状态经宿主桥(Matterix)镜像到场景关节、触摸屏读数或状态灯(' + '、'.join(seen) + ';绑定见合同model_bindings),运行层本身不驱动模型'
                                   + (';' + MODEL_NONE_ZH[k] if k in MODEL_NONE_ZH else ''))
    return dict(mode='none', description_zh=MODEL_NONE_ZH.get(k, '没有镜像到场景的状态变化'), binding_ids=[])


EXEC_TEXT = {   # asynchronous capabilities: completion criterion, partial failure
    'calibration.flat_field': ('暗场与亮场各20帧(每帧曝光+0.03 s读出)完成后写入校正与记录', '取消或失败(断电、安全按钮、打火、联锁)时不写入校正,旧校正保持;没有运动。'),
    'calibration.geometry': ('旋转一周36个投影完成后拟合钢球轨迹并写入旋转轴偏移', '取消或失败(断电、安全按钮、打火、联锁、旋转台卡滞)时不写入标定;旋转台停在当时角度。'),
    'xray.radiograph': ('曝光×帧平均完成后写入透视记录', '取消或失败时不写入图像;没有运动。'),
    'scan.start': ('按投影时间旋转完扫描范围后写入投影记录(投影在完成时计算)', '取消或失败(断电、安全按钮、打火、联锁、旋转台卡滞)时记录以cancelled/failed结束,不保留部分投影;旋转台停在当时角度。'),
    'recon.start': ('按工作站重建时间完成后计算体数据并写入记录', '取消时不写入体数据;源投影记录不变;投影数据已被清除时以failed结束。'),
}
RESOURCES = {'calibration.flat_field': ['xray_source', 'detector', 'host_clock'], 'calibration.geometry': ['xray_source', 'detector', 'rotation_stage', 'host_clock'],
             'xray.radiograph': ['xray_source', 'detector', 'host_clock'], 'scan.start': ['xray_source', 'detector', 'rotation_stage', 'host_clock'],
             'recon.start': ['workstation', 'host_clock'], 'door.open': ['door'], 'door.close': ['door'], 'xray.set': ['xray_source'], 'xray.enable': ['xray_source'],
             'filter.select': ['filter_changer'], 'stage.move': ['sample_carriage', 'sample_lift'], 'stage.go_to': ['sample_carriage', 'sample_lift'],
             'stage.rotate': ['rotation_stage'], 'power.set': ['mains'], 'power.press_button': ['mains'], 'emergency.stop': ['safety_circuit'],
             'emergency.release': ['safety_circuit'], 'plant.configure': ['room', 'mains'], 'sample.provide': ['bench'], 'sample.place': ['bench', 'sample_chuck'],
             'sample.remove': ['bench', 'sample_chuck'], 'fault.inject': ['evaluator'], 'sim.step': ['host_clock'], 'system.acknowledge': ['controller'],
             'scan.configure': ['controller'], 'records.delete': ['workstation']}
OBSERVES = {
    'power.set': ['power.enabled', 'system.state', 'panel.status_green'], 'power.press_button': ['power.enabled', 'system.state', 'panel.status_green'],
    'emergency.stop': ['emergency.stopped', 'panel.status_red'], 'emergency.release': ['emergency.stopped'], 'system.acknowledge': ['system.error_code'],
    'door.open': ['door.position_m', 'door.closed'], 'door.close': ['door.position_m', 'door.closed', 'light_curtain.blocked'],
    'xray.set': ['xray.voltage_set_v', 'xray.current_set_a', 'xray.focal_spot_m'], 'xray.enable': ['xray.on', 'xray.ready', 'xray.warmup_remaining_s', 'panel.status_orange'],
    'filter.select': ['filter.position', 'filter.label'], 'stage.move': ['stage.sod_m', 'stage.chuck_z_m', 'geometry.magnification', 'geometry.voxel_size_m'],
    'stage.go_to': ['stage.position_label', 'stage.sod_m'], 'stage.rotate': ['stage.rotation_rad'], 'scan.configure': ['scan.settings', 'scan.projections', 'scan.duration_s'],
    'calibration.flat_field': ['measurement.id', 'calibration.flat_field'], 'calibration.geometry': ['measurement.id', 'calibration.geometry'],
    'xray.radiograph': ['measurement.id', 'result.last_record_id'], 'scan.start': ['measurement.id', 'measurement.progress', 'result.last_record_id'],
    'recon.start': ['recon.id', 'recon.remaining_s', 'result.last_record_id'], 'data.porosity': ['result.last_porosity'],
    'sample.place': ['sample.on_stage'], 'sample.remove': ['sample.bench_ids'], 'records.delete': ['result.last_record_id', 'result.last_porosity'],
}
_SPEC = 'manufacturer_primary'
_BRO = 'manufacturer_brochures'
_BRL = 'manufacturer_brochures'
_CFG = 'manufacturer_configurator'
_PHOTO = 'product_photos'
_NIST = 'nist_srd126'
_PUB = 'pubchem_densities'
_LIBS = 'xray_attenuation_library'
_PHYS = 'physics_literature'
_SIM = 'simulation_assumption'
_WB = 'user_workbook'
CAP_SOURCES = {
    'plant.configure': [_SPEC, _SIM], 'sample.provide': [_LIBS, _NIST, _PUB, _WB, _SPEC], 'sample.place': [_SPEC, _BRO, _SIM], 'sample.remove': [_SIM],
    'power.set': [_SPEC, _SIM], 'power.press_button': [_SPEC, _PHOTO, _SIM], 'emergency.stop': [_BRO, _PHOTO, _SIM], 'emergency.release': [_SIM],
    'system.acknowledge': [_SIM], 'fault.inject': [_SIM], 'door.open': [_BRO, _PHOTO, _SIM], 'door.close': [_BRO, _SIM], 'xray.set': [_SPEC, _BRO, _WB],
    'xray.enable': [_SPEC, _BRO, _SIM, _PHYS], 'filter.select': [_BRO, _CFG, _SIM], 'stage.move': [_BRO, _SPEC, _SIM], 'stage.go_to': [_SIM],
    'stage.rotate': [_BRO, _CFG, _SIM], 'scan.configure': [_SPEC, _WB, _SIM], 'calibration.flat_field': [_SPEC, _SIM, _PHYS],
    'calibration.geometry': [_BRO, _SIM, _PHYS], 'xray.radiograph': [_SPEC, _WB, _LIBS, _NIST, _PHYS], 'scan.start': [_SPEC, _WB, _BRO, _LIBS, _NIST, _PHYS, _SIM],
    'recon.start': [_BRO, _WB, _PHYS], 'measurement.result': [_WB], 'data.porosity': [_WB, _PHYS], 'data.cracks': [_WB, _PHYS], 'data.attenuation': [_PHYS],
    'data.slice': [_WB], 'data.edge_resolution': [_BRO, _CFG, _PHYS], 'records.list': [_SIM], 'records.delete': [_SIM], 'events.read': [_SIM],
    'ledger.read': [_SIM, _PHYS, _NIST],
    'provenance.read': [_LIBS, _NIST, _PUB], 'sim.step': [_SIM], **{k: [_SPEC, _BRO, _CFG] for k in UNSUPPORTED}}
PARAM_ZH = {'room_k': '实验室室温(记录用;X射线计算的空气密度取NIST表列值,与室温无关)', 'mains_voltage_v': '供电电压(厂家100–240 VAC)',
            'relative_humidity': '相对湿度(记录用;不影响X射线计算)', 'noise_seed': '噪声种子(首次声明后固定)'}
_DEFAULT_LIMIT = '过程层实现;机器人与硬件层未实现;整体模型边界见mechanisms.x4_poseidon_ct.limitations。'
CAP_LIMITS = {
    'sample.provide': ['样品为解析体模:竖直圆柱(最多6层,每层为模板库材料的体积混合,各组分按表列密度)、球形孔(对数正态半径、互不重叠、在指定层内)、平面圆盘裂纹、球形夹杂、'
                       '样品管与样品针;另有钢球几何标定体模与钨刃边体模。多孔材料的孔径小于模拟体素时看不到;样品在场景中没有几何。'],
    'sample.place': ['宿主/机器人抽象:门完全打开、样品夹在门口范围时装样;样品轴相对旋转轴的偏移≤5 mm;机器人接触与夹持未建模。'],
    'xray.set': ['管电压20–100 kV(下限为工程值)、电流≤0.5 mA(工程值)且功率≤20 W(厂家);焦点=5 µm×max(1, P/4 W)(厂家给出4 W时<5 µm,增长规律为工程假设)。'],
    'xray.enable': ['每次上电后首次开启运行180 s预热程序(工程值),之后电压10 kV/s、电流100 µA/s斜坡(工程值);门、联锁传感器与安全按钮任一不满足即立即关闭。'
                    '钨反射靶谱:Kramers厚靶轫致辐射(效率K·Z·V,K=1.1e-9 /V)、Kanaya-Okayama射程1/3深度按20°出射角的自吸收、W K与L线(能量份额为工程值)、'
                    '0.25 mm铍窗;衰减系数来自NIST SRD 126模板库。'],
    'filter.select': ['7位滤片转换器为厂家标准配置;各位置的滤片(无、Al 0.25/0.5/1.0 mm、Cu 0.1/0.25 mm、Al 1.0 mm+Cu 0.1 mm)为工程选择。'],
    'stage.move': ['源-探距固定0.450 m、源-物距0.015–0.395 m、光束高度0.400 m、样品夹高度0.160–0.399 m与速度为工程值(厂家未公布);碰撞保护:样品本体、样品针、样品夹与旋转台外壳(各为绕旋转轴的竖直圆柱)在高度重叠处不得进入源头部/滤片滑块与探测器外壳的禁入区,并低于腔顶;目标位置与路径都检查(滑台20 mm/s与升降10 mm/s同时启动,路径为(源-物距,高度)平面上的两段直线),路径穿过禁入区时拒绝(COLLISION_RISK),须分两步移动;'
                   '300 mm高的样品放得进样品腔(腔顶0.640 m),升降行程加低放大视场可覆盖到距其底部约290 mm。'],
    'door.open': ['门的运动方向(向左滑入前面板后)为照片估计,行程0.40 m、速度0.1 m/s为工程值;X射线开启时门锁住。'],
    'door.close': ['安全光幕被挡时门不能关闭,关闭中被挡则停止并重新打开(工程逻辑,按厂家“安全光幕”功能)。'],
    'scan.configure': ['扫描范围180°(+锥角,Parker加权)或360°;步长0.05–2°;曝光0.02–10 s;帧平均1–16;合并1×1/2×2/4×4;随机移动(探测器每投影随机平移±4模拟列)。'],
    'calibration.flat_field': ['暗场与亮场各20帧(工程值);校正对应电压、电流、滤片、合并与曝光,任一改变即失效;亮场噪声使固定图案残留(环状伪影来源之一);'
                               '亮场饱和时亮区透过率在满量程截止;单个合并像素亮场相对误差超过10 %时校正无效(信号不足);像素增益不均由亮场除去,不单独模拟。'],
    'calibration.geometry': ['钢球体模36投影360°扫描;按精确锥束轨迹u=SDD(a+r sin(β+φ))/(SOD+r cos(β+φ))联合最小二乘求旋转轴横向偏移a;源-物距与源-探距视为已知;'
                             '体模须在所有角度完整位于视场内(否则拒绝);拟合残差超过2个窗口像素(旋转轴处,至少5 µm)或识别的钢球少于7个时不采用。'],
    'xray.radiograph': ['整幅为175×150模拟像素(每模拟像素16×16原生像素);局部区域以合并后原生像素成像(≤256×256,须在探测器有效面内);平场校正后的固定图案残留按窗口像素计。'],
    'scan.start': ['采集窗口=样品本体在所有角度的投影包络(按名义几何、已标定的轴偏移,加6 %及未知轴偏移的余量),最多175×150模拟像素且不细于一个合并后的原生像素;'
                   '投影最多模拟200个(均匀子集),光子数按名义投影数与帧数剂量等效合并;连续旋转近似步进采集;'
                   '能量分24组(强制K边分界);复合泊松量子噪声、读出噪声、16位饱和、焦点半影与闪烁体模糊;未建模散射、相位衬度、探测器余辉、射线管漂移与热漂移。'],
    'recon.start': ['FDK滤波反投影(Ram-Lak/Shepp-Logan/Hann),180°+锥角扫描用Parker权重;束硬化线性化:按指定材料与扫描能谱的多色衰减曲线把线积分映射为μ(E_eff)·L'
                    '(E_eff=未衰减束的探测能量加权平均能量);环状伪影校正=减去投影平均列剖面的高频部分;自动后对准=中心平面共轭射线一致性(±200 µm内5 µm网格加抛物线顶点),'
                    '中心平面(光束高度)没有样品数据时不执行并给出警告;手动后对准为给定值,否则用几何标定值;180°+锥角短扫描的Parker权重按本旋转方向的共轭射线(β+π−2γ)在滤波前加权;'
                    '投影数据须仍在缓存中(只保留最近8组投影/透视);感兴趣体积的中心须在采集窗口可重建的区域内(超出部分给出警告);重建网格48–128(模拟体素远大于名义体素);'
                    '工作站重建时间按名义体素×投影数/5e10每秒估计(工程值)。'],
    'data.porosity': ['包络=Otsu阈值实体区逐层与三维填孔后腐蚀1体素;孔阈值默认ISO50(空气与基体中值的中点);26连通;开孔=接触包络边界;等效直径=(6V/π)^(1/3);小于约2体素的孔无法分辨。'],
    'data.cracks': ['低衰减连通体主成分分析:厚/长<0.3且宽/长>0.35判为裂纹;面积=π·R1·R2(R=2√λ)、开口=体积/面积、长度=2R1;开口小于体素时部分体积使对比度下降,可能检测不到或开口偏大。'],
    'data.edge_resolution': ['刃边法:逐行找刃边位置并直线拟合,按到刃边距离的0.25像素分箱平均得刃边扩展函数,10–90 %宽度按探测器像素与放大换算到样品平面。'],
    'ledger.read': ['能量流在旋转2π/32、滑台与升降0.25 mm、电压1 kV、滤片位置(按滑块经过的位置)的格点中心计算(每个格内恒定,跨格时步进在格边界切分),电压与电流的斜坡与'
                    '预热按V²·I精确积分,账本与宿主步长无关;去向:靶(向靶内一侧的半球全部、出射半球沿出射路径的自吸收)、铍窗与滤片(有用发射锥内)、空气、样品本体吸收(μen/μ;'
                    '剂量=样品本体吸收÷本体质量)、样品架与台架吸收(样品针、样品管、样品夹、旋转台)、散射、探测器沉积/穿透、屏蔽(出射半球其余部分与锥内探测器以外的射线),残差为舍入误差;'
                    '发射锥(半角20°)内的射线全部计入(样品伸出探测器视野的部分同样受照):锥用格子铺满(探测器上35×30格、其外约8 mm格,格不跨探测器边缘),'
                    '与样品本体(含样品管)在该格转角下的投影框相交的格沿框边切开,框内按细步距再分(框上约1000条射线,任一方向至少16条),各去向都是正的射线项之和;'
                    '每条射线按穿过的先后吸收:样品台壳体、样品夹与样品针(位于样品下方:向上的射线先经过,向下的射线最后经过)、样品管在管内之前的壁、样品本体、管内之后的壁(从开口端离开的射线没有这一段)。'],
    'records.delete': ['删除记录及其缓存数据;进行中的操作或正被重建的投影记录不能删除;agent只能删除自己或宿主创建的记录;删除后该记录的结果不再可查。'],
    'measurement.result': ['记录中的样品剂量dose_gy取决于样品真实质量与组成,只对评测者可见(agent为null)。'],
}
LIMITATIONS = [
    '选定配置:X4 POSEIDON基本配置(反射式X射线源、7 MP平板探测器、标准旋转台、单探测器支架、固定扫描几何、7位滤片转换器);透射源、sCMOS、Multi-Vision、Best-Scan-Geometry、'
    '样品转换器、原位试验台与高级扫描模式为不支持的选件。',
    '源-探距、源-物距范围、光束高度、像素间距、闪烁体、增益与噪声、斜坡与预热、门与各轴的速度、滤片组成为工程假设(厂家未公布);电压、功率、焦点、探测器像素数、样品尺寸、'
    '外形与质量为厂家值。',
    'X射线物理:NIST SRD 126衰减系数(模板库145种物质)、多组能量Beer-Lambert、闪烁体能量沉积与复合泊松噪声;未建模康普顿散射到探测器、相位衬度、射线管漂移、探测器余辉。',
    '模拟网格:探测器175×150、投影≤200、重建网格≤128,名义体素只在记录中给出;分辨率与噪声按剂量等效合并近似。',
    '台架:黄铜样品夹按铜(8.96 g/cm³)、旋转台壳体与转盘按等面积铝圆柱(半径45.1 mm、高50 mm)参与衰减;室温与湿度只作记录,空气密度取NIST表列值。',
    '上电后各轴保持断电时的位置(绝对位置,工程假设;厂家未说明回原点行为)。',
    '没有真实X4 POSEIDON硬件与3DxSUITE软件接口;机器人装样没有接触验证。',
    '前面板层(1.1.0,近似):厂家资料只给出触摸屏与多功能状态灯的存在和产品图(标题栏、两个标签页、电压kV与电流µA读数条);面板按产品图画出状态显示,'
    '屏上不含操作控件(扫描、门与X射线由3DxSUITE或能力调用控制);电源按钮与安全按钮作为面板输入。',
]

# ---------------------------------------------------------------- contract metadata of the panel commands
_PANEL_USES = _panel_ui.spec_capabilities(PANEL_SPEC)
for _k in PANEL_GESTURES:
    EFFECTS[_k] = list(dict.fromkeys(x for c in _PANEL_USES for x in EFFECTS.get(c, [])))      # a gesture changes what the commands it calls change
    ON_STEP[_k] = list(dict.fromkeys(x for c in _PANEL_USES for x in ON_STEP.get(c, [])))
    ON_STEP_ZH[_k] = '同手势调用的能力(启动、X射线关闭、错误锁存等按其规则在之后的step中演化)'
PRECONDITIONS.update({**{k: ('已声明实验室条件;面板按panel_ui.json处理手势:断电时只有电源按钮与安全按钮起作用;被调用能力的前提同样适用(被拒时手势不被接受)',
                             ['power.enabled', 'system.state', 'emergency.stopped']) for k in PANEL_GESTURES},
                      'panel.capture': ('已声明实验室条件(断电时为暗屏)', ['power.enabled']), 'panel.frame': ('无', [])})
CAP_ERRORS.update({**{k: ['PLANT_REQUIRED'] for k in PANEL_GESTURES}, 'panel.capture': ['PLANT_REQUIRED'], 'panel.frame': []})
CAP_SOURCES.update({k: [_BRO, _PHOTO, _SIM] for k in PANEL_CAPS})
RESOURCES.update({k: ['touchscreen'] for k in PANEL_CAPS})
OBSERVES.update({k: ['power.enabled', 'system.state', 'emergency.stopped', 'system.error_code'] for k in PANEL_GESTURES})
EXEC_TEXT.update({k: ('手势瞬时完成(不推进仿真时钟):被调用能力的效果立即生效;结果只说明是否被仪器接受,画面须用panel.capture查看',
                      '同步:被调用的能力被拒时其效果不发生') for k in PANEL_GESTURES})
CAP_LIMITS.update({k: ['面板层按panel_ui.json(近似:厂家资料只有产品图与特性列表)解释手势并调用本运行层的同名能力;手势不推进仿真时钟;屏上标签页名称与布局为假设。']
                   for k in PANEL_CAPS})


def _sampling_text(k):
    if k in ('door.position_m', 'filter.slide_m', 'stage.sod_m', 'stage.chuck_z_m', 'stage.rotation_rad', 'xray.voltage_v', 'xray.current_a', 'xray.power_w',
             'clock.time_s', 'measurement.progress', 'measurement.remaining_s', 'recon.remaining_s', 'xray.warmup_remaining_s'):
        return 'Instantaneous value at the read time: constant-speed motions, voltage / current ramps and operation phases are integrated exactly and cut at every event.'
    return 'Changes when a command is accepted or at an event of the operation (phase ends, record completion); no other time dynamics.'


def _errors_of(k):
    if k in UNSUPPORTED:
        return ['UNSUPPORTED'] + _COMMON_ERRORS
    extra = ['ACCESS_DENIED'] if (k in _CONTROL_GUARDED or k in _DATA_GUARDED or k in _ANALYSIS_RECORD_ARGS or ALL_CAPS[k]['visibility'] != 'agent') else []
    return list(dict.fromkeys(CAP_ERRORS.get(k, []) + _COMMON_ERRORS + extra))


def _foreign_text(k):
    if k in _SAFETY:
        return ';安全按钮(按下与释放)不受另一个agent的操作限制'
    return ';另一个agent的采集操作进行中时,agent调用被拒(ACCESS_DENIED;未通电时开机除外)' if k in _CONTROL_GUARDED else ''


def export_bruker_x4poseidon_contract_metadata():
    slots, caps = {}, {}
    for k, v in SLOTS.items():
        slots[k] = dict(meaning_zh=v['meaning'], component_ids=[v['component']], value_schema=copy.deepcopy(v['schema']), unit=v['unit'],
                        reference_conditions=v['reference'], origin=v['origin'], visibility=v['visibility'], access='read_only',
                        read_binding=dict(backend='host', artifact_id='runtime', accessor=f'BrukerX4PoseidonSession.read(state_ids=[{k!r}])'), write_via=None,
                        sampling=dict(mode='instant', time_domain='simulation', window_description=_sampling_text(k)), source_ids=['simulation_assumption'])
    for k, v in ALL_CAPS.items():
        un = k in UNSUPPORTED
        async_ = k in ASYNC
        c = {key: copy.deepcopy(v[key]) for key in ['input_schema', 'output_schema', 'component_ids', 'function_zh', 'kind', 'visibility', 'example_arguments']}
        c.update(availability='unsupported' if un else 'implemented',
                 implementation={layer: 'absent' if un or layer in ('scene', 'robot', 'hardware') else 'implemented' for layer in ['process', 'scene', 'robot', 'hardware', 'host']},
                 binding=None if un else dict(backend='host', artifact_id='runtime', entrypoint='asset_runtime.bruker_x4poseidon.BrukerX4PoseidonSession.invoke',
                                              invocation=f'session.invoke(instance_id="x4_poseidon_01",capability_id={k!r},arguments={v["example_arguments"]!r},request_id="unique")'),
                 preconditions=[] if un or k not in PRECONDITIONS else [dict(description_zh=PRECONDITIONS[k][0] + _foreign_text(k) + ';被拒时状态不变。',
                                                                              state_ids=list(PRECONDITIONS[k][1]) + (['measurement.started_by_other_agent'] if k in _CONTROL_GUARDED else []),
                                                                              enforced_by='runtime', check='BrukerX4PoseidonRuntime.invoke / BrukerX4PoseidonSession._guard')],
                 state_effects=[dict(state_id=s, timing='on_step' if k == 'sim.step' else 'immediate',
                                     description_zh='设定、目标、状态标志或立即改变的派生量;位置、斜坡与操作进度只随step推进') for s in EFFECTS.get(k, [])],
                 observes=[] if un else list(OBSERVES.get(k, [])),
                 model_effects=_model_effects(k, un),
                 execution=dict(mode='asynchronous' if async_ else 'synchronous', time_domain='simulation',
                                completion_criterion=EXEC_TEXT[k][0] if k in EXEC_TEXT else '原子设定、配置、分析或查询;运动由step推进',
                                completion_state_ids=(['measurement.id', 'measurement.progress', 'result.last_record_id'] if k in ACQUISITION
                                                      else ['recon.id', 'result.last_record_id'] if k == 'recon.start' else []),
                                timeout_s=None, cancellation='supported' if async_ else 'unsupported', retry='same_request_id', resources=list(RESOURCES.get(k, [])),
                                partial_failure_zh=(EXEC_TEXT[k][1] if k in EXEC_TEXT else
                                                    ('不支持:不执行,状态不变' if un else '同步原子操作:成功时一次写入全部效果(运动目标),被拒时状态不变'))),
                 errors=[dict(code=e, meaning_zh=CAP_ERROR_ZH.get((k, e), ERROR_ZH.get(e, e)), side_effects='partial' if async_ and e in _PARTIAL else 'none') for e in _errors_of(k)],
                 verification=[], limitations=[UNSUPPORTED[k]] if un else list(CAP_LIMITS.get(k, [_DEFAULT_LIMIT])), source_ids=list(CAP_SOURCES[k]))
        if k in ON_STEP:
            c['state_effects'] += [dict(state_id=s, timing='on_step', description_zh=ON_STEP_ZH.get(k, '随step演化')) for s in ON_STEP[k]
                                   if not (k == 'sim.step' and s in EFFECTS.get(k, []))]
        caps[k] = c
    params = {}
    for name, (val, unit, meaning, src) in {
            'xray_kv_max_v': (XRAY_KV_MAX_V, 'V', '反射源最高100 kV(厂家、调研表)', [_SPEC, _BRO, _WB]),
            'xray_power_max_w': (XRAY_POWER_MAX_W, 'W', '反射源20 W(厂家、调研表)', [_SPEC, _BRO, _WB]),
            'spot_at_4w_m': (SPOT_AT_4W_M, 'm', '反射源焦点<5 µm@4 W(厂家、调研表;取上限)', [_SPEC, _WB]),
            'detector_pixels': (list(DETECTOR_PIXELS), '1', '7 MP平板探测器2800×2400像素(厂家、调研表)', [_SPEC, _WB, _CFG]),
            'sample_max_diameter_m': (SAMPLE_MAX_DIAMETER_M, 'm', '样品直径最大110 mm(厂家、调研表)', [_SPEC, _WB]),
            'sample_max_height_m': (SAMPLE_MAX_HEIGHT_M, 'm', '样品高度最大300 mm(厂家、调研表)', [_SPEC, _WB]),
            'filter_positions': (FILTER_POSITIONS, '1', '自动7位滤片转换器为标准配置(厂家配置器)', [_CFG]),
            'detail_detectability_m': (DETAIL_DETECTABILITY_M, 'm', '反射源细节可检测度低至5 µm(厂家配置器)', [_CFG]),
            'body_dimensions_m': (list(BODY_DIMENSIONS_M), 'm', '外形W 1250×D 604×H 690 mm(厂家规格表、调研表)', [_SPEC, _WB]),
            'mass_kg': (MASS_KG, 'kg', '重量350 kg(厂家、调研表)', [_SPEC, _BRO, _WB]),
            'mains_voltage_v': (list(MAINS_V), 'V', '电源100–240 VAC 50–60 Hz(厂家)', [_SPEC]), 'mains_current_max_a': (MAINS_A_MAX, 'A', '最大3 A(厂家)', [_SPEC])}.items():
        params[name] = dict(value=val, unit=unit, meaning_zh=meaning, evidence='documented', source_ids=list(src), uncertainty=None)
    for name, (val, unit, meaning) in {
            'xray_kv_min_v': (XRAY_KV_MIN_V, 'V', '管电压下限'), 'xray_current_max_a': (XRAY_CURRENT_MAX_A, 'A', '管电流上限(另受20 W限制)'),
            'kv_ramp_v_s': (KV_RAMP_V_S, 'V/s', '电压斜坡'), 'current_ramp_a_s': (CURRENT_RAMP_A_S, 'A/s', '电流斜坡'), 'warmup_s': (WARMUP_S, 's', '上电后首次开启的预热程序'),
            'spot_growth_exp': (SPOT_GROWTH_EXP, '1', '焦点随功率增长的指数(4 W以上)'), 'takeoff_rad': (TAKEOFF_RAD, 'rad', '反射靶出射角'),
            'be_window_m': (BE_WINDOW_M, 'm', '铍窗厚度'), 'kramers_k_per_v': (KRAMERS_K_PER_V, '1/V', 'Kramers效率常数(η=K·Z·V;文献值)'),
            'k_line_fraction_100kv': (K_LINE_FRACTION_100KV, '1', '100 kV时W K线能量份额'), 'l_line_fraction_50kv': (L_LINE_FRACTION_50KV, '1', '50 kV时W L线能量份额'),
            'cone_half_angle_rad': (CONE_HALF_ANGLE_RAD, 'rad', '源发射锥半角(账本中屏蔽份额)'), 'sdd_m': (SDD_M, 'm', '源-探距(固定扫描几何)'),
            'sod_range_m': (list(SOD_RANGE_M), 'm', '源-物距范围'), 'chuck_z_range_m': (list(CHUCK_Z_RANGE_M), 'm', '样品夹高度范围'),
            'source_xyz_m': (list(SOURCE_XYZ_M), 'm', '焦点在资产坐标中的位置(光束沿+X)'), 'filter_clear_m': (FILTER_CLEAR_M, 'm', '滤片转换器前表面到焦点距离'),
            'detector_cover_m': (DETECTOR_COVER_M, 'm', '探测器盖板到有效面距离'), 'chamber_ceiling_m': (CHAMBER_CEILING_M, 'm', '腔顶高度'),
            'source_keepout_dz_m': (list(SOURCE_KEEPOUT_DZ), 'm', '源头部与滤片滑块禁入区相对光束的高度范围'),
            'detector_keepout_dz_m': (list(DETECTOR_KEEPOUT_DZ), 'm', '探测器外壳禁入区相对光束的高度范围'),
            'chuck_r_m': (CHUCK_R_M, 'm', '样品夹半径'), 'stage_r_m': (STAGE_R_M, 'm', '旋转台壳体外接圆半径(保守包络)'),
            'pixel_pitch_m': (PIXEL_PITCH_M, 'm', '平板像素间距'), 'csi_thickness_m': (CSI_THICKNESS_M, 'm', 'CsI:Tl闪烁体厚度'),
            'adu_per_j': (ADU_PER_KEV / KEV, '1/J', '闪烁体沉积能量到ADU的转换增益(0.8 ADU/keV)'), 'read_noise_adu': (READ_NOISE_ADU, '1', '读出噪声(ADU)'),
            'dark_offset_adu': (DARK_OFFSET_ADU, '1', '暗电平(ADU)'), 'dark_current_adu_s': (DARK_CURRENT_ADU_S, '1/s', '暗电流(ADU/s)'),
            'flat_max_sigma': (FLAT_MAX_SIGMA, '1', '可用亮场的合并像素相对误差上限'), 'detector_psf_px': (DETECTOR_PSF_PX, '1', '闪烁体光扩散σ(原生像素)'),
            'step_overhead_s': (STEP_OVERHEAD_S, 's', '每投影旋转与稳定时间'), 'frame_overhead_s': (FRAME_OVERHEAD_S, 's', '每帧读出'),
            'rotation_speed_rad_s': (ROTATION_SPEED_RAD_S, 'rad/s', '旋转台定位速度'), 'carriage_speed_m_s': (CARRIAGE_SPEED_M_S, 'm/s', '样品滑台速度'),
            'lift_speed_m_s': (LIFT_SPEED_M_S, 'm/s', '升降速度'), 'door_travel_m': (DOOR_TRAVEL_M, 'm', '门行程'), 'door_speed_m_s': (DOOR_SPEED_M_S, 'm/s', '门速度'),
            'filter_s_per_step': (FILTER_S_PER_STEP, 's', '滤片每档时间'), 'boot_s': (BOOT_S, 's', '启动与回原点时间'),
            'flat_field_frames': (FLAT_FIELD_FRAMES, '1', '暗场/亮场帧数'), 'axis_offset_sd_m': (AXIS_OFFSET_SD_M, 'm', '隐藏旋转轴横向偏移(1σ)'),
            'recon_rate_voxel_proj_s': (RECON_RATE_VOXEL_PROJ_S, '1/s', '工作站重建吞吐(体素×投影/秒)'), 'sim_detector_px': (list(SIM_DETECTOR), '1', '模拟探测器网格'),
            'sim_max_projections': (SIM_MAX_PROJECTIONS, '1', '模拟投影数上限'), 'energy_groups': (ENERGY_GROUPS, '1', '能量分组数')}.items():
        params[name] = dict(value=val, unit=unit, meaning_zh=meaning, evidence='assumption', source_ids=[_SIM], uncertainty='工程值')
    mechanism = dict(
        component_ids=['source_housing', 'source_head', 'source_window', 'filter_changer', 'filter_slide', 'sample_carriage', 'sample_lift', 'sample_turntable',
                       'sample_chuck', 'detector_housing', 'detector_window', 'door_glass', 'button_emergency_stop', 'enclosure_base', 'workstation_monitor'],
        state_ids=list(slots), implementation='lumped',
        meaning_zh='X4 POSEIDON台式显微CT:钨反射源谱、滤片、锥束几何与样品旋转、平板探测器响应与噪声、暗场/亮场与几何标定、FDK重建、孔隙率与裂纹分析、安全联锁与能量账',
        equations=['Kramers: I(E) dE ~ Z i (E0 - E) dE, eta = K Z V (K = 1.1e-9 /V); self-absorption exp(-mu_W(E) R_KO(E0) / (3 sin(takeoff)))',
                   'beam: N(E) = N0(E) exp(-mu_Be t_Be - sum_f mu_f t_f - mu_air SDD / cos(alpha)); omega_pixel = A cos^3(alpha) / SDD^2',
                   'transmission: T_g = exp(-sum_m mu_m,g L_m) (energy groups, mu weighted by the detected energy); L_m exact chord lengths of the phantom',
                   'detector: E_dep(E) = E (1 - exp(-mu_CsI t)) mu_en/mu; S = G sum_g n_g T_g <E_dep>_g; Var = G^2 sum_g n_g T_g <p eps^2>_g + sigma_read^2 n_reads',
                   'flat field: t = (S - D) / (F - D); q = -ln t; magnification M = SDD/SOD; voxel = pitch x binning / M; penumbra sigma = spot/2.355 (M - 1)',
                   'FDK: f(x) = 1/2 sum_beta (SOD/U)^2 [ (SOD/sqrt(SOD^2+u^2+v^2) q) * h_ramp ](u(x), v(x)) d_beta, U = SOD + x cos b - y sin b; Parker w for pi + 2 gamma_m',
                   'Parker (short scan pi + 2 gamma_m, this stage: conjugate (b, g) ~ (b + pi - 2 g, -g)): w(b, g) with g -> -g applied to the projections before the ramp filter',
                   'beam hardening: q -> q + s (mu_m(E_eff) L(q) - q), L(q) inverse of q_poly(L) = -ln sum_g w_g exp(-mu_m,g L), E_eff = detected-energy mean of the unattenuated beam; porosity = N_pore / N_envelope (ISO 50 threshold)',
                   'geometry calibration: u_i(b) = SDD (a + r_i sin(b + phi_i)) / (SOD + r_i cos(b + phi_i)), joint least squares over the tracked steel balls for the axis offset a',
                   'auto alignment (central row): a* = argmin_a mean min((p(b, u) - p(b - pi + 2 (alpha - t), SDD tan(2 alpha - t)))^2, c^2), alpha = atan(a / SOD), t = atan(u / SDD)',
                   'energy: E_emitted = 4 pi E_un = target (2 pi E_un + 2 pi (E_un - E_self)) + window + filter (cone) + air + sample body absorbed (mu_en/mu) + holder / stage absorbed + scattered + detector deposited + passed + shielding',
                   'ledger per sub-step inside one rate cell: E_sink = base_sink(cell) int V^2 I dt / (e V_c^2), E_electrical = int V I dt (V, I linear in the sub-step); dose = E_sample_body / m_body'],
        parameters=params, source_ids=[_SIM, _LIBS, _NIST, _PUB, _SPEC, _BRO, _CFG, _PHYS, _WB], limitations=list(LIMITATIONS))
    import importlib.util
    spec = importlib.util.spec_from_file_location('_x4poseidon_local_annotations', ROOT / 'scripts/asset_runtime/_annotations.py')
    ann = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(ann)
    ann.KEY_ZH.update({k: v['meaning'] for k, v in SLOTS.items()})
    ann.PARAM_ZH.update(PARAM_ZH)
    ann.annotate_export(SLUG, caps, slots, {'x4_poseidon_ct': mechanism}, inputs={'plant.configure': (PLANT, PLANT_EXAMPLE)})
    for k, v in SLOTS.items():
        slots[k]['meaning_zh'] = v['meaning']
        if v['reference']:
            slots[k]['reference_conditions'] = v['reference']
    return dict(asset_type_id=ASSET_TYPE_ID, catalog_id=CATALOG_ID, capabilities=caps, state_slots=slots, mechanisms={'x4_poseidon_ct': mechanism})